diff options
author | Bad Diode <bd@badd10de.dev> | 2021-04-20 13:09:48 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-04-20 13:09:48 +0200 |
commit | db88922db92eb793e1c6e683dcc1f8654e4220fe (patch) | |
tree | 957b5a1a31f95e6ab08ebbdad51f2a4b391e754c | |
parent | 2315bacd456e1185516aa8dd232c8a2eb6aaee2b (diff) | |
download | gba-dev-tools-db88922db92eb793e1c6e683dcc1f8654e4220fe.tar.gz gba-dev-tools-db88922db92eb793e1c6e683dcc1f8654e4220fe.zip |
Allow larger sprite sizes
-rw-r--r-- | src/main.c | 83 |
1 files changed, 61 insertions, 22 deletions
@@ -80,15 +80,6 @@ export_c_file(Tiles *tiles, Palette *palette, FILE *file) { | |||
80 | } | 80 | } |
81 | 81 | ||
82 | void | 82 | void |
83 | print_usage(void) { | ||
84 | printf("Usage: %s [options] <filename>\n", BIN_NAME); | ||
85 | printf("\n"); | ||
86 | printf("\t-b <RRGGBB>\tSelects the background color used for transparency.\n"); | ||
87 | printf("\t-o <out_file.c>\tPath to the output file. If blank, stdout will be used.\n"); | ||
88 | printf("\n"); | ||
89 | } | ||
90 | |||
91 | void | ||
92 | extract_tile(Tiles *tiles, Palette *palette, Image *img, | 83 | extract_tile(Tiles *tiles, Palette *palette, Image *img, |
93 | size_t offset_x, size_t offset_y) { | 84 | size_t offset_x, size_t offset_y) { |
94 | for (size_t j = 0; j < TILE_SIZE; ++j) { | 85 | for (size_t j = 0; j < TILE_SIZE; ++j) { |
@@ -154,18 +145,32 @@ extract_tile(Tiles *tiles, Palette *palette, Image *img, | |||
154 | tiles->size++; | 145 | tiles->size++; |
155 | } | 146 | } |
156 | 147 | ||
148 | void | ||
149 | print_usage(void) { | ||
150 | printf("Usage: %s [options] <filename>\n", BIN_NAME); | ||
151 | printf("\n"); | ||
152 | printf("\t-b <RRGGBB>\tSelects the background color used for transparency.\n"); | ||
153 | printf("\t-o <out_file.c>\tPath to the output file. If blank, stdout will be used.\n"); | ||
154 | printf("\t-W <8>\tSelect the sprite width in pixels. Must be a multiple of the tile size.\n"); | ||
155 | printf("\t-H <8>\tSelect the sprite height in pixels. Must be a multiple of the tile size.\n"); | ||
156 | printf("\t-h\tShow help.\n"); | ||
157 | printf("\n"); | ||
158 | } | ||
159 | |||
157 | int | 160 | int |
158 | main(int argc, char *argv[]) { | 161 | main(int argc, char *argv[]) { |
162 | // Initialize default parameters. | ||
159 | u16 background_color = DEFAULT_BG_COLOR; | 163 | u16 background_color = DEFAULT_BG_COLOR; |
160 | u16 palette_size = DEFAULT_PALETTE_SIZE; | 164 | u16 palette_size = DEFAULT_PALETTE_SIZE; |
165 | size_t sprite_width = TILE_SIZE; | ||
166 | size_t sprite_height = TILE_SIZE; | ||
161 | char *out_file_path = NULL; | 167 | char *out_file_path = NULL; |
162 | 168 | ||
163 | int option; | 169 | int option; |
164 | while ((option = getopt(argc, argv, "hb:o:")) != -1) { | 170 | while ((option = getopt(argc, argv, "hb:o:W:H:")) != -1) { |
165 | switch (option) { | 171 | switch (option) { |
166 | // -shape, -size, -s: either 2 options or one with fixed | ||
167 | // number of parameters 8x8, 16x16, 16x8, etc. | ||
168 | case 'b': { | 172 | case 'b': { |
173 | // Background color. | ||
169 | char *bg = optarg; | 174 | char *bg = optarg; |
170 | if (strlen(bg) != 6) { | 175 | if (strlen(bg) != 6) { |
171 | fprintf(stderr, "%s: Invalid background color.\n", BIN_NAME); | 176 | fprintf(stderr, "%s: Invalid background color.\n", BIN_NAME); |
@@ -176,8 +181,8 @@ main(int argc, char *argv[]) { | |||
176 | unsigned int green = 0; | 181 | unsigned int green = 0; |
177 | unsigned int blue = 0; | 182 | unsigned int blue = 0; |
178 | 183 | ||
179 | // Undefined behaviour, here we go! Also, no error checks, so if | 184 | // NOTE: Undefined behaviour, here we go! Also, no error checks, |
180 | // an invalid number is given it will be parsed as 0. | 185 | // so if an invalid number is given it will be parsed as 0. |
181 | char r_str[3] = {bg[0], bg[1], '\0'}; | 186 | char r_str[3] = {bg[0], bg[1], '\0'}; |
182 | char g_str[3] = {bg[2], bg[3], '\0'}; | 187 | char g_str[3] = {bg[2], bg[3], '\0'}; |
183 | char b_str[3] = {bg[4], bg[5], '\0'}; | 188 | char b_str[3] = {bg[4], bg[5], '\0'}; |
@@ -191,10 +196,38 @@ main(int argc, char *argv[]) { | |||
191 | blue = quantize5(blue); | 196 | blue = quantize5(blue); |
192 | background_color = rgb15(red, green, blue); | 197 | background_color = rgb15(red, green, blue); |
193 | } break; | 198 | } break; |
199 | case 'W': { | ||
200 | // Sprite width. | ||
201 | char *arg = optarg; | ||
202 | sprite_width = 0; | ||
203 | sprite_width = atoi(arg); | ||
204 | |||
205 | // Check if a valid number is given. | ||
206 | if (sprite_width == 0 || sprite_width % TILE_SIZE != 0) { | ||
207 | fprintf(stderr, "%s: Invalid sprite width.\n", BIN_NAME); | ||
208 | print_usage(); | ||
209 | return EXIT_FAILURE; | ||
210 | } | ||
211 | } break; | ||
212 | case 'H': { | ||
213 | // Sprite height. | ||
214 | char *arg = optarg; | ||
215 | sprite_height = 0; | ||
216 | sprite_height = atoi(arg); | ||
217 | |||
218 | // Check if a valid number is given. | ||
219 | if (sprite_height == 0 || sprite_height % TILE_SIZE != 0) { | ||
220 | fprintf(stderr, "%s: Invalid sprite height.\n", BIN_NAME); | ||
221 | print_usage(); | ||
222 | return EXIT_FAILURE; | ||
223 | } | ||
224 | } break; | ||
194 | case 'o': { | 225 | case 'o': { |
226 | // Output file. | ||
195 | out_file_path = optarg; | 227 | out_file_path = optarg; |
196 | } break; | 228 | } break; |
197 | case 'h': { | 229 | case 'h': { |
230 | // Help. | ||
198 | print_usage(); | 231 | print_usage(); |
199 | return EXIT_SUCCESS; | 232 | return EXIT_SUCCESS; |
200 | } break; | 233 | } break; |
@@ -251,14 +284,20 @@ main(int argc, char *argv[]) { | |||
251 | .capacity = n_tiles, | 284 | .capacity = n_tiles, |
252 | }; | 285 | }; |
253 | 286 | ||
254 | int offset_x = 0; | 287 | int n_sprites_x = img.width / sprite_width; |
255 | int offset_y = 0; | 288 | int n_sprites_y = img.height / sprite_height; |
256 | for (size_t k = 0; k < n_tiles; ++k) { | 289 | int n_sprite_tiles_x = sprite_width / TILE_SIZE; |
257 | extract_tile(&tiles, &palette, &img, offset_x, offset_y); | 290 | int n_sprite_tiles_y = sprite_height / TILE_SIZE; |
258 | offset_x += TILE_SIZE; | 291 | for (size_t p = 0; p < n_sprites_y; ++p) { |
259 | if (offset_x >= img.width) { | 292 | for (size_t k = 0; k < n_sprites_x; ++k) { |
260 | offset_x = 0; | 293 | for (size_t j = 0; j < n_sprite_tiles_y; ++j) { |
261 | offset_y += TILE_SIZE; | 294 | int offset_x = k * sprite_width; |
295 | int offset_y = p * sprite_height + j * TILE_SIZE; | ||
296 | for (size_t i = 0; i < n_sprite_tiles_x; ++i) { | ||
297 | extract_tile(&tiles, &palette, &img, offset_x, offset_y); | ||
298 | offset_x += TILE_SIZE; | ||
299 | } | ||
300 | } | ||
262 | } | 301 | } |
263 | } | 302 | } |
264 | 303 | ||