-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr_data.c
316 lines (267 loc) · 7.51 KB
/
r_data.c
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "doomdef.h"
#include "d_think.h"
#include "p_pspr.h"
#include "doomstat.h"
#include "w_wad.h"
#include "r_defs.h"
#include "r_draw.h"
#include "r_main.h"
#include "i_system.h"
#include "r_bsp.h"
#include "r_things.h"
#include "p_tick.h"
#include "i_system.h"
#include "p_tick.h"
#include "z_zone.h"
typedef struct
{
short originx;
short originy;
short patch;
short stepdir;
short colormap;
} __attribute__((packed)) mappatch_t;
typedef struct
{
char name[8];
char pad2[4];
short width;
short height;
char pad[4];
short patchcount;
mappatch_t patches[1];
} __attribute__((packed)) maptexture_t;
int firstcolormaplump, lastcolormaplump;
int firstflat, lastflat, numflats;
int firstspritelump, lastspritelump, numspritelumps;
int numtextures;
texture_t **textures;
int *textureheight;
int *flattranslation;
int *texturetranslation;
const byte *R_GetTextureColumn(const rpatch_t *texpatch, int col) {
while (col < 0)
col += texpatch->width;
col &= texpatch->widthmask;
return texpatch->columns[col].pixels;
}
static void R_InitTextures (void)
{
const maptexture_t *mtexture;
texture_t *texture;
const mappatch_t *mpatch;
texpatch_t *patch;
int i, j;
int maptex_lump[2] = {-1, -1};
const int *maptex;
const int *maptex1, *maptex2;
char name[9];
int names_lump;
const char *names;
const char *name_p;
int *patchlookup;
int totalwidth;
int nummappatches;
int offset;
int maxoff, maxoff2;
int numtextures1, numtextures2;
const int *directory;
int errors = 0;
name[8] = 0;
names = W_CacheLumpNum(names_lump = W_GetNumForName("PNAMES"));
nummappatches = *((const int *)names);
name_p = names+4;
patchlookup = malloc(nummappatches*sizeof(*patchlookup));
for (i=0 ; i<nummappatches ; i++)
{
strncpy (name,name_p+i*8, 8);
patchlookup[i] = W_CheckNumForName(name, ns_global);
if (patchlookup[i] == -1)
{
patchlookup[i] = W_CheckNumForName(name, ns_sprites);
}
}
W_UnlockLumpNum(names_lump);
maptex = maptex1 = W_CacheLumpNum(maptex_lump[0] = W_GetNumForName("TEXTURE1"));
numtextures1 = *maptex;
maxoff = W_LumpLength(maptex_lump[0]);
directory = maptex+1;
if (W_CheckNumForName("TEXTURE2", ns_global) != -1)
{
maptex2 = W_CacheLumpNum(maptex_lump[1] = W_GetNumForName("TEXTURE2"));
numtextures2 = *maptex2;
maxoff2 = W_LumpLength(maptex_lump[1]);
}
else
{
maptex2 = NULL;
numtextures2 = 0;
maxoff2 = 0;
}
numtextures = numtextures1 + numtextures2;
textures = Z_Malloc(numtextures*sizeof*textures, PU_STATIC, 0);
textureheight = Z_Malloc(numtextures*sizeof*textureheight, PU_STATIC, 0);
totalwidth = 0;
for (i=0 ; i<numtextures ; i++, directory++)
{
if (i == numtextures1)
{
maptex = maptex2;
maxoff = maxoff2;
directory = maptex+1;
}
offset = *directory;
if (offset > maxoff)
I_Error("R_InitTextures: Bad texture directory");
mtexture = (const maptexture_t *) ( (const byte *)maptex + offset);
texture = textures[i] =
Z_Malloc(sizeof(texture_t) +
sizeof(texpatch_t)*(mtexture->patchcount -1),
PU_STATIC, 0);
texture->width = mtexture->width;
texture->height = mtexture->height;
texture->patchcount = mtexture->patchcount;
{
int j;
for(j=0;j<sizeof(texture->name);j++)
texture->name[j]=mtexture->name[j];
}
mpatch = mtexture->patches;
patch = texture->patches;
for (j=0 ; j<texture->patchcount ; j++, mpatch++, patch++)
{
patch->originx = mpatch->originx;
patch->originy = mpatch->originy;
patch->patch = patchlookup[mpatch->patch];
if (patch->patch == -1)
{
I_Error("R_InitTextures: Missing patch %d in texture %.8s", mpatch->patch), texture->name;
++errors;
}
}
for (j=1; j*2 <= texture->width; j<<=1)
;
texture->widthmask = j-1;
textureheight[i] = texture->height<<FRACBITS;
totalwidth += texture->width;
}
free(patchlookup);
for (i=0; i<2; i++)
if (maptex_lump[i] != -1)
W_UnlockLumpNum(maptex_lump[i]);
if (errors)
I_Error("R_InitTextures: %d errors", errors);
texturetranslation =
Z_Malloc((numtextures+1)*sizeof*texturetranslation, PU_STATIC, 0);
for (i=0 ; i<numtextures ; i++)
texturetranslation[i] = i;
for (i = 0; i<numtextures; i++)
textures[i]->index = -1;
while (--i >= 0)
{
int j = W_LumpNameHash(textures[i]->name) % (unsigned) numtextures;
textures[i]->next = textures[j]->index;
textures[j]->index = i;
}
}
static void R_InitFlats(void)
{
int i;
firstflat = W_GetNumForName("F_START") + 1;
lastflat = W_GetNumForName("F_END") - 1;
numflats = lastflat - firstflat + 1;
flattranslation =
Z_Malloc((numflats+1)*sizeof(*flattranslation), PU_STATIC, 0);
for (i=0 ; i<numflats ; i++)
flattranslation[i] = i;
}
static void R_InitSpriteLumps(void)
{
firstspritelump = W_GetNumForName("S_START") + 1;
lastspritelump = W_GetNumForName("S_END") - 1;
numspritelumps = lastspritelump - firstspritelump + 1;
}
static void R_InitColormaps(void)
{
int i;
numcolormaps = 1;
colormaps = Z_Malloc(sizeof(*colormaps) * numcolormaps, PU_STATIC, 0);
colormaps[0] = (const lighttable_t *)W_CacheLumpName("COLORMAP");
}
int R_ColormapNumForName(const char *name)
{
return 0;
}
static inline int between(int l,int u,int x)
{ return (l > x ? l : x > u ? u : x); }
const lighttable_t* R_ColourMap(int lightlevel, int spryscale)
{
if (fixedcolormap) return fixedcolormap;
else {
if (curline)
if (curline->v1->y == curline->v2->y)
lightlevel -= 1 << LIGHTSEGSHIFT;
else
if (curline->v1->x == curline->v2->x)
lightlevel += 1 << LIGHTSEGSHIFT;
lightlevel += extralight << LIGHTSEGSHIFT;
return fullcolormap + between(0,NUMCOLORMAPS-1,
((256-lightlevel)*2*NUMCOLORMAPS/256) - 4
- (FixedMul(spryscale,pspriteiscale)/2 >> LIGHTSCALESHIFT)
)*256;
}
}
void R_InitData(void)
{
R_InitTextures();
R_InitFlats();
R_InitSpriteLumps();
R_InitColormaps();
}
int R_FlatNumForName(const char *name)
{
int i = W_CheckNumForName(name, ns_flats);
if (i == -1)
I_Error("R_FlatNumForName: %.8s not found", name);
return i - firstflat;
}
int R_CheckTextureNumForName(const char *name)
{
int i = 0;
if (*name != '-')
{
i = textures[W_LumpNameHash(name) % (unsigned) numtextures]->index;
while (i >= 0 && strncasecmp(textures[i]->name,name,8))
i = textures[i]->next;
}
return i;
}
int R_TextureNumForName(const char *name)
{
int i = R_CheckTextureNumForName(name);
if (i == -1)
I_Error("R_TextureNumForName: %.8s not found", name);
return i;
}
int R_SafeTextureNumForName(const char *name, int snum)
{
int i = R_CheckTextureNumForName(name);
if (i == -1) {
i = 0;
I_Error("R_SafeTextureNumForName: Bad texture '%s' in sidedef %d\n",name,snum);
}
return i;
}
void R_SetPatchNum(patchnum_t *patchnum, const char *name)
{
const rpatch_t *patch = R_CachePatchName(name);
patchnum->width = patch->width;
patchnum->height = patch->height;
patchnum->leftoffset = patch->leftoffset;
patchnum->topoffset = patch->topoffset;
patchnum->lumpnum = W_GetNumForName(name);
R_UnlockPatchName(name);
}