Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. // sdl_szablon.cpp : Akademia ETI
  2. // Krzysztof Bruniecki 2018
  3. #include <iostream>
  4. #include <sdl.h>
  5. #undef main
  6.  
  7. /**
  8. * Loads a BMP image into a texture on the rendering device
  9. * @param file The BMP image file to load
  10. * @param ren The renderer to load the texture onto
  11. * @return the loaded texture, or nullptr if something went wrong.
  12. */
  13. SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren) {
  14. //Initialize to nullptr to avoid dangling pointer issues
  15. SDL_Texture *texture = nullptr;
  16. //Load the image
  17. SDL_Surface *loadedImage = SDL_LoadBMP(file.c_str());
  18. //If the loading went ok, convert to texture and return the texture
  19. if (loadedImage != nullptr) {
  20. texture = SDL_CreateTextureFromSurface(ren, loadedImage);
  21. SDL_FreeSurface(loadedImage);
  22. //Make sure converting went ok too
  23. if (texture == nullptr) {
  24. //logSDLError(std::cout, "CreateTextureFromSurface");
  25. }
  26. }
  27. else {
  28. //logSDLError(std::cout, "LoadBMP");
  29. }
  30. return texture;
  31. }
  32.  
  33. /**
  34. * Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
  35. * the texture's width and height
  36. * @param tex The source texture we want to draw
  37. * @param ren The renderer we want to draw to
  38. * @param x The x coordinate to draw to
  39. * @param y The y coordinate to draw to
  40. */
  41. void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y) {
  42. //Setup the destination rectangle to be at the position we want
  43. SDL_Rect dst;
  44. dst.x = x;
  45. dst.y = y;
  46. //Query the texture to get its width and height to use
  47. SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
  48. SDL_RenderCopy(ren, tex, NULL, &dst);
  49. }
  50.  
  51. #define SCREEN_WIDTH 640
  52.  
  53. #define SCREEN_HEIGHT 480
  54.  
  55. struct Pocisk {
  56. int x;
  57. int y;
  58. int dx;
  59. int dy;
  60. SDL_Texture *tex;
  61. void init() {
  62. x = -100;
  63. y = -100;
  64. dx = 0;
  65. dy = 0;
  66. }
  67.  
  68. void update() {
  69. x += dx;
  70. y += dy;
  71. }
  72. };
  73.  
  74.  
  75. int main()
  76. {
  77. #pragma region Inicjalizacja biblioteki SDL
  78. if (SDL_Init(SDL_INIT_VIDEO) != 0) {
  79. std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
  80. return 1;
  81. }
  82. SDL_Window *win = SDL_CreateWindow("Akademia ETI!", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  83. if (win == nullptr) {
  84. std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
  85. SDL_Quit();
  86. return 1;
  87. }
  88. SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  89. if (ren == nullptr) {
  90. SDL_DestroyWindow(win);
  91. std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
  92. SDL_Quit();
  93. return 1;
  94. }
  95. #pragma endregion
  96.  
  97. int cState = 2;
  98. std::string backFn = "mapa.bmp";
  99. std::string playerFn = "c0.bmp";
  100. SDL_Texture * cTex[4];
  101. Pocisk p;
  102. p.init();
  103. p.tex = loadTexture("bomba.bmp", ren);
  104.  
  105. SDL_Texture *backTex = loadTexture(backFn, ren);
  106. cTex[0] = loadTexture(playerFn, ren);
  107. cTex[1] = loadTexture("c1.bmp", ren);
  108. cTex[2] = loadTexture("c2.bmp", ren);
  109. cTex[3] = loadTexture("c3.bmp", ren);
  110.  
  111.  
  112. SDL_Event e;
  113. bool quit = false;
  114. int dx = 0;
  115. int dy = 0;
  116. int dx2 = 0;
  117. int dy2 = 0;
  118. int x = SCREEN_WIDTH / 2;
  119. int y = SCREEN_HEIGHT / 2;
  120. int x2=x, y2=y;
  121. while (!quit) {
  122. while (SDL_PollEvent(&e)) {
  123. if (e.type == SDL_QUIT) {
  124. quit = true;
  125. }
  126. if (e.type == SDL_KEYDOWN) {
  127. switch (e.key.keysym.sym) {
  128. case SDLK_DOWN:
  129. dx = 0;
  130. dy = 1;
  131. break;
  132. case SDLK_LEFT:
  133. dx = -1;
  134. dy = 0;
  135. break;
  136. case SDLK_RIGHT:
  137. dx = 1;
  138. dy = 0;
  139. break;
  140. case SDLK_UP:
  141. dx = 0;
  142. dy = -1;
  143. break;
  144. case SDLK_a:
  145. dx = -4;
  146. dy2 = 0;
  147. break;
  148. case SDLK_SPACE:
  149. p.x = x;
  150. p.y = y;
  151. p.dx = 2 * dx;
  152. p.dy = 2 * dy;
  153. break;
  154. case SDLK_ESCAPE:
  155. quit = true;
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. if (e.type == SDL_MOUSEBUTTONDOWN) {
  162. quit = true;
  163. }
  164. }
  165. //Renderujemy cala scene
  166.  
  167. //
  168. //TODO :blokada wyjscia za ekran
  169. //
  170. /////////////1
  171.  
  172. x += dx;
  173. y += dy;
  174. p.update();
  175.  
  176. if (dy < 0) cState = 0;
  177. else if(dy>0) cState = 2;
  178. else if (dx < 0) cState = 3;
  179. else if (dx>0) cState = 1;
  180.  
  181.  
  182.  
  183.  
  184. if (x <= 0) { x = 0; dx = 1; }
  185.  
  186. /////////////2
  187.  
  188.  
  189. x2 += dx2;
  190. y2 += dy2;
  191. SDL_RenderClear(ren);
  192. renderTexture(backTex, ren, -x, -y);
  193.  
  194.  
  195. renderTexture(cTex[cState], ren, x, y);
  196. renderTexture(p.tex, ren, p.x, p.y);
  197.  
  198.  
  199. //renderTexture(cTex[1], ren, x2, y2);
  200. SDL_RenderPresent(ren);
  201. }
  202.  
  203.  
  204. SDL_DestroyTexture(backTex);
  205. SDL_DestroyTexture(cTex[0]);
  206. SDL_DestroyTexture(cTex[1]);
  207. SDL_DestroyTexture(cTex[2]);
  208. SDL_DestroyTexture(cTex[3]);
  209.  
  210. SDL_DestroyRenderer(ren);
  211. SDL_DestroyWindow(win);
  212. SDL_Quit();
  213. return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement