Skip to content

Commit

Permalink
fixed irritating bug in rendering that would miscalculate the tile bo…
Browse files Browse the repository at this point in the history
…unds on occasion
  • Loading branch information
lowfatcode committed Jul 15, 2024
1 parent 78ae6d3 commit 7557452
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
22 changes: 16 additions & 6 deletions examples/c/geometry.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "stb_image_write.h"

#define PP_IMPLEMENTATION
#define PP_DEBUG
#include "pretty-poly.h"
#include "helpers.h"

Expand All @@ -14,14 +15,25 @@ void set_pen(colour c) {
pen = c;
}

void blend_tile(const pp_tile_t *t) {
for(int32_t y = t->y; y < t->y + t->h; y++) {
for(int32_t x = t->x; x < t->x + t->w; x++) {
void blend_tile(const pp_tile_t *t) {
for(int y = t->y; y < t->y + t->h; y++) {
for(int x = t->x; x < t->x + t->w; x++) {
colour alpha_pen = pen;
alpha_pen.a = alpha(pen.a, pp_tile_get(t, x, y));
buffer[y][x] = blend(buffer[y][x], alpha_pen);
}
}

// draw border of tile for debugging
colour box = create_colour(160, 180, 200, 150);
for(int32_t x = t->x; x < t->x + t->w; x++) {
buffer[t->y][x] = blend(buffer[t->y][x], box);
buffer[t->y + t->h][x] = blend(buffer[t->y + t->h][x], box);
}
for(int32_t y = t->y; y < t->y + t->h; y++) {
buffer[y][t->x] = blend(buffer[y][t->x], box);
buffer[y][t->x + t->w] = blend(buffer[y][t->x + t->w], box);
}
}


Expand All @@ -35,7 +47,7 @@ int main() {

for(int y = 0; y < HEIGHT; y++) {
for(int x = 0; x < WIDTH; x++) {
buffer[x][y] = ((x / 8) + (y / 8)) % 2 == 0 ? create_colour(16, 32, 48, 255) : create_colour(0, 0, 0, 255);
buffer[y][x] = ((x / 8) + (y / 8)) % 2 == 0 ? create_colour(16, 32, 48, 255) : create_colour(0, 0, 0, 255);
}
}

Expand All @@ -47,8 +59,6 @@ int main() {
{.points = hole, .count = 4}
};
pp_poly_t poly = {.paths = paths, .count = 2};
pp_render(&poly);


for(int i = 0; i < 1000; i += 30) {
pp_mat3_t t = pp_mat3_identity();
Expand Down
19 changes: 11 additions & 8 deletions pretty-poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pp_rect_t pp_polygon_bounds(pp_poly_t *p);

#ifdef PP_IMPLEMENTATION

pp_rect_t _pp_clip = (pp_rect_t){0, 0, 320, 240};
pp_rect_t _pp_clip = (pp_rect_t){-INT_MAX, -INT_MAX, INT_MAX, INT_MAX};
pp_tile_callback_t _pp_tile_callback = NULL;
pp_antialias_t _pp_antialias = PP_AA_X4;
pp_mat3_t *_pp_transform = NULL;
Expand Down Expand Up @@ -424,10 +424,10 @@ int compare_nodes(const void* a, const void* b) {

pp_rect_t render_nodes(pp_rect_t *tb) {
pp_rect_t rb = {PP_TILE_BUFFER_SIZE << _pp_antialias, PP_TILE_BUFFER_SIZE << _pp_antialias, 0, 0}; // render bounds

int maxx = 0, minx = PP_TILE_BUFFER_SIZE << _pp_antialias;
debug(" + render tile %d, %d - %d, %d\n", tb->x, tb->y, tb->w, tb->h);

for(uint32_t y = 0; y < ((uint32_t)PP_TILE_BUFFER_SIZE << _pp_antialias); y++) {
for(int y = 0; y < ((int)PP_TILE_BUFFER_SIZE << _pp_antialias); y++) {

// debug(" : row %d node count %d\n", y, node_counts[y]);

Expand All @@ -437,7 +437,7 @@ pp_rect_t render_nodes(pp_rect_t *tb) {

unsigned char* row_data = &tile_buffer[(y >> _pp_antialias) * PP_TILE_BUFFER_SIZE];

for(uint32_t i = 0; i < node_counts[y]; i += 2) {
for(int i = 0; i < node_counts[y]; i += 2) {
int sx = nodes[y][i + 0];
int ex = nodes[y][i + 1];

Expand All @@ -447,9 +447,10 @@ pp_rect_t render_nodes(pp_rect_t *tb) {

// update render bounds
rb.x = _pp_min(rb.x, sx);
rb.y = _pp_min(rb.y, y);
rb.w = _pp_max(rb.w, ex - rb.x);
rb.h = _pp_max(rb.h, y - rb.y + 1);
rb.y = _pp_min(rb.y, y);
minx = _pp_min(_pp_min(sx, ex), minx);
maxx = _pp_max(_pp_max(sx, ex), maxx);
rb.h = y - rb.y + 1;

//debug(" - render span at %d from %d to %d\n", y, sx, ex);

Expand All @@ -460,6 +461,8 @@ pp_rect_t render_nodes(pp_rect_t *tb) {
}
}

rb.w = maxx - minx;

// shifting the width and height effectively "floors" the result which can
// mean we lose a pixel off the right or bottom edge of the tile. by adding
// either 1 (at x4) or 3 (at x16) we change that to a "ceil" instead ensuring
Expand Down Expand Up @@ -507,7 +510,7 @@ void pp_render(pp_poly_t *polygon) {
polygon_bounds = pp_rect_transform(&polygon_bounds, _pp_transform);
}

debug(" - bounds %d, %d (%d x %d)\n", polygon_bounds.x, polygon_bounds.y, polygon_bounds.w, polygon_bounds.h);
debug(" - polygon bounds %d, %d (%d x %d)\n", polygon_bounds.x, polygon_bounds.y, polygon_bounds.w, polygon_bounds.h);
debug(" - clip %d, %d (%d x %d)\n", _pp_clip.x, _pp_clip.y, _pp_clip.w, _pp_clip.h);

#ifdef USE_RP2040_INTERP
Expand Down

0 comments on commit 7557452

Please sign in to comment.