1 /*   SCCS Id: @(#)idx2bmp.c   3.3     95/01/26                     */
2 /*   Copyright (c) NetHack PC Development Team 1993, 1994, 1995     */
3 /*   NetHack may be freely redistributed.  See license for details. */
4 
5 /*
6  * This creates an idx2bmp.exe [indexfile [outputfile]]
7  *
8  * This takes an index file (index or indexfile)
9  * (generated via txt2bmp.exe -i  or otherwise)
10  * and uses the same parsing algorithm as in the alleg_init().
11  * The bmp files referenced by the index file are then taken and put into
12  * the output file (tiles.bmp or outputfile)
13  *
14  * Edit History:
15  *
16  *      Initial Creation                        W.Cheung  00/06/23
17  *
18  */
19 
20 #define alleg_mouse_unused
21 #define alleg_timer_unused
22 #define alleg_keyboard_unused
23 #define alleg_joystick_unused
24 #define alleg_sound_unused
25 #define alleg_gui_unused
26 
27 #include <allegro.h>
28 
29 #include "hack.h"
30 #include "pcvideo.h"
31 #include "tile.h"
32 #include "pctiles.h"
33 
34 #include <ctype.h>
35 #include <dos.h>
36 #ifndef MONITOR_HEAP
37 #include <stdlib.h>
38 #endif
39 #include <time.h>
40 
41 extern char *FDECL(tilename, (int, int));
42 
43 #define Fprintf (void) fprintf
44 #define Fclose  (void) fclose
45 
46 
47 static const char
48     *output_file = "tiles.bmp",
49     *index_file = "index";
50 
51 /* These next two functions were stolen from hacklib.c */
52 
53 char *
eos(s)54 eos(s)			/* return the end of a string (pointing at '\0') */
55     register char *s;
56 {
57     while (*s) s++;	/* s += strlen(s); */
58     return s;
59 }
60 
61 /* remove excess whitespace from a string buffer (in place) */
62 char *
mungspaces(bp)63 mungspaces(bp)
64 char *bp;
65 {
66     register char c, *p, *p2;
67     boolean was_space = TRUE;
68 
69     for (p = p2 = bp; (c = *p) != '\0'; p++) {
70 	if (c == '\t') c = ' ';
71 	if (c != ' ' || !was_space) *p2++ = c;
72 	was_space = (c == ' ');
73     }
74     if (was_space && p2 > bp) p2--;
75     *p2 = '\0';
76     return bp;
77 }
78 
79 int
main(argc,argv)80 main(argc, argv)
81 int argc;
82 char *argv[];
83 {
84         int i;
85         FILE *fp;
86         char            buf[BUFSZ];
87         char            *bufp;
88         BITMAP *bigtile_bmp = (BITMAP *)0;
89         BITMAP *tilebmp;
90         struct tm *newtime;
91         time_t aclock;
92         char filename[60];
93         int tile_x = 32, tile_y = 32;
94         int col, row;
95         boolean has_index = 0, has_output = 0;
96 
97         if (argc > 3) {
98 	    	Fprintf(stderr, "Bad arg count (%d).\n", argc-1);
99 	    	(void) fflush(stderr);
100                 exit(EXIT_FAILURE);
101         }
102         has_index = (argc > 1);
103         has_output = (argc > 2);
104 
105         set_color_depth(24);
106 
107         time(&aclock);
108         newtime = localtime(&aclock);
109 
110         /* Open the index file */
111 
112 	if (has_index) sprintf(filename, argv[1]);
113 	else sprintf(filename, index_file);
114         if ((fp = fopen(filename, "r")) == (FILE *)0)
115         {
116                 Fprintf(stderr, "Could not open index file '%s'!\n", filename);
117 	        exit(EXIT_FAILURE);
118         }
119 
120         i = 0;
121 
122         while(fgets(buf,120,fp))
123         {
124                 if (*buf == '#')
125                         continue;
126 
127                 bufp = eos(buf);
128                 while (--bufp > buf && isspace(*bufp))
129                         continue;
130 
131                 if (bufp <= buf)
132                         continue;               /* skip all-blank lines */
133                 else
134                         *(bufp + 1) = '\0';     /* terminate line */
135 
136                 /* find the '=' or ':' */
137                 bufp = index(buf, ':');
138                 if (!bufp)
139                 	continue;
140 
141 		*bufp = '\0';
142 
143                 bufp++; /* we only want what's after the ':' */
144 
145                 /* skip  whitespace between '=' and value */
146                 do { ++bufp; } while (isspace(*bufp));
147 
148                 bufp = mungspaces(bufp);
149 
150                 sprintf(filename, "%s", bufp);
151                 tilebmp = load_bitmap(filename, (RGB *)0);
152 
153                 if(!tilebmp) {
154                 	Fprintf(stderr, "Could not open file '%s', continuing.\n", filename);
155                 	continue;
156                 }
157 
158                 if (!bigtile_bmp) {
159                 	tile_x = tilebmp->w;
160                 	tile_y = tilebmp->h;
161 
162 	        	bigtile_bmp = create_bitmap(tile_x * TILES_PER_ROW, tile_y * TILES_PER_COL);
163                 }
164 
165 	    	col = (int)(i % TILES_PER_ROW);
166 		row = (int)(i / TILES_PER_ROW);
167 #ifdef DEBUG
168 		Fprintf(stderr, "col: %i row: %i\n", col, row);
169 #endif
170 		blit(tilebmp, bigtile_bmp, 0, 0, col * tile_x, row * tile_y, tile_x, tile_y);
171 
172 	                i++;
173         }
174 
175 
176 	if (has_output) sprintf(filename, argv[2]);
177 	else sprintf(filename, output_file);
178       	if (save_bitmap(filename, bigtile_bmp, 0)) {
179                 Fprintf(stderr, "Could not save bitmap '%s'!\n", filename);
180 	        exit(EXIT_FAILURE);
181       	}
182 
183         exit(EXIT_SUCCESS);
184         /*NOTREACHED*/
185         return 0;
186 }
187 
188