diff options
author | Dave Chapman <dave@dchapman.com> | 2006-03-28 15:44:01 +0000 |
---|---|---|
committer | Dave Chapman <dave@dchapman.com> | 2006-03-28 15:44:01 +0000 |
commit | 47f4a458d636a889e955e68f896708f1276febc0 (patch) | |
tree | 99f770c02ef606f0abbdcd332ac39e69830d8007 /apps/plugins/doom/r_plane.c | |
parent | fff7d6157d56f233cad5c2003475e47a5ff809a7 (diff) | |
download | rockbox-47f4a458d636a889e955e68f896708f1276febc0.tar.gz rockbox-47f4a458d636a889e955e68f896708f1276febc0.zip |
Patch #2969 - Doom! Currently only working on the H300.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@9312 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/doom/r_plane.c')
-rw-r--r-- | apps/plugins/doom/r_plane.c | 434 |
1 files changed, 434 insertions, 0 deletions
diff --git a/apps/plugins/doom/r_plane.c b/apps/plugins/doom/r_plane.c new file mode 100644 index 0000000000..60d37f876b --- /dev/null +++ b/apps/plugins/doom/r_plane.c | |||
@@ -0,0 +1,434 @@ | |||
1 | /* Emacs style mode select -*- C++ -*- | ||
2 | *----------------------------------------------------------------------------- | ||
3 | * | ||
4 | * | ||
5 | * PrBoom a Doom port merged with LxDoom and LSDLDoom | ||
6 | * based on BOOM, a modified and improved DOOM engine | ||
7 | * Copyright (C) 1999 by | ||
8 | * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman | ||
9 | * Copyright (C) 1999-2000 by | ||
10 | * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This program is distributed in the hope that it will be useful, | ||
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
20 | * GNU General Public License for more details. | ||
21 | * | ||
22 | * You should have received a copy of the GNU General Public License | ||
23 | * along with this program; if not, write to the Free Software | ||
24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
25 | * 02111-1307, USA. | ||
26 | * | ||
27 | * DESCRIPTION: | ||
28 | * Here is a core component: drawing the floors and ceilings, | ||
29 | * while maintaining a per column clipping list only. | ||
30 | * Moreover, the sky areas have to be determined. | ||
31 | * | ||
32 | * MAXVISPLANES is no longer a limit on the number of visplanes, | ||
33 | * but a limit on the number of hash slots; larger numbers mean | ||
34 | * better performance usually but after a point they are wasted, | ||
35 | * and memory and time overheads creep in. | ||
36 | * | ||
37 | * For more information on visplanes, see: | ||
38 | * | ||
39 | * http://classicgaming.com/doom/editing/ | ||
40 | * | ||
41 | * Lee Killough | ||
42 | * | ||
43 | *-----------------------------------------------------------------------------*/ | ||
44 | |||
45 | #include "z_zone.h" /* memory allocation wrappers -- killough */ | ||
46 | |||
47 | #include "doomstat.h" | ||
48 | #include "w_wad.h" | ||
49 | #include "r_main.h" | ||
50 | #include "r_draw.h" | ||
51 | #include "r_things.h" | ||
52 | #include "r_sky.h" | ||
53 | #include "r_plane.h" | ||
54 | #include "rockmacros.h" | ||
55 | |||
56 | #define MAXVISPLANES 128 /* must be a power of 2 */ | ||
57 | |||
58 | static visplane_t *visplanes[MAXVISPLANES] IBSS_ATTR; // killough | ||
59 | static visplane_t *freetail; // killough | ||
60 | static visplane_t **freehead = &freetail; // killough | ||
61 | visplane_t *floorplane, *ceilingplane; | ||
62 | |||
63 | // killough -- hash function for visplanes | ||
64 | // Empirically verified to be fairly uniform: | ||
65 | |||
66 | #define visplane_hash(picnum,lightlevel,height) \ | ||
67 | ((unsigned)((picnum)*3+(lightlevel)+(height)*7) & (MAXVISPLANES-1)) | ||
68 | |||
69 | size_t maxopenings; | ||
70 | short *openings,*lastopening; | ||
71 | |||
72 | // Clip values are the solid pixel bounding the range. | ||
73 | // floorclip starts out SCREENHEIGHT | ||
74 | // ceilingclip starts out -1 | ||
75 | |||
76 | short floorclip[SCREENWIDTH], ceilingclip[SCREENWIDTH]; | ||
77 | |||
78 | // spanstart holds the start of a plane span; initialized to 0 at start | ||
79 | |||
80 | static int spanstart[SCREENHEIGHT]; // killough 2/8/98 | ||
81 | |||
82 | // | ||
83 | // texture mapping | ||
84 | // | ||
85 | |||
86 | static lighttable_t **planezlight; | ||
87 | static fixed_t planeheight; | ||
88 | |||
89 | // killough 2/8/98: make variables static | ||
90 | |||
91 | static fixed_t basexscale, baseyscale; | ||
92 | static fixed_t cachedheight[SCREENHEIGHT]; | ||
93 | static fixed_t cacheddistance[SCREENHEIGHT]; | ||
94 | static fixed_t cachedxstep[SCREENHEIGHT]; | ||
95 | static fixed_t cachedystep[SCREENHEIGHT]; | ||
96 | static fixed_t xoffs, yoffs; // killough 2/28/98: flat offsets | ||
97 | |||
98 | fixed_t yslope[SCREENHEIGHT], distscale[SCREENWIDTH]; | ||
99 | |||
100 | // | ||
101 | // R_InitPlanes | ||
102 | // Only at game startup. | ||
103 | // | ||
104 | void R_InitPlanes (void) | ||
105 | { | ||
106 | } | ||
107 | |||
108 | // | ||
109 | // R_MapPlane | ||
110 | // | ||
111 | // Uses global vars: | ||
112 | // planeheight | ||
113 | // ds_source | ||
114 | // basexscale | ||
115 | // baseyscale | ||
116 | // viewx | ||
117 | // viewy | ||
118 | // xoffs | ||
119 | // yoffs | ||
120 | // | ||
121 | // BASIC PRIMITIVE | ||
122 | // | ||
123 | |||
124 | static void R_MapPlane(int y, int x1, int x2) | ||
125 | { | ||
126 | angle_t angle; | ||
127 | fixed_t distance, length; | ||
128 | unsigned index; | ||
129 | |||
130 | #ifdef RANGECHECK | ||
131 | if (x2 < x1 || x1<0 || x2>=viewwidth || (unsigned)y>(unsigned)viewheight) | ||
132 | I_Error ("R_MapPlane: %i, %i at %i",x1,x2,y); | ||
133 | #endif | ||
134 | |||
135 | if (planeheight != cachedheight[y]) | ||
136 | { | ||
137 | cachedheight[y] = planeheight; | ||
138 | distance = cacheddistance[y] = FixedMul (planeheight, yslope[y]); | ||
139 | ds_xstep = cachedxstep[y] = FixedMul (distance,basexscale); | ||
140 | ds_ystep = cachedystep[y] = FixedMul (distance,baseyscale); | ||
141 | } | ||
142 | else | ||
143 | { | ||
144 | distance = cacheddistance[y]; | ||
145 | ds_xstep = cachedxstep[y]; | ||
146 | ds_ystep = cachedystep[y]; | ||
147 | } | ||
148 | |||
149 | length = FixedMul (distance,distscale[x1]); | ||
150 | angle = (viewangle + xtoviewangle[x1])>>ANGLETOFINESHIFT; | ||
151 | |||
152 | // killough 2/28/98: Add offsets | ||
153 | ds_xfrac = viewx + FixedMul(finecosine[angle], length) + xoffs; | ||
154 | ds_yfrac = -viewy - FixedMul(finesine[angle], length) + yoffs; | ||
155 | |||
156 | if (!(ds_colormap = fixedcolormap)) | ||
157 | { | ||
158 | index = distance >> LIGHTZSHIFT; | ||
159 | if (index >= MAXLIGHTZ ) | ||
160 | index = MAXLIGHTZ-1; | ||
161 | ds_colormap = planezlight[index]; | ||
162 | } | ||
163 | |||
164 | ds_y = y; | ||
165 | ds_x1 = x1; | ||
166 | ds_x2 = x2; | ||
167 | |||
168 | R_DrawSpan(); | ||
169 | } | ||
170 | |||
171 | // | ||
172 | // R_ClearPlanes | ||
173 | // At begining of frame. | ||
174 | // | ||
175 | |||
176 | void R_ClearPlanes(void) | ||
177 | { | ||
178 | int i; | ||
179 | angle_t angle; | ||
180 | |||
181 | // opening / clipping determination | ||
182 | for (i=0 ; i<viewwidth ; i++) | ||
183 | floorclip[i] = viewheight, ceilingclip[i] = -1; | ||
184 | |||
185 | for (i=0;i<MAXVISPLANES;i++) // new code -- killough | ||
186 | for (*freehead = visplanes[i], visplanes[i] = NULL; *freehead; ) | ||
187 | freehead = &(*freehead)->next; | ||
188 | |||
189 | lastopening = openings; | ||
190 | |||
191 | // texture calculation | ||
192 | memset (cachedheight, 0, sizeof(cachedheight)); | ||
193 | |||
194 | // left to right mapping | ||
195 | angle = (viewangle-ANG90)>>ANGLETOFINESHIFT; | ||
196 | |||
197 | // scale will be unit scale at SCREENWIDTH/2 distance | ||
198 | basexscale = FixedDiv (finecosine[angle],centerxfrac); | ||
199 | baseyscale = -FixedDiv (finesine[angle],centerxfrac); | ||
200 | } | ||
201 | |||
202 | // New function, by Lee Killough | ||
203 | |||
204 | static visplane_t *new_visplane(unsigned hash) | ||
205 | { | ||
206 | visplane_t *check = freetail; | ||
207 | if (!check) | ||
208 | check = calloc(1, sizeof *check); | ||
209 | else | ||
210 | if (!(freetail = freetail->next)) | ||
211 | freehead = &freetail; | ||
212 | check->next = visplanes[hash]; | ||
213 | visplanes[hash] = check; | ||
214 | return check; | ||
215 | } | ||
216 | |||
217 | /* | ||
218 | * R_DupPlane | ||
219 | * | ||
220 | * cph 2003/04/18 - create duplicate of existing visplane and set initial range | ||
221 | */ | ||
222 | visplane_t *R_DupPlane(const visplane_t *pl, int start, int stop) | ||
223 | { | ||
224 | unsigned hash = visplane_hash(pl->picnum, pl->lightlevel, pl->height); | ||
225 | visplane_t *new_pl = new_visplane(hash); | ||
226 | |||
227 | new_pl->height = pl->height; | ||
228 | new_pl->picnum = pl->picnum; | ||
229 | new_pl->lightlevel = pl->lightlevel; | ||
230 | new_pl->xoffs = pl->xoffs; // killough 2/28/98 | ||
231 | new_pl->yoffs = pl->yoffs; | ||
232 | new_pl->minx = start; | ||
233 | new_pl->maxx = stop; | ||
234 | memset(new_pl->top, 0xff, sizeof new_pl->top); | ||
235 | return new_pl; | ||
236 | } | ||
237 | |||
238 | // | ||
239 | // R_FindPlane | ||
240 | // | ||
241 | // killough 2/28/98: Add offsets | ||
242 | |||
243 | visplane_t *R_FindPlane(fixed_t height, int picnum, int lightlevel, | ||
244 | fixed_t xoffs, fixed_t yoffs) | ||
245 | { | ||
246 | visplane_t *check; | ||
247 | unsigned hash; // killough | ||
248 | |||
249 | if (picnum == skyflatnum || picnum & PL_SKYFLAT) | ||
250 | height = lightlevel = 0; // killough 7/19/98: most skies map together | ||
251 | |||
252 | // New visplane algorithm uses hash table -- killough | ||
253 | hash = visplane_hash(picnum,lightlevel,height); | ||
254 | |||
255 | for (check=visplanes[hash]; check; check=check->next) // killough | ||
256 | if (height == check->height && | ||
257 | picnum == check->picnum && | ||
258 | lightlevel == check->lightlevel && | ||
259 | xoffs == check->xoffs && // killough 2/28/98: Add offset checks | ||
260 | yoffs == check->yoffs) | ||
261 | return check; | ||
262 | |||
263 | check = new_visplane(hash); // killough | ||
264 | |||
265 | check->height = height; | ||
266 | check->picnum = picnum; | ||
267 | check->lightlevel = lightlevel; | ||
268 | check->minx = viewwidth; // Was SCREENWIDTH -- killough 11/98 | ||
269 | check->maxx = -1; | ||
270 | check->xoffs = xoffs; // killough 2/28/98: Save offsets | ||
271 | check->yoffs = yoffs; | ||
272 | |||
273 | memset (check->top, 0xff, sizeof check->top); | ||
274 | |||
275 | return check; | ||
276 | } | ||
277 | |||
278 | // | ||
279 | // R_CheckPlane | ||
280 | // | ||
281 | visplane_t *R_CheckPlane(visplane_t *pl, int start, int stop) | ||
282 | { | ||
283 | int intrl, intrh, unionl, unionh, x; | ||
284 | |||
285 | if (start < pl->minx) | ||
286 | intrl = pl->minx, unionl = start; | ||
287 | else | ||
288 | unionl = pl->minx, intrl = start; | ||
289 | |||
290 | if (stop > pl->maxx) | ||
291 | intrh = pl->maxx, unionh = stop; | ||
292 | else | ||
293 | unionh = pl->maxx, intrh = stop; | ||
294 | |||
295 | for (x=intrl ; x <= intrh && pl->top[x] == 0xffff; x++) | ||
296 | ; | ||
297 | |||
298 | if (x > intrh) { /* Can use existing plane; extend range */ | ||
299 | pl->minx = unionl; pl->maxx = unionh; | ||
300 | return pl; | ||
301 | } else /* Cannot use existing plane; create a new one */ | ||
302 | return R_DupPlane(pl,start,stop); | ||
303 | } | ||
304 | |||
305 | // | ||
306 | // R_MakeSpans | ||
307 | // | ||
308 | |||
309 | static void R_MakeSpans(int x, int t1, int b1, int t2, int b2) | ||
310 | { | ||
311 | for (; t1 < t2 && t1 <= b1; t1++) | ||
312 | R_MapPlane(t1, spanstart[t1], x-1); | ||
313 | for (; b1 > b2 && b1 >= t1; b1--) | ||
314 | R_MapPlane(b1, spanstart[b1] ,x-1); | ||
315 | while (t2 < t1 && t2 <= b2) | ||
316 | spanstart[t2++] = x; | ||
317 | while (b2 > b1 && b2 >= t2) | ||
318 | spanstart[b2--] = x; | ||
319 | } | ||
320 | |||
321 | // New function, by Lee Killough | ||
322 | |||
323 | static void R_DoDrawPlane(visplane_t *pl) | ||
324 | { | ||
325 | register int x; | ||
326 | if (pl->minx <= pl->maxx) { | ||
327 | if (pl->picnum == skyflatnum || pl->picnum & PL_SKYFLAT) { // sky flat | ||
328 | int texture; | ||
329 | angle_t an, flip; | ||
330 | |||
331 | // killough 10/98: allow skies to come from sidedefs. | ||
332 | // Allows scrolling and/or animated skies, as well as | ||
333 | // arbitrary multiple skies per level without having | ||
334 | // to use info lumps. | ||
335 | |||
336 | an = viewangle; | ||
337 | |||
338 | if (pl->picnum & PL_SKYFLAT) | ||
339 | { | ||
340 | // Sky Linedef | ||
341 | const line_t *l = &lines[pl->picnum & ~PL_SKYFLAT]; | ||
342 | |||
343 | // Sky transferred from first sidedef | ||
344 | const side_t *s = *l->sidenum + sides; | ||
345 | |||
346 | // Texture comes from upper texture of reference sidedef | ||
347 | texture = texturetranslation[s->toptexture]; | ||
348 | |||
349 | // Horizontal offset is turned into an angle offset, | ||
350 | // to allow sky rotation as well as careful positioning. | ||
351 | // However, the offset is scaled very small, so that it | ||
352 | // allows a long-period of sky rotation. | ||
353 | |||
354 | an += s->textureoffset; | ||
355 | |||
356 | // Vertical offset allows careful sky positioning. | ||
357 | |||
358 | dc_texturemid = s->rowoffset - 28*FRACUNIT; | ||
359 | |||
360 | // We sometimes flip the picture horizontally. | ||
361 | // | ||
362 | // Doom always flipped the picture, so we make it optional, | ||
363 | // to make it easier to use the new feature, while to still | ||
364 | // allow old sky textures to be used. | ||
365 | |||
366 | flip = l->special==272 ? 0u : ~0u; | ||
367 | } | ||
368 | else | ||
369 | { // Normal Doom sky, only one allowed per level | ||
370 | dc_texturemid = skytexturemid; // Default y-offset | ||
371 | texture = skytexture; // Default texture | ||
372 | flip = 0; // Doom flips it | ||
373 | } | ||
374 | |||
375 | /* Sky is always drawn full bright, i.e. colormaps[0] is used. | ||
376 | * Because of this hack, sky is not affected by INVUL inverse mapping. | ||
377 | * Until Boom fixed this. Compat option added in MBF. */ | ||
378 | |||
379 | if (comp[comp_skymap] || !(dc_colormap = fixedcolormap)) | ||
380 | dc_colormap = fullcolormap; // killough 3/20/98 | ||
381 | dc_texheight = textureheight[skytexture]>>FRACBITS; // killough | ||
382 | // proff 09/21/98: Changed for high-res | ||
383 | dc_iscale = FRACUNIT*200/viewheight; | ||
384 | |||
385 | // killough 10/98: Use sky scrolling offset, and possibly flip picture | ||
386 | for (x = pl->minx; (dc_x = x) <= pl->maxx; x++) | ||
387 | if ((dc_yl = pl->top[x]) <= (dc_yh = pl->bottom[x])) | ||
388 | { | ||
389 | dc_source = R_GetColumn(texture, ((an + xtoviewangle[x])^flip) >> | ||
390 | ANGLETOSKYSHIFT); | ||
391 | colfunc(); | ||
392 | } | ||
393 | } else { // regular flat | ||
394 | |||
395 | int stop, light; | ||
396 | |||
397 | ds_source = W_CacheLumpNum(firstflat + flattranslation[pl->picnum]); | ||
398 | |||
399 | xoffs = pl->xoffs; // killough 2/28/98: Add offsets | ||
400 | yoffs = pl->yoffs; | ||
401 | planeheight = D_abs(pl->height-viewz); | ||
402 | light = (pl->lightlevel >> LIGHTSEGSHIFT) + extralight; | ||
403 | |||
404 | if (light >= LIGHTLEVELS) | ||
405 | light = LIGHTLEVELS-1; | ||
406 | |||
407 | if (light < 0) | ||
408 | light = 0; | ||
409 | |||
410 | stop = pl->maxx + 1; | ||
411 | planezlight = zlight[light]; | ||
412 | pl->top[pl->minx-1] = pl->top[stop] = 0xffff; | ||
413 | |||
414 | for (x = pl->minx ; x <= stop ; x++) | ||
415 | R_MakeSpans(x,pl->top[x-1],pl->bottom[x-1],pl->top[x],pl->bottom[x]); | ||
416 | |||
417 | W_UnlockLumpNum(firstflat + flattranslation[pl->picnum]); | ||
418 | } | ||
419 | } | ||
420 | } | ||
421 | |||
422 | // | ||
423 | // RDrawPlanes | ||
424 | // At the end of each frame. | ||
425 | // | ||
426 | |||
427 | void R_DrawPlanes (void) | ||
428 | { | ||
429 | visplane_t *pl; | ||
430 | int i; | ||
431 | for (i=0;i<MAXVISPLANES;i++) | ||
432 | for (pl=visplanes[i]; pl; pl=pl->next) | ||
433 | R_DoDrawPlane(pl); | ||
434 | } | ||