diff options
author | 3gg <3gg@shellblade.net> | 2025-07-14 08:52:15 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2025-07-14 08:52:15 -0700 |
commit | 6cefdd40dd62f098874bd706720db731341bc957 (patch) | |
tree | e38a31025af970b25a9068f0ec5a36ac0945085a /src/isogfx.c | |
parent | 39ab6c2bce7434e45c3957168278cacf9dcd19f5 (diff) |
Diffstat (limited to 'src/isogfx.c')
-rw-r--r-- | src/isogfx.c | 129 |
1 files changed, 10 insertions, 119 deletions
diff --git a/src/isogfx.c b/src/isogfx.c index 52c4ae2..baf422f 100644 --- a/src/isogfx.c +++ b/src/isogfx.c | |||
@@ -1,5 +1,7 @@ | |||
1 | #include <isogfx/isogfx.h> | 1 | #include <isogfx/isogfx.h> |
2 | 2 | ||
3 | #include <isogfx/asset.h> | ||
4 | |||
3 | #include <filesystem.h> | 5 | #include <filesystem.h> |
4 | #include <mem.h> | 6 | #include <mem.h> |
5 | #include <mempool.h> | 7 | #include <mempool.h> |
@@ -38,115 +40,6 @@ typedef struct vec2 { | |||
38 | } vec2; | 40 | } vec2; |
39 | 41 | ||
40 | // ----------------------------------------------------------------------------- | 42 | // ----------------------------------------------------------------------------- |
41 | // Tile set (TS) and tile map (TM) file formats. | ||
42 | // ----------------------------------------------------------------------------- | ||
43 | |||
44 | /// Maximum length of path strings in .TS and .TM files. | ||
45 | #define MAX_PATH_LENGTH 128 | ||
46 | |||
47 | typedef struct Ts_Tile { | ||
48 | uint16_t width; /// Tile width in pixels. | ||
49 | uint16_t height; /// Tile height in pixels. | ||
50 | Pixel pixels[1]; /// Count: width * height. | ||
51 | } Ts_Tile; | ||
52 | |||
53 | typedef struct Ts_TileSet { | ||
54 | uint16_t num_tiles; | ||
55 | uint16_t max_tile_width; /// Maximum tile width in pixels. | ||
56 | uint16_t max_tile_height; /// Maximum tile height in pixels. | ||
57 | Ts_Tile tiles[1]; /// Count: num_tiles. | ||
58 | } Ts_TileSet; | ||
59 | |||
60 | typedef struct Tm_Layer { | ||
61 | union { | ||
62 | char tileset_path[MAX_PATH_LENGTH]; // Relative to the Tm_Map file. | ||
63 | }; | ||
64 | Tile tiles[1]; /// Count: world_width * world_height. | ||
65 | } Tm_Layer; | ||
66 | |||
67 | typedef struct Tm_Map { | ||
68 | uint16_t world_width; /// World width in number of tiles. | ||
69 | uint16_t world_height; /// World height in number of tiles. | ||
70 | uint16_t base_tile_width; | ||
71 | uint16_t base_tile_height; | ||
72 | uint16_t num_layers; | ||
73 | Tm_Layer layers[1]; // Count: num_layers. | ||
74 | } Tm_Map; | ||
75 | |||
76 | static inline const Tm_Layer* tm_map_get_next_layer( | ||
77 | const Tm_Map* map, const Tm_Layer* layer) { | ||
78 | assert(map); | ||
79 | assert(layer); | ||
80 | return (const Tm_Layer*)((const uint8_t*)layer + sizeof(Tm_Layer) + | ||
81 | ((map->world_width * map->world_height - 1) * | ||
82 | sizeof(Tile))); | ||
83 | } | ||
84 | |||
85 | static inline const Ts_Tile* ts_tileset_get_next_tile( | ||
86 | const Ts_TileSet* tileset, const Ts_Tile* tile) { | ||
87 | assert(tileset); | ||
88 | assert(tile); | ||
89 | return (const Ts_Tile*)((const uint8_t*)tile + sizeof(Ts_Tile) + | ||
90 | ((tile->width * tile->height - 1) * sizeof(Pixel))); | ||
91 | } | ||
92 | |||
93 | // ----------------------------------------------------------------------------- | ||
94 | // Sprite sheet file format. | ||
95 | // ----------------------------------------------------------------------------- | ||
96 | |||
97 | /// A row of sprites in a sprite sheet. | ||
98 | /// | ||
99 | /// Each row in a sprite sheet can have a different number of columns. | ||
100 | /// | ||
101 | /// The pixels of the row follow a "sprite-major" order. It contains the | ||
102 | /// 'sprite_width * sprite_height' pixels for the first column/sprite, then the | ||
103 | /// second column/sprite, etc. | ||
104 | /// | ||
105 | /// Pixels are 8-bit indices into the sprite sheet's colour palette. | ||
106 | typedef struct Ss_Row { | ||
107 | uint16_t num_cols; /// Number of columns in this row. | ||
108 | uint8_t pixels[1]; /// Count: num_cols * sprite_width * sprite_height. | ||
109 | } Ss_Row; | ||
110 | |||
111 | typedef struct Ss_Palette { | ||
112 | uint16_t num_colours; | ||
113 | Pixel colours[1]; /// Count: num_colors. | ||
114 | } Ss_Palette; | ||
115 | |||
116 | /// Sprite sheet top-level data definition. | ||
117 | /// | ||
118 | /// Sprite width and height are assumed constant throughout the sprite sheet. | ||
119 | typedef struct Ss_SpriteSheet { | ||
120 | uint16_t sprite_width; /// Sprite width in pixels. | ||
121 | uint16_t sprite_height; /// Sprite height in pixels. | ||
122 | uint16_t num_rows; | ||
123 | Ss_Palette palette; /// Variable size. | ||
124 | Ss_Row rows[1]; /// Count: num_rows. Variable offset. | ||
125 | } Ss_SpriteSheet; | ||
126 | |||
127 | static inline const Ss_Row* get_sprite_sheet_row( | ||
128 | const Ss_SpriteSheet* sheet, int row) { | ||
129 | assert(sheet); | ||
130 | assert(row >= 0); | ||
131 | assert(row < sheet->num_rows); | ||
132 | // Skip over the palette. | ||
133 | const Ss_Row* rows = | ||
134 | (const Ss_Row*)(&sheet->palette.colours[0] + sheet->palette.num_colours); | ||
135 | return &rows[row]; | ||
136 | } | ||
137 | |||
138 | static inline const uint8_t* get_sprite_sheet_sprite( | ||
139 | const Ss_SpriteSheet* sheet, const Ss_Row* row, int col) { | ||
140 | assert(sheet); | ||
141 | assert(row); | ||
142 | assert(col >= 0); | ||
143 | assert(col < row->num_cols); | ||
144 | const int sprite_offset = col * sheet->sprite_width * sheet->sprite_height; | ||
145 | const uint8_t* sprite = &row->pixels[sprite_offset]; | ||
146 | return sprite; | ||
147 | } | ||
148 | |||
149 | // ----------------------------------------------------------------------------- | ||
150 | // Renderer state. | 43 | // Renderer state. |
151 | // ----------------------------------------------------------------------------- | 44 | // ----------------------------------------------------------------------------- |
152 | 45 | ||
@@ -204,8 +97,8 @@ static inline ivec2 ivec2_scale(ivec2 a, int s) { | |||
204 | } | 97 | } |
205 | 98 | ||
206 | static inline ivec2 iso2cart(ivec2 iso, int s, int t, int w) { | 99 | static inline ivec2 iso2cart(ivec2 iso, int s, int t, int w) { |
207 | return (ivec2){ | 100 | return (ivec2){.x = (iso.x - iso.y) * (s / 2) + (w / 2), |
208 | .x = (iso.x - iso.y) * (s / 2) + (w / 2), .y = (iso.x + iso.y) * (t / 2)}; | 101 | .y = (iso.x + iso.y) * (t / 2)}; |
209 | } | 102 | } |
210 | 103 | ||
211 | // Method 1. | 104 | // Method 1. |
@@ -221,9 +114,8 @@ static inline vec2 cart2iso(vec2 cart, int s, int t, int w) { | |||
221 | const double one_over_s = 1. / (double)s; | 114 | const double one_over_s = 1. / (double)s; |
222 | const double one_over_t = 1. / (double)t; | 115 | const double one_over_t = 1. / (double)t; |
223 | const double x = cart.x - (double)(w / 2); | 116 | const double x = cart.x - (double)(w / 2); |
224 | return (vec2){ | 117 | return (vec2){.x = (one_over_s * x + one_over_t * cart.y), |
225 | .x = (one_over_s * x + one_over_t * cart.y), | 118 | .y = (-one_over_s * x + one_over_t * cart.y)}; |
226 | .y = (-one_over_s * x + one_over_t * cart.y)}; | ||
227 | } | 119 | } |
228 | 120 | ||
229 | static const Pixel* tile_xy_const_ref( | 121 | static const Pixel* tile_xy_const_ref( |
@@ -752,11 +644,10 @@ static Pixel alpha_blend(Pixel src, Pixel dst) { | |||
752 | (double)((uint16_t)s * (uint16_t)src.a + \ | 644 | (double)((uint16_t)s * (uint16_t)src.a + \ |
753 | (uint16_t)d * one_minus_alpha) / \ | 645 | (uint16_t)d * one_minus_alpha) / \ |
754 | 255.0) | 646 | 255.0) |
755 | return (Pixel){ | 647 | return (Pixel){.r = blend(src.r, dst.r), |
756 | .r = blend(src.r, dst.r), | 648 | .g = blend(src.g, dst.g), |
757 | .g = blend(src.g, dst.g), | 649 | .b = blend(src.b, dst.b), |
758 | .b = blend(src.b, dst.b), | 650 | .a = src.a}; |
759 | .a = src.a}; | ||
760 | } | 651 | } |
761 | 652 | ||
762 | /// Draw a rectangle (tile or sprite). | 653 | /// Draw a rectangle (tile or sprite). |