diff options
author | 3gg <3gg@shellblade.net> | 2023-07-14 09:25:55 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2023-07-14 09:25:55 -0700 |
commit | 2c668763a1d6e645dcfaa713b924de26260542d0 (patch) | |
tree | 94cbbdfd904ed57ebf9372d399b7f283eee1b894 | |
parent | 30920ebec7d66adbb540e907a49058dcf334383c (diff) |
Fix checkerboard demo.
-rw-r--r-- | gfx-iso/include/isogfx/isogfx.h | 8 | ||||
-rw-r--r-- | gfx-iso/src/isogfx.c | 71 |
2 files changed, 49 insertions, 30 deletions
diff --git a/gfx-iso/include/isogfx/isogfx.h b/gfx-iso/include/isogfx/isogfx.h index 5c44310..6b7ce8e 100644 --- a/gfx-iso/include/isogfx/isogfx.h +++ b/gfx-iso/include/isogfx/isogfx.h | |||
@@ -79,10 +79,6 @@ void isogfx_set_tile(IsoGfx*, int x, int y, Tile); | |||
79 | /// Set the tiles in positions in the range (x0,y0) - (x1,y1). | 79 | /// Set the tiles in positions in the range (x0,y0) - (x1,y1). |
80 | void isogfx_set_tiles(IsoGfx*, int x0, int y0, int x1, int y1, Tile); | 80 | void isogfx_set_tiles(IsoGfx*, int x0, int y0, int x1, int y1, Tile); |
81 | 81 | ||
82 | /// Translate Cartesian to isometric coordinates. | ||
83 | void isogfx_pick_tile( | ||
84 | const IsoGfx*, double xcart, double ycart, int* xiso, int* yiso); | ||
85 | |||
86 | /// Render the world. | 82 | /// Render the world. |
87 | void isogfx_render(IsoGfx*); | 83 | void isogfx_render(IsoGfx*); |
88 | 84 | ||
@@ -100,3 +96,7 @@ bool isogfx_resize(IsoGfx*, int screen_width, int screen_height); | |||
100 | /// | 96 | /// |
101 | /// Call after each call to isogfx_render() to retrieve the render output. | 97 | /// Call after each call to isogfx_render() to retrieve the render output. |
102 | const Pixel* isogfx_get_screen_buffer(const IsoGfx*); | 98 | const Pixel* isogfx_get_screen_buffer(const IsoGfx*); |
99 | |||
100 | /// Translate Cartesian to isometric coordinates. | ||
101 | void isogfx_pick_tile( | ||
102 | const IsoGfx*, double xcart, double ycart, int* xiso, int* yiso); | ||
diff --git a/gfx-iso/src/isogfx.c b/gfx-iso/src/isogfx.c index 6d2350f..3ed0fde 100644 --- a/gfx-iso/src/isogfx.c +++ b/gfx-iso/src/isogfx.c | |||
@@ -192,6 +192,15 @@ static inline Pixel* screen_xy_mut(IsoGfx* iso, int x, int y) { | |||
192 | return (Pixel*)screen_xy_const_ref(iso, x, y); | 192 | return (Pixel*)screen_xy_const_ref(iso, x, y); |
193 | } | 193 | } |
194 | 194 | ||
195 | static int calc_num_tile_blocks( | ||
196 | int base_tile_width, int base_tile_height, int tile_width, | ||
197 | int tile_height) { | ||
198 | const int base_tile_size = base_tile_width * base_tile_height; | ||
199 | const int tile_size = tile_width * tile_height; | ||
200 | const int num_blocks = tile_size / base_tile_size; | ||
201 | return num_blocks; | ||
202 | } | ||
203 | |||
195 | // ----------------------------------------------------------------------------- | 204 | // ----------------------------------------------------------------------------- |
196 | // Renderer, world and tile management. | 205 | // Renderer, world and tile management. |
197 | // ----------------------------------------------------------------------------- | 206 | // ----------------------------------------------------------------------------- |
@@ -276,8 +285,10 @@ bool isogfx_make_world(IsoGfx* iso, const WorldDesc* desc) { | |||
276 | if (!(iso->world = calloc(world_size, sizeof(Tile)))) { | 285 | if (!(iso->world = calloc(world_size, sizeof(Tile)))) { |
277 | goto cleanup; | 286 | goto cleanup; |
278 | } | 287 | } |
279 | // TODO: This needs tiles and pixels now. | 288 | if (!mempool_make_dyn(&iso->tiles, world_size, sizeof(TileData))) { |
280 | if (!mempool_make_dyn(&iso->tiles, tile_pool_size, tile_size_bytes)) { | 289 | goto cleanup; |
290 | } | ||
291 | if (!mem_make_dyn(&iso->pixels, tile_pool_size, tile_size_bytes)) { | ||
281 | goto cleanup; | 292 | goto cleanup; |
282 | } | 293 | } |
283 | 294 | ||
@@ -285,7 +296,6 @@ bool isogfx_make_world(IsoGfx* iso, const WorldDesc* desc) { | |||
285 | 296 | ||
286 | cleanup: | 297 | cleanup: |
287 | destroy_world(iso); | 298 | destroy_world(iso); |
288 | mempool_del(&iso->tiles); | ||
289 | return false; | 299 | return false; |
290 | } | 300 | } |
291 | 301 | ||
@@ -449,8 +459,15 @@ Tile isogfx_make_tile(IsoGfx* iso, const TileDesc* desc) { | |||
449 | TileData* tile = mempool_alloc(&iso->tiles); | 459 | TileData* tile = mempool_alloc(&iso->tiles); |
450 | assert(tile); // TODO: Make this a hard assert. | 460 | assert(tile); // TODO: Make this a hard assert. |
451 | 461 | ||
452 | tile->width = desc->width; | 462 | const int num_blocks = calc_num_tile_blocks( |
453 | tile->height = desc->height; | 463 | iso->tile_width, iso->tile_height, desc->width, desc->height); |
464 | |||
465 | Pixel* pixels = mem_alloc(&iso->pixels, num_blocks); | ||
466 | assert(pixels); // TODO: Make this a hard assert. | ||
467 | |||
468 | tile->width = desc->width; | ||
469 | tile->height = desc->height; | ||
470 | tile->pixels_handle = mem_get_chunk_handle(&iso->pixels, pixels); | ||
454 | 471 | ||
455 | switch (desc->type) { | 472 | switch (desc->type) { |
456 | case TileFromColour: | 473 | case TileFromColour: |
@@ -540,26 +557,6 @@ static void draw(IsoGfx* iso) { | |||
540 | } | 557 | } |
541 | } | 558 | } |
542 | 559 | ||
543 | void isogfx_pick_tile( | ||
544 | const IsoGfx* iso, double xcart, double ycart, int* xiso, int* yiso) { | ||
545 | assert(iso); | ||
546 | assert(xiso); | ||
547 | assert(yiso); | ||
548 | |||
549 | const vec2 xy_iso = cart2iso( | ||
550 | (vec2){.x = xcart, .y = ycart}, iso->tile_width, iso->tile_height, | ||
551 | iso->screen_width); | ||
552 | |||
553 | if ((0 <= xy_iso.x) && (xy_iso.x < iso->world_width) && (0 <= xy_iso.y) && | ||
554 | (xy_iso.y < iso->world_height)) { | ||
555 | *xiso = (int)xy_iso.x; | ||
556 | *yiso = (int)xy_iso.y; | ||
557 | } else { | ||
558 | *xiso = -1; | ||
559 | *yiso = -1; | ||
560 | } | ||
561 | } | ||
562 | |||
563 | void isogfx_render(IsoGfx* iso) { | 560 | void isogfx_render(IsoGfx* iso) { |
564 | assert(iso); | 561 | assert(iso); |
565 | draw(iso); | 562 | draw(iso); |
@@ -572,7 +569,9 @@ void isogfx_draw_tile(IsoGfx* iso, int x, int y, Tile tile) { | |||
572 | assert(x < iso->world_width); | 569 | assert(x < iso->world_width); |
573 | assert(y < iso->world_height); | 570 | assert(y < iso->world_height); |
574 | 571 | ||
575 | const ivec2 o = {(iso->screen_width / 2) - (iso->tile_width / 2), 0}; | 572 | // const ivec2 o = {(iso->screen_width / 2) - (iso->tile_width / 2), 0}; |
573 | const ivec2 o = { | ||
574 | (iso->screen_width / 2) - (iso->tile_width / 2), iso->tile_height}; | ||
576 | const ivec2 vx = {.x = iso->tile_width / 2, .y = iso->tile_height / 2}; | 575 | const ivec2 vx = {.x = iso->tile_width / 2, .y = iso->tile_height / 2}; |
577 | const ivec2 vy = {.x = -iso->tile_width / 2, .y = iso->tile_height / 2}; | 576 | const ivec2 vy = {.x = -iso->tile_width / 2, .y = iso->tile_height / 2}; |
578 | const ivec2 so = | 577 | const ivec2 so = |
@@ -606,3 +605,23 @@ const Pixel* isogfx_get_screen_buffer(const IsoGfx* iso) { | |||
606 | assert(iso); | 605 | assert(iso); |
607 | return iso->screen; | 606 | return iso->screen; |
608 | } | 607 | } |
608 | |||
609 | void isogfx_pick_tile( | ||
610 | const IsoGfx* iso, double xcart, double ycart, int* xiso, int* yiso) { | ||
611 | assert(iso); | ||
612 | assert(xiso); | ||
613 | assert(yiso); | ||
614 | |||
615 | const vec2 xy_iso = cart2iso( | ||
616 | (vec2){.x = xcart, .y = ycart}, iso->tile_width, iso->tile_height, | ||
617 | iso->screen_width); | ||
618 | |||
619 | if ((0 <= xy_iso.x) && (xy_iso.x < iso->world_width) && (0 <= xy_iso.y) && | ||
620 | (xy_iso.y < iso->world_height)) { | ||
621 | *xiso = (int)xy_iso.x; | ||
622 | *yiso = (int)xy_iso.y; | ||
623 | } else { | ||
624 | *xiso = -1; | ||
625 | *yiso = -1; | ||
626 | } | ||
627 | } | ||