Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.02 KB | None | 0 0
  1. #include "fxlib.h"
  2. #include "MonochromeLib.h"
  3. #include "syscall.h"
  4. #include <math.h>
  5. #include <stdio.h>
  6.  
  7. static int SysCallCode[] = {0xD201422B,0x60F20000,0x80010070};
  8. static int (*SysCall)(int R4, int R5, int R6, int R7, int FNo ) = (void*)&SysCallCode;
  9.  
  10. int RTC_GetTicks(void)
  11. {
  12.      return (*SysCall)(0, 0, 0, 0, 0x3B);
  13. }
  14.  
  15. long abs(long value)
  16. {
  17.   if (value < 0) return -value;
  18.   else return value;
  19. }
  20.  
  21. #define mapWidth 64
  22. #define mapHeight 64
  23.  
  24. int worldMap[mapWidth][mapHeight]=
  25. {
  26.   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  27.   {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  28.   {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  29.   {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  30.   {1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
  31.   {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
  32.   {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1},
  33.   {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
  34.   {1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
  35.   {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  36.   {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  37.   {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  38.   {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  39.   {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  40.   {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  41.   {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  42.   {1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  43.   {1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  44.   {1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  45.   {1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  46.   {1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  47.   {1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  48.   {1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  49.   {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
  50. };
  51.  
  52. int AddIn_main(int isAppli, unsigned short OptionNum)
  53. {
  54.   int rotSpeed, moveSpeed; // Vitesse de rotation et de mouvement
  55.   int w = 128, h = 64, x; // Dimensions de l'écran
  56.   double posX = 22, posY = 12; // Position du joueur
  57.   double dirX = -1, dirY = 0;
  58.   double planeX = 0, planeY = 0.66;
  59.  
  60.   double time = 0, oldTime = 0;
  61.   char fps[3];
  62.  
  63.   double cameraX;
  64.   double rayPosX, rayPosY;
  65.   double rayDirX, rayDirY;
  66.  
  67.   int mapX, mapY;
  68.  
  69.   double sideDistX, sideDistY;
  70.   double deltaDistX, deltaDistY;
  71.   double perpWallDist;
  72.  
  73.   int stepX, stepY;
  74.   int hit, side;
  75.   int lineHeight;
  76.  
  77.   int drawStart;
  78.  
  79.   int drawEnd;
  80.   double frameTime;
  81.  
  82.   double oldDirX;
  83.   double oldPlaneX;
  84.  
  85.   while(IsKeyUp(KEY_CTRL_EXIT))
  86.   {
  87.     ML_clear_vram();
  88.  
  89.     for(x = 0; x < w; x++)
  90.     {
  91.        cameraX = 2 * x / (double)(w) - 1;
  92.        rayPosX = posX, rayPosY = posY;
  93.        rayDirX = dirX + planeX * cameraX, rayDirY = dirY + planeY * cameraX;
  94.  
  95.        mapX = (int)(rayPosX), mapY = (int)(rayPosY);
  96.  
  97.        sideDistX, sideDistY;
  98.        deltaDistX = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX)), deltaDistY = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
  99.        perpWallDist;
  100.  
  101.        stepX, stepY;
  102.        hit = 0, side;
  103.  
  104.       if (rayDirX < 0)
  105.       {
  106.         stepX = -1;
  107.         sideDistX = (rayPosX - mapX) * deltaDistX;
  108.       }
  109.       else
  110.       {
  111.         stepX = 1;
  112.         sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
  113.       }
  114.       if (rayDirY < 0)
  115.       {
  116.         stepY = -1;
  117.         sideDistY = (rayPosY - mapY) * deltaDistY;
  118.       }
  119.       else
  120.       {
  121.         stepY = 1;
  122.         sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
  123.       }
  124.  
  125.       while (hit == 0) // DDA
  126.       {
  127.         if (sideDistX < sideDistY)
  128.         {
  129.           sideDistX += deltaDistX;
  130.           mapX += stepX;
  131.           side = 0;
  132.         }
  133.         else
  134.         {
  135.           sideDistY += deltaDistY;
  136.           mapY += stepY;
  137.           side = 1;
  138.         }
  139.  
  140.         if (worldMap[mapX][mapY] > 0) hit = 1;
  141.       }
  142.  
  143.       if (side == 0)
  144.         perpWallDist = fabs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX);
  145.       else
  146.         perpWallDist = fabs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY);
  147.  
  148.        lineHeight = abs((int)(h / perpWallDist));
  149.  
  150.        drawStart = -lineHeight / 2 + h / 2;
  151.       if(drawStart < 0)drawStart = 0;
  152.        drawEnd = lineHeight / 2 + h / 2;
  153.       if(drawEnd >= h)drawEnd = h - 1;
  154.  
  155.       ML_vertical_line(x, drawStart, drawEnd, ML_BLACK);
  156.     }
  157.  
  158.     //Gestion du temps et de la vitesse
  159.     oldTime = time;
  160.     time = RTC_GetTicks();
  161.     frameTime = (time - oldTime) / 1000.0;
  162.     sprintf(fps,"%d",frameTime / 1000.0);
  163.     PrintXY(0,0,(const unsigned char *) fps,0);
  164.  
  165.      moveSpeed = frameTime * 30.0;
  166.      rotSpeed = frameTime * 15.0;
  167.  
  168.     //Gestion des touches
  169.     if (IsKeyDown(KEY_CTRL_UP))
  170.     {
  171.       if(worldMap[(int)(posX + dirX * moveSpeed)][(int)(posY)] == 0) posX += dirX * moveSpeed;
  172.       if(worldMap[(int)(posX)][(int)(posY + dirY * moveSpeed)] == 0) posY += dirY * moveSpeed;
  173.     }
  174.  
  175.     if (IsKeyDown(KEY_CTRL_DOWN))
  176.     {
  177.       if(worldMap[(int)(posX - dirX * moveSpeed)][(int)(posY)] == 0) posX -= dirX * moveSpeed;
  178.       if(worldMap[(int)(posX)][(int)(posY - dirY * moveSpeed)] == 0) posY -= dirY * moveSpeed;
  179.     }
  180.  
  181.     if (IsKeyDown(KEY_CTRL_RIGHT))
  182.     {
  183.        oldDirX = dirX;
  184.       dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
  185.       dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
  186.        oldPlaneX = planeX;
  187.       planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
  188.       planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
  189.     }
  190.  
  191.     if (IsKeyDown(KEY_CTRL_LEFT))
  192.     {
  193.        oldDirX = dirX;
  194.       dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
  195.       dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
  196.        oldPlaneX = planeX;
  197.       planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
  198.       planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
  199.     }
  200.  
  201.     ML_display_vram();
  202.   }
  203.     return 1;
  204. }
  205.  
  206. #pragma section _BR_Size
  207. unsigned long BR_Size;
  208. #pragma section
  209. #pragma section _TOP
  210. int InitializeSystem(int isAppli, unsigned short OptionNum)
  211. {
  212.     return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
  213. }
  214. #pragma section
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement