summaryrefslogtreecommitdiff
path: root/gfx-iso/src/isogfx.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2023-07-14 09:25:55 -0700
committer3gg <3gg@shellblade.net>2023-07-14 09:25:55 -0700
commit2c668763a1d6e645dcfaa713b924de26260542d0 (patch)
tree94cbbdfd904ed57ebf9372d399b7f283eee1b894 /gfx-iso/src/isogfx.c
parent30920ebec7d66adbb540e907a49058dcf334383c (diff)
Fix checkerboard demo.
Diffstat (limited to 'gfx-iso/src/isogfx.c')
-rw-r--r--gfx-iso/src/isogfx.c71
1 files changed, 45 insertions, 26 deletions
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
195static 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
286cleanup: 297cleanup:
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
543void 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
563void isogfx_render(IsoGfx* iso) { 560void 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
609void 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}