Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #include <allegro5/allegro.h>
  2. #include <allegro5/allegro_primitives.h>
  3.  
  4. #include <iostream>
  5.  
  6.  
  7. const int screen_width = 1024;
  8. const int screen_height = 766;
  9.  
  10. typedef struct
  11. {
  12. float x;
  13. float y;
  14.  
  15. float dx;
  16. float dy;
  17.  
  18. int size;
  19. int life;
  20. } odlamek;
  21.  
  22. const int default_life = 40;
  23. const int max_n = 10000;
  24. const int fragments = 3;
  25. const float g = 0.01;
  26. int n;
  27.  
  28. odlamek o[max_n];
  29.  
  30.  
  31.  
  32. void rysuj_plansze()
  33. {
  34. al_clear_to_color(al_map_rgb(0, 0, 0));
  35.  
  36. for (int i = 0; i < n; i++) {
  37. if (o[i].life == 0) continue;
  38.  
  39. al_draw_filled_circle(o[i].x, o[i].y, 3, al_map_rgb(o[i].life * (255 / default_life), o[i].life * (255 / default_life), 10 + 2 * o[i].life));
  40. }
  41. }
  42.  
  43.  
  44. void aktualizuj_plansze()
  45. {
  46. for (int i = 0; i < n; i++) {
  47. if (o[i].life == 0) continue;
  48.  
  49. o[i].life--;
  50. if (o[i].life == 0 && o[i].size > 0) {
  51. for (int j = 0; j < fragments; j++) {
  52. o[n].x = o[i].x;
  53. o[n].y = o[i].y;
  54. o[n].dx = o[i].dx * (0.5 + (rand() % 100) / 100.0);
  55. o[n].dy = o[i].dy * (0.5 + (rand() % 100) / 100.0);
  56. o[n].size = o[i].size - 1;
  57. o[n].life = default_life;
  58.  
  59. n++;
  60. }
  61. }
  62.  
  63. o[i].x = o[i].x + o[i].dx;
  64. o[i].y = o[i].y + o[i].dy;
  65.  
  66. o[i].dy = o[i].dy + g;
  67. }
  68. }
  69.  
  70.  
  71. void co_robia_gracze()
  72. {
  73. }
  74.  
  75.  
  76. void inicjalizacja()
  77. {
  78. n = 0;
  79. o[n].x = screen_width / 2;
  80. o[n].y = screen_height - 20;
  81. o[n].dx = (float)(rand() % 100) / 50.0 - 1.0;
  82. o[n].dy = -2 - (float)(rand() % 100) / 100.0;
  83. o[n].size = 4;
  84. o[n].life = default_life;
  85. n++;
  86.  
  87. o[n].x = screen_width / 2;
  88. o[n].y = screen_height - 20;
  89. o[n].dx = (float)(rand() % 100) / 50.0 - 1.0;
  90. o[n].dy = -2 - (float)(rand() % 100) / 100.0;
  91. o[n].size = 4;
  92. o[n].life = default_life;
  93. n++;
  94.  
  95. }
  96.  
  97.  
  98. const float FPS = 60;
  99. bool key[ALLEGRO_KEY_MAX];
  100.  
  101. ALLEGRO_DISPLAY* display = NULL;
  102. ALLEGRO_EVENT_QUEUE* event_queue = NULL;
  103. ALLEGRO_TIMER* timer = NULL;
  104.  
  105. int init()
  106. {
  107. if (!al_init()) {
  108. std::cout << "Błąd inicjalizacji allegro." << std::endl;
  109. return -1;
  110. }
  111.  
  112. if (!al_init_primitives_addon()) {
  113. std::cout << "Błąd inicjalizacji dodatku 'primitives'." << std::endl;
  114. return -1;
  115. }
  116.  
  117. if (!al_install_keyboard()) {
  118. std::cout << "Błąd inicjalizacji klawiatury." << std::endl;
  119. return -1;
  120. }
  121.  
  122. if (!al_install_mouse()) {
  123. std::cout << "Błąd inicjalizacji myszy." << std::endl;
  124. return -1;
  125. }
  126.  
  127. timer = al_create_timer(1.0 / FPS);
  128. if (!timer) {
  129. std::cout << "Błąd inicjalizacji zegara." << std::endl;
  130. return -1;
  131. }
  132.  
  133. display = al_create_display(screen_width, screen_height);
  134. if (!display) {
  135. std::cout << "Błąd inicjalizacji ekranu." << std::endl;
  136. al_destroy_timer(timer);
  137. return -1;
  138. }
  139.  
  140. event_queue = al_create_event_queue();
  141. if (!event_queue) {
  142. std::cout << "Błąd inicjalizacji kolejki zdarzeń." << std::endl;
  143. al_destroy_display(display);
  144. al_destroy_timer(timer);
  145. return -1;
  146. }
  147.  
  148. al_register_event_source(event_queue, al_get_display_event_source(display));
  149. al_register_event_source(event_queue, al_get_timer_event_source(timer));
  150. al_register_event_source(event_queue, al_get_keyboard_event_source());
  151. al_clear_to_color(al_map_rgb(0, 0, 0));
  152.  
  153. al_flip_display();
  154. al_start_timer(timer);
  155.  
  156. return 0;
  157. }
  158.  
  159. int main(int argc, char** argv)
  160. {
  161.  
  162.  
  163. if (init() != 0) {
  164. std::cout << "Inicjalizacja nie powiodła się." << std::endl;
  165. return -1;
  166. }
  167.  
  168. inicjalizacja();
  169.  
  170. bool przerysuj = true;
  171. bool wyjdz = false;
  172.  
  173.  
  174.  
  175. while (!wyjdz)
  176. {
  177. ALLEGRO_EVENT ev;
  178. al_wait_for_event(event_queue, &ev);
  179.  
  180. if (ev.type == ALLEGRO_EVENT_TIMER) {
  181.  
  182.  
  183. przerysuj = true;
  184.  
  185. co_robia_gracze();
  186.  
  187. aktualizuj_plansze();
  188.  
  189. }
  190. else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
  191. key[ev.keyboard.keycode] = true;
  192. }
  193. else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
  194. key[ev.keyboard.keycode] = false;
  195.  
  196. if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
  197. wyjdz = true;
  198. }
  199. }
  200.  
  201. if (przerysuj && al_is_event_queue_empty(event_queue)) {
  202. przerysuj = false;
  203.  
  204. rysuj_plansze();
  205.  
  206. al_flip_display();
  207. }
  208. }
  209.  
  210. return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement