OSDN Git Service

no bone
[nethackexpress/trunk.git] / sys / msdos / tile2bin.c
1 /*   SCCS Id: @(#)tile2bin.c   3.4     1995/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  * Edit History:
7  *
8  *      Initial Creation                        M.Allison       93/10/21
9  *      ifndef MONITOR_HEAP for heaputil.c      P.Winner        94/03/12
10  *      added Borland C _stklen variable        Y.Sapir         94/05/01
11  *      fixed to use text tiles from win/share  M.Allison       95/01/31
12  *
13  */
14
15
16 #include "hack.h"
17 #include "pcvideo.h"
18 #include "tile.h"
19 #include "pctiles.h"
20
21 #include <dos.h>
22 #ifndef MONITOR_HEAP
23 #include <stdlib.h>
24 #endif
25 #include <time.h>
26
27 #ifdef __GO32__
28 #include <unistd.h>
29 #endif
30
31 #if defined(_MSC_VER) && _MSC_VER >= 700
32 #pragma warning(disable:4309)   /* initializing */
33 #pragma warning(disable:4018)   /* signed/unsigned mismatch */
34 #pragma warning(disable:4131)   /* old style declarator */
35 #pragma warning(disable:4127)   /* conditional express. is constant */
36 #endif
37
38 #ifdef __BORLANDC__
39 extern unsigned _stklen = STKSIZ;
40 #endif
41
42 extern char *FDECL(tilename, (int, int));
43
44 #ifdef PLANAR_FILE
45 char masktable[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
46 char charcolors[MAXCOLORMAPSIZE];
47 #ifdef OVERVIEW_FILE
48 struct overview_planar_cell_struct planetile;
49 #else
50 struct planar_cell_struct planetile;
51 #endif
52 FILE *tibfile1;
53 #endif
54
55 #ifdef PACKED_FILE
56 char packtile[TILE_Y][TILE_X];
57 FILE *tibfile2;
58 #endif
59
60 int num_colors;
61 pixel pixels[TILE_Y][TILE_X];
62 struct tibhdr_struct tibheader;
63
64 static void FDECL(write_tibtile, (int));
65 static void FDECL(write_tibheader, (FILE *, struct tibhdr_struct *));
66 static void FDECL(build_tibtile,  (pixel (*)[TILE_X]));
67
68 #ifndef OVERVIEW_FILE
69 char *tilefiles[] = {   "../win/share/monsters.txt",
70                         "../win/share/objects.txt",
71                         "../win/share/other.txt"};
72 #else
73 char *tilefiles[] = {   "../win/share/monthin.txt",
74                         "../win/share/objthin.txt",
75                         "../win/share/oththin.txt"};
76 #endif
77
78 int tilecount;
79 int filenum;
80 int paletteflag;
81
82 int
83 main(argc, argv)
84 int argc;
85 char *argv[];
86 {
87         int i;
88         struct tm *newtime;
89         time_t aclock;
90         char *paletteptr;
91
92         if (argc != 1) {
93                 Fprintf(stderr, "usage: tile2bin (from the util directory)\n");
94                 exit(EXIT_FAILURE);
95         }
96
97 #ifdef PLANAR_FILE
98 # ifndef OVERVIEW_FILE
99         tibfile1 = fopen(NETHACK_PLANAR_TILEFILE, WRBMODE);
100 # else
101         tibfile1 = fopen(NETHACK_OVERVIEW_TILEFILE, WRBMODE);
102 # endif
103         if (tibfile1 == (FILE *)0) {
104                 Fprintf(stderr, "Unable to open output file %s\n",
105 # ifndef OVERVIEW_FILE
106                                 NETHACK_PLANAR_TILEFILE);
107 #else
108                                 NETHACK_OVERVIEW_TILEFILE);
109 #endif
110                 exit(EXIT_FAILURE);
111         }
112 #endif
113
114 #ifdef PACKED_FILE
115         tibfile2 = fopen(NETHACK_PACKED_TILEFILE, WRBMODE);
116         if (tibfile2 == (FILE *)0) {
117                 Fprintf(stderr, "Unable to open output file %s\n",
118                                 NETHACK_PACKED_TILEFILE);
119                 exit(EXIT_FAILURE);
120         }
121 #endif
122         time(&aclock);
123         newtime = localtime(&aclock);
124
125         tilecount = 0;
126         paletteflag = 0;        
127         filenum = 0;
128         while (filenum < 3) {
129                 if (!fopen_text_file(tilefiles[filenum], RDTMODE)) {
130                         Fprintf(stderr,
131                          "usage: tile2bin (from the util or src directory)\n");
132                         exit(EXIT_FAILURE);
133                 }
134                 num_colors = colorsinmap;
135                 if (num_colors > 62) {
136                         Fprintf(stderr, "too many colors (%d)\n", num_colors);
137                         exit(EXIT_FAILURE);
138                 }
139
140                 if (!paletteflag) {
141                         paletteptr = tibheader.palette;
142                         for (i = 0; i < num_colors; i++) {
143                                 *paletteptr++ = ColorMap[CM_RED][i],
144                                 *paletteptr++ = ColorMap[CM_GREEN][i],
145                                 *paletteptr++ = ColorMap[CM_BLUE][i];
146                         }
147                         paletteflag++;
148                 }
149
150                                 
151                 while (read_text_tile(pixels)) {
152                         build_tibtile(pixels);
153                         write_tibtile(tilecount);
154                         tilecount++;
155                 }
156
157                 (void) fclose_text_file();
158                 ++filenum;
159         }
160
161 #  if defined(_MSC_VER)
162         tibheader.compiler = MSC_COMP;
163 #  elif defined(__BORLANDC__)
164         tibheader.compiler = BC_COMP;
165 #  elif defined(__GO32__)
166         tibheader.compiler = DJGPP_COMP;
167 #  else
168         tibheader.compiler = OTHER_COMP;
169 #  endif
170         
171         strncpy(tibheader.ident,
172                 "NetHack 3.4 MSDOS Port binary tile file", 80);
173         strncpy(tibheader.timestamp, asctime(newtime), 24);
174         tibheader.timestamp[25] = '\0';
175         tibheader.tilecount = tilecount;
176         tibheader.numcolors = num_colors;
177 # ifdef PLANAR_FILE
178         tibheader.tilestyle = PLANAR_STYLE;
179         write_tibheader(tibfile1, &tibheader);
180         (void) fclose(tibfile1);
181 #  ifndef OVERVIEW_FILE
182         Fprintf(stderr, "Total of %d planar tiles written to %s.\n", 
183                 tilecount, NETHACK_PLANAR_TILEFILE);
184 #  else
185         Fprintf(stderr, "Total of %d planar tiles written to %s.\n", 
186                 tilecount, NETHACK_OVERVIEW_TILEFILE);
187 #  endif
188 # endif
189
190 # ifdef PACKED_FILE
191         tibheader.tilestyle = PACKED_STYLE;
192         write_tibheader(tibfile2, &tibheader);
193         Fprintf(stderr, "Total of %d packed tiles written to %s.\n", 
194                 tilecount, NETHACK_PACKED_TILEFILE);
195         (void) fclose(tibfile2);
196 # endif
197
198         exit(EXIT_SUCCESS);
199         /*NOTREACHED*/
200         return 0;
201 }
202
203
204 static void
205 write_tibheader(fileptr,tibhdr)
206 FILE *fileptr;
207 struct tibhdr_struct *tibhdr;
208 {
209
210         if (fseek(fileptr,0L,SEEK_SET)) {
211                 Fprintf(stderr, "Error writing header to tile file\n");
212         }
213         fwrite(tibhdr, sizeof(struct tibhdr_struct), 1, fileptr);
214 }
215
216 static void
217 build_tibtile(pixels)
218 pixel (*pixels)[TILE_X];
219 {
220         int i, j, k, co_off;
221         unsigned char co_mask,tmp;
222
223 #ifndef OVERVIEW_FILE
224         memset((void *)&planetile,0,sizeof(struct planar_cell_struct));
225 #else
226         memset((void *)&planetile,0,
227                 sizeof(struct overview_planar_cell_struct));
228 #endif
229         for (j = 0; j < TILE_Y; j++) {
230                 for (i = 0; i < TILE_X; i++) {
231                         for (k = 0; k < num_colors; k++) {                              
232                                 if (ColorMap[CM_RED][k] == pixels[j][i].r &&
233                                     ColorMap[CM_GREEN][k] == pixels[j][i].g &&
234                                     ColorMap[CM_BLUE][k] == pixels[j][i].b)
235                                         break;
236                         }
237                         if (k >= num_colors)
238                                 Fprintf(stderr, "color not in colormap!\n");
239 #ifdef PACKED_FILE
240                         packtile[j][i] = k;
241 #endif
242
243 #ifdef PLANAR_FILE
244                         if (i > 7) {
245                                 co_off = 1;
246                                 co_mask = masktable[i - 8];
247                         } else {
248                                 co_off = 0;
249                                 co_mask = masktable[i];
250                         }
251
252                         tmp  = planetile.plane[0].image[j][co_off];
253                         planetile.plane[0].image[j][co_off] = (k & 0x0008) ?
254                                 (tmp | co_mask) :
255                                 (tmp & ~co_mask);
256
257                         tmp  = planetile.plane[1].image[j][co_off];
258                         planetile.plane[1].image[j][co_off] = (k & 0x0004) ?
259                                 (tmp | co_mask) :
260                                 (tmp & ~co_mask);
261
262                         tmp  = planetile.plane[2].image[j][co_off];
263                         planetile.plane[2].image[j][co_off] = (k & 0x0002) ?
264                                 (tmp | co_mask) :
265                                 (tmp & ~co_mask);
266
267                         tmp  = planetile.plane[3].image[j][co_off];
268                         planetile.plane[3].image[j][co_off] = (k & 0x0001) ?
269                                 (tmp | co_mask) :
270                                 (tmp & ~co_mask);
271 #endif /* PLANAR_FILE */
272                 }
273         }
274 }
275
276 static void
277 write_tibtile(recnum)
278 int recnum;
279 {
280         long fpos;
281
282 #ifdef PLANAR_FILE
283 # ifndef OVERVIEW_FILE
284         fpos = ((long)(recnum) * (long)sizeof(struct planar_cell_struct)) +
285                 (long)TIBHEADER_SIZE;
286 # else
287         fpos = ((long)(recnum) * 
288                         (long)sizeof(struct overview_planar_cell_struct)) +
289                         (long)TIBHEADER_SIZE;
290 # endif
291         if (fseek(tibfile1,fpos,SEEK_SET)) {
292                 Fprintf(stderr, "Error seeking before planar tile write %d\n",
293                         recnum);
294         }
295 # ifndef OVERVIEW_FILE
296         fwrite(&planetile, sizeof(struct planar_cell_struct), 1, tibfile1);
297 # else
298         fwrite(&planetile,
299                 sizeof(struct overview_planar_cell_struct), 1, tibfile1);
300 # endif
301 #endif
302
303 #ifdef PACKED_FILE
304         fpos = ((long)(recnum) * (long)sizeof(packtile)) +
305                 (long)TIBHEADER_SIZE;
306         if (fseek(tibfile2,fpos,SEEK_SET)) {
307                 Fprintf(stderr, "Error seeking before packed tile write %d\n",
308                         recnum);
309         }
310         fwrite(&packtile, sizeof(packtile), 1, tibfile2);
311 #endif
312 }