File: pctiles.c

package info (click to toggle)
glhack 1.2-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 24,604 kB
  • ctags: 18,992
  • sloc: ansic: 208,570; cpp: 13,139; yacc: 2,005; makefile: 1,161; lex: 377; sh: 321; awk: 89; sed: 11
file content (259 lines) | stat: -rw-r--r-- 6,549 bytes parent folder | download | duplicates (23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*   SCCS Id: @(#)pctiles.c   3.4     1995/07/31		     */
/*   Copyright (c) NetHack PC Development Team 1993, 1994           */
/*   NetHack may be freely redistributed.  See license for details. */
/*                                                                  */
/*
 * pctiles.c - PC Graphical Tile Support Routines
 *                                                  
 *Edit History:
 *     Initial Creation              M. Allison      93/10/30
 *
 */

#include "hack.h"

#ifdef USE_TILES

#if defined(__GO32__) || defined(__DJGPP__)
#include <unistd.h>
#define TILES_IN_RAM	/* allow tiles to be read into ram */
#endif

# if defined(_MSC_VER)
#  if _MSC_VER >= 700
#pragma warning(disable:4018)	/* signed/unsigned mismatch */
#pragma warning(disable:4127)	/* conditional expression is constant */
#pragma warning(disable:4131)	/* old style declarator */
#pragma warning(disable:4309)	/* initializing */
#  endif
#include <conio.h>
# endif

#include "pcvideo.h"
#include "tile.h"
#include "pctiles.h"

STATIC_VAR FILE *tilefile;
STATIC_VAR FILE *tilefile_O;
extern short glyph2tile[];              /* in tile.c (made from tilemap.c) */

#ifdef TILES_IN_RAM
struct planar_cell_struct *ramtiles;
struct overview_planar_cell_struct *oramtiles;
boolean tiles_in_ram = FALSE;
boolean otiles_in_ram = FALSE;
extern int total_tiles_used;		/* tile.c */
#endif

# ifdef OVLB

/*
 * Read the header/palette information at the start of the
 * NetHack.tib file.
 *
 * There is 1024 bytes (1K) of header information
 * at the start of the file, including a palette.
 *
 */
int ReadTileFileHeader(tibhdr, filestyle)
struct tibhdr_struct *tibhdr;
boolean filestyle;
{
	FILE *x;
	x = filestyle ? tilefile_O : tilefile;
	if (fseek(x,0L,SEEK_SET)) {
		return 1;
	} else {
	  	fread(tibhdr, sizeof(struct tibhdr_struct), 1, x);
	}
	return 0;
} 

/*
 * Open the requested tile file.
 *
 * NetHack1.tib file is a series of
 * 'struct planar_tile_struct' structures, one for each
 * glyph tile.
 *
 * NetHack2.tib file is a series of
 * char arrays [TILE_Y][TILE_X] in dimensions, one for each
 * glyph tile.
 *
 * There is 1024 bytes (1K) of header information
 * at the start of each .tib file. The first glyph tile starts at
 * location 1024.
 *
 */
int
OpenTileFile(tilefilename, filestyle)
char *tilefilename;
boolean filestyle;
{
#ifdef TILES_IN_RAM
	int k;
#endif
	if (filestyle) { 
		tilefile_O = fopen(tilefilename,"rb");
		if (tilefile_O == (FILE *)0) return 1;
	} else {
		tilefile = fopen(tilefilename,"rb");
		if (tilefile == (FILE *)0) return 1;
	}
#ifdef TILES_IN_RAM
    if (iflags.preload_tiles) {
	if (filestyle) {
	    struct overview_planar_cell_struct *gp;
	    long ram_needed = sizeof(struct overview_planar_cell_struct) *
				total_tiles_used;
	    if (fseek(tilefile_O,(long)TIBHEADER_SIZE, SEEK_SET)) { /*failure*/
	    }
	    oramtiles = (struct overview_planar_cell_struct *)alloc(ram_needed);
	    /* Todo: fall back to file method here if alloc failed */
	    gp = oramtiles;
	    for(k=0; k < total_tiles_used; ++k) {
		fread(gp, sizeof(struct overview_planar_cell_struct), 
			1, tilefile_O);
		++gp;
	    }
#ifdef DEBUG_RAMTILES
	    pline("%d overview tiles read into ram.", k);
	    mark_synch();
#endif
	    otiles_in_ram = TRUE;
	} else {
	    struct planar_cell_struct *gp;
	    long ram_needed = sizeof(struct planar_cell_struct) *
				total_tiles_used;
	    if (fseek(tilefile,(long)TIBHEADER_SIZE, SEEK_SET)) { /*failure*/
	    }
	    ramtiles = (struct planar_cell_struct *)alloc(ram_needed);
	    /* Todo: fall back to file method here if alloc failed */
	    gp = ramtiles;
	    for(k=0; k < total_tiles_used; ++k) {
		fread(gp, sizeof(struct planar_cell_struct), 
			1, tilefile);
		++gp;
	    }
#ifdef DEBUG_RAMTILES
	    pline("%d tiles read into ram.", k);
	    mark_synch();
#endif
	    tiles_in_ram = TRUE;
	}
    }
#endif
	return 0;
}

void
CloseTileFile(filestyle)
boolean filestyle;
{
	fclose(filestyle ? tilefile_O : tilefile);
#ifdef TILES_IN_RAM
	if (!filestyle && tiles_in_ram) {
		if (ramtiles) free((genericptr_t) ramtiles);
		tiles_in_ram = FALSE;
	} else if (filestyle && otiles_in_ram) {
		if (oramtiles) free((genericptr_t) oramtiles);
		otiles_in_ram = FALSE;
	}
#endif
}
# endif /* OVLB      */

# ifdef OVL0

struct planar_cell_struct plancell;
struct overview_planar_cell_struct oplancell;

/* This routine retrieves the requested NetHack glyph tile
 * from the planar style binary .tib file.
 * This is currently done 'on demand', so if the player
 * is running without a disk cache (ie. smartdrv) operating,
 * things can really be slowed down.  We don't have any
 * base memory under MSDOS, in which to store the pictures.
 *
 * Todo: Investigate the possibility of loading the glyph
 *       tiles into extended or expanded memory using
 *       the MSC virtual memory routines.
 *
 * Under an environment like djgpp, it should be possible to
 * read the entire set of glyph tiles into a large
 * array of 'struct planar_cell_struct' structures at
 * game initialization time, and recall them from the array
 * as needed.  That should speed things up (at the cost of
 * increasing the memory requirement - can't have everything).
 *
 */
#  ifdef PLANAR_FILE
int ReadPlanarTileFile(tilenum,gp)
int tilenum;
struct planar_cell_struct **gp;
{
	long fpos;

#ifdef TILES_IN_RAM
	if (tiles_in_ram) {
	    *gp = ramtiles + tilenum;
	    return 0;
	}
#endif
	fpos = ((long)(tilenum) * (long)sizeof(struct planar_cell_struct)) +
		(long)TIBHEADER_SIZE;
	if (fseek(tilefile,fpos,SEEK_SET)) {
		return 1;
	} else {
	      fread(&plancell, sizeof(struct planar_cell_struct), 1, tilefile);
	}
	*gp = &plancell;
	return 0;
}
int ReadPlanarTileFile_O(tilenum,gp)
int tilenum;
struct overview_planar_cell_struct **gp;
{
	long fpos;

#ifdef TILES_IN_RAM
	if (otiles_in_ram) {
	    *gp = oramtiles + tilenum;
	    return 0;
	}
#endif
	fpos = ((long)(tilenum) * 
		(long)sizeof(struct overview_planar_cell_struct)) +
		(long)TIBHEADER_SIZE;
	if (fseek(tilefile_O,fpos,SEEK_SET)) {
		return 1;
	} else {
	  	fread(&oplancell, sizeof(struct overview_planar_cell_struct),
			1, tilefile_O);
	}
	*gp = &oplancell;
	return 0;
}
#  endif

#  ifdef PACKED_FILE
int ReadPackedTileFile(tilenum,pta)
int tilenum;
char (*pta)[TILE_X];
{
	long fpos;
	
	fpos = ((long)(tilenum) * (long)(TILE_Y * TILE_X) +
		(long)TIBHEADER_SIZE;
	if (fseek(tilefile,fpos,SEEK_SET)) {
		return 1;
	} else {
	  	fread(pta, (TILE_Y * TILE_X), 1, tilefile);
	}
	return 0;
}
#  endif
# endif /* OVL0 */
#endif /* USE_TILES */

/* pctiles.c */