Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <SDL2/SDL.h>
  4. #include <SDL2/SDL_timer.h>
  5. #include <SDL2/SDL_image.h>
  6. #include <math.h>
  7.  
  8. #define WINDOW_HEIGHT (760)
  9. #define WINDOW_WIDTH (1024)
  10.  
  11. //pixel / second
  12. #define SPEED (600)
  13. #define SHAR_SPD (450)
  14.  
  15.  
  16. void check(float *x_vel, float *y_vel, float *x, float *y, float sh_w, float sh_h, SDL_Rect *all_blcks, int nmb_blcks) {
  17. int i;
  18. if (*x < 0) {
  19. *x_vel *= -1;
  20. *x = -*x;
  21. }
  22. if (*x + sh_w > WINDOW_WIDTH) {
  23. *x_vel *= -1;
  24. *x = WINDOW_WIDTH - sh_w;
  25. }
  26. if (*y < 0) {
  27. *y_vel *= -1;
  28. *y = -*y;
  29. }
  30. if (*y + sh_h > WINDOW_HEIGHT) {
  31. *y_vel *= -1;
  32. *y = WINDOW_HEIGHT - sh_h;
  33. }
  34. for (i = 0; i < nmb_blcks; i++) {
  35. float xb, yb, wb, hb;
  36. xb = all_blcks[i].x;
  37. yb = all_blcks[i].y;
  38. wb = all_blcks[i].w;
  39. hb = all_blcks[i].h;
  40. if ((*x < xb && *x + sh_w > xb) || (*x > xb && *x + sh_w < xb + wb) ||
  41. (*x < xb + wb && *x + sh_w > xb + wb)) {
  42. if ((*y + sh_h > yb - 1 && *y < yb ) || (*y < yb + hb + 1 && *y + sh_h > yb + hb)) {
  43. if (*y_vel > 0)
  44. *y = yb - sh_h;
  45. else
  46. *y = yb + hb;
  47. *y_vel *= -1;
  48. break;
  49. }
  50. } else if ((*y < yb && *y + sh_h > yb) || (*y > yb && *y + sh_h < yb + hb) ||
  51. (*y < yb + hb && *y + sh_h > yb + hb)) {
  52. if ((*x + sh_w > xb && *x < xb) || (*x < xb + wb && *x + sh_w > xb + wb)) {
  53. if (*x_vel > 0)
  54. *x = xb - sh_w;
  55. else
  56. *x = xb + wb;
  57. *x_vel *= -1;
  58.  
  59. break;
  60. }
  61. }
  62. // if ((*x > xb) && (*x < xb + wb) && (*y > yb) && (*y < yb + hb)) {
  63. // float wlx, wly;
  64. // wlx = xb + (*x_vel - fabsf(*x_vel)) * (wb) / (*x_vel * 2);
  65. // wly = yb + (*y_vel - fabsf(*y_vel)) * (hb) / (*y_vel * 2);
  66. // if (fabsf((wlx - *x) * *y_vel) > fabsf((wly - *y) * *x_vel)) {
  67. // *y += (wly - *y) * 2;
  68. // *y_vel *= -1;
  69. // } else {
  70. // *x += (wlx - *x) * 2;
  71. // *x_vel *= -1;
  72. // }
  73. // }
  74. }
  75. }
  76.  
  77.  
  78. int main(int argc, char *argv[]) {
  79. //initialize
  80. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
  81. printf("%s", SDL_GetError());
  82. return 1;
  83. }
  84. SDL_Window *win = SDL_CreateWindow("Hello!", SDL_WINDOWPOS_CENTERED,
  85. SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
  86. if (!win) {
  87. SDL_Quit();
  88. return 1;
  89. }
  90. //create a render
  91. Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
  92. SDL_Renderer *rend = SDL_CreateRenderer(win, -1, render_flags);
  93. if (!rend) {
  94. SDL_DestroyWindow(win);
  95. SDL_Quit();
  96. }
  97. //load image into memory using SDL_image
  98. SDL_Surface *surface = IMG_Load("D:/CArcanoid/resources/hello1.png");
  99. SDL_Surface *block1 = IMG_Load("D:/CArcanoid/resources/hello2.png");
  100. SDL_Surface *block2 = IMG_Load("D:/CArcanoid/resources/hello3.png");
  101. SDL_Surface *shar = IMG_Load("D:/CArcanoid/resources/hello.png");
  102. if (!surface) {
  103. SDL_DestroyRenderer(rend);
  104. SDL_DestroyWindow(win);
  105. SDL_Quit();
  106. }
  107. //load image data imto the graphics hardware's memory
  108. SDL_Texture *tex = SDL_CreateTextureFromSurface(rend, surface);
  109. SDL_Texture *tex1 = SDL_CreateTextureFromSurface(rend, block1);
  110. SDL_Texture *tex2 = SDL_CreateTextureFromSurface(rend, block2);
  111. SDL_Texture *tex_shar = SDL_CreateTextureFromSurface(rend, shar);
  112. SDL_FreeSurface(surface);
  113. if (!tex) {
  114. SDL_DestroyRenderer(rend);
  115. SDL_DestroyWindow(win);
  116. SDL_Quit();
  117. }
  118. //struct to hold the pos and size of the sprite
  119. int nmb_blcks = 0; // колво блоков(включая платформу)
  120. int mx_nm_bl = 10; // макс колво блоков которое будет создано в задаче
  121. SDL_Rect *all_blcks = malloc(sizeof(SDL_Rect) * mx_nm_bl);
  122. SDL_Rect dest;
  123. SDL_Rect dest2;
  124. SDL_Rect dest1;
  125. SDL_Rect shr;
  126. nmb_blcks += 3;
  127.  
  128. //get the dimensions of the texture
  129. SDL_QueryTexture(tex, NULL, NULL, &dest.w, &dest.h);
  130. SDL_QueryTexture(tex1, NULL, NULL, &dest1.w, &dest1.h);
  131. SDL_QueryTexture(tex2, NULL, NULL, &dest2.w, &dest2.h);
  132. SDL_QueryTexture(tex_shar, NULL, NULL, &shr.w, &shr.h);
  133. dest.w /= 4;
  134. dest.h /= 4;
  135. dest1.w /= 4;
  136. dest1.h /= 4;
  137. dest2.w /= 4;
  138. dest2.h /= 4;
  139. shr.w /= 10;
  140. shr.h /= 10;
  141. all_blcks[0] = dest;
  142. all_blcks[1] = dest1;
  143. all_blcks[2] = dest2;
  144. //pos the sprite at the bottom center of the window
  145. // origin is the top left corner, positive y - down
  146. float x_pos = (WINDOW_WIDTH - all_blcks[0].w) / 2;
  147. float y_pos = (WINDOW_HEIGHT - all_blcks[0].h);
  148. float x_sh = (WINDOW_WIDTH - shr.w) / 2;
  149. float y_sh = (WINDOW_HEIGHT - shr.h) / 2;
  150. //initial velocities
  151. float x_vel = 0;
  152. float y_vel = 0;
  153. float x_sh_vel = SHAR_SPD;
  154. float y_sh_vel = SHAR_SPD;
  155. //keep track of which inputs are given
  156. int up = 0;
  157. int down = 0;
  158. int left = 0;
  159. int right = 0;
  160. //set to 1 when the close button is pressed
  161. int close_requested = 0;
  162. //animation loop
  163. int cnt = 0;
  164. while (!close_requested) {
  165. cnt++;
  166. //process events
  167. SDL_Event event;
  168. while (SDL_PollEvent(&event)) {
  169. switch (event.type) {
  170. case SDL_QUIT:
  171. close_requested = 1;
  172. break;
  173.  
  174.  
  175. //keyboard control
  176. case SDL_KEYDOWN:
  177. switch (event.key.keysym.scancode) {
  178. // case SDL_SCANCODE_W:
  179. // case SDL_SCANCODE_UP:
  180. // up = 1;
  181. // break;
  182. case SDL_SCANCODE_A:
  183. case SDL_SCANCODE_LEFT:
  184. left = 1;
  185. break;
  186. // case SDL_SCANCODE_S:
  187. // case SDL_SCANCODE_DOWN:
  188. // down = 1;
  189. // break;
  190. case SDL_SCANCODE_D:
  191. case SDL_SCANCODE_RIGHT:
  192. right = 1;
  193. break;
  194. }
  195. break;
  196. case SDL_KEYUP:
  197. switch (event.key.keysym.scancode) {
  198. // case SDL_SCANCODE_W:
  199. // case SDL_SCANCODE_UP:
  200. // up = 0;
  201. // break;
  202. case SDL_SCANCODE_A:
  203. case SDL_SCANCODE_LEFT:
  204. left = 0;
  205. break;
  206. // case SDL_SCANCODE_S:
  207. // case SDL_SCANCODE_DOWN:
  208. // down = 0;
  209. // break;
  210. case SDL_SCANCODE_D:
  211. case SDL_SCANCODE_RIGHT:
  212. right = 0;
  213. break;
  214. }
  215. break;
  216. }
  217. }
  218.  
  219.  
  220. x_vel = y_vel = 0;
  221. // if (up && !down) y_vel = -SPEED;
  222. // if (down && !up) y_vel = SPEED;
  223. if (left && !right) x_vel = -SPEED;
  224. if (right && !left) x_vel = SPEED;
  225.  
  226.  
  227. // int mouse_x, mouse_y;
  228. // int buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
  229. //
  230. // int target_x = mouse_x - dest.w / 2;
  231. // int target_y = mouse_y - dest.h / 2;
  232. // float delta_x = target_x - x_pos;
  233. // float delta_y = target_y - y_pos;
  234. // float distance = sqrt(delta_x * delta_x + delta_y * delta_y);
  235. //
  236. // //prevent jitter
  237. // if (distance < 5)
  238. // x_vel = y_vel = 0;
  239. // else {
  240. // x_vel = delta_x * SPEED / distance;
  241. // y_vel = delta_y * SPEED / distance;
  242. // }
  243. //
  244. // //reverse velocity if mouse buuton 1 pressed
  245. // if (buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) {
  246. // x_vel = -x_vel;
  247. // y_vel = -y_vel;
  248. // }
  249.  
  250. //update pos
  251. x_pos += x_vel / 30;
  252. y_pos += y_vel / 30;
  253. x_sh += x_sh_vel / 60;
  254. y_sh += y_sh_vel / 60;
  255. check(&x_sh_vel, &y_sh_vel, &x_sh, &y_sh, shr.w, shr.h, all_blcks, nmb_blcks);
  256.  
  257. if (x_pos <= 0)
  258. x_pos = 0;
  259.  
  260. if (y_pos <= 0)
  261. y_pos = 0;
  262.  
  263. if (x_pos >= WINDOW_WIDTH - all_blcks[0].w)
  264. x_pos = WINDOW_WIDTH - all_blcks[0].w;
  265.  
  266. if (y_pos >= WINDOW_HEIGHT - all_blcks[0].h)
  267. y_pos = WINDOW_HEIGHT - all_blcks[0].h;
  268.  
  269. //set the y pos in the struct
  270. all_blcks[0].y = (int) y_pos;
  271. all_blcks[0].x = (int) x_pos;
  272. all_blcks[1].y = (int) 0;
  273. all_blcks[1].x = (int) 0;
  274. all_blcks[2].y = (int) 0;
  275. all_blcks[2].x = all_blcks[2].w;
  276. shr.y = (int) y_sh;
  277. shr.x = (int) x_sh;
  278.  
  279.  
  280. //clear the window
  281. SDL_RenderClear(rend);
  282.  
  283. //draw image to the window
  284. SDL_RenderCopy(rend, tex_shar, NULL, &shr);
  285. SDL_RenderCopy(rend, tex, NULL, &all_blcks[0]);
  286. SDL_RenderCopy(rend, tex1, NULL, &all_blcks[1]);
  287. SDL_RenderCopy(rend, tex2, NULL, &all_blcks[2]);
  288. SDL_RenderPresent(rend);
  289.  
  290. //update sprite pos
  291. //y_pos -= (float) SPEED / 60;
  292.  
  293. //wait 1/60th of a sec
  294. SDL_Delay(1000 / 60);
  295. }
  296.  
  297.  
  298.  
  299. //clear the window
  300. SDL_DestroyTexture(tex);
  301. SDL_DestroyTexture(tex1);
  302. SDL_DestroyTexture(tex2);
  303. SDL_DestroyRenderer(rend);
  304. SDL_DestroyWindow(win);
  305. SDL_Quit();
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement