]> granicus.if.org Git - nethack/commitdiff
some tilebmp.c code cleanup
authornhmall <nhmall@nethack.org>
Mon, 16 Jan 2023 18:58:58 +0000 (13:58 -0500)
committernhmall <nhmall@nethack.org>
Mon, 16 Jan 2023 18:58:58 +0000 (13:58 -0500)
Remove some conditional code that isn't needed these days.

Use C99 fixed width integer types for all platforms, instead
of using it for some and Microsoft types for others.

win/share/tile2bmp.c

index a2d5a580c49ba6e237f3f83dc44020326b09a219..4edc3ba225985cbdaeccfc0c0d7f80ccd664567d 100644 (file)
@@ -10,8 +10,6 @@
  *
  */
 
-/* #pragma warning(4103:disable) */
-
 #ifndef __GNUC__
 #include "win32api.h"
 #endif
 #include "tile.h"
 extern void monst_globals_init(void);
 extern void objects_globals_init(void);
-static void examine_tilefiles(void);
-
-#include <stdint.h>
-#if defined(UINT32_MAX) && defined(INT32_MAX) && defined(UINT16_MAX)
-#define UINT8 uint8_t
-#define UINT16 uint16_t
-#define UINT32 uint32_t
-#define INT32 int32_t
-#else
-# ifdef _MSC_VER
-#define UINT8 unsigned char
-#define UINT16 unsigned short
-#define UINT32 unsigned long
-#define INT32 long
-# endif
-#endif
+static int examine_tilefiles(void);
+static int set_tilefile_path(const char *, const char *, char *, size_t);
 
 #if defined(_MSC_VER) && defined(_WIN64)
 #define UNALIGNED_POINTER __unaligned
@@ -43,17 +27,9 @@ static void examine_tilefiles(void);
 #define UNALIGNED_POINTER
 #endif
 
-#if (TILE_X == 32)
 #define COLORS_IN_USE 256
-#else
-/*#define COLORS_IN_USE 16 */ /* 16 colors */
-#define COLORS_IN_USE 256 /* 256 colors */
-#endif
-
 #define BITCOUNT 8
 
-extern char *tilename(int, int);
-
 /* GCC fix by Paolo Bonzini 1999/03/28 */
 #ifdef __GNUC__
 #define PACK __attribute__((packed))
@@ -71,8 +47,8 @@ leshort(short x)
 #endif
 }
 
