NHDT->ANH, nethack->anethack, nhdat->anhdat
[aNetHack.git] / sys / msdos / pctiles.c
blobbcf04ed995ddea0369c9ced5186bca14874699a8
1 /* aNetHack 0.0.1 pctiles.c $ANH-Date: 1432512791 2015/05/25 00:13:11 $ $ANH-Branch: master $:$ANH-Revision: 1.11 $ */
2 /* Copyright (c) aNetHack PC Development Team 1993, 1994 */
3 /* aNetHack may be freely redistributed. See license for details. */
4 /* */
5 /*
6 * pctiles.c - PC Graphical Tile Support Routines
8 *Edit History:
9 * Initial Creation M. Allison 93/10/30
13 #include "hack.h"
15 #ifdef USE_TILES
17 #if defined(__GO32__) || defined(__DJGPP__)
18 #include <unistd.h>
19 #define TILES_IN_RAM /* allow tiles to be read into ram */
20 #endif
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
32 #include "pcvideo.h"
33 #include "tile.h"
34 #include "pctiles.h"
36 STATIC_VAR FILE *tilefile;
37 STATIC_VAR FILE *tilefile_O;
38 extern short glyph2tile[]; /* in tile.c (made from tilemap.c) */
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
49 * Read the header/palette information at the start of the
50 * aNetHack.tib file.
52 * There is 1024 bytes (1K) of header information
53 * at the start of the file, including a palette.
56 int
57 ReadTileFileHeader(tibhdr, filestyle)
58 struct tibhdr_struct *tibhdr;
59 boolean filestyle;
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);
68 return 0;
72 * Open the requested tile file.
74 * aNetHack1.tib file is a series of
75 * 'struct planar_tile_struct' structures, one for each
76 * glyph tile.
78 * aNetHack2.tib file is a series of
79 * char arrays [TILE_Y][TILE_X] in dimensions, one for each
80 * glyph tile.
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.
87 int
88 OpenTileFile(tilefilename, filestyle)
89 char *tilefilename;
90 boolean filestyle;
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;
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*/
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;
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*/
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;
141 #ifdef DEBUG_RAMTILES
142 pline("%d tiles read into ram.", k);
143 mark_synch();
144 #endif
145 tiles_in_ram = TRUE;
148 #endif
149 return 0;
152 void
153 CloseTileFile(filestyle)
154 boolean filestyle;
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;
167 #endif
170 struct planar_cell_struct plancell;
171 struct overview_planar_cell_struct oplancell;
173 /* This routine retrieves the requested aNetHack 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.
180 * Todo: Investigate the possibility of loading the glyph
181 * tiles into extended or expanded memory using
182 * the MSC virtual memory routines.
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).
192 #ifdef PLANAR_FILE
194 ReadPlanarTileFile(tilenum, gp)
195 int tilenum;
196 struct planar_cell_struct **gp;
198 long fpos;
200 #ifdef TILES_IN_RAM
201 if (tiles_in_ram) {
202 *gp = ramtiles + tilenum;
203 return 0;
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);
213 *gp = &plancell;
214 return 0;
217 ReadPlanarTileFile_O(tilenum, gp)
218 int tilenum;
219 struct overview_planar_cell_struct **gp;
221 long fpos;
223 #ifdef TILES_IN_RAM
224 if (otiles_in_ram) {
225 *gp = oramtiles + tilenum;
226 return 0;
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);
238 *gp = &oplancell;
239 return 0;
241 #endif
243 #ifdef PACKED_FILE
245 ReadPackedTileFile(tilenum, pta)
246 int tilenum;
247 char (*pta)[TILE_X];
249 long fpos;
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);
258 return 0;
260 #endif
261 #endif /* USE_TILES */
263 /* pctiles.c */