Advertisement
Siapran

mode7.c

Oct 2nd, 2011
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.62 KB | None | 0 0
  1. #include "mode7.h"
  2. #include <tigcclib.h>
  3.  
  4. INT_HANDLER OldInt1 = NULL;
  5. #if SHOW_FPS == 1
  6. char fpsStr[3];
  7. #endif
  8. char hwVersion;
  9. volatile char quit = 0;
  10. char modifying = 0;
  11. char nextModif = 0;
  12. short timeCount = 0;
  13. volatile short fCount = 0;
  14. void (*m7_Modif)();
  15. void (*m7_MoreDisplay)();
  16. long K3;
  17. long K4;
  18. long K5;
  19. signed char CosTable[NBDIR];
  20. signed char SinTable[NBDIR];
  21. short * Vert;
  22. short * Horz;
  23. float tabstep[SIZE_Y];
  24. short Dir=0;    // direction
  25. #if ENABLE_GRAYSCALE == 1
  26. unsigned char SkyChange = 2;
  27. #else
  28. unsigned char SkyChange = 1;
  29. #endif
  30. short X=0;      // x camera
  31. short Y=0;      // y camera
  32. unsigned char * MapData;
  33. #if ENABLE_LARGE_MAPS == 2
  34. unsigned char ** MapTiles;
  35. unsigned short TilesOffset;
  36. #else
  37. unsigned char * Map0;
  38. #endif
  39. unsigned char * Sky0;
  40. #if ENABLE_GRAYSCALE == 1
  41. unsigned char * Buff;
  42. #if ENABLE_LARGE_MAPS != 2
  43. unsigned char * Map1;
  44. #endif
  45. unsigned char * Sky1;
  46. unsigned char Plane = 0;
  47. unsigned char * Plane0;
  48. unsigned char * Plane1;
  49. unsigned char * Plane00;
  50. unsigned char * Plane10;
  51. unsigned char * Plane01;
  52. unsigned char * Plane11;
  53. #else
  54. char DispSky[SIZE_SKY_X*SIZE_SKY_Y];
  55. char Virtual[3000];
  56. #endif
  57. short DimX;
  58. short DimY;
  59. short NumByte=0;
  60.  
  61. short m7_ProjXYZ(Sprite * sprite, short * Xscreen, short * Yscreen, short * Zscreen, short * scaling, short nbscale);
  62.  
  63. #if ENABLE_ROTATION == 1
  64.  
  65.     #define COORD_XY_ROT \
  66.         coordy = (coordy0 - *(short*)(horzjsizex)*sindir)>>(SINCOS + ZOOM); \
  67.         coordx = (coordx0 + *(short*)(horzjsizex++)*cosdir)>>(SINCOS + ZOOM); \
  68.  
  69. #else
  70.  
  71.     #define COORD_XY_ROT \
  72.         coordx = ((x >> SINCOS) - *(short*)(horzjsizex++)) >> ZOOM;
  73.    
  74. #endif
  75.  
  76. #if ENABLE_GRAYSCALE == 1
  77.  
  78. #define IN_MAP_2(SHIFT) \
  79.     if(map0[bytemap] & offsetmap)   *dispword0 |= (PIX >> SHIFT); \
  80.     if(map1[bytemap] & offsetmap)   *dispword1 |= (PIX >> SHIFT);
  81.  
  82. #define IN_MAP_3(SHIFT) \
  83.     if(*mapoffset)  *dispword0 |= (PIX >> SHIFT); \
  84.     if(*(mapoffset+tilesoffset))    *dispword1 |= (PIX >> SHIFT);  
  85.  
  86. #else
  87.  
  88. #define IN_MAP_2(SHIFT) \
  89.     if(map0[bytemap] & offsetmap)   *dispword0 |= (PIX >> SHIFT); \
  90.  
  91. #define IN_MAP_3(SHIFT) \
  92.     if(*mapoffset)  *dispword0 |= (PIX >> SHIFT);
  93.  
  94. #endif
  95.        
  96. #if ENABLE_LARGE_MAPS == 0
  97.  
  98. #define IN_MAP(SHIFT) \
  99.     COORD_XY_ROT \
  100.     bytemap = coordx+coordy*dimx; \
  101.     offsetmap = 1 << (bytemap & 0x7); \
  102.     bytemap >>= 3; \
  103.     IN_MAP_2(SHIFT)
  104.  
  105. #endif
  106.  
  107. #if ENABLE_LARGE_MAPS == 1
  108.  
  109. #define IN_MAP(SHIFT) \
  110.     COORD_XY_ROT \
  111.     offsetmap = 1 << (coordx & 0x7); \
  112.     bytemap = ((long)coordx+(long)coordy*(long)dimx) >> 3; \
  113.     IN_MAP_2(SHIFT)
  114.  
  115. #endif
  116.  
  117. #if ENABLE_LARGE_MAPS == 2
  118.  
  119. #define IN_MAP(SHIFT) \
  120.     COORD_XY_ROT \
  121.     mapoffset = maptiles[(coordy >> TILES_SHIFT) * dimx + (coordx >> TILES_SHIFT)] + ((coordy & (TILES_SIZE - 1)) << TILES_SHIFT) + (coordx & (TILES_SIZE - 1)); \
  122.     IN_MAP_3(SHIFT)
  123.    
  124. #endif
  125.  
  126.  
  127. #if ZOOM_SCR_HORZ == 1
  128. #define PIX 0x8000
  129. #elif ZOOM_SCR_HORZ == 2
  130. #define PIX 0xC000
  131. #elif ZOOM_SCR_HORZ == 4
  132. #define PIX 0xF000
  133. #elif ZOOM_SCR_HORZ == 8
  134. #define PIX 0xFF00
  135. #endif
  136.  
  137. // [Thanks to Geogeo from pws.tigen.org for this code]
  138. #define __HARDWARE_FREQUENCY        20970 //256
  139. static volatile unsigned short __COUNTER_HARDWARE=0;
  140.  
  141. DEFINE_INT_HANDLER (SceneModif)
  142. {
  143.   ExecuteHandler (OldInt1);
  144.     //HARDWARE VERSION 1.0
  145.     if (hwVersion==1)
  146.     {
  147.         //Incrémentation
  148.         __COUNTER_HARDWARE+=__HARDWARE_FREQUENCY;
  149.    
  150.         //Execution interrupotion
  151.         if (__COUNTER_HARDWARE<=32768)
  152.             return;
  153.         //Remise à zéro
  154.         __COUNTER_HARDWARE-=32768;
  155.     }
  156. // [/Thanks to Geogeo from pws.tigen.org for this code]
  157.     timeCount++;
  158.     if (timeCount == (256))
  159.     {
  160.         timeCount = 0;
  161. #if SHOW_FPS == 1
  162.         sprintf (fpsStr, "%d", fCount);
  163.         DrawStr (24, 0, fpsStr, A_REPLACE);
  164. #endif
  165.         fCount = 0;
  166.     }
  167.    
  168.     if ((modifying + nextModif) <= 0)
  169.     {
  170.         modifying = 1;
  171.         nextModif = GAME_SPEED;
  172.         m7_Modif();
  173.         modifying = 0;
  174.     }
  175.     if(nextModif)
  176.         nextModif--;
  177. }
  178.  
  179. static inline void m7_DrawSky()
  180. {
  181.     register unsigned char i;
  182. #if ENABLE_GRAYSCALE == 1
  183.     unsigned char * offset0=OFFSET_SKY_HORZ+ 30*OFFSET_SKY_VERT + Plane0;
  184.     unsigned char * offset1=OFFSET_SKY_HORZ+ 30*OFFSET_SKY_VERT + Plane1;
  185.     unsigned char * offset12 = NULL, *sky1offset, *sky1offset2 = NULL;
  186. #else
  187.     unsigned char * offset0=OFFSET_SKY_HORZ+ 30*OFFSET_SKY_VERT + Virtual;
  188. #endif
  189.     unsigned char * offset02 = NULL;
  190.     unsigned char * sky0offset;
  191.     unsigned char * sky0offset2 = NULL;
  192.     unsigned short length, length2=0;
  193.     const short Byte = NumByte >> NBDIVSCROLL;
  194.    
  195.     length = SIZE_SKY_X - Byte;
  196.     if(length > SIZE_SKY_HORZ)
  197.     {
  198.         length = SIZE_SKY_HORZ;
  199.     } else
  200.     {
  201.         length2 = SIZE_SKY_HORZ - length;
  202.         sky0offset2 = Sky0;
  203. #if ENABLE_GRAYSCALE == 1
  204.         sky1offset2 = Sky1;
  205. #endif
  206.         offset02 = offset0 + length;
  207. #if ENABLE_GRAYSCALE == 1
  208.         offset12 = offset1 + length;
  209. #endif
  210.     }
  211.     sky0offset = Sky0+Byte;
  212. #if ENABLE_GRAYSCALE == 1
  213.     sky1offset = Sky1+Byte;
  214. #endif
  215.    
  216.     for(i=SIZE_SKY_Y;i--;)
  217.     {
  218.    
  219.         memcpy(offset0, sky0offset, length);
  220. #if ENABLE_GRAYSCALE == 1
  221.         memcpy(offset1, sky1offset, length);
  222. #endif
  223.         sky0offset+=SIZE_SKY_X;
  224. #if ENABLE_GRAYSCALE == 1
  225.         sky1offset+=SIZE_SKY_X;
  226. #endif
  227.        
  228.         if (length < SIZE_SKY_HORZ)
  229.         {
  230.             memcpy(offset02, sky0offset2, length2);
  231. #if ENABLE_GRAYSCALE == 1
  232.             memcpy(offset12, sky1offset2, length2);
  233. #endif
  234.             sky0offset2+=SIZE_SKY_X;
  235. #if ENABLE_GRAYSCALE == 1
  236.             sky1offset2+=SIZE_SKY_X;
  237. #endif
  238.             offset02+=30;
  239. #if ENABLE_GRAYSCALE == 1
  240.             offset12+=30;
  241. #endif
  242.         }
  243.         offset0+=30;
  244. #if ENABLE_GRAYSCALE == 1
  245.         offset1+=30;
  246. #endif
  247.     }
  248.  
  249. }
  250.  
  251. static inline void m7_DrawFrame()
  252. {
  253.     register unsigned char i,j;
  254.     #if ENABLE_ROTATION == 1
  255.     signed short coordy0, coordx0;
  256.     const short y = Y;
  257.    
  258.     const short cosdir = CosTable[Dir];
  259.     const short sindir = SinTable[Dir];
  260.     #else
  261.     const short y = (Y >> SINCOS);
  262.     #endif
  263.     signed short * horzjsizex = Horz, coordy, coordx, vertj;
  264.     short * pvertj = Vert;
  265.     const signed short x = X, dimx = DimX;
  266. #if ENABLE_LARGE_MAPS == 2
  267.     unsigned char ** maptiles = MapTiles;
  268.     unsigned char * mapoffset;
  269. #if ENABLE_GRAYSCALE == 1
  270.     const unsigned short tilesoffset = TilesOffset;
  271. #endif
  272. #else
  273.     unsigned short bytemap, offsetmap;
  274.     const unsigned char * map0 = Map0;
  275. #if ENABLE_GRAYSCALE == 1
  276.     const unsigned char * map1 = Map1;
  277. #endif
  278. #endif
  279.  
  280. #if ENABLE_GRAYSCALE == 1
  281.     unsigned short * dispword0 = &((short*)Plane0)[OFFSET_SCR_HORZ+ 15*OFFSET_SCR_VERT];
  282.     unsigned short * dispword1 = &((short*)Plane1)[OFFSET_SCR_HORZ+ 15*OFFSET_SCR_VERT];
  283. #else
  284.     unsigned short * dispword0 = &((short*)Virtual)[OFFSET_SCR_HORZ+ 15*OFFSET_SCR_VERT];
  285. #endif
  286.    
  287.     for(j=SIZE_Y; j--;)
  288.     {
  289.         vertj = *(short*)(pvertj++);
  290. #if ENABLE_ROTATION == 1
  291.         coordy0 = vertj*cosdir+y;
  292.         coordx0 = vertj*sindir+x;
  293. #else
  294.         coordy = (y - vertj) >> ZOOM;
  295. #endif
  296.        
  297.         for(i=(SIZE_X*ZOOM_SCR_HORZ/16); i--;)
  298.         {
  299.             *dispword0 = 0;
  300. #if ENABLE_GRAYSCALE == 1
  301.             *dispword1 = 0;
  302. #endif
  303.  
  304. #if ZOOM_SCR_HORZ <= 8
  305.             IN_MAP(0)
  306.            
  307.             IN_MAP(ZOOM_SCR_HORZ)
  308. #endif
  309.  
  310. #if ZOOM_SCR_HORZ <= 4
  311.             IN_MAP(ZOOM_SCR_HORZ * 2)
  312.            
  313.             IN_MAP(ZOOM_SCR_HORZ * 3)
  314. #endif
  315.  
  316. #if ZOOM_SCR_HORZ <= 2
  317.             IN_MAP(ZOOM_SCR_HORZ * 4)
  318.            
  319.             IN_MAP(ZOOM_SCR_HORZ * 5)
  320.            
  321.             IN_MAP(ZOOM_SCR_HORZ * 6)
  322.            
  323.             IN_MAP(ZOOM_SCR_HORZ * 7)
  324. #endif
  325.  
  326. #if ZOOM_SCR_HORZ == 1
  327.             IN_MAP(ZOOM_SCR_HORZ * 8)
  328.            
  329.             IN_MAP(ZOOM_SCR_HORZ * 9)
  330.            
  331.             IN_MAP(ZOOM_SCR_HORZ * 10)
  332.            
  333.             IN_MAP(ZOOM_SCR_HORZ * 11)
  334.            
  335.             IN_MAP(ZOOM_SCR_HORZ * 12)
  336.            
  337.             IN_MAP(ZOOM_SCR_HORZ * 13)
  338.            
  339.             IN_MAP(ZOOM_SCR_HORZ * 14)
  340.            
  341.             IN_MAP(ZOOM_SCR_HORZ * 15)
  342. #endif
  343.  
  344. #if ZOOM_SCR_VERT == 2
  345.             *(dispword0 + 15) = *dispword0;
  346. #if ENABLE_GRAYSCALE == 1
  347.             *(dispword1 + 15) = *dispword1;
  348. #endif
  349.  
  350. #elif ZOOM_SCR_VERT > 1
  351.             register unsigned short offset = 0;
  352.             register unsigned char k;
  353.            
  354.             for(k=(ZOOM_SCR_VERT - 1); k--;)
  355.             {
  356.                 offset += 15;
  357.                
  358.                 *(dispword0+offset) = *dispword0;
  359. #if ENABLE_GRAYSCALE == 1
  360.                 *(dispword1+offset) = *dispword1;
  361. #endif
  362.             }
  363. #endif
  364.  
  365.             dispword0++;
  366. #if ENABLE_GRAYSCALE == 1
  367.             dispword1++;
  368. #endif
  369.         }
  370.        
  371.         dispword0 += 15*ZOOM_SCR_VERT-((ZOOM_SCR_HORZ*SIZE_X)/16);
  372. #if ENABLE_GRAYSCALE == 1
  373.         dispword1 += 15*ZOOM_SCR_VERT-((ZOOM_SCR_HORZ*SIZE_X)/16);
  374. #endif
  375.     }
  376. }
  377.  
  378. static inline void m7_Display()
  379. {
  380. #if (ENABLE_SPRITES == 1) || (ENABLE_FIRST_PLANE_SPRITES == 1)
  381.     register unsigned short i;
  382. #endif
  383. #if ENABLE_SPRITES == 1
  384.     Sprite * sprite;
  385.     short Xscreen, Yscreen, Zscreen, scaling;
  386. #endif
  387.  
  388. #if ENABLE_SKY == 1
  389.     if(SkyChange)
  390.     {
  391.         SkyChange--;
  392.         m7_DrawSky();
  393.     }
  394. #endif
  395.     m7_DrawFrame();
  396.    
  397. #if ENABLE_SPRITES == 1
  398.  
  399.     nbDisplayedSprites = 0;
  400.    
  401.     for (i=MAXSPRT; i--;)
  402.         if (AddedSprites[i] == 1)
  403.         {
  404.             sprite = AllSpriteList[i];
  405.             if (m7_ProjXYZ(sprite, &Xscreen, &Yscreen, &Zscreen, &scaling, sprite->nbScales))
  406.                 m7sprites_SetDisplayed(i, Xscreen, Yscreen, Zscreen, Dir, sprite->dir, scaling);
  407.         }
  408.    
  409.     m7sprites_SortDisplayedSpriteList();
  410.  
  411.     for (i=0; i < nbDisplayedSprites; i++)
  412. #if ENABLE_GRAYSCALE == 1
  413.         m7sprite_Display(i, Plane0, Plane1);
  414. #else
  415.         m7sprite_Display(i, Virtual);
  416. #endif
  417.    
  418.    
  419. #endif
  420.  
  421. #if ENABLE_FIRST_PLANE_SPRITES == 1
  422.     for (i=0; i < MAXSPRT; i++)
  423.         if (AddedSprites[i] == 2)
  424.         {
  425. #if ENABLE_GRAYSCALE == 1
  426.             m7sprite_Display1stPlane(i, Plane0, Plane1);
  427. #else
  428.             m7sprite_Display1stPlane(i, Virtual);
  429. #endif
  430.         }
  431.  
  432. #endif
  433.  
  434. #if ENABLE_MORE_DISPLAY == 1
  435.     m7_MoreDisplay();
  436. #endif
  437.  
  438. #if ENABLE_GRAYSCALE == 1
  439.     m7_DBufToggle();
  440. #else
  441.     memcpy(LCD_MEM, Virtual, LCD_SIZE);
  442. #endif
  443. }
  444.  
  445.  
  446. void m7_SetLoopFunc(void (*modifFunc)())
  447. {
  448.     m7_Modif = modifFunc;
  449. }
  450.  
  451.  
  452. void m7_LoopInit()
  453. {
  454.     quit = 0;
  455.     modifying = 0;
  456.     timeCount = 0;
  457.     fCount = 0;
  458.     hwVersion = HW_VERSION;
  459.   OldInt1 = GetIntVec (AUTO_INT_1);
  460. }
  461.  
  462. void m7_LoopEnd()
  463. {
  464.   SetIntVec (AUTO_INT_1, OldInt1); 
  465. }
  466.  
  467. void m7_MainLoop()
  468. {
  469.   SetIntVec (AUTO_INT_1, SceneModif);
  470.     while(!quit)
  471.     {
  472.         m7_Display();
  473.         fCount++;      
  474.     }  
  475. }
  476.  
  477. void m7_LoopQuit()
  478. {
  479.     quit = 1;
  480. }
  481.  
  482. void m7_InitMoreDisplay(void (*moreDispFunc)())
  483. {
  484.     m7_MoreDisplay = moreDispFunc;
  485. }
  486.  
  487. static inline void m7_CosAndSin()
  488. {
  489.     register short i;
  490.    
  491.     for(i = NBDIR; i--;)
  492.     {
  493.         const float angle = (i+i)*(PI/NBDIR);  // Now, PI/NBDIR will be a constant value and (i+i) is faster than i*2
  494.         CosTable[i] = (signed char)(cos(angle)*(1<<SINCOS));
  495.         SinTable[i] = (signed char)(sin(angle)*(1<<SINCOS));
  496.     }
  497. }
  498.  
  499. void m7_CreateMatrix(short SizeHorz, short ViewL, short ViewF)
  500. {
  501.     register short i,j;
  502.     K3 = SizeHorz * (SIZE_Y * (ViewF - 1));
  503.     const long K1 = -K3*ViewL*ViewL;
  504.     const long K2 = 2*SIZE_X*ViewF;
  505.     K4 = (SizeHorz-1)*ViewF;
  506.    
  507.     K5 = SizeHorz*(-(ViewF-1)*ViewL);
  508.    
  509.     long val = ((SIZE_Y - 1) * K4 - K3);
  510.  
  511.     for(i=SIZE_Y; i--;)
  512.     {
  513.             tabstep[SIZE_Y - i - 1] = (float)K1/(K2 * val);
  514.             Vert[SIZE_Y - i - 1] = (i*K5)/(val + val);
  515.             val -= K4;
  516.     }
  517.    
  518.     for(j=SIZE_Y; j--;)
  519.     {
  520.         const float _tabstep = tabstep[j];
  521.         float _tabstep1 = ((SIZE_X/2)+1)*_tabstep;
  522.         float _tabstep2 = (-((SIZE_X/2)-1))*_tabstep;
  523.         const short offset = SIZE_X * j;
  524.        
  525.         for(i=SIZE_X/2; i--;)
  526.         {
  527.             Horz[((SIZE_X/2) - i%(SIZE_X/2) - 1) + offset] = _tabstep1;
  528.             Horz[((SIZE_X/2) + i%(SIZE_X/2)) + offset] = _tabstep2;
  529.            
  530.             _tabstep1 -= _tabstep;
  531.             _tabstep2 += _tabstep;
  532.         }
  533.     }
  534. }
  535.  
  536. short m7_ProjXYZ(Sprite * sprite, short * Xscreen, short * Yscreen, short * Zscreen, short * scaling, short nbscale)
  537. {
  538.     const short x = sprite->x - (X>>SINCOS), y = sprite->y - (Y>>SINCOS), z = sprite->z;
  539.     const short cosdir = CosTable[Dir];
  540.     const short sindir = SinTable[Dir];
  541.     short Py = (y * cosdir + x * sindir)>>SINCOS;
  542.     if (Py > FRONT_CLIP_DIST)   return 0;
  543.     if (Py < -BACK_CLIP_DIST)   return 0;
  544.     const short Px = (y * sindir - x * cosdir)>>SINCOS;
  545.     Py += Py;
  546.    
  547.     *Yscreen = K3*Py/(K4*Py-K5);
  548.     const short Yscreen2 = (*Yscreen < 0 ? 0 : *Yscreen);
  549.    
  550.     *scaling = nbscale - 1 - ((nbscale*Vert[SIZE_Y - Yscreen2 - 1])/Vert[0]);
  551.     *Zscreen = z - ((z * Vert[SIZE_Y - Yscreen2 - 1])/Vert[0]);
  552.     if(*scaling < 0)
  553.         *scaling = 0;
  554.     *Xscreen = (Px*ZOOM_SCR_HORZ)/tabstep[SIZE_Y - Yscreen2 - 1] + (SIZE_X*ZOOM_SCR_HORZ)/2 + (OFFSET_SCR_HORZ << 3);
  555.     *Yscreen = OFFSET_SCR_VERT + (SIZE_Y*ZOOM_SCR_VERT) - (*Yscreen * ZOOM_SCR_VERT) - 1;
  556.    
  557.     return (*Xscreen >= ((CLIP_LEFT*8) - HORZ_MARGE) && *Xscreen < ((CLIP_RIGHT*8) + HORZ_MARGE) &&
  558.     (*Yscreen)-(*Zscreen) >= (CLIP_UP - VERT_MARGE) && *Yscreen < (CLIP_DOWN + VERT_MARGE));
  559. }
  560.  
  561. void m7_Init(short SizeHorz, short ViewL, short ViewF)
  562. {
  563.     //#warning "Note:  Need to add checks here"
  564.     Vert = (short *)malloc(sizeof(short)*SIZE_Y);
  565.     Horz = (short *)malloc(sizeof(short)*SIZE_X*SIZE_Y);
  566.     m7_CosAndSin();
  567.     m7_CreateMatrix(SizeHorz, ViewL, ViewF);   
  568. }
  569.  
  570. void m7_ReInit(short SizeHorz, short ViewL, short ViewF)
  571. {
  572.     if(Horz != NULL && Vert != NULL) // If init had already been called
  573.         m7_CreateMatrix(SizeHorz, ViewL, ViewF);
  574. }
  575.  
  576. void m7_End()
  577. {
  578.     free(Vert);
  579.     free(Horz);
  580. }
  581.  
  582. void m7_LoadIdentity()
  583. {
  584.     Dir=0;
  585.     X=0;
  586.     Y=0;
  587. }
  588.  
  589. void m7_ScrollSky(short s)
  590. {
  591.     NumByte+=s+(SIZE_SKY_X << NBDIVSCROLL);
  592.     NumByte%=(SIZE_SKY_X << NBDIVSCROLL);  
  593. #if ENABLE_GRAYSCALE == 1
  594.     SkyChange = 2;
  595. #else
  596.     SkyChange = 1;
  597. #endif
  598. }
  599.  
  600. void m7_Rotate(short angle)
  601. {
  602.     short prevDir = Dir;
  603.     Dir+=angle+NBDIR;
  604.     Dir%=NBDIR;
  605.     m7_ScrollSky((prevDir-Dir) * SCROLL);
  606. }
  607.  
  608. void m7_SetDir(unsigned short dir)
  609. {
  610.     short prevDir = Dir;
  611.     Dir=dir;
  612.     m7_ScrollSky((prevDir-Dir) * SCROLL);
  613. }
  614.  
  615. void m7_LookAt(short x, short y)
  616. {
  617.     register unsigned short i, newdir = Dir;
  618.     signed short Py;
  619.     signed short proj = 0;
  620.    
  621.     y -=(Y>>SINCOS);
  622.     x -=(X>>SINCOS);
  623.    
  624.     for (i = NBDIR; i--;)
  625.     {
  626.         Py = (y * CosTable[i] + x * SinTable[i]);
  627.         if (proj < Py)
  628.         {
  629.             proj = Py;
  630.             newdir = i;
  631.         }
  632.     }
  633.    
  634.     m7_SetDir(newdir);
  635. }
  636.  
  637. void m7_TranslateXY(short x, short y)
  638. {
  639.     X+=x << SINCOS;
  640.     Y+=y << SINCOS;
  641. }
  642.  
  643. void m7_TranslateDir(short dist)
  644. {
  645.     X+= (dist*SinTable[Dir]);
  646.     Y+= (dist*CosTable[Dir]);
  647. }
  648.  
  649. void m7_SetCameraPos(short x, short y)
  650. {
  651.     X=x << SINCOS;
  652.     Y=y << SINCOS;
  653. }
  654.  
  655. void m7_SetCameraPos2(short x, short y)
  656. {
  657.     X=x;
  658.     Y=y;
  659. }
  660.  
  661. void m7_GetCameraPos(short * x, short * y)
  662. {
  663.     *x=X >> SINCOS;
  664.     *y=Y >> SINCOS;
  665. }
  666.  
  667. #if ENABLE_GRAYSCALE == 1
  668.  
  669. static inline void m7_GetPlanes()
  670. {
  671.     Plane00 = (unsigned char *)GrayDBufGetHiddenPlane (DARK_PLANE);
  672.     Plane10 = (unsigned char *)GrayDBufGetHiddenPlane (LIGHT_PLANE);
  673.    
  674.     GrayDBufToggle();
  675.  
  676.     Plane01 = (unsigned char *)GrayDBufGetHiddenPlane (DARK_PLANE);
  677.     Plane11 = (unsigned char *)GrayDBufGetHiddenPlane (LIGHT_PLANE);
  678.  
  679.     GrayDBufToggle();
  680. }
  681.  
  682. void m7_GrayEnd()
  683. {
  684.   GrayOff ();
  685.     free(Buff);
  686. }
  687.  
  688. void m7_DBufToggle()
  689. {
  690.     if(Plane)
  691.     {
  692.         Plane0 = Plane00;
  693.         Plane1 = Plane10;
  694.     }
  695.     else
  696.     {
  697.         Plane0 = Plane01;
  698.         Plane1 = Plane11;
  699.     }
  700.     GrayDBufToggle();
  701.     Plane = 1 - Plane;
  702. }
  703.  
  704. void m7_GrayInit()
  705. {
  706.     // Need to add checks here!
  707.     //#warning "Note:  Add a check here"
  708.     Buff = (unsigned char *)calloc(GRAYDBUFFER_SIZE,1);
  709.     //#warning "Note:  Make sure GrayOn() is successful"
  710.     GrayOn ();
  711.     GrayDBufInit (Buff);
  712.     m7_GetPlanes();
  713.     m7_DBufToggle();
  714. }
  715.  
  716. #if ENABLE_LARGE_MAPS == 2
  717.  
  718. void m7_SetMap(ExtraMap * m)
  719. {
  720.     MapData = m->map;
  721.     MapTiles = m->maptiles;
  722.     DimX = m->dimX;
  723.     DimY = m->dimY;
  724.     TilesOffset = m->tiles->tilesWidth * m->tiles->tilesHeight << 3;
  725. }
  726.  
  727. #else
  728.  
  729. void m7_SetMap(Map * m)
  730. {
  731.     MapData = m->map;
  732.     Map0 = m->map0;
  733.     Map1 = m->map1;
  734.     DimX = m->dimX;
  735.     DimY = m->dimY;
  736. }
  737.  
  738. #endif
  739.  
  740.  
  741. void m7_SetSky(unsigned char *sky0, unsigned char *sky1)
  742. {
  743.     Sky0 = sky0;
  744.     Sky1 = sky1;
  745. }
  746.  
  747. #else // no grayscale
  748.  
  749. #if ENABLE_LARGE_MAPS == 2
  750.  
  751. void m7_SetMap(ExtraMap * m)
  752. {
  753.     MapData = m->map;
  754.     MapTiles = m->maptiles;
  755.     DimX = m->dimX;
  756.     DimY = m->dimY;
  757.     TilesOffset = m->tiles->tilesWidth * m->tiles->tilesHeight << 3;
  758. }
  759.  
  760. #else
  761.  
  762. void m7_SetMap(Map * m)
  763. {
  764.     MapData = m->map;
  765.     Map0 = m->map0;
  766.     DimX = m->dimX;
  767.     DimY = m->dimY;
  768. }
  769.  
  770. #endif
  771.  
  772. void m7_SetSky(unsigned char *sky)
  773. {
  774.     Sky0 = sky;
  775. }
  776.  
  777. #endif
  778.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement