#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)
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))
#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];
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",
#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;
main(int argc, char *argv[])
{
int i, j;
+ uchar *c;
if (argc != 2) {
Fprintf(stderr, "usage: %s outfile.bmp\n", argv[0]);
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;
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);
#else
xoffset += TILE_X;
#endif
- if (xoffset >= MAX_X) {
+ if (xoffset >= max_x) {
yoffset += TILE_Y;
xoffset = 0;
}
(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);
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)));
}
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);
{
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++) {
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*/