From db88922db92eb793e1c6e683dcc1f8654e4220fe Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 20 Apr 2021 13:09:48 +0200 Subject: Allow larger sprite sizes --- src/main.c | 83 +++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 22 deletions(-) diff --git a/src/main.c b/src/main.c index 87c4b73..1e57c19 100644 --- a/src/main.c +++ b/src/main.c @@ -79,15 +79,6 @@ export_c_file(Tiles *tiles, Palette *palette, FILE *file) { fprintf(file, "};\n"); } -void -print_usage(void) { - printf("Usage: %s [options] \n", BIN_NAME); - printf("\n"); - printf("\t-b \tSelects the background color used for transparency.\n"); - printf("\t-o \tPath to the output file. If blank, stdout will be used.\n"); - printf("\n"); -} - void extract_tile(Tiles *tiles, Palette *palette, Image *img, size_t offset_x, size_t offset_y) { @@ -154,18 +145,32 @@ extract_tile(Tiles *tiles, Palette *palette, Image *img, tiles->size++; } +void +print_usage(void) { + printf("Usage: %s [options] \n", BIN_NAME); + printf("\n"); + printf("\t-b \tSelects the background color used for transparency.\n"); + printf("\t-o \tPath to the output file. If blank, stdout will be used.\n"); + printf("\t-W <8>\tSelect the sprite width in pixels. Must be a multiple of the tile size.\n"); + printf("\t-H <8>\tSelect the sprite height in pixels. Must be a multiple of the tile size.\n"); + printf("\t-h\tShow help.\n"); + printf("\n"); +} + int main(int argc, char *argv[]) { + // Initialize default parameters. u16 background_color = DEFAULT_BG_COLOR; u16 palette_size = DEFAULT_PALETTE_SIZE; + size_t sprite_width = TILE_SIZE; + size_t sprite_height = TILE_SIZE; char *out_file_path = NULL; int option; - while ((option = getopt(argc, argv, "hb:o:")) != -1) { + while ((option = getopt(argc, argv, "hb:o:W:H:")) != -1) { switch (option) { - // -shape, -size, -s: either 2 options or one with fixed - // number of parameters 8x8, 16x16, 16x8, etc. case 'b': { + // Background color. char *bg = optarg; if (strlen(bg) != 6) { fprintf(stderr, "%s: Invalid background color.\n", BIN_NAME); @@ -176,8 +181,8 @@ main(int argc, char *argv[]) { unsigned int green = 0; unsigned int blue = 0; - // Undefined behaviour, here we go! Also, no error checks, so if - // an invalid number is given it will be parsed as 0. + // NOTE: Undefined behaviour, here we go! Also, no error checks, + // so if an invalid number is given it will be parsed as 0. char r_str[3] = {bg[0], bg[1], '\0'}; char g_str[3] = {bg[2], bg[3], '\0'}; char b_str[3] = {bg[4], bg[5], '\0'}; @@ -191,10 +196,38 @@ main(int argc, char *argv[]) { blue = quantize5(blue); background_color = rgb15(red, green, blue); } break; + case 'W': { + // Sprite width. + char *arg = optarg; + sprite_width = 0; + sprite_width = atoi(arg); + + // Check if a valid number is given. + if (sprite_width == 0 || sprite_width % TILE_SIZE != 0) { + fprintf(stderr, "%s: Invalid sprite width.\n", BIN_NAME); + print_usage(); + return EXIT_FAILURE; + } + } break; + case 'H': { + // Sprite height. + char *arg = optarg; + sprite_height = 0; + sprite_height = atoi(arg); + + // Check if a valid number is given. + if (sprite_height == 0 || sprite_height % TILE_SIZE != 0) { + fprintf(stderr, "%s: Invalid sprite height.\n", BIN_NAME); + print_usage(); + return EXIT_FAILURE; + } + } break; case 'o': { + // Output file. out_file_path = optarg; } break; case 'h': { + // Help. print_usage(); return EXIT_SUCCESS; } break; @@ -251,14 +284,20 @@ main(int argc, char *argv[]) { .capacity = n_tiles, }; - int offset_x = 0; - int offset_y = 0; - for (size_t k = 0; k < n_tiles; ++k) { - extract_tile(&tiles, &palette, &img, offset_x, offset_y); - offset_x += TILE_SIZE; - if (offset_x >= img.width) { - offset_x = 0; - offset_y += TILE_SIZE; + int n_sprites_x = img.width / sprite_width; + int n_sprites_y = img.height / sprite_height; + int n_sprite_tiles_x = sprite_width / TILE_SIZE; + int n_sprite_tiles_y = sprite_height / TILE_SIZE; + for (size_t p = 0; p < n_sprites_y; ++p) { + for (size_t k = 0; k < n_sprites_x; ++k) { + for (size_t j = 0; j < n_sprite_tiles_y; ++j) { + int offset_x = k * sprite_width; + int offset_y = p * sprite_height + j * TILE_SIZE; + for (size_t i = 0; i < n_sprite_tiles_x; ++i) { + extract_tile(&tiles, &palette, &img, offset_x, offset_y); + offset_x += TILE_SIZE; + } + } } } -- cgit v1.2.1