tools: bmp2tiles with cmd line args
authorRobin Krens <robin@robinkrens.nl>
Sun, 12 Jun 2022 15:51:37 +0000 (17:51 +0200)
committerRobin Krens <robin@robinkrens.nl>
Sun, 12 Jun 2022 15:51:37 +0000 (17:51 +0200)
tools/bmp2tiles/bmp2tiles.c

index 472442a..40db317 100644 (file)
@@ -11,6 +11,8 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include <string.h>
+#include <unistd.h>
+#include <getopt.h>
 #include <SDL2/SDL_image.h>
 
 #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);