diff options
| author | 3gg <3gg@shellblade.net> | 2025-09-04 19:02:21 -0700 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-09-04 19:02:21 -0700 |
| commit | 17d7532c796eced679e7e234c4e6dc7546045c22 (patch) | |
| tree | dc78de0ccfc0c8baf40e6366a50040753f246a14 | |
| parent | 380d74403d63768fa808dfc0bcfa5102ae66cc58 (diff) | |
Add support for ortho sprite rendering. Now tested.
| -rw-r--r-- | src/gfx2d.c | 45 |
1 files 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) { | |||
| 690 | } | 690 | } |
| 691 | } | 691 | } |
| 692 | 692 | ||
| 693 | static void draw_sprite( | 693 | /// Draw a sprite in an orthogonal/Cartesian coordinate system. |
| 694 | static void draw_sprite_ortho( | ||
| 695 | IsoGfx* iso, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) { | ||
| 696 | assert(iso); | ||
| 697 | assert(sprite); | ||
| 698 | assert(sheet); | ||
| 699 | assert(sprite->animation >= 0); | ||
| 700 | assert(sprite->animation < sheet->num_rows); | ||
| 701 | assert(sprite->frame >= 0); | ||
| 702 | |||
| 703 | // Apply an offset similarly to how we offset tiles. The sprite is offset by | ||
| 704 | // -base_tile_width/2 along the x-axis to align the sprite with the leftmost | ||
| 705 | // edge of the tile it is on. | ||
| 706 | const ivec2 screen_origin = map2screen( | ||
| 707 | iso->camera, iso->map->base_tile_width, iso->map->base_tile_height, | ||
| 708 | sprite->position.x, sprite->position.y); | ||
| 709 | |||
| 710 | const Ss_Row* row = ss_get_sprite_sheet_row(sheet, sprite->animation); | ||
| 711 | const uint8_t* frame = ss_get_sprite_sheet_sprite(sheet, row, sprite->frame); | ||
| 712 | draw_rect( | ||
| 713 | &iso->screen, screen_origin, sheet->sprite_width, sheet->sprite_height, | ||
| 714 | sheet->palette.colours, frame); | ||
| 715 | } | ||
| 716 | |||
| 717 | /// Draw a sprite in an isometric coordinate system. | ||
| 718 | static void draw_sprite_iso( | ||
| 694 | IsoGfx* iso, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) { | 719 | IsoGfx* iso, const SpriteInstance* sprite, const Ss_SpriteSheet* sheet) { |
| 695 | assert(iso); | 720 | assert(iso); |
| 696 | assert(sprite); | 721 | assert(sprite); |
| @@ -716,10 +741,22 @@ static void draw_sprite( | |||
| 716 | 741 | ||
| 717 | static void draw_sprites(IsoGfx* iso) { | 742 | static void draw_sprites(IsoGfx* iso) { |
| 718 | assert(iso); | 743 | assert(iso); |
| 744 | assert(iso->map); | ||
| 719 | 745 | ||
| 720 | for (const SpriteInstance* sprite = iso->head_sprite; sprite; | 746 | const Tm_Flags* flags = (const Tm_Flags*)&iso->map->flags; |
| 721 | sprite = sprite->next) { | 747 | switch (flags->orientation) { |
| 722 | draw_sprite(iso, sprite, sprite->sheet); | 748 | case Tm_Orthogonal: |
| 749 | for (const SpriteInstance* sprite = iso->head_sprite; sprite; | ||
| 750 | sprite = sprite->next) { | ||
| 751 | draw_sprite_ortho(iso, sprite, sprite->sheet); | ||
| 752 | } | ||
| 753 | break; | ||
| 754 | case Tm_Isometric: | ||
| 755 | for (const SpriteInstance* sprite = iso->head_sprite; sprite; | ||
| 756 | sprite = sprite->next) { | ||
| 757 | draw_sprite_iso(iso, sprite, sprite->sheet); | ||
| 758 | } | ||
| 759 | break; | ||
| 723 | } | 760 | } |
| 724 | } | 761 | } |
| 725 | 762 | ||
