tools: bmp2tiles write seperate .gfx and .pal files
authorRobin Krens <robin@robinkrens.nl>
Wed, 15 Jun 2022 13:52:54 +0000 (15:52 +0200)
committerRobin Krens <robin@robinkrens.nl>
Wed, 15 Jun 2022 13:52:54 +0000 (15:52 +0200)
tools/bmp2tiles/bmp2tiles.c

index 380481e..7da5a38 100644 (file)
@@ -2,7 +2,7 @@
  * File              : bmp2tiles.c
  * Author            : Robin Krens <robin@robinkrens.nl>
  * Date              : 04.06.2022
- * Last Modified Date: 12.06.2022
+ * Last Modified Date: 15.06.2022
  * Last Modified By  : Robin Krens <robin@robinkrens.nl>
  */
 
@@ -12,6 +12,7 @@
 #include <stdbool.h>
 #include <string.h>
 #include <unistd.h>
+#include <assert.h>
 #include <getopt.h>
 #include <SDL2/SDL_image.h>
 
@@ -93,6 +94,27 @@ void generate_4bpp_tile(unsigned char *buf, swan_gfx_t * gfx,
        }
 }
 
+FILE * openstream(const char * name, const char * prefix,
+               unsigned tile_nr, bool append)
+{
+       assert(strlen(name) < 200);
+       
+       FILE * stream;
+
+       unsigned buf_len = 256;
+       char buf[buf_len];
+       snprintf(buf, buf_len, "%s%d.%s", name, tile_nr, prefix);
+
+       //printf("%s\n", buf);
+       if (!append) { 
+               stream = fopen(buf, "w");
+       } else {
+               stream = fopen(buf, "a");
+       }
+
+       return stream;
+}
+
 int main(int argc, char *argv[])
 {
        SDL_Surface * rawbmp;
@@ -102,11 +124,15 @@ int main(int argc, char *argv[])
        char * infile;
        char outfile[256];
        bool bout = false;
+       const short fill = 0xFFFF;
+       bool seperate_gfx = false;
+       bool seperate_pal = false;
 
-       while ((opt = getopt(argc, argv, "po:sv:")) != -1) {
+       while ((opt = getopt(argc, argv, "po:sv")) != -1) {
                switch (opt) {
                        case 'p':
-                               printf("include palette");
+                               seperate_pal = true;
+                               printf("generate individual .pal for each tile");
                                break;
                        case 'o':
                                printf("output file %s\n", optarg);
@@ -114,9 +140,11 @@ int main(int argc, char *argv[])
                                bout = true;
                                break;
                        case 's':
-                               printf("generate gfx per 16x16 tile \n");
+                               seperate_gfx = true;
+                               printf("generate .gfx for each tile\n");
                                break;
                        case 'v':
+                               printf("verbose");
                                break;
                        default: /* '?' */
                                fprintf(stderr, "Usage: %s [-s] [-o output] file \n", argv[0]);
@@ -125,7 +153,7 @@ int main(int argc, char *argv[])
         }
 
        if (optind >= argc) {
-               fprintf(stderr, "Expected infile\n");
+               fprintf(stderr, "Expected input .bmp file!\n");
                fprintf(stderr, "Usage: %s [-s] [-o output] [file]\n", argv[0]);
                exit(EXIT_FAILURE);
        }
@@ -138,58 +166,70 @@ int main(int argc, char *argv[])
                exit(EXIT_FAILURE);
        }
 
-       swan_gfx_t * gfx = malloc(sizeof(swan_gfx_t));
+       swan_gfx_t * gfx = calloc(1, sizeof(swan_gfx_t));
        int ret = set_info(gfx, rawbmp);
 
        if (ret != 0)
                goto cleanup;
 
-       unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ);
-       
-       if (!bout) 
-               gfxstream = fopen("out.gfx", "w");
-       else { 
-               printf("opening %s\n", outfile);
-               gfxstream = fopen(outfile, "w");
+       unsigned char *tile_buf =  calloc(TILE_SZ, sizeof(unsigned char));
+
+       if (!bout) {
+               sprintf(outfile, "out");
        }
        
-       if (!gfxstream) {
-               fprintf(stderr, "can not open file for writing!\n");
-               goto close;
-       }
+       fprintf(stdout, "generating %d tile(s) \
+                       in %d gfx file(s) \
+                       with %d pal file(s)\n",
+                       gfx->row_tiles * gfx->col_tiles,
+                       seperate_gfx ? gfx->row_tiles * gfx->col_tiles : 1,
+                       seperate_pal ? gfx->row_tiles * gfx->col_tiles : 1);
+
+       
+       for (int i = 0, cnt = 0; i < gfx->row_tiles; ++i) {
+               for (int j = 0; j < gfx->col_tiles; ++j, ++cnt) {
+
+                       if (i == 0 && j == 0) {
+                               gfxstream = openstream(outfile, "gfx", 0, false);
+                               palstream = openstream(outfile, "pal", 0, false);
+                       } else {
+                               if (seperate_gfx) {
+                                       gfxstream = openstream(outfile, "gfx", cnt, false);
+                               }
+                               if (seperate_pal) {
+                                       palstream = openstream(outfile, "pal", cnt, false);
+                               }
+                       }
 
-       for (int i = 0; i < gfx->row_tiles; ++i) {
-               for (int j = 0; j < gfx->col_tiles; ++j) {
-                       //printf("row[%d]col[%d]\n",i,j);
                        generate_4bpp_tile(tile_buf, gfx, true);
                        int ret = fwrite(tile_buf, sizeof(unsigned char), TILE_SZ, gfxstream);
                        if (ret != TILE_SZ) {
                                fprintf(stderr, "failed to convert, only write %d instead of %d\n", ret, TILE_SZ);
+                               fclose(gfxstream);
                                goto cleanup;
                        }
+
+                       fwrite(gfx->outpal, sizeof(unsigned short), 8, palstream);
+                       for (int i = 0; i < 8; ++i) 
+                               ret += fwrite(&fill, sizeof(unsigned short), 1, palstream);
+                       
                        gfx->data += 8; /* next column */
-                       //printf("\n");
+                       
+                       if (gfx->row_tiles == (i-1) && gfx->col_tiles == (j-1)) {
+                               fclose(gfxstream);
+                               fclose(palstream);
+                       } else {
+                               if (seperate_gfx) 
+                                       fclose(gfxstream);
+                               if (seperate_pal)
+                                       fclose(palstream);
+                       }
                }
                gfx->data += (gfx->col_tiles * 64) - (8 * gfx->col_tiles);
        }
        
-       palstream = fopen("out.pal", "w");
-       if (!palstream) {
-               fprintf(stderr, "can not open file for writing!\n");
-               fclose(palstream);
-               goto close;
-       }
-
-       fwrite(gfx->outpal, sizeof(unsigned short), 8, palstream);
-       const short fill = 0xFFFF;
-       for (int i = 0; i < 8; ++i) 
-               fwrite(&fill, sizeof(unsigned short), 1, palstream);
-
-       fclose(palstream);
-
-close:
-       fclose(gfxstream);
 cleanup:
        free(gfx);
+       free(tile_buf);
        SDL_FreeSurface(rawbmp);
 }