diff options
| author | 3gg <3gg@shellblade.net> | 2023-07-13 08:29:36 -0700 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2023-07-13 08:29:36 -0700 |
| commit | 30920ebec7d66adbb540e907a49058dcf334383c (patch) | |
| tree | 90272acdd4c4b5c52b9b8afd8c4b1843d735f8d6 /gfx-iso | |
| parent | 3220f35e5b3a2285d8c79c81b7f75eedd66faa14 (diff) | |
Use new mem allocator that supports variable-sized chunks.
Diffstat (limited to 'gfx-iso')
| -rw-r--r-- | gfx-iso/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | gfx-iso/src/isogfx.c | 54 |
2 files changed, 17 insertions, 38 deletions
diff --git a/gfx-iso/CMakeLists.txt b/gfx-iso/CMakeLists.txt index b57a83f..993bbb3 100644 --- a/gfx-iso/CMakeLists.txt +++ b/gfx-iso/CMakeLists.txt | |||
| @@ -14,6 +14,7 @@ target_include_directories(isogfx PUBLIC | |||
| 14 | 14 | ||
| 15 | target_link_libraries(isogfx PRIVATE | 15 | target_link_libraries(isogfx PRIVATE |
| 16 | filesystem | 16 | filesystem |
| 17 | mem | ||
| 17 | mempool) | 18 | mempool) |
| 18 | 19 | ||
| 19 | target_compile_options(isogfx PRIVATE -Wall -Wextra -Wpedantic) | 20 | target_compile_options(isogfx PRIVATE -Wall -Wextra -Wpedantic) |
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 @@ | |||
| 1 | #include <isogfx/isogfx.h> | 1 | #include <isogfx/isogfx.h> |
| 2 | 2 | ||
| 3 | #include <filesystem.h> | 3 | #include <filesystem.h> |
| 4 | #include <mem.h> | ||
| 4 | #include <mempool.h> | 5 | #include <mempool.h> |
| 5 | 6 | ||
| 6 | #include <linux/limits.h> | 7 | #include <linux/limits.h> |
| @@ -72,17 +73,14 @@ static inline const Ts_Tile* ts_tileset_get_next_tile( | |||
| 72 | // Renderer state. | 73 | // Renderer state. |
| 73 | // ----------------------------------------------------------------------------- | 74 | // ----------------------------------------------------------------------------- |
| 74 | 75 | ||
| 75 | // typedef Ts_Tile TileData; | ||
| 76 | |||
| 77 | typedef struct TileData { | 76 | typedef struct TileData { |
| 78 | uint16_t width; | 77 | uint16_t width; |
| 79 | uint16_t height; | 78 | uint16_t height; |
| 80 | uint16_t num_blocks; // Number of pixel blocks in the pixels mempool. | 79 | uint16_t pixels_handle; // Handle to the tile's pixels in the pixel pool. |
| 81 | uint16_t pixels_index; // Offset into the pixels mempool. | ||
| 82 | } TileData; | 80 | } TileData; |
| 83 | 81 | ||
| 84 | DEF_MEMPOOL_DYN(TilePool, TileData) | 82 | DEF_MEMPOOL_DYN(TilePool, TileData) |
| 85 | DEF_MEMPOOL_DYN(PixelPool, Pixel) | 83 | DEF_MEM_DYN(PixelPool, Pixel) |
| 86 | 84 | ||
| 87 | typedef struct IsoGfx { | 85 | typedef struct IsoGfx { |
| 88 | int screen_width; | 86 | int screen_width; |
| @@ -148,8 +146,7 @@ static const Pixel* tile_xy_const_ref( | |||
| 148 | assert(y >= 0); | 146 | assert(y >= 0); |
| 149 | assert(x < tile->width); | 147 | assert(x < tile->width); |
| 150 | assert(y < tile->height); | 148 | assert(y < tile->height); |
| 151 | return &mempool_get_block( | 149 | return &mem_get_chunk(&iso->pixels, tile->pixels_handle)[y * tile->width + x]; |
| 152 | &iso->pixels, tile->pixels_index)[y * tile->width + x]; | ||
| 153 | } | 150 | } |
| 154 | 151 | ||
| 155 | static Pixel tile_xy(const IsoGfx* iso, const TileData* tile, int x, int y) { | 152 | static Pixel tile_xy(const IsoGfx* iso, const TileData* tile, int x, int y) { |
| @@ -235,7 +232,7 @@ static void destroy_world(IsoGfx* iso) { | |||
| 235 | iso->world = 0; | 232 | iso->world = 0; |
| 236 | } | 233 | } |
| 237 | mempool_del(&iso->tiles); | 234 | mempool_del(&iso->tiles); |
| 238 | mempool_del(&iso->pixels); | 235 | mem_del(&iso->pixels); |
| 239 | } | 236 | } |
| 240 | 237 | ||
| 241 | void isogfx_del(IsoGfx** pIso) { | 238 | void isogfx_del(IsoGfx** pIso) { |
| @@ -279,6 +276,7 @@ bool isogfx_make_world(IsoGfx* iso, const WorldDesc* desc) { | |||
| 279 | if (!(iso->world = calloc(world_size, sizeof(Tile)))) { | 276 | if (!(iso->world = calloc(world_size, sizeof(Tile)))) { |
| 280 | goto cleanup; | 277 | goto cleanup; |
| 281 | } | 278 | } |
| 279 | // TODO: This needs tiles and pixels now. | ||
| 282 | if (!mempool_make_dyn(&iso->tiles, tile_pool_size, tile_size_bytes)) { | 280 | if (!mempool_make_dyn(&iso->tiles, tile_pool_size, tile_size_bytes)) { |
| 283 | goto cleanup; | 281 | goto cleanup; |
| 284 | } | 282 | } |
| @@ -320,7 +318,7 @@ bool isogfx_load_world(IsoGfx* iso, const char* filepath) { | |||
| 320 | if (!mempool_make_dyn(&iso->tiles, tile_pool_size, sizeof(TileData))) { | 318 | if (!mempool_make_dyn(&iso->tiles, tile_pool_size, sizeof(TileData))) { |
| 321 | goto cleanup; | 319 | goto cleanup; |
| 322 | } | 320 | } |
| 323 | if (!mempool_make_dyn(&iso->pixels, tile_pool_size, base_tile_size_bytes)) { | 321 | if (!mem_make_dyn(&iso->pixels, tile_pool_size, base_tile_size_bytes)) { |
| 324 | goto cleanup; | 322 | goto cleanup; |
| 325 | } | 323 | } |
| 326 | 324 | ||
| @@ -349,26 +347,20 @@ bool isogfx_load_world(IsoGfx* iso, const char* filepath) { | |||
| 349 | assert((tile->width % map->base_tile_width) == 0); | 347 | assert((tile->width % map->base_tile_width) == 0); |
| 350 | assert((tile->height % map->base_tile_height) == 0); | 348 | assert((tile->height % map->base_tile_height) == 0); |
| 351 | 349 | ||
| 352 | const uint16_t tile_size = tile->width * tile->height; | 350 | // Allocate N base tile size blocks for the tile. |
| 353 | 351 | const uint16_t tile_size = tile->width * tile->height; | |
| 354 | // TODO: Add function in mempool to alloc N consecutive blocks. | 352 | const int num_blocks = tile_size / base_tile_size; |
| 355 | const int num_blocks = tile_size / base_tile_size; | 353 | Pixel* pixels = mem_alloc(&iso->pixels, num_blocks); |
| 356 | Pixel* pixels = mempool_alloc(&iso->pixels); | ||
| 357 | assert(pixels); | 354 | assert(pixels); |
| 358 | // This is ugly and assumes that blocks are allocated consecutively. | ||
| 359 | for (int b = 1; b < num_blocks; ++b) { | ||
| 360 | Pixel* block = mempool_alloc(&iso->pixels); | ||
| 361 | assert(block); | ||
| 362 | } | ||
| 363 | memcpy(pixels, tile->pixels, tile_size * sizeof(Pixel)); | 355 | memcpy(pixels, tile->pixels, tile_size * sizeof(Pixel)); |
| 364 | 356 | ||
| 357 | // Allocate the tile data. | ||
| 365 | TileData* tile_data = mempool_alloc(&iso->tiles); | 358 | TileData* tile_data = mempool_alloc(&iso->tiles); |
| 366 | assert(tile_data); | 359 | assert(tile_data); |
| 367 | tile_data->width = tile->width; | 360 | tile_data->width = tile->width; |
| 368 | tile_data->height = tile->height; | 361 | tile_data->height = tile->height; |
| 369 | tile_data->num_blocks = (uint16_t)num_blocks; | 362 | tile_data->pixels_handle = |
| 370 | tile_data->pixels_index = | 363 | (uint16_t)mem_get_chunk_handle(&iso->pixels, pixels); |
| 371 | (uint16_t)mempool_get_block_index(&iso->pixels, pixels); | ||
| 372 | 364 | ||
| 373 | tile = ts_tileset_get_next_tile(tileset, tile); | 365 | tile = ts_tileset_get_next_tile(tileset, tile); |
| 374 | } | 366 | } |
| @@ -517,20 +509,6 @@ static void draw_tile(IsoGfx* iso, ivec2 origin, Tile tile) { | |||
| 517 | } | 509 | } |
| 518 | } | 510 | } |
| 519 | } | 511 | } |
| 520 | |||
| 521 | // for (int py = 0; py < tile_data->height; ++py) { | ||
| 522 | // for (int px = 0; px < tile_data->width; ++px) { | ||
| 523 | // const Pixel colour = tile_xy(iso, tile_data, px, py); | ||
| 524 | // if (colour.a > 0) { | ||
| 525 | // const int sx = origin.x + px; | ||
| 526 | // const int sy = origin.y + py; | ||
| 527 | // if ((sx >= 0) && (sy >= 0) && (sx < iso->screen_width) && | ||
| 528 | // (sy < iso->screen_height)) { | ||
| 529 | // *screen_xy_mut(iso, sx, sy) = colour; | ||
| 530 | // } | ||
| 531 | // } | ||
| 532 | // } | ||
| 533 | // } | ||
| 534 | } | 512 | } |
| 535 | 513 | ||
| 536 | static void draw(IsoGfx* iso) { | 514 | static void draw(IsoGfx* iso) { |
