From cf5f90d20f12d3e13a285bd199639b7aaea8395e Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 4 Sep 2025 18:54:17 -0700 Subject: Add support for ortho maps. Not properly tested because the demo map apparently has alpha=0 everywhere. --- src/gfx2d.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 11 deletions(-) (limited to 'src/gfx2d.c') 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( } } -/// Draw a tile. -static void draw_tile(IsoGfx* iso, Tile tile, int iso_x, int iso_y) { +/// Draw a tile in an orthogonal map. +static void draw_tile_ortho(IsoGfx* iso, Tile tile, int x, int y) { + assert(iso); + assert(iso->tileset); + assert(x >= 0); + assert(y >= 0); + assert(x < iso->map->world_width); + assert(y < iso->map->world_height); + + const Ts_Tile* pTile = ts_tileset_get_tile(iso->tileset, tile); + const Pixel* pixels = ts_tileset_get_tile_pixels(iso->tileset, tile); + + const ivec2 screen_origin = map2screen( + iso->camera, iso->map->base_tile_width, iso->map->base_tile_height, x, y); + + draw_rect( + &iso->screen, screen_origin, pTile->width, pTile->height, pixels, + nullptr); +} + +/// Draw a tile in an isometric map. +static void draw_tile_iso(IsoGfx* iso, Tile tile, int iso_x, int iso_y) { assert(iso); assert(iso->tileset); assert(iso_x >= 0); @@ -613,14 +633,27 @@ static void draw_tile(IsoGfx* iso, Tile tile, int iso_x, int iso_y) { &iso->screen, top_left, pTile->width, pTile->height, pixels, nullptr); } -static void draw_map(IsoGfx* iso) { +static void draw_map_ortho(IsoGfx* iso) { assert(iso); + assert(iso->map); - const int W = iso->screen.width; - const int H = iso->screen.height; + // TODO: Same TODOs as in draw_map_iso(). - memset(iso->screen.pixels, 0, W * H * sizeof(Pixel)); + const Tm_Layer* layer = tm_map_get_layer(iso->map, 0); + + for (int wy = 0; wy < iso->map->world_height; ++wy) { + for (int wx = 0; wx < iso->map->world_width; ++wx) { + const Tile tile = tm_layer_get_tile(iso->map, layer, wx, wy); + draw_tile_ortho(iso, tile, wx, wy); + } + } +} + +static void draw_map_iso(IsoGfx* iso) { + assert(iso); + assert(iso->map); + // TODO: Support for multiple layers. const Tm_Layer* layer = tm_map_get_layer(iso->map, 0); // TODO: Culling. @@ -631,11 +664,32 @@ static void draw_map(IsoGfx* iso) { for (int wy = 0; wy < iso->map->world_height; ++wy) { for (int wx = 0; wx < iso->map->world_width; ++wx) { const Tile tile = tm_layer_get_tile(iso->map, layer, wx, wy); - draw_tile(iso, tile, wx, wy); + draw_tile_iso(iso, tile, wx, wy); } } } +static void draw_map(IsoGfx* iso) { + assert(iso); + assert(iso->map); + assert(iso->screen.pixels); + + const int W = iso->screen.width; + const int H = iso->screen.height; + + memset(iso->screen.pixels, 0, W * H * sizeof(Pixel)); + + const Tm_Flags* flags = (const Tm_Flags*)&iso->map->flags; + switch (flags->orientation) { + case Tm_Orthogonal: + draw_map_ortho(iso); + break; + case Tm_Isometric: + draw_map_iso(iso); + break; + } +} + static void draw_sprite( IsoGfx* iso, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) { assert(iso); @@ -665,9 +719,7 @@ static void draw_sprites(IsoGfx* iso) { for (const SpriteInstance* sprite = iso->head_sprite; sprite; sprite = sprite->next) { - const Ss_SpriteSheet* sheet = sprite->sheet; - assert(sheet); - draw_sprite(iso, sprite, sheet); + draw_sprite(iso, sprite, sprite->sheet); } } @@ -683,7 +735,18 @@ void isogfx_render(IsoGfx* iso) { } void isogfx_draw_tile(IsoGfx* iso, int x, int y, Tile tile) { - draw_tile(iso, tile, x, y); + assert(iso); + assert(iso->map); + + const Tm_Flags* flags = (const Tm_Flags*)&iso->map->flags; + switch (flags->orientation) { + case Tm_Orthogonal: + draw_tile_ortho(iso, tile, x, y); + break; + case Tm_Isometric: + draw_tile_iso(iso, tile, x, y); + break; + } } void isogfx_get_screen_size(const IsoGfx* iso, int* width, int* height) { -- cgit v1.2.3