From 30920ebec7d66adbb540e907a49058dcf334383c Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 13 Jul 2023 08:29:36 -0700 Subject: Use new mem allocator that supports variable-sized chunks. --- gfx-iso/src/isogfx.c | 54 ++++++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 38 deletions(-) (limited to 'gfx-iso/src/isogfx.c') diff --git a/gfx-iso/src/isogfx.c b/gfx-iso/src/isogfx.c index ee33cad..6d2350f 100644 --- a/gfx-iso/src/isogfx.c +++ b/gfx-iso/src/isogfx.c @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -72,17 +73,14 @@ static inline const Ts_Tile* ts_tileset_get_next_tile( // Renderer state. // ----------------------------------------------------------------------------- -// typedef Ts_Tile TileData; - typedef struct TileData { uint16_t width; uint16_t height; - uint16_t num_blocks; // Number of pixel blocks in the pixels mempool. - uint16_t pixels_index; // Offset into the pixels mempool. + uint16_t pixels_handle; // Handle to the tile's pixels in the pixel pool. } TileData; DEF_MEMPOOL_DYN(TilePool, TileData) -DEF_MEMPOOL_DYN(PixelPool, Pixel) +DEF_MEM_DYN(PixelPool, Pixel) typedef struct IsoGfx { int screen_width; @@ -148,8 +146,7 @@ static const Pixel* tile_xy_const_ref( assert(y >= 0); assert(x < tile->width); assert(y < tile->height); - return &mempool_get_block( - &iso->pixels, tile->pixels_index)[y * tile->width + x]; + return &mem_get_chunk(&iso->pixels, tile->pixels_handle)[y * tile->width + x]; } static Pixel tile_xy(const IsoGfx* iso, const TileData* tile, int x, int y) { @@ -235,7 +232,7 @@ static void destroy_world(IsoGfx* iso) { iso->world = 0; } mempool_del(&iso->tiles); - mempool_del(&iso->pixels); + mem_del(&iso->pixels); } void isogfx_del(IsoGfx** pIso) { @@ -279,6 +276,7 @@ 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)) { goto cleanup; } @@ -320,7 +318,7 @@ bool isogfx_load_world(IsoGfx* iso, const char* filepath) { if (!mempool_make_dyn(&iso->tiles, tile_pool_size, sizeof(TileData))) { goto cleanup; } - if (!mempool_make_dyn(&iso->pixels, tile_pool_size, base_tile_size_bytes)) { + if (!mem_make_dyn(&iso->pixels, tile_pool_size, base_tile_size_bytes)) { goto cleanup; } @@ -349,26 +347,20 @@ bool isogfx_load_world(IsoGfx* iso, const char* filepath) { assert((tile->width % map->base_tile_width) == 0); assert((tile->height % map->base_tile_height) == 0); - const uint16_t tile_size = tile->width * tile->height; - - // TODO: Add function in mempool to alloc N consecutive blocks. - const int num_blocks = tile_size / base_tile_size; - Pixel* pixels = mempool_alloc(&iso->pixels); + // Allocate N base tile size blocks for the tile. + const uint16_t tile_size = tile->width * tile->height; + const int num_blocks = tile_size / base_tile_size; + Pixel* pixels = mem_alloc(&iso->pixels, num_blocks); assert(pixels); - // This is ugly and assumes that blocks are allocated consecutively. - for (int b = 1; b < num_blocks; ++b) { - Pixel* block = mempool_alloc(&iso->pixels); - assert(block); - } memcpy(pixels, tile->pixels, tile_size * sizeof(Pixel)); + // Allocate the tile data. TileData* tile_data = mempool_alloc(&iso->tiles); assert(tile_data); - tile_data->width = tile->width; - tile_data->height = tile->height; - tile_data->num_blocks = (uint16_t)num_blocks; - tile_data->pixels_index = - (uint16_t)mempool_get_block_index(&iso->pixels, pixels); + tile_data->width = tile->width; + tile_data->height = tile->height; + tile_data->pixels_handle = + (uint16_t)mem_get_chunk_handle(&iso->pixels, pixels); tile = ts_tileset_get_next_tile(tileset, tile); } @@ -517,20 +509,6 @@ static void draw_tile(IsoGfx* iso, ivec2 origin, Tile tile) { } } } - - // for (int py = 0; py < tile_data->height; ++py) { - // for (int px = 0; px < tile_data->width; ++px) { - // const Pixel colour = tile_xy(iso, tile_data, px, py); - // if (colour.a > 0) { - // const int sx = origin.x + px; - // const int sy = origin.y + py; - // if ((sx >= 0) && (sy >= 0) && (sx < iso->screen_width) && - // (sy < iso->screen_height)) { - // *screen_xy_mut(iso, sx, sy) = colour; - // } - // } - // } - // } } static void draw(IsoGfx* iso) { -- cgit v1.2.3