OSDN Git Service

import nethack-3.6.0
[jnethack/source.git] / sys / msdos / pctiles.c
1 /* NetHack 3.6  pctiles.c       $NHDT-Date: 1432512791 2015/05/25 00:13:11 $  $NHDT-Branch: master $:$NHDT-Revision: 1.11 $ */
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 /*
49  * Read the header/palette information at the start of the
50  * NetHack.tib file.
51  *
52  * There is 1024 bytes (1K) of header information
53  * at the start of the file, including a palette.
54  *
55  */
56 int
57 ReadTileFileHeader(tibhdr, filestyle)
58 struct tibhdr_struct *tibhdr;
59 boolean filestyle;
60 {
61     FILE *x;
62     x = filestyle ? tilefile_O : tilefile;
63     if (fseek(x, 0L, SEEK_SET)) {
64         return 1;
65     } else {
66         fread(tibhdr, sizeof(struct tibhdr_struct), 1, x);
67     }
68     return 0;
69 }
70
71 /*
72  * Open the requested tile file.
73  *
74  * NetHack1.tib file is a series of
75  * 'struct planar_tile_struct' structures, one for each
76  * glyph tile.
77  *
78  * NetHack2.tib file is a series of
79  * char arrays [TILE_Y][TILE_X] in dimensions, one for each
80  * glyph tile.
81  *
82  * There is 1024 bytes (1K) of header information
83  * at the start of each .tib file. The first glyph tile starts at
84  * location 1024.
85  *
86  */
87 int
88 OpenTileFile(tilefilename, filestyle)
89 char *tilefilename;
90 boolean filestyle;
91 {
92 #ifdef TILES_IN_RAM
93     int k;
94 #endif
95     if (filestyle) {
96         tilefile_O = fopen(tilefilename, "rb");
97         if (tilefile_O == (FILE *) 0)
98             return 1;
99     } else {
100         tilefile = fopen(tilefilename, "rb");
101         if (tilefile == (FILE *) 0)
102             return 1;
103     }
104 #ifdef TILES_IN_RAM
105     if (iflags.preload_tiles) {
106         if (filestyle) {
107             struct overview_planar_cell_struct *gp;
108             long ram_needed =
109                 sizeof(struct overview_planar_cell_struct) * total_tiles_used;
110             if (fseek(tilefile_O, (long) TIBHEADER_SIZE,
111                       SEEK_SET)) { /*failure*/
112             }
113             oramtiles =
114                 (struct overview_planar_cell_struct *) alloc(ram_needed);
115             /* Todo: fall back to file method here if alloc failed */
116             gp = oramtiles;
117             for (k = 0; k < total_tiles_used; ++k) {
118                 fread(gp, sizeof(struct overview_planar_cell_struct), 1,
119                       tilefile_O);
120                 ++gp;
121             }
122 #ifdef DEBUG_RAMTILES
123             pline("%d overview tiles read into ram.", k);
124             mark_synch();
125 #endif
126             otiles_in_ram = TRUE;
127         } else {
128             struct planar_cell_struct *gp;
129             long ram_needed =
130                 sizeof(struct planar_cell_struct) * total_tiles_used;
131             if (fseek(tilefile, (long) TIBHEADER_SIZE,
132                       SEEK_SET)) { /*failure*/
133             }
134             ramtiles = (struct planar_cell_struct *) alloc(ram_needed);
135             /* Todo: fall back to file method here if alloc failed */
136             gp = ramtiles;
137             for (k = 0; k < total_tiles_used; ++k) {
138                 fread(gp, sizeof(struct planar_cell_struct), 1, tilefile);
139                 ++gp;
140             }
141 #ifdef DEBUG_RAMTILES
142             pline("%d tiles read into ram.", k);
143             mark_synch();
144 #endif
145             tiles_in_ram = TRUE;
146         }
147     }
148 #endif
149     return 0;
150 }
151
152 void
153 CloseTileFile(filestyle)
154 boolean filestyle;
155 {
156     fclose(filestyle ? tilefile_O : tilefile);
157 #ifdef TILES_IN_RAM
158     if (!filestyle && tiles_in_ram) {
159         if (ramtiles)
160             free((genericptr_t) ramtiles);
161         tiles_in_ram = FALSE;
162     } else if (filestyle && otiles_in_ram) {
163         if (oramtiles)
164             free((genericptr_t) oramtiles);
165         otiles_in_ram = FALSE;
166     }
167 #endif
168 }
169
170 struct planar_cell_struct plancell;
171 struct overview_planar_cell_struct oplancell;
172
173 /* This routine retrieves the requested NetHack glyph tile
174  * from the planar style binary .tib file.
175  * This is currently done 'on demand', so if the player
176  * is running without a disk cache (ie. smartdrv) operating,
177  * things can really be slowed down.  We don't have any
178  * base memory under MSDOS, in which to store the pictures.
179  *
180  * Todo: Investigate the possibility of loading the glyph
181  *       tiles into extended or expanded memory using
182  *       the MSC virtual memory routines.
183  *
184  * Under an environment like djgpp, it should be possible to
185  * read the entire set of glyph tiles into a large
186  * array of 'struct planar_cell_struct' structures at
187  * game initialization time, and recall them from the array
188  * as needed.  That should speed things up (at the cost of
189  * increasing the memory requirement - can't have everything).
190  *
191  */
192 #ifdef PLANAR_FILE
193 int
194 ReadPlanarTileFile(tilenum, gp)
195 int tilenum;
196 struct planar_cell_struct **gp;
197 {
198     long fpos;
199
200 #ifdef TILES_IN_RAM
201     if (tiles_in_ram) {
202         *gp = ramtiles + tilenum;
203         return 0;
204     }
205 #endif
206     fpos = ((long) (tilenum) * (long) sizeof(struct planar_cell_struct))
207            + (long) TIBHEADER_SIZE;
208     if (fseek(tilefile, fpos, SEEK_SET)) {
209         return 1;
210     } else {
211         fread(&plancell, sizeof(struct planar_cell_struct), 1, tilefile);
212     }
213     *gp = &plancell;
214     return 0;
215 }
216 int
217 ReadPlanarTileFile_O(tilenum, gp)
218 int tilenum;
219 struct overview_planar_cell_struct **gp;
220 {
221     long fpos;
222
223 #ifdef TILES_IN_RAM
224     if (otiles_in_ram) {
225         *gp = oramtiles + tilenum;
226         return 0;
227     }
228 #endif
229     fpos =
230         ((long) (tilenum) * (long) sizeof(struct overview_planar_cell_struct))
231         + (long) TIBHEADER_SIZE;
232     if (fseek(tilefile_O, fpos, SEEK_SET)) {
233         return 1;
234     } else {
235         fread(&oplancell, sizeof(struct overview_planar_cell_struct), 1,
236               tilefile_O);
237     }
238     *gp = &oplancell;
239     return 0;
240 }
241 #endif
242
243 #ifdef PACKED_FILE
244 int
245 ReadPackedTileFile(tilenum, pta)
246 int tilenum;
247 char (*pta)[TILE_X];
248 {
249     long fpos;
250
251     fpos =
252         ((long) (tilenum) * (long) (TILE_Y * TILE_X) + (long) TIBHEADER_SIZE);
253     if (fseek(tilefile, fpos, SEEK_SET)) {
254         return 1;
255     } else {
256         fread(pta, (TILE_Y * TILE_X), 1, tilefile);
257     }
258     return 0;
259 }
260 #endif
261 #endif /* USE_TILES */
262
263 /* pctiles.c */