Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <SDL_image.h>
  3. #include <cstdio>
  4. #include <string>
  5.  
  6. const int SCREEN_WIDTH = 512;
  7. const int SCREEN_HEIGHT = 512;
  8.  
  9. bool initialize_game();
  10. bool load_game();
  11. void close_game();
  12. SDL_Texture* load_texture(std::string path);
  13.  
  14. bool check_collision(SDL_Rect a, SDL_Rect b);
  15.  
  16. SDL_Texture* playerOneTexture = NULL;
  17. SDL_Window* globalWindow = NULL;
  18. SDL_Renderer* globalRenderer = NULL;
  19.  
  20. class Timer
  21. {
  22. public:
  23. Timer();
  24.  
  25. void start_timer();
  26. void stop_timer();
  27. bool is_started();
  28.  
  29. Uint32 get_ticks();
  30.  
  31. private:
  32. Uint32 startTicks;
  33. Uint32 pausedTicks;
  34.  
  35. bool isPaused;
  36. bool isStarted;
  37. };
  38.  
  39. class Player
  40. {
  41. public:
  42. const int PLAYER_WIDTH = 32;
  43. const int PLAYER_HEIGHT = 32;
  44. const int PLAYER_VELOCITY = 160;
  45.  
  46. Player();
  47.  
  48. void move_player(float timeStep, SDL_Rect objectWeHit);
  49. void render_player(float positionX, float positionY, SDL_Texture* texture, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE);
  50. void handle_player_event(SDL_Event &event);
  51.  
  52. float positionX, positionY;
  53. float velocityX, velocityY;
  54.  
  55. private:
  56. SDL_Rect playerCollider;
  57. };
  58.  
  59. Timer::Timer()
  60. {
  61. startTicks = 0;
  62. pausedTicks = 0;
  63.  
  64. isPaused = false;
  65. isStarted = false;
  66. }
  67.  
  68. void Timer::start_timer()
  69. {
  70. isStarted = true;
  71. isPaused = false;
  72.  
  73. startTicks = SDL_GetTicks();
  74. pausedTicks = 0;
  75. }
  76.  
  77. void Timer::stop_timer()
  78. {
  79. isStarted = false;
  80. isPaused = false;
  81.  
  82. startTicks = 0;
  83. pausedTicks = 0;
  84. }
  85.  
  86. Uint32 Timer::get_ticks()
  87. {
  88. Uint32 time = 0;
  89.  
  90. if(isStarted){
  91. time = SDL_GetTicks() - startTicks;
  92. }
  93. return time;
  94. }
  95.  
  96. bool Timer::is_started()
  97. {
  98. return isStarted;
  99. }
  100.  
  101. Player::Player()
  102. {
  103. positionX = 0.0;
  104. positionY = 0.0;
  105.  
  106. velocityX = 0.0;
  107. velocityY = 0.0;
  108.  
  109. playerCollider.w = PLAYER_WIDTH;
  110. playerCollider.h = PLAYER_HEIGHT;
  111. }
  112.  
  113. void Player::move_player(float timeStep, SDL_Rect objectWeHit)
  114. {
  115. positionX += velocityX * timeStep;
  116. playerCollider.x = positionX;
  117.  
  118. if(positionX < 0){
  119. positionX = 0;
  120. playerCollider.x = positionX;
  121. }
  122. if(positionX > SCREEN_WIDTH - PLAYER_WIDTH){
  123. positionX = SCREEN_WIDTH - PLAYER_WIDTH;
  124. playerCollider.x = positionX;
  125. }
  126.  
  127. positionY += velocityY * timeStep;
  128. playerCollider.y = positionY;
  129.  
  130. if(positionY < 0){
  131. positionY = 0;
  132. playerCollider.y = positionY;
  133. }
  134. if(positionY > SCREEN_HEIGHT - PLAYER_HEIGHT){
  135. positionY = SCREEN_HEIGHT - PLAYER_HEIGHT;
  136. playerCollider.y = positionY;
  137. }
  138.  
  139. if(check_collision(playerCollider, objectWeHit)){
  140. positionX -= (velocityX * timeStep);
  141. positionY -= (velocityY * timeStep);
  142. playerCollider.x = positionX;
  143. playerCollider.y = positionY;
  144. }
  145. }
  146.  
  147. void Player::render_player(float positionX, float positionY, SDL_Texture* texture, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip)
  148. {
  149. SDL_Rect renderQuad = { positionX, positionY, PLAYER_WIDTH, PLAYER_HEIGHT };
  150.  
  151. if( clip != NULL )
  152. {
  153. renderQuad.w = clip->w;
  154. renderQuad.h = clip->h;
  155. }
  156.  
  157. SDL_RenderCopyEx(globalRenderer, texture, clip, &renderQuad, angle, center, flip );
  158. }
  159.  
  160. void Player::handle_player_event(SDL_Event &event)
  161. {
  162. if(event.type == SDL_KEYDOWN && event.key.repeat == 0){
  163. switch(event.key.keysym.sym){
  164. case SDLK_UP:
  165. velocityY -= PLAYER_VELOCITY;
  166. break;
  167.  
  168. case SDLK_DOWN:
  169. velocityY += PLAYER_VELOCITY;
  170. break;
  171.  
  172. case SDLK_LEFT:
  173. velocityX -= PLAYER_VELOCITY;
  174. break;
  175.  
  176. case SDLK_RIGHT:
  177. velocityX += PLAYER_VELOCITY;
  178. break;
  179. }
  180. }
  181. else if(event.type == SDL_KEYUP && event.key.repeat == 0){
  182. switch(event.key.keysym.sym){
  183. case SDLK_UP:
  184. velocityY += PLAYER_VELOCITY;
  185. break;
  186.  
  187. case SDLK_DOWN:
  188. velocityY -= PLAYER_VELOCITY;
  189. break;
  190.  
  191. case SDLK_LEFT:
  192. velocityX += PLAYER_VELOCITY;
  193. break;
  194.  
  195. case SDLK_RIGHT:
  196. velocityX -= PLAYER_VELOCITY;
  197. break;
  198. }
  199. }
  200. }
  201.  
  202. bool initialize_game()
  203. {
  204. bool isSuccessful = true;
  205.  
  206. if(SDL_Init(SDL_INIT_VIDEO) < 0){
  207. printf("Video failed to load... SDL Error: %sn", SDL_GetError());
  208. isSuccessful = false;
  209. } else {
  210. globalWindow = SDL_CreateWindow("Robo Kill", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  211. if(globalWindow == NULL){
  212. printf("Window failed to be created... SDL Error: %sn", SDL_GetError());
  213. isSuccessful = false;
  214. } else {
  215. globalRenderer = SDL_CreateRenderer(globalWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  216. if(globalRenderer == NULL){
  217. printf("Renderer failed to be created... SDL Error: %sn", SDL_GetError());
  218. isSuccessful = false;
  219. } else {
  220. SDL_SetRenderDrawColor(globalRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
  221. int imageFlags = IMG_INIT_PNG;
  222. if(!(IMG_Init(imageFlags) &imageFlags)){
  223. printf("SDL_image failed to load... SDL_image error: %sn", IMG_GetError());
  224. isSuccessful = false;
  225. }
  226. }
  227. }
  228. }
  229. return isSuccessful;
  230. }
  231.  
  232. bool load_game()
  233. {
  234. bool isSuccessful = true;
  235.  
  236. playerOneTexture = load_texture("images/player1.png");
  237. if(playerOneTexture == NULL){
  238. printf("player1.png failed to load...");
  239. isSuccessful = false;
  240. }
  241. return isSuccessful;
  242. }
  243.  
  244. void close_game()
  245. {
  246. SDL_DestroyTexture(playerOneTexture);
  247. playerOneTexture = NULL;
  248.  
  249. SDL_DestroyRenderer(globalRenderer);
  250. globalRenderer = NULL;
  251.  
  252. SDL_DestroyWindow(globalWindow);
  253. globalWindow = NULL;
  254.  
  255. IMG_Quit();
  256. SDL_Quit();
  257. }
  258.  
  259. SDL_Texture* load_texture(std::string path)
  260. {
  261. SDL_Texture* finalTexture = NULL;
  262. SDL_Surface* loadedSurface = IMG_Load(path.c_str());
  263.  
  264. if(loadedSurface == NULL){
  265. printf("Image %s failed to load... SDL_image error: %sn", path.c_str(), IMG_GetError());
  266. } else {
  267. finalTexture = SDL_CreateTextureFromSurface(globalRenderer, loadedSurface);
  268. if(finalTexture == NULL){
  269. printf("Failed to create texture from surface...n");
  270. }
  271. SDL_FreeSurface(loadedSurface);
  272. }
  273. return finalTexture;
  274. }
  275.  
  276. bool check_collision(SDL_Rect a, SDL_Rect b)
  277. {
  278. int leftA, leftB;
  279. int rightA, rightB;
  280. int topA, topB;
  281. int bottomA, bottomB;
  282.  
  283. leftA = a.x;
  284. rightA = a.x + a.w;
  285. topA = a.y;
  286. bottomA = a.y + a.h;
  287.  
  288. leftB = b.x;
  289. rightB = b.x + b.w;
  290. topB = b.y;
  291. bottomB = b.y + b.h;
  292.  
  293. if(bottomA <= topB){
  294. return false;
  295. }
  296. if(topA >= bottomB){
  297. return false;
  298. }
  299. if(rightA <= leftB){
  300. return false;
  301. }
  302. if(leftA >= rightB){
  303. return false;
  304. }
  305. return true;
  306. }
  307.  
  308. int main()
  309. {
  310. if(!initialize_game()){
  311. printf("Failed to initialize...n");
  312. } else {
  313. if(!load_game()) {
  314. printf("Failed to load game...n");
  315. } else {
  316. bool isRunning = true;
  317.  
  318. SDL_Event event;
  319. Player Player;
  320. Timer stepTimer;
  321.  
  322. SDL_Rect wall;
  323. wall.x = 90;
  324. wall.y = 90;
  325. wall.w = 90;
  326. wall.h = 90;
  327.  
  328. SDL_Rect wall2;
  329. wall2.x = 256;
  330. wall2.y = 256;
  331. wall2.w = 30;
  332. wall2.h = 40;
  333.  
  334. while(isRunning) {
  335. while(SDL_PollEvent(&event) != 0) {
  336. if(event.type == SDL_QUIT){
  337. isRunning = false;
  338. }
  339. Player.handle_player_event(event);
  340. }
  341. float timeStep = stepTimer.get_ticks() / 1000.f;
  342.  
  343. Player.move_player(timeStep, wall);
  344.  
  345. stepTimer.start_timer();
  346.  
  347. SDL_SetRenderDrawColor(globalRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
  348. SDL_RenderClear(globalRenderer);
  349.  
  350. SDL_SetRenderDrawColor(globalRenderer, 0x00, 0x00, 0x00, 0xFF);
  351. SDL_RenderDrawRect(globalRenderer, &wall);
  352. SDL_RenderDrawRect(globalRenderer, &wall2);
  353.  
  354. Player.render_player(Player.positionX, Player.positionY, playerOneTexture, NULL, 0.0, NULL, SDL_FLIP_NONE);
  355.  
  356. SDL_RenderPresent(globalRenderer);
  357. }
  358. }
  359. }
  360. close_game();
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement