From 2c668763a1d6e645dcfaa713b924de26260542d0 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 14 Jul 2023 09:25:55 -0700 Subject: Fix checkerboard demo. --- gfx-iso/include/isogfx/isogfx.h | 8 ++--- gfx-iso/src/isogfx.c | 71 ++++++++++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 30 deletions(-) (limited to 'gfx-iso') 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); /// Set the tiles in positions in the range (x0,y0) - (x1,y1). void isogfx_set_tiles(IsoGfx*, int x0, int y0, int x1, int y1, Tile); -/// Translate Cartesian to isometric coordinates. -void isogfx_pick_tile( - const IsoGfx*, double xcart, double ycart, int* xiso, int* yiso); - /// Render the world. void isogfx_render(IsoGfx*); @@ -100,3 +96,7 @@ bool isogfx_resize(IsoGfx*, int screen_width, int screen_height); /// /// Call after each call to isogfx_render() to retrieve the render output. const Pixel* isogfx_get_screen_buffer(const IsoGfx*); + +/// Translate Cartesian to isometric coordinates. +void isogfx_pick_tile( + 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) { return (Pixel*)screen_xy_const_ref(iso, x, y); } +static int calc_num_tile_blocks( + int base_tile_width, int base_tile_height, int tile_width, + int tile_height) { + const int base_tile_size = base_tile_width * base_tile_height; + const int tile_size = tile_width * tile_height; + const int num_blocks = tile_size / base_tile_size; + return num_blocks; +} + // ----------------------------------------------------------------------------- // Renderer, world and tile management. // ----------------------------------------------------------------------------- @@ -276,8 +285,10 @@ bool isogfx_make_world(IsoGfx* iso, const WorldDesc* desc) { if (!(iso->world = calloc(world_size, sizeof(Tile)))) { goto cleanup; } - // TODO: This needs tiles and pixels now. - if (!mempool_make_dyn(&iso->tiles, tile_pool_size, tile_size_bytes)) { + if (!mempool_make_dyn(&iso->tiles, world_size, sizeof(TileData))) { + goto cleanup; + } + if (!mem_make_dyn(&iso->pixels, tile_pool_size, tile_size_bytes)) { goto cleanup; } @@ -285,7 +296,6 @@ bool isogfx_make_world(IsoGfx* iso, const WorldDesc* desc) { cleanup: destroy_world(iso); - mempool_del(&iso->tiles); return false; } @@ -449,8 +459,15 @@ Tile isogfx_make_tile(IsoGfx* iso, const TileDesc* desc) { TileData* tile = mempool_alloc(&iso->tiles); assert(tile); // TODO: Make this a hard assert. - tile->width = desc->width; - tile->height = desc->height; + const int num_blocks = calc_num_tile_blocks( + iso->tile_width, iso->tile_height, desc->width, desc->height); + + Pixel* pixels = mem_alloc(&iso->pixels, num_blocks); + assert(pixels); // TODO: Make this a hard assert. + + tile->width = desc->width; + tile->height = desc->height; + tile->pixels_handle = mem_get_chunk_handle(&iso->pixels, pixels); switch (desc->type) { case TileFromColour: @@ -540,26 +557,6 @@ static void draw(IsoGfx* iso) { } } -void isogfx_pick_tile( - const IsoGfx* iso, double xcart, double ycart, int* xiso, int* yiso) { - assert(iso); - assert(xiso); - assert(yiso); - - const vec2 xy_iso = cart2iso( - (vec2){.x = xcart, .y = ycart}, iso->tile_width, iso->tile_height, - iso->screen_width); - - if ((0 <= xy_iso.x) && (xy_iso.x < iso->world_width) && (0 <= xy_iso.y) && - (xy_iso.y < iso->world_height)) { - *xiso = (int)xy_iso.x; - *yiso = (int)xy_iso.y; - } else { - *xiso = -1; - *yiso = -1; - } -} - void isogfx_render(IsoGfx* iso) { assert(iso); draw(iso); @@ -572,7 +569,9 @@ void isogfx_draw_tile(IsoGfx* iso, int x, int y, Tile tile) { assert(x < iso->world_width); assert(y < iso->world_height); - const ivec2 o = {(iso->screen_width / 2) - (iso->tile_width / 2), 0}; + // const ivec2 o = {(iso->screen_width / 2) - (iso->tile_width / 2), 0}; + const ivec2 o = { + (iso->screen_width / 2) - (iso->tile_width / 2), iso->tile_height}; const ivec2 vx = {.x = iso->tile_width / 2, .y = iso->tile_height / 2}; const ivec2 vy = {.x = -iso->tile_width / 2, .y = iso->tile_height / 2}; const ivec2 so = @@ -606,3 +605,23 @@ const Pixel* isogfx_get_screen_buffer(const IsoGfx* iso) { assert(iso); return iso->screen; } + +void isogfx_pick_tile( + const IsoGfx* iso, double xcart, double ycart, int* xiso, int* yiso) { + assert(iso); + assert(xiso); + assert(yiso); + + const vec2 xy_iso = cart2iso( + (vec2){.x = xcart, .y = ycart}, iso->tile_width, iso->tile_height, + iso->screen_width); + + if ((0 <= xy_iso.x) && (xy_iso.x < iso->world_width) && (0 <= xy_iso.y) && + (xy_iso.y < iso->world_height)) { + *xiso = (int)xy_iso.x; + *yiso = (int)xy_iso.y; + } else { + *xiso = -1; + *yiso = -1; + } +} -- cgit v1.2.3