Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <stdbool.h>
  5. #include <SDL.h>
  6.  
  7. #define true 1
  8. #define false 0
  9.  
  10. #define iWINDOWSIZE_X 1280
  11. #define iWINDOWSIZE_Y 720
  12. #define iWORLDSIZE_X 26
  13. #define iWORLDSIZE_Y 15
  14. #define iBLOCKSIZE 50.0f
  15.  
  16. #define iCOLLOFFSET_X 0
  17. #define iCOLLOFFSET_Y iBLOCKSIZE
  18.  
  19. #define iSIDESCOLLOFFSET 2
  20.  
  21. #define iMOVEMENTSPEED 1.5
  22. #define iJUMPFORHOWLONG 120
  23.  
  24. Uint32 red, green, blue, white;
  25. SDL_Surface *window = NULL;
  26.  
  27. 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},
  28. {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},
  29. {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},
  30. {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},
  31. {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},
  32. {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},
  33. {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},
  34. {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},
  35. {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},
  36. {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},
  37. {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},
  38. {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},
  39. {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},
  40. {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},
  41. {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}};
  42.  
  43. int up = false;
  44. int down = false;
  45. int left = false;
  46. int right = false;
  47.  
  48. int getBlockType(int xPos, int yPos) {
  49. return world[yPos][xPos];
  50. }
  51.  
  52. void setBlock(int xPos, int yPos, int type) {
  53. world[yPos][xPos] = type;
  54. }
  55.  
  56. // return : up, down, left, right
  57. int* check4Collisions(int x, int y) {
  58. static int coll[4] = {0, 0, 0, 0};
  59. int rawColl[4] = {0, 0, 0, 0};
  60.  
  61. int playerPosRounded_x = (x + iCOLLOFFSET_X) / iBLOCKSIZE;
  62. int playerPosRounded_y = (y + iCOLLOFFSET_Y) / iBLOCKSIZE;
  63. float playerPos_x = (x + iCOLLOFFSET_X) / iBLOCKSIZE;
  64. float playerPos_y = (y + iCOLLOFFSET_Y) / iBLOCKSIZE;
  65.  
  66. // the player is aligned with the grid
  67. if(playerPosRounded_x == playerPos_x && playerPosRounded_y == playerPos_y) {
  68. printf("x y\n");
  69. rawColl[0] = (getBlockType(playerPosRounded_x-1,playerPosRounded_y-1) != 0);
  70. rawColl[1] = (getBlockType(playerPosRounded_x+1,playerPosRounded_y-1) != 0);
  71. rawColl[2] = (getBlockType(playerPosRounded_x,playerPosRounded_y) != 0);
  72. rawColl[3] = (getBlockType(playerPosRounded_x,playerPosRounded_y-2) != 0);
  73. } else if(playerPosRounded_x != playerPos_x && playerPosRounded_y == playerPos_y) {
  74. rawColl[0] = (getBlockType(playerPosRounded_x-1,playerPosRounded_y-1) != 0);
  75. rawColl[1] = (getBlockType(playerPosRounded_x+2,playerPosRounded_y-1) != 0);
  76. rawColl[2] = (getBlockType(playerPosRounded_x,playerPosRounded_y) != 0 || getBlockType(playerPosRounded_x+1,playerPosRounded_y) != 0);
  77. rawColl[3] = (getBlockType(playerPosRounded_x,playerPosRounded_y-2) != 0 || getBlockType(playerPosRounded_x+1,playerPosRounded_y-2) != 0);
  78. } else if(playerPosRounded_x == playerPos_x && playerPosRounded_y != playerPos_y) {
  79. rawColl[0] = (getBlockType(playerPosRounded_x-1,playerPosRounded_y-1) != 0 || getBlockType(playerPosRounded_x-1,playerPosRounded_y) != 0);
  80. rawColl[1] = (getBlockType(playerPosRounded_x+1,playerPosRounded_y-1) != 0 || getBlockType(playerPosRounded_x+1,playerPosRounded_y) != 0);
  81. rawColl[2] = (getBlockType(playerPosRounded_x,playerPosRounded_y+1) != 0);
  82. rawColl[3] = (getBlockType(playerPosRounded_x,playerPosRounded_y-2) != 0);
  83. } else if(playerPosRounded_x != playerPos_x && playerPosRounded_y != playerPos_y) {
  84. coll[0] = (getBlockType(playerPosRounded_x-1,playerPosRounded_y-1) != 0 || getBlockType(playerPosRounded_x-1,playerPosRounded_y) != 0);
  85. coll[1] = (getBlockType(playerPosRounded_x+2,playerPosRounded_y-1) != 0 || getBlockType(playerPosRounded_x+2,playerPosRounded_y) != 0);
  86. coll[2] = (getBlockType(playerPosRounded_x,playerPosRounded_y+1) != 0 || getBlockType(playerPosRounded_x+1,playerPosRounded_y+1) != 0);
  87. coll[3] = (getBlockType(playerPosRounded_x,playerPosRounded_y-2) != 0 || getBlockType(playerPosRounded_x+1,playerPosRounded_y-2) != 0);
  88. }
  89.  
  90. 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);
  91. 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);
  92. coll[2] = (rawColl[2] && y-(playerPosRounded_y)*iBLOCKSIZE < iSIDESCOLLOFFSET);
  93. coll[3] = (rawColl[3] && ((playerPosRounded_y-1)*iBLOCKSIZE) - y < iSIDESCOLLOFFSET);
  94.  
  95. return coll;
  96. }
  97.  
  98. int main(int argc, char *argv[]) {
  99. SDL_Init(SDL_INIT_VIDEO);
  100. window = SDL_SetVideoMode(iWINDOWSIZE_X, iWINDOWSIZE_Y, 32, SDL_HWSURFACE);
  101.  
  102. white = SDL_MapRGB(window -> format, 255, 255, 255);
  103. red = SDL_MapRGB(window -> format, 255, 0, 0);
  104. green = SDL_MapRGB(window -> format, 0, 255, 0);
  105. blue = SDL_MapRGB(window -> format, 0, 0, 255);
  106.  
  107. SDL_FillRect(window, NULL, white);
  108.  
  109. SDL_Surface *player;
  110. player = SDL_CreateRGBSurface(SDL_HWSURFACE, iBLOCKSIZE, iBLOCKSIZE, 32, 0, 0, 0, 0);
  111. SDL_FillRect(player, NULL, green);
  112.  
  113. SDL_Rect playerPos;
  114. playerPos.x = 400;
  115. playerPos.y = 50;
  116.  
  117. SDL_BlitSurface(player, NULL, window, &playerPos);
  118.  
  119. int yVel = 0;
  120. int mouseX;
  121. int mouseY;
  122. int counter = 0;
  123.  
  124. SDL_Flip(window);
  125.  
  126. // main loop
  127. while(1) {
  128. SDL_Event event;
  129. int *colls = check4Collisions(playerPos.x, playerPos.y);
  130.  
  131. while(SDL_PollEvent(&event)) {
  132. if(event.type == SDL_QUIT) {
  133. SDL_FreeSurface(window);
  134. SDL_Quit();
  135. return 0;
  136. break;
  137. }
  138. else if(event.type == SDL_KEYDOWN) {
  139. switch(event.key.keysym.sym) {
  140. case SDLK_ESCAPE :
  141. SDL_FreeSurface(window);
  142. SDL_Quit();
  143. return 0;
  144. break;
  145. case SDLK_RIGHT :
  146. right = true;
  147. break;
  148. case SDLK_LEFT :
  149. left = true;
  150. break;
  151. case SDLK_SPACE :
  152. if(counter <= iJUMPFORHOWLONG && *(colls+2)) up = true;
  153. break;
  154. }
  155. } else if(event.type == SDL_KEYUP) {
  156. switch(event.key.keysym.sym) {
  157. case SDLK_RIGHT :
  158. right = false;
  159. break;
  160. case SDLK_LEFT :
  161. left = false;
  162. break;
  163. case SDLK_SPACE :
  164. counter = iJUMPFORHOWLONG;
  165. break;
  166. }
  167. } else if(event.type == SDL_MOUSEBUTTONDOWN) {
  168. int mouseX_normalised = event.button.x / iBLOCKSIZE;
  169. int mouseY_normalised = event.button.y / iBLOCKSIZE;
  170. printf("x: %i\n", mouseX_normalised);
  171. printf("y: %i\n", mouseY_normalised);
  172. printf("\n");
  173.  
  174. switch(event.button.button) {
  175. case SDL_BUTTON_LEFT :
  176. setBlock(mouseY_normalised, mouseX_normalised, 1);
  177. break;
  178. case SDL_BUTTON_RIGHT :
  179. setBlock(mouseY_normalised, mouseX_normalised, 0);
  180. break;
  181. }
  182.  
  183. drawWorld(world, playerPos);
  184. SDL_Flip(window);
  185. }
  186. }
  187.  
  188. if(counter <= iJUMPFORHOWLONG && !*(colls+3) && up == true) {
  189. counter++;
  190. } else if(*(colls+3)) {
  191. printf("Ceiling\n");
  192. up = false;
  193. counter = iJUMPFORHOWLONG;
  194. } else {
  195. counter = 0;
  196. up = false;
  197. }
  198.  
  199. // Is there a block underneath the player ?
  200. // Check 4 y collisions
  201. if(!*(colls+2) && up == false) {
  202. yVel = iMOVEMENTSPEED;
  203. } else if(!*(colls+3) && up == true){
  204. yVel = -iMOVEMENTSPEED;
  205. } else {
  206. yVel = 0;
  207. counter = 0;
  208. }
  209.  
  210. if(yVel != 0 || up == true || down == true || left == true || right == true) {
  211. SDL_FreeSurface(window);
  212. drawWorld(world, playerPos);
  213.  
  214. if(!*(check4Collisions(playerPos.x+iMOVEMENTSPEED+iSIDESCOLLOFFSET, playerPos.y)+1) && right == true && getBlockType(floor((playerPos.x+iSIDESCOLLOFFSET+iBLOCKSIZE+iMOVEMENTSPEED)/iBLOCKSIZE),floor(playerPos.y/iBLOCKSIZE)) == 0) {
  215. 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;
  216. }
  217.  
  218. if(!*(check4Collisions(playerPos.x-iMOVEMENTSPEED, playerPos.y)+0) && left == true && getBlockType(floor((playerPos.x-iSIDESCOLLOFFSET-iMOVEMENTSPEED)/iBLOCKSIZE),floor(playerPos.y/iBLOCKSIZE)) == 0) {
  219. if(yVel == 0 || getBlockType(floor((playerPos.x-iMOVEMENTSPEED)/iBLOCKSIZE),floor((playerPos.y+iBLOCKSIZE)/iBLOCKSIZE)) == 0 && yVel != 0) playerPos.x -= iMOVEMENTSPEED;
  220. }
  221.  
  222. playerPos.y += yVel;
  223.  
  224. SDL_FillRect(player, NULL, green);
  225. SDL_BlitSurface(player, NULL, window, &playerPos);
  226.  
  227. SDL_BlitSurface(player, NULL, window, &playerPos);
  228. SDL_Flip(window);
  229. }
  230. }
  231.  
  232. SDL_FreeSurface(window);
  233. SDL_Quit();
  234. return 0;
  235. }
  236.  
  237. void drawWorld(int map[iWORLDSIZE_Y][iWORLDSIZE_X], SDL_Rect playerPos) {
  238. SDL_FillRect(window, NULL, white);
  239. for(int y = 0; y <= iWORLDSIZE_Y-1; y++) {
  240. for(int x = 0; x <= iWORLDSIZE_X-1; x++) {
  241. if(map[y][x] != 0) {
  242. SDL_Surface *dirt;
  243. dirt = SDL_CreateRGBSurface(SDL_HWSURFACE, iBLOCKSIZE, iBLOCKSIZE, 32, 0, 0, 0, 0);
  244. if(map[y][x] == 1) SDL_FillRect(dirt, NULL, blue);
  245. if(map[y][x] == 2) SDL_FillRect(dirt, NULL, red);
  246. SDL_Rect dirtPos;
  247. dirtPos.x = x*50;
  248. dirtPos.y = y*50;
  249. SDL_BlitSurface(dirt, NULL, window, &dirtPos);
  250.  
  251. SDL_FreeSurface(dirt);
  252. }
  253. }
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement