OSDN Git Service

Initial Import
[nethackexpress/trunk.git] / sys / msdos / pctiles.c
1 /*   SCCS Id: @(#)pctiles.c   3.4     1995/07/31                     */
2 /*   Copyright (c) NetHack PC Development Team 1993, 1994           */
3 /*   NetHack may be freely redistributed.  See license for details. */
4 /*                                                                  */
5 /*
6  * pctiles.c - PC Graphical Tile Support Routines
7  *                                                  
8  *Edit History:
9  *     Initial Creation              M. Allison      93/10/30
10  *
11  */
12
13 #include "hack.h"
14
15 #ifdef USE_TILES
16
17 #if defined(__GO32__) || defined(__DJGPP__)
18 #include <unistd.h>
19 #define TILES_IN_RAM    /* allow tiles to be read into ram */
20 #endif
21
22 # if defined(_MSC_VER)
23 #  if _MSC_VER >= 700
24 #pragma warning(disable:4018)   /* signed/unsigned mismatch */
25 #pragma warning(disable:4127)   /* conditional expression is constant */
26 #pragma warning(disable:4131)   /* old style declarator */
27 #pragma warning(disable:4309)   /* initializing */
28 #  endif
29 #include <conio.h>
30 # endif
31
32 #include "pcvideo.h"
33 #include "tile.h"
34 #include "pctiles.h"
35
36 STATIC_VAR FILE *tilefile;
37 STATIC_VAR FILE *tilefile_O;
38 extern short glyph2tile[];              /* in tile.c (made from tilemap.c) */
39
40 #ifdef TILES_IN_RAM
41 struct planar_cell_struct *ramtiles;
42 struct overview_planar_cell_struct *oramtiles;
43 boolean tiles_in_ram = FALSE;
44 boolean otiles_in_ram = FALSE;
45 extern int total_tiles_used;            /* tile.c */
46 #endif
47
48 # ifdef OVLB
49
50 /*
51  * Read the header/palette information at the start of the
52  * NetHack.tib file.
53  *
54  * There is 1024 bytes (1K) of header information
55  * at the start of the file, including a palette.
56  *
57  */
58 int ReadTileFileHeader(tibhdr, filestyle)
59 struct tibhdr_struct *tibhdr;
60 boolean filestyle;
61 {
62         FILE *x;
63         x = filestyle ? tilefile_O : tilefile;
64         if (fseek(x,0L,SEEK_SET)) {
65                 return 1;
66         } else {
67                 fread(tibhdr, sizeof(struct tibhdr_struct), 1, x);
68         }
69         return 0;
70
71
72 /*
73  * Open the requested tile file.
74  *
75  * NetHack1.tib file is a series of
76  * 'struct planar_tile_struct' structures, one for each
77  * glyph tile.
78  *
79  * NetHack2.tib file is a series of
80  * char arrays [TILE_Y][TILE_X] in dimensions, one for each
81  * glyph tile.
82  *
83  * There is 1024 bytes (1K) of header information
84  * at the start of each .tib file. The first glyph tile starts at
85  * location 1024.
86  *
87  */
88 int
89 OpenTileFile(tilefilename, filestyle)
90 char *tilefilename;
91 boolean filestyle;
92 {
93 #ifdef TILES_IN_RAM
94         int k;
95 #endif
96         if (filestyle) { 
97                 tilefile_O = fopen(tilefilename,"rb");
98                 if (tilefile_O == (FILE *)0) return 1;
99         } else {
100                 tilefile = fopen(tilefilename,"rb");
101                 if (tilefile == (FILE *)0) return 1;
102         }
103 #ifdef TILES_IN_RAM
104     if (iflags.preload_tiles) {
105         if (filestyle) {
106             struct overview_planar_cell_struct *gp;
107             long ram_needed = sizeof(struct overview_planar_cell_struct) *
108                                 total_tiles_used;
109             if (fseek(tilefile_O,(long)TIBHEADER_SIZE, SEEK_SET)) { /*failure*/
110             }
111             oramtiles = (struct overview_planar_cell_struct *)alloc(ram_needed);
112             /* Todo: fall back to file method here if alloc failed */
113             gp = oramtiles;
114             for(k=0; k < total_tiles_used; ++k) {
115                 fread(gp, sizeof(struct overview_planar_cell_struct), 
116                         1, tilefile_O);
117                 ++gp;
118             }
119 #ifdef DEBUG_RAMTILES
120             pline("%d overview tiles read into ram.", k);
121             mark_synch();
122 #endif
123             otiles_in_ram = TRUE;
124         } else {
125             struct planar_cell_struct *gp;
126             long ram_needed = sizeof(struct planar_cell_struct) *
127                                 total_tiles_used;
128             if (fseek(tilefile,(long)TIBHEADER_SIZE, SEEK_SET)) { /*failure*/
129             }
130             ramtiles = (struct planar_cell_struct *)alloc(ram_needed);
131             /* Todo: fall back to file method here if alloc failed */
132             gp = ramtiles;
133             for(k=0; k < total_tiles_used; ++k) {
134                 fread(gp, sizeof(struct planar_cell_struct), 
135                         1, tilefile);
136                 ++gp;
137             }
138 #ifdef DEBUG_RAMTILES
139             pline("%d tiles read into ram.", k);
140             mark_synch();
141 #endif
142             tiles_in_ram = TRUE;
143         }
144     }
145 #endif
146         return 0;
147 }
148
149 void
150 CloseTileFile(filestyle)
151 boolean filestyle;
152 {
153         fclose(filestyle ? tilefile_O : tilefile);
154 #ifdef TILES_IN_RAM
155         if (!filestyle && tiles_in_ram) {
156                 if (ramtiles) free((genericptr_t) ramtiles);
157                 tiles_in_ram = FALSE;
158         } else if (filestyle && otiles_in_ram) {
159                 if (oramtiles) free((genericptr_t) oramtiles);
160                 otiles_in_ram = FALSE;
161         }
162 #endif
163 }
164 # endif /* OVLB      */
165
166 # ifdef OVL0
167
168 struct planar_cell_struct plancell;
169 struct overview_planar_cell_struct oplancell;
170
171 /* This routine retrieves the requested NetHack glyph tile
172  * from the planar style binary .tib file.
173  * This is currently done 'on demand', so if the player
174  * is running without a disk cache (ie. smartdrv) operating,
175  * things can really be slowed down.  We don't have any
176  * base memory under MSDOS, in which to store the pictures.
177  *
178  * Todo: Investigate the possibility of loading the glyph
179  *       tiles into extended or expanded memory using
180  *       the MSC virtual memory routines.
181  *
182  * Under an environment like djgpp, it should be possible to
183  * read the entire set of glyph tiles into a large
184  * array of 'struct planar_cell_struct' structures at
185  * game initialization time, and recall them from the array
186  * as needed.  That should speed things up (at the cost of
187  * increasing the memory requirement - can't have everything).
188  *
189  */
190 #  ifdef PLANAR_FILE
191 int ReadPlanarTileFile(tilenum,gp)
192 int tilenum;
193 struct planar_cell_struct **gp;
194 {
195         long fpos;
196
197 #ifdef TILES_IN_RAM
198         if (tiles_in_ram) {
199             *gp = ramtiles + tilenum;
200             return 0;
201         }
202 #endif
203         fpos = ((long)(tilenum) * (long)sizeof(struct planar_cell_struct)) +
204                 (long)TIBHEADER_SIZE;
205         if (fseek(tilefile,fpos,SEEK_SET)) {
206                 return 1;
207         } else {
208               fread(&plancell, sizeof(struct planar_cell_struct), 1, tilefile);
209         }
210         *gp = &plancell;
211         return 0;
212 }
213 int ReadPlanarTileFile_O(tilenum,gp)
214 int tilenum;
215 struct overview_planar_cell_struct **gp;
216 {
217         long fpos;
218
219 #ifdef TILES_IN_RAM
220         if (otiles_in_ram) {
221             *gp = oramtiles + tilenum;
222             return 0;
223         }
224 #endif
225         fpos = ((long)(tilenum) * 
226                 (long)sizeof(struct overview_planar_cell_struct)) +
227                 (long)TIBHEADER_SIZE;
228         if (fseek(tilefile_O,fpos,SEEK_SET)) {
229                 return 1;
230         } else {
231                 fread(&oplancell, sizeof(struct overview_planar_cell_struct),
232                         1, tilefile_O);
233         }
234         *gp = &oplancell;
235         return 0;
236 }
237 #  endif
238
239 #  ifdef PACKED_FILE
240 int ReadPackedTileFile(tilenum,pta)
241 int tilenum;
242 char (*pta)[TILE_X];
243 {
244         long fpos;
245         
246         fpos = ((long)(tilenum) * (long)(TILE_Y * TILE_X) +
247                 (long)TIBHEADER_SIZE;
248         if (fseek(tilefile,fpos,SEEK_SET)) {
249                 return 1;
250         } else {
251                 fread(pta, (TILE_Y * TILE_X), 1, tilefile);
252         }
253         return 0;
254 }
255 #  endif
256 # endif /* OVL0 */
257 #endif /* USE_TILES */
258
259 /* pctiles.c */