From a22b56174fd3cdfcd6101f9f99c17b553aa22811 Mon Sep 17 00:00:00 2001 From: nhmall Date: Thu, 12 Jan 2023 08:51:59 -0500 Subject: [PATCH] fix actual problem of #955 by counting the tiles --- win/share/monsters.txt | 2 - win/share/objects.txt | 2 - win/share/other.txt | 2 - win/share/tile2bmp.c | 106 +++++++++++++++++++++++++---------------- 4 files changed, 64 insertions(+), 48 deletions(-) diff --git a/win/share/monsters.txt b/win/share/monsters.txt index 3496f08a9..d222bfc25 100644 --- a/win/share/monsters.txt +++ b/win/share/monsters.txt @@ -1,6 +1,4 @@ # monsters.txt - tile defintions for monsters -# Note: if the number of monster tiles, object tiles, or other tiles -# changes, 'MAGICTILENO' in tile2bmp.c needs to be updated to match! # Note too: lines beginning with '# tile ' are not comments; other # lines beginning with '#' are. . = (71, 108, 108) diff --git a/win/share/objects.txt b/win/share/objects.txt index 8d4bf8474..ed25ad164 100644 --- a/win/share/objects.txt +++ b/win/share/objects.txt @@ -1,6 +1,4 @@ # objects.txt - tile defintions for objects -# Note: if the number of monster tiles, object tiles, or other tiles -# changes, 'MAGICTILENO' in tile2bmp.c needs to be updated to match! # Note too: lines beginning with '# tile ' are not comments; other # lines beginning with '#' are. . = (71, 108, 108) diff --git a/win/share/other.txt b/win/share/other.txt index bde9d191c..550795899 100644 --- a/win/share/other.txt +++ b/win/share/other.txt @@ -1,6 +1,4 @@ # other.txt - tile defintions for 'other' (walls, furniture, explosions) -# Note: if the number of monster tiles, object tiles, or other tiles -# changes, 'MAGICTILENO' in tile2bmp.c needs to be updated to match! # Note too: lines beginning with '# tile ' are not comments; other # lines beginning with '#' are. . = (71, 108, 108) diff --git a/win/share/tile2bmp.c b/win/share/tile2bmp.c index 1548aabe5..909cb85c9 100644 --- a/win/share/tile2bmp.c +++ b/win/share/tile2bmp.c @@ -20,6 +20,7 @@ #include "tile.h" extern void monst_globals_init(void); extern void objects_globals_init(void); +static void examine_tilefiles(void); #include #if defined(UINT32_MAX) && defined(INT32_MAX) && defined(UINT16_MAX) @@ -53,24 +54,6 @@ extern void objects_globals_init(void); extern char *tilename(int, int); -/* The numbers in the following calculation are the - count of tiles present in: - monsters.txt objects.txt other.txt monsters.txt */ -#define MAGICTILENO (789 + 477 + 240 + 789) - -#if BITCOUNT == 4 -#define MAX_X 320 /* 2 per byte, 4 bits per pixel */ -#define MAX_Y 480 -#else -#if (TILE_X == 32) -#define MAX_X (32 * 40) -#define MAX_Y ((MAGICTILENO * 32) / 40) * 2 -#else -#define MAX_X (16 * 40) -#define MAX_Y (((16 * MAGICTILENO) / 40) + 16) -#endif -#endif - /* GCC fix by Paolo Bonzini 1999/03/28 */ #ifdef __GNUC__ #define PACK __attribute__((packed)) @@ -154,17 +137,10 @@ struct tagBMP { #endif RGBQUAD bmaColors[RGBQUAD_COUNT]; #endif -#if (COLORS_IN_USE == 16) - uchar packtile[MAX_Y][MAX_X]; -#else - uchar packtile[MAX_Y][MAX_X]; -/* uchar packtile[TILE_Y][TILE_X]; */ -#endif -} PACK bmp; + uchar packtile; /* start */ +} PACK bmp, *newbmp; #pragma pack() -#define BMPFILESIZE (sizeof(struct tagBMP)) - FILE *tibfile2; pixel tilepixels[TILE_Y][TILE_X]; @@ -173,6 +149,7 @@ 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", @@ -183,6 +160,9 @@ const char *tilefiles[] = { #endif }; +int tilecnt[SIZE(tilefiles)]; +int max_x, max_y; +int magictileno = 0, bmpsize; int num_colors = 0; int tilecount; int max_tiles_in_row = 40; @@ -200,6 +180,7 @@ int main(int argc, char *argv[]) { int i, j; + uchar *c; if (argc != 2) { Fprintf(stderr, "usage: %s outfile.bmp\n", argv[0]); @@ -219,6 +200,20 @@ main(int argc, char *argv[]) objects_globals_init(); monst_globals_init(); + examine_tilefiles(); + for (i = 0; i < SIZE(tilecnt); ++i) + magictileno += tilecnt[i]; + /* count monsters twice for grayscale variation */ + magictileno += tilecnt[0]; + + max_x = 16 * 40; + max_y = ((16 * magictileno) / 40) + 16; + bmpsize = (sizeof bmp - sizeof bmp.packtile) + (max_y * (max_x * sizeof(uchar))); + newbmp = malloc(bmpsize); + if (!newbmp) { + printf("memory allocation failure, %d %d, aborting.\n", + bmpsize, magictileno); + } tilecount = 0; xoffset = yoffset = 0; initflag = 0; @@ -246,23 +241,26 @@ main(int argc, char *argv[]) build_bmfh(&bmp.bmfh); bmih = &bmp.bmih; build_bmih(bmih); - for (i = 0; i < MAX_Y; ++i) - for (j = 0; j < MAX_X; ++j) - bmp.packtile[i][j] = (uchar) 0; for (i = 0; i < num_colors; i++) { bmp.bmaColors[i].rgbRed = ColorMap[CM_RED][i]; bmp.bmaColors[i].rgbGreen = ColorMap[CM_GREEN][i]; bmp.bmaColors[i].rgbBlue = ColorMap[CM_BLUE][i]; bmp.bmaColors[i].rgbReserved = 0; } + *newbmp = bmp; + for (i = 0; i < max_y; ++i) + for (j = 0; j < max_x; ++j) { + c = &newbmp->packtile + ((i * max_x) + j); + *c = (uchar) 0; + } initflag = 1; } set_grayscale(pass == 3); /* printf("Colormap initialized\n"); */ while (read_text_tile(tilepixels)) { - if (tilecount >= MAGICTILENO) { + if (tilecount >= magictileno) { Fprintf(stderr, "tile2bmp: more than %d tiles!\n", - MAGICTILENO); + magictileno); exit(EXIT_FAILURE); } build_bmptile(tilepixels); @@ -272,7 +270,7 @@ main(int argc, char *argv[]) #else xoffset += TILE_X; #endif - if (xoffset >= MAX_X) { + if (xoffset >= max_x) { yoffset += TILE_Y; xoffset = 0; } @@ -280,7 +278,7 @@ main(int argc, char *argv[]) (void) fclose_text_file(); ++pass; } - fwrite(&bmp, sizeof bmp, 1, fp); + fwrite(newbmp, bmpsize, 1, fp); fclose(fp); Fprintf(stderr, "Total of %d tiles written to %s.\n", tilecount, bmpname); @@ -295,10 +293,10 @@ static void build_bmfh(BITMAPFILEHEADER* pbmfh) { pbmfh->bfType = leshort(0x4D42); - pbmfh->bfSize = lelong(BMPFILESIZE); + pbmfh->bfSize = lelong(bmpsize); pbmfh->bfReserved1 = (UINT32) 0; pbmfh->bfReserved2 = (UINT32) 0; - pbmfh->bfOffBits = lelong(sizeof(bmp.bmfh) + sizeof(bmp.bmih) + pbmfh->bfOffBits = lelong(sizeof bmp.bmfh + sizeof bmp.bmih + (RGBQUAD_COUNT * sizeof(RGBQUAD))); } @@ -309,11 +307,11 @@ build_bmih(UNALIGNED_POINTER BITMAPINFOHEADER* pbmih) int w, h; pbmih->biSize = lelong(sizeof(bmp.bmih)); #if BITCOUNT == 4 - pbmih->biWidth = lelong(w = MAX_X * 2); + pbmih->biWidth = lelong(w = max_x * 2); #else - pbmih->biWidth = lelong(w = MAX_X); + pbmih->biWidth = lelong(w = max_x); #endif - pbmih->biHeight = lelong(h = MAX_Y); + pbmih->biHeight = lelong(h = max_y); pbmih->biPlanes = leshort(1); #if BITCOUNT == 4 pbmih->biBitCount = leshort(4); @@ -358,6 +356,7 @@ build_bmptile(pixel(*pixels)[TILE_X]) { int cur_x, cur_y, cur_color, apply_color; int x, y; + uchar *c; for (cur_y = 0; cur_y < TILE_Y; cur_y++) { for (cur_x = 0; cur_x < TILE_X; cur_x++) { @@ -370,19 +369,42 @@ build_bmptile(pixel(*pixels)[TILE_X]) if (cur_color >= num_colors) Fprintf(stderr, "color not in colormap! (tile #%d)\n", tilecount); - y = (MAX_Y - 1) - (cur_y + yoffset); + y = (max_y - 1) - (cur_y + yoffset); apply_color = cur_color; #if BITCOUNT == 4 x = (cur_x / 2) + xoffset; - bmp.packtile[y][x] = (cur_x % 2 != 0) + newbmp.packtile[y][x] = (cur_x % 2 != 0) ? (uchar) (bmp.packtile[y][x] | cur_color) : (uchar) (cur_color << 4); #else x = cur_x + xoffset; - bmp.packtile[y][x] = (uchar) apply_color; + c = &newbmp->packtile + ((y * max_x) + x); + *c = (uchar) apply_color; #endif } } } +static void +examine_tilefiles(void) +{ + FILE *fp2; + int i, tiles_in_file; + + for (i = 0; i < SIZE(tilefiles); ++i) { + tiles_in_file = 0; + fp2 = fopen(tilefiles[i], "r"); + if (fp2) { + char line[256]; + + while (fgets(line, sizeof line, fp2)) { + if (!strncmp(line, "# tile ", 7)) + tiles_in_file++; + } + (void) fclose(fp2); + tilecnt[i] = tiles_in_file; + } + } +} + /*tile2bmp.c*/