Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mode7.h"
- #include <tigcclib.h>
- INT_HANDLER OldInt1 = NULL;
- #if SHOW_FPS == 1
- char fpsStr[3];
- #endif
- char hwVersion;
- volatile char quit = 0;
- char modifying = 0;
- char nextModif = 0;
- short timeCount = 0;
- volatile short fCount = 0;
- void (*m7_Modif)();
- void (*m7_MoreDisplay)();
- long K3;
- long K4;
- long K5;
- signed char CosTable[NBDIR];
- signed char SinTable[NBDIR];
- short * Vert;
- short * Horz;
- float tabstep[SIZE_Y];
- short Dir=0; // direction
- #if ENABLE_GRAYSCALE == 1
- unsigned char SkyChange = 2;
- #else
- unsigned char SkyChange = 1;
- #endif
- short X=0; // x camera
- short Y=0; // y camera
- unsigned char * MapData;
- #if ENABLE_LARGE_MAPS == 2
- unsigned char ** MapTiles;
- unsigned short TilesOffset;
- #else
- unsigned char * Map0;
- #endif
- unsigned char * Sky0;
- #if ENABLE_GRAYSCALE == 1
- unsigned char * Buff;
- #if ENABLE_LARGE_MAPS != 2
- unsigned char * Map1;
- #endif
- unsigned char * Sky1;
- unsigned char Plane = 0;
- unsigned char * Plane0;
- unsigned char * Plane1;
- unsigned char * Plane00;
- unsigned char * Plane10;
- unsigned char * Plane01;
- unsigned char * Plane11;
- #else
- char DispSky[SIZE_SKY_X*SIZE_SKY_Y];
- char Virtual[3000];
- #endif
- short DimX;
- short DimY;
- short NumByte=0;
- short m7_ProjXYZ(Sprite * sprite, short * Xscreen, short * Yscreen, short * Zscreen, short * scaling, short nbscale);
- #if ENABLE_ROTATION == 1
- #define COORD_XY_ROT \
- coordy = (coordy0 - *(short*)(horzjsizex)*sindir)>>(SINCOS + ZOOM); \
- coordx = (coordx0 + *(short*)(horzjsizex++)*cosdir)>>(SINCOS + ZOOM); \
- #else
- #define COORD_XY_ROT \
- coordx = ((x >> SINCOS) - *(short*)(horzjsizex++)) >> ZOOM;
- #endif
- #if ENABLE_GRAYSCALE == 1
- #define IN_MAP_2(SHIFT) \
- if(map0[bytemap] & offsetmap) *dispword0 |= (PIX >> SHIFT); \
- if(map1[bytemap] & offsetmap) *dispword1 |= (PIX >> SHIFT);
- #define IN_MAP_3(SHIFT) \
- if(*mapoffset) *dispword0 |= (PIX >> SHIFT); \
- if(*(mapoffset+tilesoffset)) *dispword1 |= (PIX >> SHIFT);
- #else
- #define IN_MAP_2(SHIFT) \
- if(map0[bytemap] & offsetmap) *dispword0 |= (PIX >> SHIFT); \
- #define IN_MAP_3(SHIFT) \
- if(*mapoffset) *dispword0 |= (PIX >> SHIFT);
- #endif
- #if ENABLE_LARGE_MAPS == 0
- #define IN_MAP(SHIFT) \
- COORD_XY_ROT \
- bytemap = coordx+coordy*dimx; \
- offsetmap = 1 << (bytemap & 0x7); \
- bytemap >>= 3; \
- IN_MAP_2(SHIFT)
- #endif
- #if ENABLE_LARGE_MAPS == 1
- #define IN_MAP(SHIFT) \
- COORD_XY_ROT \
- offsetmap = 1 << (coordx & 0x7); \
- bytemap = ((long)coordx+(long)coordy*(long)dimx) >> 3; \
- IN_MAP_2(SHIFT)
- #endif
- #if ENABLE_LARGE_MAPS == 2
- #define IN_MAP(SHIFT) \
- COORD_XY_ROT \
- mapoffset = maptiles[(coordy >> TILES_SHIFT) * dimx + (coordx >> TILES_SHIFT)] + ((coordy & (TILES_SIZE - 1)) << TILES_SHIFT) + (coordx & (TILES_SIZE - 1)); \
- IN_MAP_3(SHIFT)
- #endif
- #if ZOOM_SCR_HORZ == 1
- #define PIX 0x8000
- #elif ZOOM_SCR_HORZ == 2
- #define PIX 0xC000
- #elif ZOOM_SCR_HORZ == 4
- #define PIX 0xF000
- #elif ZOOM_SCR_HORZ == 8
- #define PIX 0xFF00
- #endif
- // [Thanks to Geogeo from pws.tigen.org for this code]
- #define __HARDWARE_FREQUENCY 20970 //256
- static volatile unsigned short __COUNTER_HARDWARE=0;
- DEFINE_INT_HANDLER (SceneModif)
- {
- ExecuteHandler (OldInt1);
- //HARDWARE VERSION 1.0
- if (hwVersion==1)
- {
- //Incrémentation
- __COUNTER_HARDWARE+=__HARDWARE_FREQUENCY;
- //Execution interrupotion
- if (__COUNTER_HARDWARE<=32768)
- return;
- //Remise à zéro
- __COUNTER_HARDWARE-=32768;
- }
- // [/Thanks to Geogeo from pws.tigen.org for this code]
- timeCount++;
- if (timeCount == (256))
- {
- timeCount = 0;
- #if SHOW_FPS == 1
- sprintf (fpsStr, "%d", fCount);
- DrawStr (24, 0, fpsStr, A_REPLACE);
- #endif
- fCount = 0;
- }
- if ((modifying + nextModif) <= 0)
- {
- modifying = 1;
- nextModif = GAME_SPEED;
- m7_Modif();
- modifying = 0;
- }
- if(nextModif)
- nextModif--;
- }
- static inline void m7_DrawSky()
- {
- register unsigned char i;
- #if ENABLE_GRAYSCALE == 1
- unsigned char * offset0=OFFSET_SKY_HORZ+ 30*OFFSET_SKY_VERT + Plane0;
- unsigned char * offset1=OFFSET_SKY_HORZ+ 30*OFFSET_SKY_VERT + Plane1;
- unsigned char * offset12 = NULL, *sky1offset, *sky1offset2 = NULL;
- #else
- unsigned char * offset0=OFFSET_SKY_HORZ+ 30*OFFSET_SKY_VERT + Virtual;
- #endif
- unsigned char * offset02 = NULL;
- unsigned char * sky0offset;
- unsigned char * sky0offset2 = NULL;
- unsigned short length, length2=0;
- const short Byte = NumByte >> NBDIVSCROLL;
- length = SIZE_SKY_X - Byte;
- if(length > SIZE_SKY_HORZ)
- {
- length = SIZE_SKY_HORZ;
- } else
- {
- length2 = SIZE_SKY_HORZ - length;
- sky0offset2 = Sky0;
- #if ENABLE_GRAYSCALE == 1
- sky1offset2 = Sky1;
- #endif
- offset02 = offset0 + length;
- #if ENABLE_GRAYSCALE == 1
- offset12 = offset1 + length;
- #endif
- }
- sky0offset = Sky0+Byte;
- #if ENABLE_GRAYSCALE == 1
- sky1offset = Sky1+Byte;
- #endif
- for(i=SIZE_SKY_Y;i--;)
- {
- memcpy(offset0, sky0offset, length);
- #if ENABLE_GRAYSCALE == 1
- memcpy(offset1, sky1offset, length);
- #endif
- sky0offset+=SIZE_SKY_X;
- #if ENABLE_GRAYSCALE == 1
- sky1offset+=SIZE_SKY_X;
- #endif
- if (length < SIZE_SKY_HORZ)
- {
- memcpy(offset02, sky0offset2, length2);
- #if ENABLE_GRAYSCALE == 1
- memcpy(offset12, sky1offset2, length2);
- #endif
- sky0offset2+=SIZE_SKY_X;
- #if ENABLE_GRAYSCALE == 1
- sky1offset2+=SIZE_SKY_X;
- #endif
- offset02+=30;
- #if ENABLE_GRAYSCALE == 1
- offset12+=30;
- #endif
- }
- offset0+=30;
- #if ENABLE_GRAYSCALE == 1
- offset1+=30;
- #endif
- }
- }
- static inline void m7_DrawFrame()
- {
- register unsigned char i,j;
- #if ENABLE_ROTATION == 1
- signed short coordy0, coordx0;
- const short y = Y;
- const short cosdir = CosTable[Dir];
- const short sindir = SinTable[Dir];
- #else
- const short y = (Y >> SINCOS);
- #endif
- signed short * horzjsizex = Horz, coordy, coordx, vertj;
- short * pvertj = Vert;
- const signed short x = X, dimx = DimX;
- #if ENABLE_LARGE_MAPS == 2
- unsigned char ** maptiles = MapTiles;
- unsigned char * mapoffset;
- #if ENABLE_GRAYSCALE == 1
- const unsigned short tilesoffset = TilesOffset;
- #endif
- #else
- unsigned short bytemap, offsetmap;
- const unsigned char * map0 = Map0;
- #if ENABLE_GRAYSCALE == 1
- const unsigned char * map1 = Map1;
- #endif
- #endif
- #if ENABLE_GRAYSCALE == 1
- unsigned short * dispword0 = &((short*)Plane0)[OFFSET_SCR_HORZ+ 15*OFFSET_SCR_VERT];
- unsigned short * dispword1 = &((short*)Plane1)[OFFSET_SCR_HORZ+ 15*OFFSET_SCR_VERT];
- #else
- unsigned short * dispword0 = &((short*)Virtual)[OFFSET_SCR_HORZ+ 15*OFFSET_SCR_VERT];
- #endif
- for(j=SIZE_Y; j--;)
- {
- vertj = *(short*)(pvertj++);
- #if ENABLE_ROTATION == 1
- coordy0 = vertj*cosdir+y;
- coordx0 = vertj*sindir+x;
- #else
- coordy = (y - vertj) >> ZOOM;
- #endif
- for(i=(SIZE_X*ZOOM_SCR_HORZ/16); i--;)
- {
- *dispword0 = 0;
- #if ENABLE_GRAYSCALE == 1
- *dispword1 = 0;
- #endif
- #if ZOOM_SCR_HORZ <= 8
- IN_MAP(0)
- IN_MAP(ZOOM_SCR_HORZ)
- #endif
- #if ZOOM_SCR_HORZ <= 4
- IN_MAP(ZOOM_SCR_HORZ * 2)
- IN_MAP(ZOOM_SCR_HORZ * 3)
- #endif
- #if ZOOM_SCR_HORZ <= 2
- IN_MAP(ZOOM_SCR_HORZ * 4)
- IN_MAP(ZOOM_SCR_HORZ * 5)
- IN_MAP(ZOOM_SCR_HORZ * 6)
- IN_MAP(ZOOM_SCR_HORZ * 7)
- #endif
- #if ZOOM_SCR_HORZ == 1
- IN_MAP(ZOOM_SCR_HORZ * 8)
- IN_MAP(ZOOM_SCR_HORZ * 9)
- IN_MAP(ZOOM_SCR_HORZ * 10)
- IN_MAP(ZOOM_SCR_HORZ * 11)
- IN_MAP(ZOOM_SCR_HORZ * 12)
- IN_MAP(ZOOM_SCR_HORZ * 13)
- IN_MAP(ZOOM_SCR_HORZ * 14)
- IN_MAP(ZOOM_SCR_HORZ * 15)
- #endif
- #if ZOOM_SCR_VERT == 2
- *(dispword0 + 15) = *dispword0;
- #if ENABLE_GRAYSCALE == 1
- *(dispword1 + 15) = *dispword1;
- #endif
- #elif ZOOM_SCR_VERT > 1
- register unsigned short offset = 0;
- register unsigned char k;
- for(k=(ZOOM_SCR_VERT - 1); k--;)
- {
- offset += 15;
- *(dispword0+offset) = *dispword0;
- #if ENABLE_GRAYSCALE == 1
- *(dispword1+offset) = *dispword1;
- #endif
- }
- #endif
- dispword0++;
- #if ENABLE_GRAYSCALE == 1
- dispword1++;
- #endif
- }
- dispword0 += 15*ZOOM_SCR_VERT-((ZOOM_SCR_HORZ*SIZE_X)/16);
- #if ENABLE_GRAYSCALE == 1
- dispword1 += 15*ZOOM_SCR_VERT-((ZOOM_SCR_HORZ*SIZE_X)/16);
- #endif
- }
- }
- static inline void m7_Display()
- {
- #if (ENABLE_SPRITES == 1) || (ENABLE_FIRST_PLANE_SPRITES == 1)
- register unsigned short i;
- #endif
- #if ENABLE_SPRITES == 1
- Sprite * sprite;
- short Xscreen, Yscreen, Zscreen, scaling;
- #endif
- #if ENABLE_SKY == 1
- if(SkyChange)
- {
- SkyChange--;
- m7_DrawSky();
- }
- #endif
- m7_DrawFrame();
- #if ENABLE_SPRITES == 1
- nbDisplayedSprites = 0;
- for (i=MAXSPRT; i--;)
- if (AddedSprites[i] == 1)
- {
- sprite = AllSpriteList[i];
- if (m7_ProjXYZ(sprite, &Xscreen, &Yscreen, &Zscreen, &scaling, sprite->nbScales))
- m7sprites_SetDisplayed(i, Xscreen, Yscreen, Zscreen, Dir, sprite->dir, scaling);
- }
- m7sprites_SortDisplayedSpriteList();
- for (i=0; i < nbDisplayedSprites; i++)
- #if ENABLE_GRAYSCALE == 1
- m7sprite_Display(i, Plane0, Plane1);
- #else
- m7sprite_Display(i, Virtual);
- #endif
- #endif
- #if ENABLE_FIRST_PLANE_SPRITES == 1
- for (i=0; i < MAXSPRT; i++)
- if (AddedSprites[i] == 2)
- {
- #if ENABLE_GRAYSCALE == 1
- m7sprite_Display1stPlane(i, Plane0, Plane1);
- #else
- m7sprite_Display1stPlane(i, Virtual);
- #endif
- }
- #endif
- #if ENABLE_MORE_DISPLAY == 1
- m7_MoreDisplay();
- #endif
- #if ENABLE_GRAYSCALE == 1
- m7_DBufToggle();
- #else
- memcpy(LCD_MEM, Virtual, LCD_SIZE);
- #endif
- }
- void m7_SetLoopFunc(void (*modifFunc)())
- {
- m7_Modif = modifFunc;
- }
- void m7_LoopInit()
- {
- quit = 0;
- modifying = 0;
- timeCount = 0;
- fCount = 0;
- hwVersion = HW_VERSION;
- OldInt1 = GetIntVec (AUTO_INT_1);
- }
- void m7_LoopEnd()
- {
- SetIntVec (AUTO_INT_1, OldInt1);
- }
- void m7_MainLoop()
- {
- SetIntVec (AUTO_INT_1, SceneModif);
- while(!quit)
- {
- m7_Display();
- fCount++;
- }
- }
- void m7_LoopQuit()
- {
- quit = 1;
- }
- void m7_InitMoreDisplay(void (*moreDispFunc)())
- {
- m7_MoreDisplay = moreDispFunc;
- }
- static inline void m7_CosAndSin()
- {
- register short i;
- for(i = NBDIR; i--;)
- {
- const float angle = (i+i)*(PI/NBDIR); // Now, PI/NBDIR will be a constant value and (i+i) is faster than i*2
- CosTable[i] = (signed char)(cos(angle)*(1<<SINCOS));
- SinTable[i] = (signed char)(sin(angle)*(1<<SINCOS));
- }
- }
- void m7_CreateMatrix(short SizeHorz, short ViewL, short ViewF)
- {
- register short i,j;
- K3 = SizeHorz * (SIZE_Y * (ViewF - 1));
- const long K1 = -K3*ViewL*ViewL;
- const long K2 = 2*SIZE_X*ViewF;
- K4 = (SizeHorz-1)*ViewF;
- K5 = SizeHorz*(-(ViewF-1)*ViewL);
- long val = ((SIZE_Y - 1) * K4 - K3);
- for(i=SIZE_Y; i--;)
- {
- tabstep[SIZE_Y - i - 1] = (float)K1/(K2 * val);
- Vert[SIZE_Y - i - 1] = (i*K5)/(val + val);
- val -= K4;
- }
- for(j=SIZE_Y; j--;)
- {
- const float _tabstep = tabstep[j];
- float _tabstep1 = ((SIZE_X/2)+1)*_tabstep;
- float _tabstep2 = (-((SIZE_X/2)-1))*_tabstep;
- const short offset = SIZE_X * j;
- for(i=SIZE_X/2; i--;)
- {
- Horz[((SIZE_X/2) - i%(SIZE_X/2) - 1) + offset] = _tabstep1;
- Horz[((SIZE_X/2) + i%(SIZE_X/2)) + offset] = _tabstep2;
- _tabstep1 -= _tabstep;
- _tabstep2 += _tabstep;
- }
- }
- }
- short m7_ProjXYZ(Sprite * sprite, short * Xscreen, short * Yscreen, short * Zscreen, short * scaling, short nbscale)
- {
- const short x = sprite->x - (X>>SINCOS), y = sprite->y - (Y>>SINCOS), z = sprite->z;
- const short cosdir = CosTable[Dir];
- const short sindir = SinTable[Dir];
- short Py = (y * cosdir + x * sindir)>>SINCOS;
- if (Py > FRONT_CLIP_DIST) return 0;
- if (Py < -BACK_CLIP_DIST) return 0;
- const short Px = (y * sindir - x * cosdir)>>SINCOS;
- Py += Py;
- *Yscreen = K3*Py/(K4*Py-K5);
- const short Yscreen2 = (*Yscreen < 0 ? 0 : *Yscreen);
- *scaling = nbscale - 1 - ((nbscale*Vert[SIZE_Y - Yscreen2 - 1])/Vert[0]);
- *Zscreen = z - ((z * Vert[SIZE_Y - Yscreen2 - 1])/Vert[0]);
- if(*scaling < 0)
- *scaling = 0;
- *Xscreen = (Px*ZOOM_SCR_HORZ)/tabstep[SIZE_Y - Yscreen2 - 1] + (SIZE_X*ZOOM_SCR_HORZ)/2 + (OFFSET_SCR_HORZ << 3);
- *Yscreen = OFFSET_SCR_VERT + (SIZE_Y*ZOOM_SCR_VERT) - (*Yscreen * ZOOM_SCR_VERT) - 1;
- return (*Xscreen >= ((CLIP_LEFT*8) - HORZ_MARGE) && *Xscreen < ((CLIP_RIGHT*8) + HORZ_MARGE) &&
- (*Yscreen)-(*Zscreen) >= (CLIP_UP - VERT_MARGE) && *Yscreen < (CLIP_DOWN + VERT_MARGE));
- }
- void m7_Init(short SizeHorz, short ViewL, short ViewF)
- {
- //#warning "Note: Need to add checks here"
- Vert = (short *)malloc(sizeof(short)*SIZE_Y);
- Horz = (short *)malloc(sizeof(short)*SIZE_X*SIZE_Y);
- m7_CosAndSin();
- m7_CreateMatrix(SizeHorz, ViewL, ViewF);
- }
- void m7_ReInit(short SizeHorz, short ViewL, short ViewF)
- {
- if(Horz != NULL && Vert != NULL) // If init had already been called
- m7_CreateMatrix(SizeHorz, ViewL, ViewF);
- }
- void m7_End()
- {
- free(Vert);
- free(Horz);
- }
- void m7_LoadIdentity()
- {
- Dir=0;
- X=0;
- Y=0;
- }
- void m7_ScrollSky(short s)
- {
- NumByte+=s+(SIZE_SKY_X << NBDIVSCROLL);
- NumByte%=(SIZE_SKY_X << NBDIVSCROLL);
- #if ENABLE_GRAYSCALE == 1
- SkyChange = 2;
- #else
- SkyChange = 1;
- #endif
- }
- void m7_Rotate(short angle)
- {
- short prevDir = Dir;
- Dir+=angle+NBDIR;
- Dir%=NBDIR;
- m7_ScrollSky((prevDir-Dir) * SCROLL);
- }
- void m7_SetDir(unsigned short dir)
- {
- short prevDir = Dir;
- Dir=dir;
- m7_ScrollSky((prevDir-Dir) * SCROLL);
- }
- void m7_LookAt(short x, short y)
- {
- register unsigned short i, newdir = Dir;
- signed short Py;
- signed short proj = 0;
- y -=(Y>>SINCOS);
- x -=(X>>SINCOS);
- for (i = NBDIR; i--;)
- {
- Py = (y * CosTable[i] + x * SinTable[i]);
- if (proj < Py)
- {
- proj = Py;
- newdir = i;
- }
- }
- m7_SetDir(newdir);
- }
- void m7_TranslateXY(short x, short y)
- {
- X+=x << SINCOS;
- Y+=y << SINCOS;
- }
- void m7_TranslateDir(short dist)
- {
- X+= (dist*SinTable[Dir]);
- Y+= (dist*CosTable[Dir]);
- }
- void m7_SetCameraPos(short x, short y)
- {
- X=x << SINCOS;
- Y=y << SINCOS;
- }
- void m7_SetCameraPos2(short x, short y)
- {
- X=x;
- Y=y;
- }
- void m7_GetCameraPos(short * x, short * y)
- {
- *x=X >> SINCOS;
- *y=Y >> SINCOS;
- }
- #if ENABLE_GRAYSCALE == 1
- static inline void m7_GetPlanes()
- {
- Plane00 = (unsigned char *)GrayDBufGetHiddenPlane (DARK_PLANE);
- Plane10 = (unsigned char *)GrayDBufGetHiddenPlane (LIGHT_PLANE);
- GrayDBufToggle();
- Plane01 = (unsigned char *)GrayDBufGetHiddenPlane (DARK_PLANE);
- Plane11 = (unsigned char *)GrayDBufGetHiddenPlane (LIGHT_PLANE);
- GrayDBufToggle();
- }
- void m7_GrayEnd()
- {
- GrayOff ();
- free(Buff);
- }
- void m7_DBufToggle()
- {
- if(Plane)
- {
- Plane0 = Plane00;
- Plane1 = Plane10;
- }
- else
- {
- Plane0 = Plane01;
- Plane1 = Plane11;
- }
- GrayDBufToggle();
- Plane = 1 - Plane;
- }
- void m7_GrayInit()
- {
- // Need to add checks here!
- //#warning "Note: Add a check here"
- Buff = (unsigned char *)calloc(GRAYDBUFFER_SIZE,1);
- //#warning "Note: Make sure GrayOn() is successful"
- GrayOn ();
- GrayDBufInit (Buff);
- m7_GetPlanes();
- m7_DBufToggle();
- }
- #if ENABLE_LARGE_MAPS == 2
- void m7_SetMap(ExtraMap * m)
- {
- MapData = m->map;
- MapTiles = m->maptiles;
- DimX = m->dimX;
- DimY = m->dimY;
- TilesOffset = m->tiles->tilesWidth * m->tiles->tilesHeight << 3;
- }
- #else
- void m7_SetMap(Map * m)
- {
- MapData = m->map;
- Map0 = m->map0;
- Map1 = m->map1;
- DimX = m->dimX;
- DimY = m->dimY;
- }
- #endif
- void m7_SetSky(unsigned char *sky0, unsigned char *sky1)
- {
- Sky0 = sky0;
- Sky1 = sky1;
- }
- #else // no grayscale
- #if ENABLE_LARGE_MAPS == 2
- void m7_SetMap(ExtraMap * m)
- {
- MapData = m->map;
- MapTiles = m->maptiles;
- DimX = m->dimX;
- DimY = m->dimY;
- TilesOffset = m->tiles->tilesWidth * m->tiles->tilesHeight << 3;
- }
- #else
- void m7_SetMap(Map * m)
- {
- MapData = m->map;
- Map0 = m->map0;
- DimX = m->dimX;
- DimY = m->dimY;
- }
- #endif
- void m7_SetSky(unsigned char *sky)
- {
- Sky0 = sky;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement