I have finished a raycaster, and I want to optimize it before I add other features, such as variable height walls, texture mapping and lighting. I'd like someone to review the code so I can improve it further.

[Pastebin](https://pastebin.com/raw/91rLfcwd)

```
#include <stdio.h>
#include <SDL2/SDL.h>
#include <sys/time.h>

const int RESX = 960;
const int RESY = 540;
const float ROTSPEED = 0.03;
const float MOVESPEED = 0.08;
const float STRAFEMOVESPEED = 0.05657;
char placeDist = 3;

char MAP[32][32] = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,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0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};

int main() {

  float posX = 3, posY = 4;
  float dirX = 1, dirY = 0; //direction vector
  float planeX = 0, planeY = 0.66; //camera plane

  float time = 0;
  float oldtime = 0; //for FPS calculations

  SDL_Window* window = NULL; //init SDL
  SDL_Renderer* renderer = NULL;
  SDL_Surface* surface = NULL;

  if( SDL_Init( SDL_INIT_VIDEO ) < 0 )  {
    printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
  }

  window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, RESX, RESY, SDL_WINDOW_SHOWN );

  if( window==NULL) {
    printf( "SDL_Error: %s\n", SDL_GetError() );
  }

  surface = SDL_GetWindowSurface( window );
  renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);

  if(renderer==NULL) {
    printf("Renderer error: %s\n", SDL_GetError() );
  }

  char running = 1;
  const Uint8* keystate = SDL_GetKeyboardState( NULL );
  clock_t t1, t2;

  while( running == 1 ) {

    t1 = clock();
    SDL_Rect rect;
    rect.w = 1;
    SDL_Event e;

    SDL_SetRenderDrawColor( renderer, 0x11, 0x11, 0x11, 0xFF);
    SDL_RenderClear( renderer );
    if( SDL_PollEvent( &e ) > 0) {
      if( e.type == SDL_QUIT) running = 0;
    }

      float oldPlaneX, oldDirX;
      if(keystate[SDL_SCANCODE_RIGHT]) {
        oldDirX = dirX;
        dirX = dirX * cos(ROTSPEED) - dirY * sin(ROTSPEED);
        dirY = oldDirX * sin(ROTSPEED) + dirY * cos(ROTSPEED);
        oldPlaneX = planeX;
        planeX = planeX * cos(ROTSPEED) - planeY * sin(ROTSPEED);
        planeY = oldPlaneX * sin(ROTSPEED) + planeY * cos(ROTSPEED);
      }
      if(keystate[SDL_SCANCODE_LEFT]) {
        oldDirX = dirX;
        dirX = dirX * cos(-ROTSPEED) - dirY * sin(-ROTSPEED);
        dirY = oldDirX * sin(-ROTSPEED) + dirY * cos(-ROTSPEED);
        oldPlaneX = planeX;
        planeX = planeX * cos(-ROTSPEED) - planeY * sin(-ROTSPEED);
        planeY = oldPlaneX * sin(-ROTSPEED) + planeY * cos(-ROTSPEED);
      }
      if(keystate[SDL_SCANCODE_D]) {
        if(keystate[SDL_SCANCODE_W] ^ keystate[SDL_SCANCODE_S]) {
          if(!MAP[(char)(posX-dirY*STRAFEMOVESPEED)][(char)posY]) posX-=dirY*STRAFEMOVESPEED;
          if(!MAP[(char)posX][(char)(posY-dirX*STRAFEMOVESPEED)]) posY+=dirX*STRAFEMOVESPEED;
        }
        else {
          if(!MAP[(char)(posX-dirY*MOVESPEED)][(char)posY]) posX-=dirY*MOVESPEED;
          if(!MAP[(char)posX][(char)(posY-dirX*MOVESPEED)]) posY+=dirX*MOVESPEED; 
        }
      }
      if(keystate[SDL_SCANCODE_A]) {
        if(keystate[SDL_SCANCODE_W] ^ keystate[SDL_SCANCODE_S]) {
          if(!MAP[(char)(posX+dirY*STRAFEMOVESPEED)][(char)posY]) posX+=dirY*STRAFEMOVESPEED;
          if(!MAP[(char)posX][(char)(posY-dirX*STRAFEMOVESPEED)]) posY-=dirX*STRAFEMOVESPEED;
        }
        else {
          if(!MAP[(char)(posX+dirY*MOVESPEED)][(char)posY]) posX+=dirY*MOVESPEED;
          if(!MAP[(char)posX][(char)(posY-dirX*MOVESPEED)]) posY-=dirX*MOVESPEED;      
        }
      }
      if(keystate[SDL_SCANCODE_W]) {
        if(keystate[SDL_SCANCODE_D] ^ keystate[SDL_SCANCODE_A]) {
          if(!MAP[(char)(posX+dirX*STRAFEMOVESPEED)][(char)posY]) posX+=dirX*STRAFEMOVESPEED;
          if(!MAP[(char)posX][(char)(posY+dirY*STRAFEMOVESPEED)]) posY+=dirY*STRAFEMOVESPEED;
        }
        else {
          if(!MAP[(char)(posX+dirX*MOVESPEED)][(char)posY]) posX+=dirX*MOVESPEED;
          if(!MAP[(char)posX][(char)(posY+dirY*MOVESPEED)]) posY+=dirY*MOVESPEED;
        }
      }
      if(keystate[SDL_SCANCODE_S]) {
        if(keystate[SDL_SCANCODE_D] ^ keystate[SDL_SCANCODE_A]) {
          if(!MAP[(char)(posX-dirX*STRAFEMOVESPEED)][(char)posY]) posX-=dirX*STRAFEMOVESPEED;
          if(!MAP[(char)posX][(char)(posY-dirY*STRAFEMOVESPEED)]) posY-=dirY*STRAFEMOVESPEED;
        }
        else {
          if(!MAP[(char)(posX-dirX*MOVESPEED)][(char)posY]) posX-=dirX*MOVESPEED;
          if(!MAP[(char)posX][(char)(posY-dirY*MOVESPEED)]) posY-=dirY*MOVESPEED;
        }
      }
      if(keystate[SDL_SCANCODE_SPACE]) {
        MAP[(char)(posX+dirX*placeDist)][(char)(posY+dirY*placeDist)] =  1;
      }
      if(keystate[SDL_SCANCODE_LCTRL]) {
        MAP[(char)(posX+dirX*placeDist)][(char)(posY+dirY*placeDist)] =  0;
      }
      if(keystate[SDL_SCANCODE_LSHIFT]) {
        MAP[(char)posX][(char)posY] =  0;
      }

    for(int z = 0; z < RESX; z += 1 ) { //raycasting code 

      rect.x = z;
      float cameraX = 2 * z/(float)RESX -1; // cam vector, -1 to 1
      float raydX = dirX + planeX * cameraX; // frustum thingy
      float raydY = dirY + planeY * cameraX;

      unsigned char mapX = (char)posX;
      unsigned char mapY = (char)posY;

      float sdistX;
      float sdistY;

      float deltaX = fabsf(1/raydX);
      float deltaY = fabsf(1/raydY);
      float pWallDist;
      char stepX;
      char stepY;
      char side; //either 0 (NS), or 1 (EW)

      if(raydX < 0) {
        stepX = -1;
        sdistX = (posX - mapX) * deltaX; // one side
      }
      else {
        stepX = 1;
        sdistX = (mapX + 1.0 - posX) * deltaX; // have to round the other way for this side.
      }
      if(raydY < 0) {
        stepY = -1;
        sdistY = (posY - mapY) * deltaY; // one side
      }
      else {
        stepY = 1;
        sdistY = (mapY + 1.0 - posY) * deltaY; // have to round the other way for this side.
      }
      for( unsigned char step = 0; step < 26; step +=1 ) {

        if(sdistX<sdistY){
          sdistX += deltaX;
          mapX += stepX;
          side = 0;
        }
        else {
          sdistY += deltaY;
          mapY += stepY;
          side = 1;
        }
        if(MAP[mapX][mapY] !=0) step = 27;
      }
      if(side == 0) pWallDist = (mapX - posX + (1-stepX) / 2) / raydX;
      else pWallDist = (mapY - posY + (1-stepY) / 2) / raydY;

      int lineHeight = (int)( RESY / pWallDist);

      signed int drawColour = 255-pWallDist*15;
      if(drawColour < 0x11) drawColour = 0x11;
      SDL_SetRenderDrawColor( renderer, drawColour, drawColour, drawColour, 0xFF);

      rect.h = lineHeight*2;
      rect.y = (RESY/2 - lineHeight);
      SDL_RenderDrawRect( renderer, &rect);
    }

    SDL_RenderPresent( renderer );
    t2 = clock();
    float ms = (float)(t2-t1)/1000.0;
    printf("%f\n", ms );
    if(ms < 16) SDL_Delay(16.0 - ms);

  }
  SDL_DestroyWindow( window );
  SDL_Quit();
}
```

```