Skip to content

Commit 7557452

Browse files
committed
fixed irritating bug in rendering that would miscalculate the tile bounds on occasion
1 parent 78ae6d3 commit 7557452

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

examples/c/geometry.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "stb_image_write.h"
33

44
#define PP_IMPLEMENTATION
5+
#define PP_DEBUG
56
#include "pretty-poly.h"
67
#include "helpers.h"
78

@@ -14,14 +15,25 @@ void set_pen(colour c) {
1415
pen = c;
1516
}
1617

17-
void blend_tile(const pp_tile_t *t) {
18-
for(int32_t y = t->y; y < t->y + t->h; y++) {
19-
for(int32_t x = t->x; x < t->x + t->w; x++) {
18+
void blend_tile(const pp_tile_t *t) {
19+
for(int y = t->y; y < t->y + t->h; y++) {
20+
for(int x = t->x; x < t->x + t->w; x++) {
2021
colour alpha_pen = pen;
2122
alpha_pen.a = alpha(pen.a, pp_tile_get(t, x, y));
2223
buffer[y][x] = blend(buffer[y][x], alpha_pen);
2324
}
2425
}
26+
27+
// draw border of tile for debugging
28+
colour box = create_colour(160, 180, 200, 150);
29+
for(int32_t x = t->x; x < t->x + t->w; x++) {
30+
buffer[t->y][x] = blend(buffer[t->y][x], box);
31+
buffer[t->y + t->h][x] = blend(buffer[t->y + t->h][x], box);
32+
}
33+
for(int32_t y = t->y; y < t->y + t->h; y++) {
34+
buffer[y][t->x] = blend(buffer[y][t->x], box);
35+
buffer[y][t->x + t->w] = blend(buffer[y][t->x + t->w], box);
36+
}
2537
}
2638

2739

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

3648
for(int y = 0; y < HEIGHT; y++) {
3749
for(int x = 0; x < WIDTH; x++) {
38-
buffer[x][y] = ((x / 8) + (y / 8)) % 2 == 0 ? create_colour(16, 32, 48, 255) : create_colour(0, 0, 0, 255);
50+
buffer[y][x] = ((x / 8) + (y / 8)) % 2 == 0 ? create_colour(16, 32, 48, 255) : create_colour(0, 0, 0, 255);
3951
}
4052
}
4153

@@ -47,8 +59,6 @@ int main() {
4759
{.points = hole, .count = 4}
4860
};
4961
pp_poly_t poly = {.paths = paths, .count = 2};
50-
pp_render(&poly);
51-
5262

5363
for(int i = 0; i < 1000; i += 30) {
5464
pp_mat3_t t = pp_mat3_identity();

pretty-poly.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pp_rect_t pp_polygon_bounds(pp_poly_t *p);
145145

146146
#ifdef PP_IMPLEMENTATION
147147

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

425425
pp_rect_t render_nodes(pp_rect_t *tb) {
426426
pp_rect_t rb = {PP_TILE_BUFFER_SIZE << _pp_antialias, PP_TILE_BUFFER_SIZE << _pp_antialias, 0, 0}; // render bounds
427-
427+
int maxx = 0, minx = PP_TILE_BUFFER_SIZE << _pp_antialias;
428428
debug(" + render tile %d, %d - %d, %d\n", tb->x, tb->y, tb->w, tb->h);
429429

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

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

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

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

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

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

448448
// update render bounds
449449
rb.x = _pp_min(rb.x, sx);
450-
rb.y = _pp_min(rb.y, y);
451-
rb.w = _pp_max(rb.w, ex - rb.x);
452-
rb.h = _pp_max(rb.h, y - rb.y + 1);
450+
rb.y = _pp_min(rb.y, y);
451+
minx = _pp_min(_pp_min(sx, ex), minx);
452+
maxx = _pp_max(_pp_max(sx, ex), maxx);
453+
rb.h = y - rb.y + 1;
453454

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

@@ -460,6 +461,8 @@ pp_rect_t render_nodes(pp_rect_t *tb) {
460461
}
461462
}
462463

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

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

513516
#ifdef USE_RP2040_INTERP

0 commit comments

Comments
 (0)