Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <stdbool.h>
- #include <SDL.h>
- #define true 1
- #define false 0
- #define iWINDOWSIZE_X 1280
- #define iWINDOWSIZE_Y 720
- #define iWORLDSIZE_X 26
- #define iWORLDSIZE_Y 15
- #define iBLOCKSIZE 50.0f
- #define iCOLLOFFSET_X 0
- #define iCOLLOFFSET_Y iBLOCKSIZE
- #define iSIDESCOLLOFFSET 2
- #define iMOVEMENTSPEED 1.5
- #define iJUMPFORHOWLONG 120
- Uint32 red, green, blue, white;
- SDL_Surface *window = NULL;
- int world[iWORLDSIZE_Y][iWORLDSIZE_X] = {{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,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,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,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,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,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,0,0},
- {0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0},
- {1,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,1,1,1},
- {1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0,0,0,1,1,1,1,1},
- {1,2,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1},
- {1,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
- {1,1,2,2,2,1,1,1,1,2,2,2,2,1,1,1,1,1,1,1,1,2,2,1,1,1},
- {1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,2,2,2,1,1},
- {1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,2,1,1,1}};
- int up = false;
- int down = false;
- int left = false;
- int right = false;
- int getBlockType(int xPos, int yPos) {
- return world[yPos][xPos];
- }
- void setBlock(int xPos, int yPos, int type) {
- world[yPos][xPos] = type;
- }
- // return : up, down, left, right
- int* check4Collisions(int x, int y) {
- static int coll[4] = {0, 0, 0, 0};
- int rawColl[4] = {0, 0, 0, 0};
- int playerPosRounded_x = (x + iCOLLOFFSET_X) / iBLOCKSIZE;
- int playerPosRounded_y = (y + iCOLLOFFSET_Y) / iBLOCKSIZE;
- float playerPos_x = (x + iCOLLOFFSET_X) / iBLOCKSIZE;
- float playerPos_y = (y + iCOLLOFFSET_Y) / iBLOCKSIZE;
- // the player is aligned with the grid
- if(playerPosRounded_x == playerPos_x && playerPosRounded_y == playerPos_y) {
- printf("x y\n");
- rawColl[0] = (getBlockType(playerPosRounded_x-1,playerPosRounded_y-1) != 0);
- rawColl[1] = (getBlockType(playerPosRounded_x+1,playerPosRounded_y-1) != 0);
- rawColl[2] = (getBlockType(playerPosRounded_x,playerPosRounded_y) != 0);
- rawColl[3] = (getBlockType(playerPosRounded_x,playerPosRounded_y-2) != 0);
- } else if(playerPosRounded_x != playerPos_x && playerPosRounded_y == playerPos_y) {
- rawColl[0] = (getBlockType(playerPosRounded_x-1,playerPosRounded_y-1) != 0);
- rawColl[1] = (getBlockType(playerPosRounded_x+2,playerPosRounded_y-1) != 0);
- rawColl[2] = (getBlockType(playerPosRounded_x,playerPosRounded_y) != 0 || getBlockType(playerPosRounded_x+1,playerPosRounded_y) != 0);
- rawColl[3] = (getBlockType(playerPosRounded_x,playerPosRounded_y-2) != 0 || getBlockType(playerPosRounded_x+1,playerPosRounded_y-2) != 0);
- } else if(playerPosRounded_x == playerPos_x && playerPosRounded_y != playerPos_y) {
- rawColl[0] = (getBlockType(playerPosRounded_x-1,playerPosRounded_y-1) != 0 || getBlockType(playerPosRounded_x-1,playerPosRounded_y) != 0);
- rawColl[1] = (getBlockType(playerPosRounded_x+1,playerPosRounded_y-1) != 0 || getBlockType(playerPosRounded_x+1,playerPosRounded_y) != 0);
- rawColl[2] = (getBlockType(playerPosRounded_x,playerPosRounded_y+1) != 0);
- rawColl[3] = (getBlockType(playerPosRounded_x,playerPosRounded_y-2) != 0);
- } else if(playerPosRounded_x != playerPos_x && playerPosRounded_y != playerPos_y) {
- coll[0] = (getBlockType(playerPosRounded_x-1,playerPosRounded_y-1) != 0 || getBlockType(playerPosRounded_x-1,playerPosRounded_y) != 0);
- coll[1] = (getBlockType(playerPosRounded_x+2,playerPosRounded_y-1) != 0 || getBlockType(playerPosRounded_x+2,playerPosRounded_y) != 0);
- coll[2] = (getBlockType(playerPosRounded_x,playerPosRounded_y+1) != 0 || getBlockType(playerPosRounded_x+1,playerPosRounded_y+1) != 0);
- coll[3] = (getBlockType(playerPosRounded_x,playerPosRounded_y-2) != 0 || getBlockType(playerPosRounded_x+1,playerPosRounded_y-2) != 0);
- }
- coll[0] = (rawColl[0] && x-(playerPosRounded_x)*iBLOCKSIZE < iSIDESCOLLOFFSET && playerPosRounded_y == playerPos_y || rawColl[0] && playerPosRounded_y != playerPos_y && getBlockType(playerPosRounded_x-1,playerPosRounded_y) != 0);
- coll[1] = (rawColl[1] && ((playerPosRounded_x+1)*iBLOCKSIZE) - x < iSIDESCOLLOFFSET && playerPosRounded_y == playerPos_y || rawColl[1] && playerPosRounded_y != playerPos_y && getBlockType(playerPosRounded_x+1,playerPosRounded_y) != 0);
- coll[2] = (rawColl[2] && y-(playerPosRounded_y)*iBLOCKSIZE < iSIDESCOLLOFFSET);
- coll[3] = (rawColl[3] && ((playerPosRounded_y-1)*iBLOCKSIZE) - y < iSIDESCOLLOFFSET);
- return coll;
- }
- int main(int argc, char *argv[]) {
- SDL_Init(SDL_INIT_VIDEO);
- window = SDL_SetVideoMode(iWINDOWSIZE_X, iWINDOWSIZE_Y, 32, SDL_HWSURFACE);
- white = SDL_MapRGB(window -> format, 255, 255, 255);
- red = SDL_MapRGB(window -> format, 255, 0, 0);
- green = SDL_MapRGB(window -> format, 0, 255, 0);
- blue = SDL_MapRGB(window -> format, 0, 0, 255);
- SDL_FillRect(window, NULL, white);
- SDL_Surface *player;
- player = SDL_CreateRGBSurface(SDL_HWSURFACE, iBLOCKSIZE, iBLOCKSIZE, 32, 0, 0, 0, 0);
- SDL_FillRect(player, NULL, green);
- SDL_Rect playerPos;
- playerPos.x = 400;
- playerPos.y = 50;
- SDL_BlitSurface(player, NULL, window, &playerPos);
- int yVel = 0;
- int mouseX;
- int mouseY;
- int counter = 0;
- SDL_Flip(window);
- // main loop
- while(1) {
- SDL_Event event;
- int *colls = check4Collisions(playerPos.x, playerPos.y);
- while(SDL_PollEvent(&event)) {
- if(event.type == SDL_QUIT) {
- SDL_FreeSurface(window);
- SDL_Quit();
- return 0;
- break;
- }
- else if(event.type == SDL_KEYDOWN) {
- switch(event.key.keysym.sym) {
- case SDLK_ESCAPE :
- SDL_FreeSurface(window);
- SDL_Quit();
- return 0;
- break;
- case SDLK_RIGHT :
- right = true;
- break;
- case SDLK_LEFT :
- left = true;
- break;
- case SDLK_SPACE :
- if(counter <= iJUMPFORHOWLONG && *(colls+2)) up = true;
- break;
- }
- } else if(event.type == SDL_KEYUP) {
- switch(event.key.keysym.sym) {
- case SDLK_RIGHT :
- right = false;
- break;
- case SDLK_LEFT :
- left = false;
- break;
- case SDLK_SPACE :
- counter = iJUMPFORHOWLONG;
- break;
- }
- } else if(event.type == SDL_MOUSEBUTTONDOWN) {
- int mouseX_normalised = event.button.x / iBLOCKSIZE;
- int mouseY_normalised = event.button.y / iBLOCKSIZE;
- printf("x: %i\n", mouseX_normalised);
- printf("y: %i\n", mouseY_normalised);
- printf("\n");
- switch(event.button.button) {
- case SDL_BUTTON_LEFT :
- setBlock(mouseY_normalised, mouseX_normalised, 1);
- break;
- case SDL_BUTTON_RIGHT :
- setBlock(mouseY_normalised, mouseX_normalised, 0);
- break;
- }
- drawWorld(world, playerPos);
- SDL_Flip(window);
- }
- }
- if(counter <= iJUMPFORHOWLONG && !*(colls+3) && up == true) {
- counter++;
- } else if(*(colls+3)) {
- printf("Ceiling\n");
- up = false;
- counter = iJUMPFORHOWLONG;
- } else {
- counter = 0;
- up = false;
- }
- // Is there a block underneath the player ?
- // Check 4 y collisions
- if(!*(colls+2) && up == false) {
- yVel = iMOVEMENTSPEED;
- } else if(!*(colls+3) && up == true){
- yVel = -iMOVEMENTSPEED;
- } else {
- yVel = 0;
- counter = 0;
- }
- if(yVel != 0 || up == true || down == true || left == true || right == true) {
- SDL_FreeSurface(window);
- drawWorld(world, playerPos);
- if(!*(check4Collisions(playerPos.x+iMOVEMENTSPEED+iSIDESCOLLOFFSET, playerPos.y)+1) && right == true && getBlockType(floor((playerPos.x+iSIDESCOLLOFFSET+iBLOCKSIZE+iMOVEMENTSPEED)/iBLOCKSIZE),floor(playerPos.y/iBLOCKSIZE)) == 0) {
- if(yVel == 0 && getBlockType(floor((playerPos.x+iSIDESCOLLOFFSET+iBLOCKSIZE+iMOVEMENTSPEED)/iBLOCKSIZE),floor(playerPos.y/iBLOCKSIZE)) == 0 || getBlockType(floor((playerPos.x+iBLOCKSIZE+iMOVEMENTSPEED)/iBLOCKSIZE),floor((playerPos.y+iBLOCKSIZE)/iBLOCKSIZE)) == 0 || yVel != -1 && getBlockType(floor((playerPos.x+iMOVEMENTSPEED)/iBLOCKSIZE),floor(playerPos.y/iBLOCKSIZE)) == 0) playerPos.x += iMOVEMENTSPEED;
- }
- if(!*(check4Collisions(playerPos.x-iMOVEMENTSPEED, playerPos.y)+0) && left == true && getBlockType(floor((playerPos.x-iSIDESCOLLOFFSET-iMOVEMENTSPEED)/iBLOCKSIZE),floor(playerPos.y/iBLOCKSIZE)) == 0) {
- if(yVel == 0 || getBlockType(floor((playerPos.x-iMOVEMENTSPEED)/iBLOCKSIZE),floor((playerPos.y+iBLOCKSIZE)/iBLOCKSIZE)) == 0 && yVel != 0) playerPos.x -= iMOVEMENTSPEED;
- }
- playerPos.y += yVel;
- SDL_FillRect(player, NULL, green);
- SDL_BlitSurface(player, NULL, window, &playerPos);
- SDL_BlitSurface(player, NULL, window, &playerPos);
- SDL_Flip(window);
- }
- }
- SDL_FreeSurface(window);
- SDL_Quit();
- return 0;
- }
- void drawWorld(int map[iWORLDSIZE_Y][iWORLDSIZE_X], SDL_Rect playerPos) {
- SDL_FillRect(window, NULL, white);
- for(int y = 0; y <= iWORLDSIZE_Y-1; y++) {
- for(int x = 0; x <= iWORLDSIZE_X-1; x++) {
- if(map[y][x] != 0) {
- SDL_Surface *dirt;
- dirt = SDL_CreateRGBSurface(SDL_HWSURFACE, iBLOCKSIZE, iBLOCKSIZE, 32, 0, 0, 0, 0);
- if(map[y][x] == 1) SDL_FillRect(dirt, NULL, blue);
- if(map[y][x] == 2) SDL_FillRect(dirt, NULL, red);
- SDL_Rect dirtPos;
- dirtPos.x = x*50;
- dirtPos.y = y*50;
- SDL_BlitSurface(dirt, NULL, window, &dirtPos);
- SDL_FreeSurface(dirt);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement