summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-09-04 18:54:17 -0700
committer3gg <3gg@shellblade.net>2025-09-04 18:54:19 -0700
commitcf5f90d20f12d3e13a285bd199639b7aaea8395e (patch)
tree4054473cc196c550c77ae88e74965e71129c014f
parent1f2d5b4fcfe59e36b8c0a9fec1551b42432397bb (diff)
Add support for ortho maps.
Not properly tested because the demo map apparently has alpha=0 everywhere.
-rw-r--r--src/gfx2d.c85
1 files changed, 74 insertions, 11 deletions
diff --git a/src/gfx2d.c b/src/gfx2d.c
index 1c3cc39..6630c95 100644
--- a/src/gfx2d.c
+++ b/src/gfx2d.c
@@ -583,8 +583,28 @@ static void draw_rect(
583 } 583 }
584} 584}
585 585
586/// Draw a tile. 586/// Draw a tile in an orthogonal map.
587static void draw_tile(IsoGfx* iso, Tile tile, int iso_x, int iso_y) { 587static void draw_tile_ortho(IsoGfx* iso, Tile tile, int x, int y) {
588 assert(iso);
589 assert(iso->tileset);
590 assert(x >= 0);
591 assert(y >= 0);
592 assert(x < iso->map->world_width);
593 assert(y < iso->map->world_height);
594
595 const Ts_Tile* pTile = ts_tileset_get_tile(iso->tileset, tile);
596 const Pixel* pixels = ts_tileset_get_tile_pixels(iso->tileset, tile);
597
598 const ivec2 screen_origin = map2screen(
599 iso->camera, iso->map->base_tile_width, iso->map->base_tile_height, x, y);
600
601 draw_rect(
602 &iso->screen, screen_origin, pTile->width, pTile->height, pixels,
603 nullptr);
604}
605
606/// Draw a tile in an isometric map.
607static void draw_tile_iso(IsoGfx* iso, Tile tile, int iso_x, int iso_y) {
588 assert(iso); 608 assert(iso);
589 assert(iso->tileset); 609 assert(iso->tileset);
590 assert(iso_x >= 0); 610 assert(iso_x >= 0);
@@ -613,14 +633,27 @@ static void draw_tile(IsoGfx* iso, Tile tile, int iso_x, int iso_y) {
613 &iso->screen, top_left, pTile->width, pTile->height, pixels, nullptr); 633 &iso->screen, top_left, pTile->width, pTile->height, pixels, nullptr);
614} 634}
615 635
616static void draw_map(IsoGfx* iso) { 636static void draw_map_ortho(IsoGfx* iso) {
617 assert(iso); 637 assert(iso);
638 assert(iso->map);
618 639
619 const int W = iso->screen.width; 640 // TODO: Same TODOs as in draw_map_iso().
620 const int H = iso->screen.height;
621 641
622 memset(iso->screen.pixels, 0, W * H * sizeof(Pixel)); 642 const Tm_Layer* layer = tm_map_get_layer(iso->map, 0);
643
644 for (int wy = 0; wy < iso->map->world_height; ++wy) {
645 for (int wx = 0; wx < iso->map->world_width; ++wx) {
646 const Tile tile = tm_layer_get_tile(iso->map, layer, wx, wy);
647 draw_tile_ortho(iso, tile, wx, wy);
648 }
649 }
650}
651
652static void draw_map_iso(IsoGfx* iso) {
653 assert(iso);
654 assert(iso->map);
623 655
656 // TODO: Support for multiple layers.
624 const Tm_Layer* layer = tm_map_get_layer(iso->map, 0); 657 const Tm_Layer* layer = tm_map_get_layer(iso->map, 0);
625 658
626 // TODO: Culling. 659 // TODO: Culling.
@@ -631,11 +664,32 @@ static void draw_map(IsoGfx* iso) {
631 for (int wy = 0; wy < iso->map->world_height; ++wy) { 664 for (int wy = 0; wy < iso->map->world_height; ++wy) {
632 for (int wx = 0; wx < iso->map->world_width; ++wx) { 665 for (int wx = 0; wx < iso->map->world_width; ++wx) {
633 const Tile tile = tm_layer_get_tile(iso->map, layer, wx, wy); 666 const Tile tile = tm_layer_get_tile(iso->map, layer, wx, wy);
634 draw_tile(iso, tile, wx, wy); 667 draw_tile_iso(iso, tile, wx, wy);
635 } 668 }
636 } 669 }
637} 670}
638 671
672static void draw_map(IsoGfx* iso) {
673 assert(iso);
674 assert(iso->map);
675 assert(iso->screen.pixels);
676
677 const int W = iso->screen.width;
678 const int H = iso->screen.height;
679
680 memset(iso->screen.pixels, 0, W * H * sizeof(Pixel));
681
682 const Tm_Flags* flags = (const Tm_Flags*)&iso->map->flags;
683 switch (flags->orientation) {
684 case Tm_Orthogonal:
685 draw_map_ortho(iso);
686 break;
687 case Tm_Isometric:
688 draw_map_iso(iso);
689 break;
690 }
691}
692
639static void draw_sprite( 693static void draw_sprite(
640 IsoGfx* iso, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) { 694 IsoGfx* iso, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) {
641 assert(iso); 695 assert(iso);
@@ -665,9 +719,7 @@ static void draw_sprites(IsoGfx* iso) {
665 719
666 for (const SpriteInstance* sprite = iso->head_sprite; sprite; 720 for (const SpriteInstance* sprite = iso->head_sprite; sprite;
667 sprite = sprite->next) { 721 sprite = sprite->next) {
668 const Ss_SpriteSheet* sheet = sprite->sheet; 722 draw_sprite(iso, sprite, sprite->sheet);
669 assert(sheet);
670 draw_sprite(iso, sprite, sheet);
671 } 723 }
672} 724}
673 725
@@ -683,7 +735,18 @@ void isogfx_render(IsoGfx* iso) {
683} 735}
684 736
685void isogfx_draw_tile(IsoGfx* iso, int x, int y, Tile tile) { 737void isogfx_draw_tile(IsoGfx* iso, int x, int y, Tile tile) {
686 draw_tile(iso, tile, x, y); 738 assert(iso);
739 assert(iso->map);
740
741 const Tm_Flags* flags = (const Tm_Flags*)&iso->map->flags;
742 switch (flags->orientation) {
743 case Tm_Orthogonal:
744 draw_tile_ortho(iso, tile, x, y);
745 break;
746 case Tm_Isometric:
747 draw_tile_iso(iso, tile, x, y);
748 break;
749 }
687} 750}
688 751
689void isogfx_get_screen_size(const IsoGfx* iso, int* width, int* height) { 752void isogfx_get_screen_size(const IsoGfx* iso, int* width, int* height) {