]> icculus.org git repositories - theoddone33/hheretic.git/blob - base/r_plane.c
Some 64-bit fixes
[theoddone33/hheretic.git] / base / r_plane.c
1 // R_planes.c
2
3 #include <stdlib.h>
4 #include "doomdef.h"
5 #include "r_local.h"
6
7 planefunction_t         floorfunc, ceilingfunc;
8
9 //
10 // sky mapping
11 //
12 int                     skyflatnum;
13 int                     skytexture;
14 int                     skytexturemid;
15 fixed_t         skyiscale;
16
17 //
18 // opening
19 //
20
21 visplane_t              visplanes[MAXVISPLANES], *lastvisplane;
22 visplane_t              *floorplane, *ceilingplane;
23
24 short                   openings[MAXOPENINGS], *lastopening;
25
26 //
27 // clip values are the solid pixel bounding the range
28 // floorclip starts out SCREENHEIGHT
29 // ceilingclip starts out -1
30 //
31 short           floorclip[SCREENWIDTH];
32 short           ceilingclip[SCREENWIDTH];
33
34 //
35 // spanstart holds the start of a plane span
36 // initialized to 0 at start
37 //
38 int                     spanstart[SCREENHEIGHT];
39 int                     spanstop[SCREENHEIGHT];
40
41 //
42 // texture mapping
43 //
44 lighttable_t    **planezlight;
45 fixed_t         planeheight;
46
47 fixed_t         yslope[SCREENHEIGHT];
48 fixed_t         distscale[SCREENWIDTH];
49 fixed_t         basexscale, baseyscale;
50
51 fixed_t         cachedheight[SCREENHEIGHT];
52 fixed_t         cacheddistance[SCREENHEIGHT];
53 fixed_t         cachedxstep[SCREENHEIGHT];
54 fixed_t         cachedystep[SCREENHEIGHT];
55
56
57 /*
58 ================
59 =
60 = R_InitSkyMap
61 =
62 = Called whenever the view size changes
63 =
64 ================
65 */
66
67 void R_InitSkyMap (void)
68 {
69         skyflatnum = R_FlatNumForName ("F_SKY1");
70         skytexturemid = 200*FRACUNIT;
71         skyiscale = FRACUNIT;
72 }
73
74
75 /*
76 ====================
77 =
78 = R_InitPlanes
79 =
80 = Only at game startup
81 ====================
82 */
83
84 void R_InitPlanes (void)
85 {
86 }
87
88
89 /*
90 ================
91 =
92 = R_MapPlane
93 =
94 global vars:
95
96 planeheight
97 ds_source
98 basexscale
99 baseyscale
100 viewx
101 viewy
102
103 BASIC PRIMITIVE
104 ================
105 */
106
107 void R_MapPlane (int y, int x1, int x2)
108 {
109 #ifndef RENDER3D
110         angle_t         angle;
111         fixed_t         distance, length;
112         unsigned        index;
113         
114 #ifdef RANGECHECK
115         if (x2 < x1 || x1<0 || x2>=viewwidth || (unsigned)y>viewheight)
116                 I_Error ("R_MapPlane: %i, %i at %i",x1,x2,y);
117 #endif
118
119         if (planeheight != cachedheight[y])
120         {
121                 cachedheight[y] = planeheight;
122                 distance = cacheddistance[y] = FixedMul (planeheight, yslope[y]);
123
124                 ds_xstep = cachedxstep[y] = FixedMul (distance,basexscale);
125                 ds_ystep = cachedystep[y] = FixedMul (distance,baseyscale);
126         }
127         else
128         {
129                 distance = cacheddistance[y];
130                 ds_xstep = cachedxstep[y];
131                 ds_ystep = cachedystep[y];
132         }
133         
134         length = FixedMul (distance,distscale[x1]);
135         angle = (viewangle + xtoviewangle[x1])>>ANGLETOFINESHIFT;
136         ds_xfrac = viewx + FixedMul(finecosine[angle], length);
137         ds_yfrac = -viewy - FixedMul(finesine[angle], length);
138
139         if (fixedcolormap)
140                 ds_colormap = fixedcolormap;
141         else
142         {
143                 index = distance >> LIGHTZSHIFT;
144                 if (index >= MAXLIGHTZ )
145                         index = MAXLIGHTZ-1;
146                 ds_colormap = planezlight[index];
147         }
148         
149         ds_y = y;
150         ds_x1 = x1;
151         ds_x2 = x2;
152         
153         spanfunc ();            // high or low detail
154 #endif  // !RENDER3D
155 }
156
157 //=============================================================================
158
159 /*
160 ====================
161 =
162 = R_ClearPlanes
163 =
164 = At begining of frame
165 ====================
166 */
167
168 void R_ClearPlanes (void)
169 {
170         int             i;
171         angle_t angle;
172         
173 //
174 // opening / clipping determination
175 //      
176         for (i=0 ; i<viewwidth ; i++)
177         {
178                 floorclip[i] = viewheight;
179                 ceilingclip[i] = -1;
180         }
181
182         lastvisplane = visplanes;
183         lastopening = openings;
184         
185 //
186 // texture calculation
187 //
188         memset (cachedheight, 0, sizeof(cachedheight)); 
189         angle = (viewangle-ANG90)>>ANGLETOFINESHIFT;    // left to right mapping
190         
191         // scale will be unit scale at SCREENWIDTH/2 distance
192         basexscale = FixedDiv (finecosine[angle],centerxfrac);
193         baseyscale = -FixedDiv (finesine[angle],centerxfrac);
194 }
195
196
197
198 /*
199 ===============
200 =
201 = R_FindPlane
202 =
203 ===============
204 */
205
206 visplane_t *R_FindPlane(fixed_t height, int picnum,
207         int lightlevel, int special)
208 {
209         visplane_t *check;
210
211         if(picnum == skyflatnum)
212         {
213                 // all skies map together
214                 height = 0;
215                 lightlevel = 0;
216         }
217
218         for(check = visplanes; check < lastvisplane; check++)
219         {
220                 if(height == check->height
221                 && picnum == check->picnum
222                 && lightlevel == check->lightlevel
223                 && special == check->special)
224                         break;
225         }
226
227         if(check < lastvisplane)
228         {
229                 return(check);
230         }
231
232         if(lastvisplane-visplanes == MAXVISPLANES)
233         {
234                 I_Error("R_FindPlane: no more visplanes");
235         }
236
237         lastvisplane++;
238         check->height = height;
239         check->picnum = picnum;
240         check->lightlevel = lightlevel;
241         check->special = special;
242         check->minx = SCREENWIDTH;
243         check->maxx = -1;
244         memset(check->top,0xff,sizeof(check->top));
245         return(check);
246 }
247
248 /*
249 ===============
250 =
251 = R_CheckPlane
252 =
253 ===============
254 */
255
256 visplane_t *R_CheckPlane (visplane_t *pl, int start, int stop)
257 {
258         int                     intrl, intrh;
259         int                     unionl, unionh;
260         int                     x;
261         
262         if (start < pl->minx)
263         {
264                 intrl = pl->minx;
265                 unionl = start;
266         }
267         else
268         {
269                 unionl = pl->minx;
270                 intrl = start;
271         }
272         
273         if (stop > pl->maxx)
274         {
275                 intrh = pl->maxx;
276                 unionh = stop;
277         }
278         else
279         {
280                 unionh = pl->maxx;
281                 intrh = stop;
282         }
283
284         for (x=intrl ; x<= intrh ; x++)
285                 if (pl->top[x] != 0xff)
286                         break;
287
288         if (x > intrh)
289         {
290                 pl->minx = unionl;
291                 pl->maxx = unionh;
292                 return pl;                      // use the same one
293         }
294         
295 // make a new visplane
296
297         lastvisplane->height = pl->height;
298         lastvisplane->picnum = pl->picnum;
299         lastvisplane->lightlevel = pl->lightlevel;
300         lastvisplane->special = pl->special;
301         pl = lastvisplane++;
302         pl->minx = start;
303         pl->maxx = stop;
304         memset (pl->top,0xff,sizeof(pl->top));
305                 
306         return pl;
307 }
308
309
310
311 //=============================================================================
312
313 /*
314 ================
315 =
316 = R_MakeSpans
317 =
318 ================
319 */
320
321 void R_MakeSpans (int x, int t1, int b1, int t2, int b2)
322 {
323         while (t1 < t2 && t1<=b1)
324         {
325                 R_MapPlane (t1,spanstart[t1],x-1);
326                 t1++;
327         }
328         while (b1 > b2 && b1>=t1)
329         {
330                 R_MapPlane (b1,spanstart[b1],x-1);
331                 b1--;
332         }
333         
334         while (t2 < t1 && t2<=b2)
335         {
336                 spanstart[t2] = x;
337                 t2++;
338         }
339         while (b2 > b1 && b2>=t2)
340         {
341                 spanstart[b2] = x;
342                 b2--;
343         }
344 }
345
346
347
348 /*
349 ================
350 =
351 = R_DrawPlanes
352 =
353 = At the end of each frame
354 ================
355 */
356
357 void R_DrawPlanes (void)
358 {
359 #ifndef RENDER3D
360         visplane_t      *pl;
361         int                     light;
362         int                     x, stop;
363         int                     angle;
364         byte *tempSource;
365         
366         byte *dest;
367         int count;
368         fixed_t frac, fracstep;
369                                 
370 extern byte *ylookup[MAXHEIGHT];
371 extern int columnofs[MAXWIDTH];
372
373 #ifdef RANGECHECK
374         if (ds_p - drawsegs > MAXDRAWSEGS)
375                 I_Error ("R_DrawPlanes: drawsegs overflow (%i)", ds_p - drawsegs);
376         if (lastvisplane - visplanes > MAXVISPLANES)
377                 I_Error ("R_DrawPlanes: visplane overflow (%i)", lastvisplane - visplanes);
378         if (lastopening - openings > MAXOPENINGS)
379                 I_Error ("R_DrawPlanes: opening overflow (%i)", lastopening - openings);
380 #endif
381
382         for (pl = visplanes ; pl < lastvisplane ; pl++)
383         {
384                 if (pl->minx > pl->maxx)
385                         continue;
386         //
387         // sky flat
388         //
389                 if (pl->picnum == skyflatnum)
390                 {
391                         dc_iscale = skyiscale;
392                         dc_colormap = colormaps;// sky is allways drawn full bright
393                         dc_texturemid = skytexturemid;
394                         for (x=pl->minx ; x <= pl->maxx ; x++)
395                         {
396                                 dc_yl = pl->top[x];
397                                 dc_yh = pl->bottom[x];
398                                 if (dc_yl <= dc_yh)
399                                 {
400                                         angle = (viewangle + xtoviewangle[x])>>ANGLETOSKYSHIFT;
401                                         dc_x = x;
402                                         dc_source = R_GetColumn(skytexture, angle);
403
404                                         count = dc_yh - dc_yl;
405                                         if (count < 0)
406                                                 return;
407                                 
408 #ifdef RANGECHECK
409         if ((unsigned)dc_x >= SCREENWIDTH || dc_yl < 0 || dc_yh >= SCREENHEIGHT)
410                 I_Error ("R_DrawColumn: %i to %i at %i", dc_yl, dc_yh, dc_x);
411 #endif
412
413                                         dest = ylookup[dc_yl] + columnofs[dc_x]; 
414         
415                                         fracstep = 1;
416                                         frac = (dc_texturemid>>FRACBITS) + (dc_yl-centery);             
417                                         do
418                                         {
419                                                 *dest = dc_source[frac];
420                                                 dest += SCREENWIDTH;
421                                                 frac += fracstep;
422                                         } while (count--);
423         
424 //                                      colfunc ();
425                                 }
426                         }
427                         continue;
428                 }
429                 
430         //
431         // regular flat
432         //
433                 tempSource = W_CacheLumpNum(firstflat +
434                         flattranslation[pl->picnum], PU_STATIC);
435
436                 switch(pl->special)
437                 {
438                         case 25: case 26: case 27: case 28: case 29: // Scroll_North
439                                 ds_source = tempSource;
440                                 break;
441                         case 20: case 21: case 22: case 23: case 24: // Scroll_East
442                                 ds_source = tempSource+((63-((leveltime>>1)&63))<<
443                                         (pl->special-20)&63);
444                                 //ds_source = tempSource+((leveltime>>1)&63);
445                                 break;
446                         case 30: case 31: case 32: case 33: case 34: // Scroll_South
447                                 ds_source = tempSource;
448                                 break;
449                         case 35: case 36: case 37: case 38: case 39: // Scroll_West
450                                 ds_source = tempSource;
451                                 break;
452                         case 4: // Scroll_EastLavaDamage
453                                 ds_source = tempSource+(((63-((leveltime>>1)&63))<<3)&63);
454                                 break;
455                         default:
456                                 ds_source = tempSource;
457                 }
458                 planeheight = abs(pl->height-viewz);
459                 light = (pl->lightlevel >> LIGHTSEGSHIFT)+extralight;
460                 if (light >= LIGHTLEVELS)
461                         light = LIGHTLEVELS-1;
462                 if (light < 0)
463                         light = 0;
464                 planezlight = zlight[light];
465
466                 pl->top[pl->maxx+1] = 0xff;
467                 pl->top[pl->minx-1] = 0xff;
468                 
469                 stop = pl->maxx + 1;
470                 for (x=pl->minx ; x<= stop ; x++)
471                         R_MakeSpans (x,pl->top[x-1],pl->bottom[x-1]
472                         ,pl->top[x],pl->bottom[x]);
473                 
474                 Z_ChangeTag (tempSource, PU_CACHE);
475         }
476 #endif // !RENDER3D
477 }