From 17d7532c796eced679e7e234c4e6dc7546045c22 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 4 Sep 2025 19:02:21 -0700 Subject: Add support for ortho sprite rendering. Now tested. --- src/gfx2d.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/gfx2d.c b/src/gfx2d.c index 6630c95..698e32c 100644 --- a/src/gfx2d.c +++ b/src/gfx2d.c @@ -690,7 +690,32 @@ static void draw_map(IsoGfx* iso) { } } -static void draw_sprite( +/// Draw a sprite in an orthogonal/Cartesian coordinate system. +static void draw_sprite_ortho( + IsoGfx* iso, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) { + assert(iso); + assert(sprite); + assert(sheet); + assert(sprite->animation >= 0); + assert(sprite->animation < sheet->num_rows); + assert(sprite->frame >= 0); + + // Apply an offset similarly to how we offset tiles. The sprite is offset by + // -base_tile_width/2 along the x-axis to align the sprite with the leftmost + // edge of the tile it is on. + const ivec2 screen_origin = map2screen( + iso->camera, iso->map->base_tile_width, iso->map->base_tile_height, + sprite->position.x, sprite->position.y); + + const Ss_Row* row = ss_get_sprite_sheet_row(sheet, sprite->animation); + const uint8_t* frame = ss_get_sprite_sheet_sprite(sheet, row, sprite->frame); + draw_rect( + &iso->screen, screen_origin, sheet->sprite_width, sheet->sprite_height, + sheet->palette.colours, frame); +} + +/// Draw a sprite in an isometric coordinate system. +static void draw_sprite_iso( IsoGfx* iso, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) { assert(iso); assert(sprite); @@ -716,10 +741,22 @@ static void draw_sprite( static void draw_sprites(IsoGfx* iso) { assert(iso); + assert(iso->map); - for (const SpriteInstance* sprite = iso->head_sprite; sprite; - sprite = sprite->next) { - draw_sprite(iso, sprite, sprite->sheet); + const Tm_Flags* flags = (const Tm_Flags*)&iso->map->flags; + switch (flags->orientation) { + case Tm_Orthogonal: + for (const SpriteInstance* sprite = iso->head_sprite; sprite; + sprite = sprite->next) { + draw_sprite_ortho(iso, sprite, sprite->sheet); + } + break; + case Tm_Isometric: + for (const SpriteInstance* sprite = iso->head_sprite; sprite; + sprite = sprite->next) { + draw_sprite_iso(iso, sprite, sprite->sheet); + } + break; } } -- cgit v1.2.3