-static INT32
-lelong(INT32 x)
+static int32_t
+lelong(int32_t x)
 {
 #ifdef __BIG_ENDIAN__
     return ((x & 0xff) << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00)
@@ -86,38 +62,36 @@ unsigned FITSuint_(unsigned long long, const char *, int);
 
 #ifdef __GNUC__
 typedef struct tagBMIH {
-    UINT32 biSize;
-    INT32 biWidth;
-    INT32 biHeight;
-    UINT16 biPlanes;
-    UINT16 biBitCount;
-    UINT32 biCompression;
-    UINT32 biSizeImage;
-    INT32 biXPelsPerMeter;
-    INT32 biYPelsPerMeter;
-    UINT32 biClrUsed;
-    UINT32 biClrImportant;
+    uint32_t biSize;
+    int32_t biWidth;
+    int32_t biHeight;
+    uint16_t biPlanes;
+    uint16_t biBitCount;
+    uint32_t biCompression;
+    uint32_t biSizeImage;
+    int32_t biXPelsPerMeter;
+    int32_t biYPelsPerMeter;
+    uint32_t biClrUsed;
+    uint32_t biClrImportant;
 } PACK BITMAPINFOHEADER;
 
 typedef struct tagBMFH {
-    UINT16 bfType;
-    UINT32 bfSize;
-    UINT16 bfReserved1;
-    UINT16 bfReserved2;
-    UINT32 bfOffBits;
+    uint16_t bfType;
+    uint32_t bfSize;
+    uint16_t bfReserved1;
+    uint16_t bfReserved2;
+    uint32_t bfOffBits;
 } PACK BITMAPFILEHEADER;
 
 typedef struct tagRGBQ {
-    UINT8 rgbBlue;
-    UINT8 rgbGreen;
-    UINT8 rgbRed;
-    UINT8 rgbReserved;
+    uint8_t rgbBlue;
+    uint8_t rgbGreen;
+    uint8_t rgbRed;
+    uint8_t rgbReserved;
 } PACK RGBQUAD;
-#define DWORD UINT32
-#define WORD UINT16
-#define BI_RGB 0L
-#define BI_RLE8 1L
-#define BI_RLE4 2L
+#define BI_RGB       0L
+#define BI_RLE8      1L
+#define BI_RLE4      2L
 #define BI_BITFIELDS 3L
 #endif /* __GNUC__ */
 #define RGBQUAD_COUNT 256
@@ -132,29 +106,28 @@ struct tagBMP {
 #pragma pack()
 
 FILE *tibfile2;
-
 pixel tilepixels[TILE_Y][TILE_X];
-
 static void build_bmfh(BITMAPFILEHEADER *);
 static void build_bmih(UNALIGNED_POINTER BITMAPINFOHEADER *);
 static void build_bmptile(pixel(*) [TILE_X]);
 
-/* monsters.txt must be first */
-const char *tilefiles[] = {
-#if (TILE_X == 32)
-    "../win/share/mon32.txt", "../win/share/obj32.txt",
-    "../win/share/oth32.txt",
-#else
-    "../win/share/monsters.txt", "../win/share/objects.txt",
-    "../win/share/other.txt",
+#define TILESETS 1
+static const char *const relative_tiledir = "../win/share/";
+/* monsters must be first because main uses it twice */
+const char *const tilefilenames[TILESETS][3] = {
+    {"monsters.txt", "objects.txt", "other.txt"},
+#if 0
+    {"mon32.txt", "obj32.txt", "oth32.txt"},
+    {"mon64.txt", "obj64.txt", "oth64.txt"},
 #endif
 };
 
-int tilecnt[SIZE(tilefiles)];
+int tilecnt[SIZE(tilefilenames[0])];
+int tilefileset = 0;
+int tiles_counted;
 int max_x, max_y;
 int magictileno = 0, bmpsize;
 int num_colors = 0;
-int tilecount;
 int max_tiles_in_row = 40;
 int tiles_in_row;
 int filenum;
@@ -171,26 +144,28 @@ main(int argc, char *argv[])
 {
     int i, j;
     uchar *c;
+    char tilefile_full_path[256] = { 0 };
 
     if (argc != 2) {
         Fprintf(stderr, "usage: %s outfile.bmp\n", argv[0]);
         exit(EXIT_FAILURE);
-    } else
-        strcpy(bmpname, argv[1]);
-
-#ifdef OBSOLETE
-    bmpfile2 = fopen(NETHACK_PACKED_TILEFILE, WRBMODE);
-    if (bmpfile2 == (FILE *) 0) {
-        Fprintf(stderr, "Unable to open output file %s\n",
-                NETHACK_PACKED_TILEFILE);
+    } else if (argv[1] == 0 || strlen(argv[1]) >= sizeof bmpname - 1) {
+        Fprintf(stderr, "invalid output bmp file name %s, aborting.\n",
+                argv[0]);
         exit(EXIT_FAILURE);
+    } else {
+        strcpy(bmpname, argv[1]);
     }
-#endif
 
     objects_globals_init();
     monst_globals_init();
 
-    examine_tilefiles();
+/*    tilefileset = (TILE_X == 64) ? 2 : (TILE_X == 32) ? 1 : 0; */
+    tilefileset = 0;
+    if (!examine_tilefiles()) {
+        Fprintf(stderr, "unable to open all of the tile files, aborting.\n");
+        exit(EXIT_FAILURE);
+    }
     for (i = 0; i < SIZE(tilecnt); ++i)
         magictileno += tilecnt[i];
     /* count monsters twice for grayscale variation */
@@ -206,7 +181,7 @@ main(int argc, char *argv[])
                 bmpsize, magictileno);
         exit(EXIT_FAILURE);
     }
-    tilecount = 0;
+    tiles_counted = 0;
     xoffset = yoffset = 0;
     initflag = 0;
     filenum = 0;
@@ -214,17 +189,25 @@ main(int argc, char *argv[])
     fp = fopen(bmpname, "wb");
     if (!fp) {
         printf("Error creating tile file %s, aborting.\n", bmpname);
-        exit(1);
+        exit(EXIT_FAILURE);
     }
     while (pass < 4) {
-        filenum = pass % (sizeof tilefiles / sizeof (char *));
-        if (!fopen_text_file(tilefiles[filenum], RDTMODE)) {
+        filenum = pass % SIZE(tilefilenames[tilefileset]);
+        if (!set_tilefile_path(relative_tiledir,
+                               tilefilenames[tilefileset][filenum],
+                               tilefile_full_path, sizeof tilefile_full_path)) {
+            Fprintf(stderr, "tile2bmp path issue %s %s %d\n",
+                    relative_tiledir, tilefilenames[tilefileset][filenum],
+                    (int) sizeof tilefile_full_path);
+            exit(EXIT_FAILURE);
+        }
+        if (!fopen_text_file(tilefile_full_path, RDTMODE)) {
             Fprintf(stderr, "usage: tile2bmp (from the util directory)\n");
             exit(EXIT_FAILURE);
         }
         num_colors = colorsinmap;
         if (num_colors > 62) {
-            Fprintf(stderr, "too many colors (%d)\n", num_colors);
+            Fprintf(stderr, "too many colors (%d), aborting.\n", num_colors);
             exit(EXIT_FAILURE);
         }
         if (!initflag) {
@@ -250,13 +233,13 @@ main(int argc, char *argv[])
         set_grayscale(pass == 3);
         /* printf("Colormap initialized\n"); */
         while (read_text_tile(tilepixels)) {
-            if (tilecount >= magictileno) {
+            if (tiles_counted >= magictileno) {
                 Fprintf(stderr, "tile2bmp: more than %d tiles!\n",
                         magictileno);
                 exit(EXIT_FAILURE);
             }
             build_bmptile(tilepixels);
-            tilecount++;
+            tiles_counted++;
             xoffset += TILE_X;
             if (xoffset >= max_x) {
                 yoffset += TILE_Y;
@@ -268,7 +251,8 @@ main(int argc, char *argv[])
     }
     fwrite(newbmp, bmpsize, 1, fp);
     fclose(fp);
-    Fprintf(stderr, "Total of %d tiles written to %s.\n", tilecount, bmpname);
+    Fprintf(stderr, "Total of %d tiles written to %s.\n",
+            tiles_counted, bmpname);
     free((genericptr_t) newbmp);
 
     exit(EXIT_SUCCESS);
@@ -283,8 +267,8 @@ build_bmfh(BITMAPFILEHEADER* pbmfh)
 {
     pbmfh->bfType = leshort(0x4D42);
     pbmfh->bfSize = lelong(bmpsize);
-    pbmfh->bfReserved1 = (UINT32) 0;
-    pbmfh->bfReserved2 = (UINT32) 0;
+    pbmfh->bfReserved1 = (uint32_t) 0;
+    pbmfh->bfReserved2 = (uint32_t) 0;
     pbmfh->bfOffBits = lelong(sizeof bmp.bmfh + sizeof bmp.bmih
                               + (RGBQUAD_COUNT * sizeof(RGBQUAD)));
 }
@@ -292,7 +276,7 @@ build_bmfh(BITMAPFILEHEADER* pbmfh)
 static void
 build_bmih(UNALIGNED_POINTER BITMAPINFOHEADER* pbmih)
 {
-    WORD cClrBits;
+    uint16_t cClrBits;
     int w, h;
     pbmih->biSize = lelong(sizeof(bmp.bmih));
     pbmih->biWidth = lelong(w = max_x);
@@ -321,14 +305,8 @@ build_bmih(UNALIGNED_POINTER BITMAPINFOHEADER* pbmih)
 #else
     pbmih->biClrUsed = lelong(RGBQUAD_COUNT);
 #endif
-
-#if (TILE_X == 16)
-    /* pbmih->biSizeImage = lelong(0); */
     pbmih->biSizeImage = lelong(((w * cClrBits + 31) & ~31) / 8 * h);
-#else
-    pbmih->biSizeImage = lelong(((w * cClrBits + 31) & ~31) / 8 * h);
-#endif
-    pbmih->biClrImportant = (DWORD) 0;
+    pbmih->biClrImportant = (uint32_t) 0;
 }
 
 static void
@@ -348,7 +326,7 @@ build_bmptile(pixel(*pixels)[TILE_X])
             }
             if (cur_color >= num_colors)
                 Fprintf(stderr, "color not in colormap! (tile #%d)\n",
-                        tilecount);
+                        tiles_counted);
             y = (max_y - 1) - (cur_y + yoffset);
             apply_color = cur_color;
             x = cur_x + xoffset;
@@ -358,15 +336,39 @@ build_bmptile(pixel(*pixels)[TILE_X])
     }
 }
 
-static void
+static int
+set_tilefile_path(const char *fdir, const char *fname, char *buf, size_t sz)
+{
+    size_t consuming = 0;
+
+    *buf = '\0';
+    consuming = strlen(fdir) + strlen(fname) + 1;
+    if (consuming > sz)
+        return 0;
+    Strcpy(buf, fdir);
+    Strcat(buf, fname);
+    return 1;
+}
+
+static int
 examine_tilefiles(void)
 {
     FILE *fp2;
     int i, tiles_in_file;
+    char tilefile_full_path[256];
 
-    for (i = 0; i < SIZE(tilefiles); ++i) {
+    for (i = 0; i < SIZE(tilefilenames[0]); ++i) {
         tiles_in_file = 0;
-        fp2 = fopen(tilefiles[i], "r");
+        if (!set_tilefile_path(relative_tiledir,
+                               tilefilenames[tilefileset][i],
+                               tilefile_full_path,
+                               sizeof tilefile_full_path)) {
+            Fprintf(stderr, "tile2bmp path issue %s/%s %d\n",
+                    relative_tiledir, tilefilenames[tilefileset][i],
+                    (int) sizeof tilefile_full_path);
+            return 0;
+        }
+        fp2 = fopen(tilefile_full_path, "r");
         if (fp2) {
             char line[256];
 
@@ -378,6 +380,7 @@ examine_tilefiles(void)
             tilecnt[i] = tiles_in_file;
         }
     }
+    return 1;
 }
 
 /*tile2bmp.c*/