Guest User

Raycaster

a guest
Dec 4th, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <SDL2/SDL.h>
  3. #include <stdbool.h>
  4. #include "map.h"
  5.  
  6. #define RESX 960
  7. #define RESY 540
  8. #define HALFRESX 480
  9. #define HALFRESY 270
  10. #define ROTATESPEED 0.03
  11. #define MOVESPEED 0.08
  12. #define STRAFEMOVESPEED 0.0565685424948
  13. #define BGCOL 17
  14. #define clipDistance 26
  15. #define playerSize 1
  16.  
  17. struct Point3D {
  18.   float x, y, z;
  19. };
  20.  
  21. struct pos {
  22.   unsigned char x, y, z;
  23. };
  24.  
  25.  
  26. void main() {
  27.  
  28.   char placeDist = 3;
  29.  
  30.   SDL_Window* window = NULL; //init SDL
  31.   SDL_Renderer* renderer = NULL;
  32.   SDL_Surface* surface = NULL;
  33.  
  34.   bool running = true;
  35.  
  36.   if( SDL_Init( SDL_INIT_VIDEO ) < 0 )  {
  37.     printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
  38.     running = false;
  39.   }
  40.  
  41.   window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, RESX, RESY, SDL_WINDOW_SHOWN );
  42.  
  43.   if( window==NULL) {
  44.     printf( "SDL_Error: %s\n", SDL_GetError() );
  45.     running = false;
  46.   }
  47.  
  48.   surface = SDL_GetWindowSurface( window );
  49.   renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
  50.  
  51.   if(renderer==NULL) {
  52.     printf("Renderer error: %s\n", SDL_GetError() );
  53.     running = false;
  54.   }
  55.  
  56.   SDL_Texture * texture = SDL_CreateTexture(renderer,
  57.         SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, RESX,RESY);
  58.   Uint32 pixels[RESX*RESY];
  59.  
  60.  
  61.   const Uint8* keystate = SDL_GetKeyboardState( NULL );
  62.   int t1, t2;
  63.   struct Point3D pos;
  64.   pos.x = 4;
  65.   pos.y = 4;
  66.   pos.z = 4;
  67.   struct Point3D dir;
  68.   dir.x = 1;
  69.   dir.y = 0; //direction vector
  70.   dir.z = 0;
  71.   struct Point3D plane;
  72.   plane.x = 0;
  73.   plane.y = 0.8;
  74.   plane.z = 0.45;
  75.   struct Point3D vel;
  76.  
  77.   float time = 0;
  78.   float oldtime = 0; //for FPS calculations
  79.  
  80.   void rotate(float rotspeed) {
  81.     float oldDirX = dir.x;
  82.     dir.x = dir.x * cos(rotspeed) - dir.y * sin(rotspeed);
  83.     dir.y = oldDirX * sin(rotspeed) + dir.y * cos(rotspeed);
  84.     float oldPlaneX = plane.x;
  85.     plane.x = plane.x * cos(rotspeed) - plane.y * sin(rotspeed);
  86.     plane.y = oldPlaneX * sin(rotspeed) + plane.y * cos(rotspeed);
  87.   }
  88.  
  89.   void move() {
  90.     float velocity = ((keystate[SDL_SCANCODE_D] ^ keystate[SDL_SCANCODE_A]) &&
  91.                       (keystate[SDL_SCANCODE_W] ^ keystate[SDL_SCANCODE_S])) ? STRAFEMOVESPEED : MOVESPEED;
  92.  
  93.     struct Point3D oldpos = pos;
  94.  
  95.     if(keystate[SDL_SCANCODE_D]) {
  96.       pos.x -= dir.y * velocity;
  97.       pos.y += dir.x * velocity;
  98.     }
  99.     if(keystate[SDL_SCANCODE_A]) {
  100.       pos.x += dir.y * velocity;
  101.       pos.y -= dir.x * velocity;
  102.     }
  103.     if(keystate[SDL_SCANCODE_W]) {
  104.       pos.x += dir.x * velocity;
  105.       pos.y += dir.y * velocity;
  106.     }
  107.     if(keystate[SDL_SCANCODE_S]) {
  108.       pos.x -= dir.x * velocity;
  109.       pos.y -= dir.y * velocity;
  110.     }
  111.  
  112.     if(MAP[(char)pos.x][(char)pos.y]) pos = oldpos;
  113.   } //remove oldpos and velocity when not needed
  114.  
  115.   while( running ) {
  116.  
  117.     t1 = SDL_GetTicks();
  118.  
  119.     SDL_Event e;
  120.     running  = !(SDL_PollEvent( &e ) && e.type == SDL_QUIT);
  121.  
  122.     move();
  123.  
  124.     if(keystate[SDL_SCANCODE_LEFT]) {
  125.       rotate(-ROTATESPEED);
  126.     }
  127.     if(keystate[SDL_SCANCODE_RIGHT]){
  128.       rotate(ROTATESPEED);
  129.     }
  130.  
  131.     if(keystate[SDL_SCANCODE_SPACE] && MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z] == 0) {
  132.       MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z] =  1;
  133.     }
  134.     else if(keystate[SDL_SCANCODE_LCTRL] && MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z] == 1) {
  135.       MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z] =  0;
  136.     }
  137.     if(keystate[SDL_SCANCODE_L]) running = 0;
  138.  
  139.     for(int x = 0; x < RESX; x += 1 ) { //raycasting code
  140.       for(int y = 0; y < RESY; y += 1) {
  141.  
  142.         struct Point3D camera;
  143.         camera.x = (float)(x/HALFRESX) -1; // ray direction from camera, -1 to 1
  144.         camera.y = (float)(y/HALFRESY) -1;
  145.         struct Point3D rayd;
  146.         rayd.x = dir.x + plane.x * camera.x; // frustum thingy
  147.         rayd.y = dir.y + plane.y * camera.x;
  148.         rayd.z = dir.z + plane.z * camera.y;
  149.        
  150.         struct pos map;
  151.         map.x = (char)pos.x;
  152.         map.y = (char)pos.y;
  153.         map.z = (char)pos.z;
  154.        
  155.         struct Point3D sdist;
  156.         struct Point3D delta;
  157.         delta.x = fabsf(1/rayd.x);
  158.         delta.y = fabsf(1/rayd.y);
  159.         delta.z = fabsf(1/rayd.z);
  160.         struct pos step;
  161.        
  162.         if(rayd.x < 0) {
  163.           step.x = -1;
  164.           sdist.x = (pos.x - map.x) * delta.x; // one side
  165.         }
  166.         else {
  167.           step.x = 1;
  168.           sdist.x = (map.x + 1.0 - pos.x) * delta.x; // have to round the other way for this side.
  169.         }
  170.         if(rayd.y < 0) {
  171.           step.y = -1;
  172.           sdist.y = (pos.y - map.y) * delta.y; // one side
  173.         }
  174.         else {
  175.           step.y = 1;
  176.           sdist.y = (map.y + 1.0 - pos.y) * delta.y; // have to round the other way for this side.
  177.         }
  178.         if(rayd.z < 0) {
  179.           step.z = -1;
  180.           sdist.z = (pos.z - map.z) * delta.z; // one side
  181.         }
  182.         else {
  183.           step.z = 1;
  184.           sdist.z = (map.z + 1.0 - pos.z) * delta.z; // have to round the other way for this side.
  185.         }
  186.  
  187.         char side; //either 0 (NS), or 1 (EW), or 2(UD)
  188.         for( unsigned char stepd = 0; stepd < clipDistance; stepd +=1 ) {
  189.           if(sdist.x < sdist.y ) {
  190.             if(sdist.x < sdist.z) {
  191.               sdist.x += delta.x;
  192.               map.x += step.x;
  193.               side = 0;
  194.             }
  195.             else {
  196.               sdist.z += delta.z;
  197.               map.z += step.z;
  198.               side = 2;
  199.             }
  200.           }
  201.           else {
  202.             sdist.y += delta.y;
  203.             map.y += step.y;
  204.             side = 1;
  205.           }
  206.           if(MAP[map.x][map.y][map.z] !=0) {
  207.             break;
  208.           }
  209.         }
  210.  
  211.        
  212.         float pWallDist;
  213.         if(side == 0) pWallDist = (map.x - pos.x + (1-step.x) / 2) / rayd.x;
  214.         else if( side == 1) pWallDist = (map.y - pos.y + (1-step.y) / 2) / rayd.y; //this
  215.         else pWallDist = (map.z - pos.z + (1-step.z) / 2) / rayd.z;
  216.         unsigned char drawColour;
  217.  
  218.         if(pWallDist>16)
  219.         {
  220.           drawColour = 17;
  221.         }
  222.         else
  223.         {
  224.           drawColour = 255-pWallDist*15;
  225.         }
  226.  
  227.         SDL_RenderDrawPoint( renderer, x, y);
  228.         pixels[y*RESX+x] = drawColour*0x010101;
  229.         }
  230.       }
  231.     SDL_UpdateTexture(texture, NULL, pixels, RESX * sizeof(Uint32));
  232.     SDL_RenderCopy(renderer, texture, NULL, NULL);
  233.     SDL_RenderPresent( renderer );
  234.     t2 = SDL_GetTicks();
  235.     int ms = (float)(t2-t1);
  236.     printf("%d\n", ms );
  237.     SDL_Delay(15);
  238.  
  239.   }
  240.   SDL_DestroyWindow( window );
  241.   SDL_DestroyRenderer( renderer );
  242.   SDL_DestroyTexture( texture );
  243.   SDL_Quit();
  244. }
Advertisement
Add Comment
Please, Sign In to add comment