Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <SDL2/SDL.h>
- #include <stdbool.h>
- #include "map.h"
- #define RESX 960
- #define RESY 540
- #define HALFRESX 480
- #define HALFRESY 270
- #define ROTATESPEED 0.03
- #define MOVESPEED 0.08
- #define STRAFEMOVESPEED 0.0565685424948
- #define BGCOL 17
- #define clipDistance 26
- #define playerSize 1
- struct Point3D {
- float x, y, z;
- };
- struct pos {
- unsigned char x, y, z;
- };
- void main() {
- char placeDist = 3;
- SDL_Window* window = NULL; //init SDL
- SDL_Renderer* renderer = NULL;
- SDL_Surface* surface = NULL;
- bool running = true;
- if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
- printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
- running = false;
- }
- 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() );
- running = false;
- }
- surface = SDL_GetWindowSurface( window );
- renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
- if(renderer==NULL) {
- printf("Renderer error: %s\n", SDL_GetError() );
- running = false;
- }
- SDL_Texture * texture = SDL_CreateTexture(renderer,
- SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, RESX,RESY);
- Uint32 pixels[RESX*RESY];
- const Uint8* keystate = SDL_GetKeyboardState( NULL );
- int t1, t2;
- struct Point3D pos;
- pos.x = 4;
- pos.y = 4;
- pos.z = 4;
- struct Point3D dir;
- dir.x = 1;
- dir.y = 0; //direction vector
- dir.z = 0;
- struct Point3D plane;
- plane.x = 0;
- plane.y = 0.8;
- plane.z = 0.45;
- struct Point3D vel;
- float time = 0;
- float oldtime = 0; //for FPS calculations
- void rotate(float rotspeed) {
- float oldDirX = dir.x;
- dir.x = dir.x * cos(rotspeed) - dir.y * sin(rotspeed);
- dir.y = oldDirX * sin(rotspeed) + dir.y * cos(rotspeed);
- float oldPlaneX = plane.x;
- plane.x = plane.x * cos(rotspeed) - plane.y * sin(rotspeed);
- plane.y = oldPlaneX * sin(rotspeed) + plane.y * cos(rotspeed);
- }
- void move() {
- float velocity = ((keystate[SDL_SCANCODE_D] ^ keystate[SDL_SCANCODE_A]) &&
- (keystate[SDL_SCANCODE_W] ^ keystate[SDL_SCANCODE_S])) ? STRAFEMOVESPEED : MOVESPEED;
- struct Point3D oldpos = pos;
- if(keystate[SDL_SCANCODE_D]) {
- pos.x -= dir.y * velocity;
- pos.y += dir.x * velocity;
- }
- if(keystate[SDL_SCANCODE_A]) {
- pos.x += dir.y * velocity;
- pos.y -= dir.x * velocity;
- }
- if(keystate[SDL_SCANCODE_W]) {
- pos.x += dir.x * velocity;
- pos.y += dir.y * velocity;
- }
- if(keystate[SDL_SCANCODE_S]) {
- pos.x -= dir.x * velocity;
- pos.y -= dir.y * velocity;
- }
- if(MAP[(char)pos.x][(char)pos.y]) pos = oldpos;
- } //remove oldpos and velocity when not needed
- while( running ) {
- t1 = SDL_GetTicks();
- SDL_Event e;
- running = !(SDL_PollEvent( &e ) && e.type == SDL_QUIT);
- move();
- if(keystate[SDL_SCANCODE_LEFT]) {
- rotate(-ROTATESPEED);
- }
- if(keystate[SDL_SCANCODE_RIGHT]){
- rotate(ROTATESPEED);
- }
- if(keystate[SDL_SCANCODE_SPACE] && MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z] == 0) {
- MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z] = 1;
- }
- else if(keystate[SDL_SCANCODE_LCTRL] && MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z] == 1) {
- MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z] = 0;
- }
- if(keystate[SDL_SCANCODE_L]) running = 0;
- for(int x = 0; x < RESX; x += 1 ) { //raycasting code
- for(int y = 0; y < RESY; y += 1) {
- struct Point3D camera;
- camera.x = (float)(x/HALFRESX) -1; // ray direction from camera, -1 to 1
- camera.y = (float)(y/HALFRESY) -1;
- struct Point3D rayd;
- rayd.x = dir.x + plane.x * camera.x; // frustum thingy
- rayd.y = dir.y + plane.y * camera.x;
- rayd.z = dir.z + plane.z * camera.y;
- struct pos map;
- map.x = (char)pos.x;
- map.y = (char)pos.y;
- map.z = (char)pos.z;
- struct Point3D sdist;
- struct Point3D delta;
- delta.x = fabsf(1/rayd.x);
- delta.y = fabsf(1/rayd.y);
- delta.z = fabsf(1/rayd.z);
- struct pos step;
- if(rayd.x < 0) {
- step.x = -1;
- sdist.x = (pos.x - map.x) * delta.x; // one side
- }
- else {
- step.x = 1;
- sdist.x = (map.x + 1.0 - pos.x) * delta.x; // have to round the other way for this side.
- }
- if(rayd.y < 0) {
- step.y = -1;
- sdist.y = (pos.y - map.y) * delta.y; // one side
- }
- else {
- step.y = 1;
- sdist.y = (map.y + 1.0 - pos.y) * delta.y; // have to round the other way for this side.
- }
- if(rayd.z < 0) {
- step.z = -1;
- sdist.z = (pos.z - map.z) * delta.z; // one side
- }
- else {
- step.z = 1;
- sdist.z = (map.z + 1.0 - pos.z) * delta.z; // have to round the other way for this side.
- }
- char side; //either 0 (NS), or 1 (EW), or 2(UD)
- for( unsigned char stepd = 0; stepd < clipDistance; stepd +=1 ) {
- if(sdist.x < sdist.y ) {
- if(sdist.x < sdist.z) {
- sdist.x += delta.x;
- map.x += step.x;
- side = 0;
- }
- else {
- sdist.z += delta.z;
- map.z += step.z;
- side = 2;
- }
- }
- else {
- sdist.y += delta.y;
- map.y += step.y;
- side = 1;
- }
- if(MAP[map.x][map.y][map.z] !=0) {
- break;
- }
- }
- float pWallDist;
- if(side == 0) pWallDist = (map.x - pos.x + (1-step.x) / 2) / rayd.x;
- else if( side == 1) pWallDist = (map.y - pos.y + (1-step.y) / 2) / rayd.y; //this
- else pWallDist = (map.z - pos.z + (1-step.z) / 2) / rayd.z;
- unsigned char drawColour;
- if(pWallDist>16)
- {
- drawColour = 17;
- }
- else
- {
- drawColour = 255-pWallDist*15;
- }
- SDL_RenderDrawPoint( renderer, x, y);
- pixels[y*RESX+x] = drawColour*0x010101;
- }
- }
- SDL_UpdateTexture(texture, NULL, pixels, RESX * sizeof(Uint32));
- SDL_RenderCopy(renderer, texture, NULL, NULL);
- SDL_RenderPresent( renderer );
- t2 = SDL_GetTicks();
- int ms = (float)(t2-t1);
- printf("%d\n", ms );
- SDL_Delay(15);
- }
- SDL_DestroyWindow( window );
- SDL_DestroyRenderer( renderer );
- SDL_DestroyTexture( texture );
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment