From 2b0ba2a6947c1879d6c03b784d57919750fcc170 Mon Sep 17 00:00:00 2001 From: Robin Krens Date: Sun, 12 Jun 2022 17:51:37 +0200 Subject: [PATCH] tools: bmp2tiles with cmd line args --- tools/bmp2tiles/bmp2tiles.c | 53 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/tools/bmp2tiles/bmp2tiles.c b/tools/bmp2tiles/bmp2tiles.c index 472442a..40db317 100644 --- a/tools/bmp2tiles/bmp2tiles.c +++ b/tools/bmp2tiles/bmp2tiles.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #define TILE_SZ 32 @@ -56,16 +58,47 @@ void generate_4bpp_tile(unsigned char *buf, void * userdata, } } -int main(void) +int main(int argc, char *argv[]) { SDL_Surface * rawbmp; FILE * ostream; - char filename[] = "test.bmp"; - char outname[] = "test.gfx"; - rawbmp = SDL_LoadBMP(filename); - + int opt; + char * infile; + char outfile[256]; + bool bout = false; + + while ((opt = getopt(argc, argv, "po:sv:")) != -1) { + switch (opt) { + case 'p': + printf("include palette"); + break; + case 'o': + printf("output file %s\n", optarg); + strcpy(outfile, optarg); + bout = true; + break; + case 's': + printf("generate gfx per 16x16 tile \n"); + break; + case 'v': + break; + default: /* '?' */ + fprintf(stderr, "Usage: %s [-s] [-o output] file \n", argv[0]); + exit(EXIT_FAILURE); + } + } + + if (optind >= argc) { + fprintf(stderr, "Expected infile\n"); + fprintf(stderr, "Usage: %s [-s] [-o output] [file]\n", argv[0]); + exit(EXIT_FAILURE); + } + + infile = argv[optind]; + + rawbmp = SDL_LoadBMP(infile); if (!rawbmp) { - fprintf(stderr, "can not load .bmp file\n"); + fprintf(stderr, "can not load %s file\n", infile); exit(EXIT_FAILURE); } @@ -80,7 +113,13 @@ int main(void) exit(EXIT_FAILURE); } - ostream = fopen(outname, "w"); + if (!bout) + ostream = fopen("out.gfx", "w"); + else { + printf("opening %s\n", outfile); + ostream = fopen(outfile, "w"); + } + if (!ostream) { fprintf(stderr, "can not open file for writing!\n"); exit(EXIT_FAILURE); -- 2.7.4