fixed tilemap output and added -o option
authorJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 18 Feb 2019 03:05:30 +0000 (05:05 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 18 Feb 2019 03:05:30 +0000 (05:05 +0200)
src/main.c

index 5d9e19c..d4698cc 100644 (file)
@@ -13,7 +13,10 @@ void wrtilemap_carr(FILE *fp, const char *name, int *tmap, int xtiles, int ytile
 void wrpalette_carr(FILE *fp, const char *name, struct cmapent *cmap, int ncol);
 
 enum { OUT_IMG, OUT_C, OUT_ASM } outmode;
+static const char *otype_suffix[] = {".png", ".c", ".s"};
+
 int tile_xsz = 8, tile_ysz = 8;
+const char *output_filename;
 
 int main(int argc, char **argv)
 {
@@ -23,6 +26,10 @@ int main(int argc, char **argv)
                if(argv[i][0] == '-') {
                        if(argv[i][2] == 0) {
                                switch(argv[i][1]) {
+                               case 'o':
+                                       output_filename = argv[++i];
+                                       break;
+
                                case 't':
                                        if(sscanf(argv[++i], "%dx%d", &tile_xsz, &tile_ysz) != 2) {
                                                fprintf(stderr, "-t must be followed by the tile size (WxH)\n");
@@ -67,6 +74,7 @@ void print_usage(const char *argv0)
 {
        printf("Usage: %s [options] <img1> [<img2> ... <imgN>]\n", argv0);
        printf("Options:\n");
+       printf(" -o <file>: output file\n");
        printf(" -t WxH: tile size (default 8x8)\n");
        printf(" -c: output C array\n");
        printf(" -s: output GNU assembler data\n");
@@ -176,10 +184,13 @@ int proc_image(const char *fname)
                        }
 
                        *mapptr++ = idx;
+                       if(i < 2) {
+                               printf("%2dx%-2d: %d\n", j, i, idx);
+                       }
 
                        sptr += tile.scansz;
                }
-               sptr += (tile_ysz - 1) * tile.scansz;
+               sptr += (tile_ysz - 1) * tile.pitch;
        }
 
        ntiles = dynarr_size(tiles);
@@ -192,12 +203,15 @@ int proc_image(const char *fname)
        tile = tiles[0];
        tile.height = ntiles * tile_ysz;
 
-       outfile = alloca(strlen(basename) + 5);
+       if(output_filename) {
+               outfile = (char*)output_filename;
+       } else {
+               outfile = alloca(strlen(basename) + 5);
+               sprintf(outfile, "%s%s", basename, otype_suffix[outmode]);
+       }
 
        switch(outmode) {
        case OUT_IMG:
-               sprintf(outfile, "%s.png", basename);
-
                if(save_image(&tile, outfile) == -1) {
                        fprintf(stderr, "failed to write output image\n");
                        goto err;
@@ -205,8 +219,6 @@ int proc_image(const char *fname)
                break;
 
        case OUT_C:
-               sprintf(outfile, "%s.c", basename);
-
                if(!(fp = fopen(outfile, "w"))) {
                        fprintf(stderr, "failed to open output file: %s: %s\n", outfile, strerror(errno));
                        goto err;
@@ -217,7 +229,6 @@ int proc_image(const char *fname)
                break;
 
        case OUT_ASM:
-               sprintf(outfile, "%s.s", basename);
                /* TODO */
                break;
        }