Guest User

Untitled

a guest
Mar 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <SDL2/SDL.h>
  4. #include <SDL2/SDL_timer.h>
  5. #include <SDL_image.h>
  6.  
  7.  
  8. #define WINDOW_WIDTH (640)
  9. #define WINDOW_HEIGHT (480)
  10. // Speed in Pixels/Seconds
  11. #define SCROLL_SPEED (300)
  12.  
  13. // Spirte Size
  14. #define SPRITE_SIZE 32
  15.  
  16.  
  17. int main(int argc, const char * argv[]) {
  18.  
  19. // Attempt to initialize graphics library
  20. if (SDL_Init(SDL_INIT_VIDEO) != 0) {
  21. SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
  22. return 1;
  23. }
  24.  
  25. //Initialize Key Variables
  26. SDL_Window *window = NULL;
  27. SDL_Renderer *renderer = NULL;
  28. SDL_Surface *windowSurface = NULL;
  29. SDL_Surface *imageSurface = NULL;
  30.  
  31.  
  32.  
  33. // Creating Surfaces and Addresses
  34. SDL_Surface *temp = NULL;
  35. SDL_Surface *sprite = NULL;
  36. SDL_Rect dstSprite;
  37.  
  38. // set sprite position
  39. dstSprite.x = 0;
  40. dstSprite.y = 0;
  41. dstSprite.w = 32;
  42. dstSprite.h = 32;
  43.  
  44.  
  45. // Attempt to create SDL Window
  46. window = SDL_CreateWindow("Atari Lunar Lander", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
  47.  
  48. // Print an Error if the window fails to initialize
  49. if (window == NULL) {
  50. printf("Could not create window: %sn", SDL_GetError());
  51. return 1;
  52. }
  53.  
  54. // Create The Title of the Window
  55. SDL_SetWindowTitle(window, "Atari Lunar Lander");
  56.  
  57. // Create SDL Render Window
  58. Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE;
  59. renderer = SDL_CreateRenderer(window, -1, render_flags);
  60.  
  61. // if rendering the window fails
  62. if (!renderer) {
  63. printf("error creating render: %sn", SDL_GetError());
  64. SDL_DestroyWindow(window);
  65. SDL_Quit();
  66. return 1;
  67. }
  68.  
  69. windowSurface = SDL_GetWindowSurface(window);
  70.  
  71. // Load A bitmap image as an Surface
  72. windowSurface = SDL_LoadBMP("/Users/jeremiahonwubuya/Desktop/AtariLunarLander/outerspace.bmp");
  73.  
  74. if (!windowSurface) {
  75. printf("error creating surface: %sn", SDL_GetError());
  76. SDL_DestroyRenderer(renderer);
  77. SDL_DestroyWindow(window);
  78. SDL_Quit();
  79. return 1;
  80. }
  81.  
  82. //Load Image data into graphics hardware memory
  83. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, windowSurface);
  84.  
  85.  
  86. if (!texture) {
  87. printf("error creating texture: %sn", SDL_GetError());
  88. SDL_DestroyRenderer(renderer);
  89. SDL_DestroyWindow(window);
  90. SDL_Quit();
  91. return 1;
  92. }
  93.  
  94.  
  95. // load bitmap image into an SDL_Surface "temp" and store "temp" into the SDL_Surface "sprite" that is optimized for Blitting
  96. temp = SDL_LoadBMP("/Users/jeremiahonwubuya/Desktop/AtariLunarLander/player.bmp");
  97. sprite = SDL_ConvertSurface(temp, temp->format, 0);
  98. SDL_FreeSurface(temp);
  99.  
  100. SDL_Texture *playerTexture = SDL_CreateTextureFromSurface(renderer, sprite);
  101.  
  102.  
  103. if (!sprite) {
  104. printf("error creating sprite: %sn", SDL_GetError());
  105. SDL_DestroyRenderer(renderer);
  106. SDL_DestroyWindow(window);
  107. SDL_Quit();
  108. return 1;
  109. }
  110.  
  111.  
  112.  
  113. // updating the Window Surface to be the BitMap Image
  114. SDL_RenderClear(renderer);
  115. SDL_RenderCopy(renderer, texture, NULL, NULL);
  116. SDL_RenderCopy(renderer, playerTexture, NULL, &dstSprite);
  117. SDL_RenderPresent(renderer);
  118.  
  119.  
  120.  
  121. bool isRunning = true;
  122. SDL_Event event;
  123.  
  124. while (isRunning) {
  125.  
  126. while (SDL_PollEvent(&event) != 0 ) {
  127. if (event.type == SDL_QUIT) {
  128. // close button clicked
  129. isRunning = false;
  130. break;
  131. }
  132.  
  133. if (event.type == SDL_KEYDOWN) {
  134. // close program upon buttons pressed
  135. switch(event.key.keysym.sym){
  136. case SDLK_q:
  137. printf("program stopped");
  138. isRunning = false;
  139. break;
  140. case SDLK_ESCAPE:
  141. printf("programm stopped");
  142. isRunning = false;
  143. break;
  144.  
  145. // Animating the Sprite
  146.  
  147. case SDLK_LEFT | SDLK_a:
  148. dstSprite.x -= 5;
  149. printf("sprite moved leftt");
  150. break;
  151. case SDLK_RIGHT | SDLK_d:
  152. dstSprite.x += 5;
  153. printf("sprite moved right");
  154. break;
  155. case SDLK_UP | SDLK_w:
  156. dstSprite.y -= 5;
  157. printf("sprite moved up");
  158. break;
  159. case SDLK_DOWN | SDLK_s:
  160. dstSprite.y += 5;
  161. printf("sprite moved down");
  162. break;
  163.  
  164. }
  165.  
  166. }
  167.  
  168.  
  169. // Prevent Sprite from colliding with the edges of the screen
  170.  
  171. if ( dstSprite.x < 0 ) {
  172. dstSprite.x = 0;
  173. }
  174. else if ( dstSprite.x > WINDOW_WIDTH-SPRITE_SIZE ) {
  175. dstSprite.x = WINDOW_WIDTH-SPRITE_SIZE;
  176. }
  177. if ( dstSprite.y < 0 ) {
  178. dstSprite.y = 0;
  179. }
  180. else if ( dstSprite.y > WINDOW_HEIGHT-SPRITE_SIZE ) {
  181. dstSprite.y = WINDOW_HEIGHT-SPRITE_SIZE;
  182. }
  183.  
  184.  
  185. // Draw the Sprite
  186. SDL_BlitSurface(sprite, NULL, windowSurface, &dstSprite);
  187.  
  188.  
  189. SDL_UpdateWindowSurface(window);
  190. }
  191.  
  192. }
  193.  
  194.  
  195. SDL_Delay(5000);
  196.  
  197. // Clean Up
  198.  
  199. // Free SDL Surfaces
  200. SDL_FreeSurface(sprite);
  201. sprite = NULL;
  202. SDL_FreeSurface(windowSurface);
  203. windowSurface = NULL;
  204. SDL_FreeSurface(imageSurface);
  205. imageSurface = NULL;
  206. // Destroy Texture and Window
  207. texture = NULL;
  208. SDL_DestroyTexture(texture);
  209. playerTexture = NULL;
  210. SDL_DestroyTexture(playerTexture);
  211. window = NULL;
  212. SDL_DestroyWindow(window);
  213. SDL_Quit();
  214.  
  215. }
Add Comment
Please, Sign In to add comment