📄 r_main.c
字号:
//**************************************************************************//**//** r_main.c : Heretic 2 : Raven Software, Corp.//**//** $RCSfile: r_main.c,v $//** $Revision: 1.16 $//** $Date: 96/01/06 18:37:41 $//** $Author: bgokey $//**//**************************************************************************#include <math.h>#include "h2def.h"#include "r_local.h"int viewangleoffset;#ifdef __WATCOMC__
int newViewAngleOff;
#endif
int validcount = 1; // increment every time a check is madelighttable_t *fixedcolormap;extern lighttable_t **walllights;int centerx, centery;fixed_t centerxfrac, centeryfrac;fixed_t projection;int framecount; // just for profiling purposesint sscount, linecount, loopcount;fixed_t viewx, viewy, viewz;angle_t viewangle;fixed_t viewcos, viewsin;player_t *viewplayer;int detailshift; // 0 = high, 1 = low//// precalculated math tables//angle_t clipangle;// The viewangletox[viewangle + FINEANGLES/4] lookup maps the visible view// angles to screen X coordinates, flattening the arc to a flat projection// plane. There will be many angles mapped to the same X.int viewangletox[FINEANGLES/2];// The xtoviewangleangle[] table maps a screen pixel to the lowest viewangle// that maps back to x ranges from clipangle to -clipangleangle_t xtoviewangle[SCREENWIDTH+1];// the finetangentgent[angle+FINEANGLES/4] table holds the fixed_t tangent// values for view angles, ranging from MININT to 0 to MAXINT.// fixed_t finetangent[FINEANGLES/2];// fixed_t finesine[5*FINEANGLES/4];fixed_t *finecosine = &finesine[FINEANGLES/4];lighttable_t *scalelight[LIGHTLEVELS][MAXLIGHTSCALE];lighttable_t *scalelightfixed[MAXLIGHTSCALE];lighttable_t *zlight[LIGHTLEVELS][MAXLIGHTZ];int extralight; // bumped light from gun blastsvoid (*colfunc) (void);void (*basecolfunc) (void);void (*fuzzcolfunc) (void);void (*transcolfunc) (void);void (*spanfunc) (void);/*===================== R_AddPointToBox====================*//*void R_AddPointToBox (int x, int y, fixed_t *box){ if (x< box[BOXLEFT]) box[BOXLEFT] = x; if (x> box[BOXRIGHT]) box[BOXRIGHT] = x; if (y< box[BOXBOTTOM]) box[BOXBOTTOM] = y; if (y> box[BOXTOP]) box[BOXTOP] = y;}*//*================================================================================= R_PointOnSide== Returns side 0 (front) or 1 (back)===============================================================================*/int R_PointOnSide (fixed_t x, fixed_t y, node_t *node){ fixed_t dx,dy; fixed_t left, right; if (!node->dx) { if (x <= node->x) return node->dy > 0; return node->dy < 0; } if (!node->dy) { if (y <= node->y) return node->dx < 0; return node->dx > 0; } dx = (x - node->x); dy = (y - node->y);// try to quickly decide by looking at sign bits if ( (node->dy ^ node->dx ^ dx ^ dy)&0x80000000 ) { if ( (node->dy ^ dx) & 0x80000000 ) return 1; // (left is negative) return 0; } left = FixedMul ( node->dy>>FRACBITS , dx ); right = FixedMul ( dy , node->dx>>FRACBITS ); if (right < left) return 0; // front side return 1; // back side}int R_PointOnSegSide (fixed_t x, fixed_t y, seg_t *line){ fixed_t lx, ly; fixed_t ldx, ldy; fixed_t dx,dy; fixed_t left, right; lx = line->v1->x; ly = line->v1->y; ldx = line->v2->x - lx; ldy = line->v2->y - ly; if (!ldx) { if (x <= lx) return ldy > 0; return ldy < 0; } if (!ldy) { if (y <= ly) return ldx < 0; return ldx > 0; } dx = (x - lx); dy = (y - ly);// try to quickly decide by looking at sign bits if ( (ldy ^ ldx ^ dx ^ dy)&0x80000000 ) { if ( (ldy ^ dx) & 0x80000000 ) return 1; // (left is negative) return 0; } left = FixedMul ( ldy>>FRACBITS , dx ); right = FixedMul ( dy , ldx>>FRACBITS ); if (right < left) return 0; // front side return 1; // back side}/*================================================================================= R_PointToAngle================================================================================*/// to get a global angle from cartesian coordinates, the coordinates are// flipped until they are in the first octant of the coordinate system, then// the y (<=x) is scaled and divided by x to get a tangent (slope) value// which is looked up in the tantoangle[] table. The +1 size is to handle// the case when x==y without additional checking.#define SLOPERANGE 2048#define SLOPEBITS 11#define DBITS (FRACBITS-SLOPEBITS)extern int tantoangle[SLOPERANGE+1]; // get from tables.c// int tantoangle[SLOPERANGE+1];int SlopeDiv (unsigned num, unsigned den){ unsigned ans; if (den < 512) return SLOPERANGE; ans = (num<<3)/(den>>8); return ans <= SLOPERANGE ? ans : SLOPERANGE;}angle_t R_PointToAngle (fixed_t x, fixed_t y){ x -= viewx; y -= viewy; if ( (!x) && (!y) ) return 0; if (x>= 0) { // x >=0 if (y>= 0) { // y>= 0 if (x>y) return tantoangle[ SlopeDiv(y,x)]; // octant 0 else return ANG90-1-tantoangle[ SlopeDiv(x,y)]; // octant 1 } else { // y<0 y = -y; if (x>y) return -tantoangle[SlopeDiv(y,x)]; // octant 8 else return ANG270+tantoangle[ SlopeDiv(x,y)]; // octant 7 } } else { // x<0 x = -x; if (y>= 0) { // y>= 0 if (x>y) return ANG180-1-tantoangle[ SlopeDiv(y,x)]; // octant 3 else return ANG90+ tantoangle[ SlopeDiv(x,y)]; // octant 2 } else { // y<0 y = -y; if (x>y) return ANG180+tantoangle[ SlopeDiv(y,x)]; // octant 4 else return ANG270-1-tantoangle[ SlopeDiv(x,y)]; // octant 5 } } return 0;}angle_t R_PointToAngle2 (fixed_t x1, fixed_t y1, fixed_t x2, fixed_t y2){ viewx = x1; viewy = y1; return R_PointToAngle (x2, y2);}fixed_t R_PointToDist (fixed_t x, fixed_t y){ int angle; fixed_t dx, dy, temp; fixed_t dist; dx = abs(x - viewx); dy = abs(y - viewy); if (dy>dx) { temp = dx; dx = dy; dy = temp; } angle = (tantoangle[ FixedDiv(dy,dx)>>DBITS ]+ANG90) >> ANGLETOFINESHIFT; dist = FixedDiv (dx, finesine[angle] ); // use as cosine return dist;}/*=================== R_InitPointToAngle==================*/void R_InitPointToAngle (void){// now getting from tables.c#if 0 int i; long t; float f;//// slope (tangent) to angle lookup// for (i=0 ; i<=SLOPERANGE ; i++) { f = atan( (float)i/SLOPERANGE )/(3.141592657*2); t = 0xffffffff*f; tantoangle[i] = t; }#endif}//=============================================================================/*================== R_ScaleFromGlobalAngle== Returns the texture mapping scale for the current line at the given angle= rw_distance must be calculated first================*/fixed_t R_ScaleFromGlobalAngle (angle_t visangle){ fixed_t scale; int anglea, angleb; int sinea, sineb; fixed_t num,den;#if 0{ fixed_t dist,z; fixed_t sinv, cosv; sinv = finesine[(visangle-rw_normalangle)>>ANGLETOFINESHIFT]; dist = FixedDiv (rw_distance, sinv); cosv = finecosine[(viewangle-visangle)>>ANGLETOFINESHIFT]; z = abs(FixedMul (dist, cosv)); scale = FixedDiv(projection, z); return scale;}#endif anglea = ANG90 + (visangle-viewangle); angleb = ANG90 + (visangle-rw_normalangle);// bothe sines are allways positive sinea = finesine[anglea>>ANGLETOFINESHIFT]; sineb = finesine[angleb>>ANGLETOFINESHIFT]; num = FixedMul(projection,sineb)<<detailshift; den = FixedMul(rw_distance,sinea); if (den > num>>16) { scale = FixedDiv (num, den); if (scale > 64*FRACUNIT) scale = 64*FRACUNIT; else if (scale < 256) scale = 256; } else scale = 64*FRACUNIT; return scale;}/*=================== R_InitTables==================*/void R_InitTables (void){// now getting from tables.c#if 0 int i; float a, fv; int t;//// viewangle tangent table// for (i=0 ; i<FINEANGLES/2 ; i++) { a = (i-FINEANGLES/4+0.5)*PI*2/FINEANGLES; fv = FRACUNIT*tan (a); t = fv; finetangent[i] = t; }//// finesine table// for (i=0 ; i<5*FINEANGLES/4 ; i++) {// OPTIMIZE: mirror... a = (i+0.5)*PI*2/FINEANGLES; t = FRACUNIT*sin (a); finesine[i] = t; }#endif}/*=================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -