Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <allegro5/allegro.h>
  3.  
  4.  
  5. const int SCREEN_WIDTH = 1024;
  6. const int SCREEN_HEIGHT = 768;
  7. const int KWADRAT_SIZE = 32;
  8. const float FPS = 60;
  9.  
  10. int main(int argc, char** argv) {
  11.  
  12. ALLEGRO_DISPLAY* display;
  13. ALLEGRO_BITMAP* kwadrat;
  14. ALLEGRO_EVENT_QUEUE* event_queue;
  15. ALLEGRO_TIMER* timer;
  16.  
  17. float kwadrat_x = SCREEN_WIDTH / 2.0 - KWADRAT_SIZE / 2.0;
  18. float kwadrat_y = SCREEN_HEIGHT / 2.0 - KWADRAT_SIZE / 2.0;
  19. float kwadrat_dx = -4.0, kwadrat_dy = 4.0;
  20. bool redraw = true;
  21.  
  22. if (!al_init()) {
  23. fprintf(stderr, "Błąd inicjalizacji allegro!\n");
  24. return -1;
  25. }
  26.  
  27. timer = al_create_timer(1.0 / FPS);
  28. if (!timer) {
  29. fprintf(stderr, "Błąd inicjalizacji timerów!\n");
  30. return -1;
  31. }
  32.  
  33. display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
  34. if (!display) {
  35. fprintf(stderr, "Błąd inicjalizacji ekranu!\n");
  36. al_destroy_timer(timer);
  37. return -1;
  38. }
  39.  
  40. kwadrat = al_create_bitmap(KWADRAT_SIZE, KWADRAT_SIZE);
  41. if (!kwadrat) {
  42. fprintf(stderr, "Błąd inicjalizacji bitmapy!\n");
  43. al_destroy_display(display);
  44. al_destroy_timer(timer);
  45. return -1;
  46. }
  47.  
  48. al_set_target_bitmap(kwadrat);
  49.  
  50. al_clear_to_color(al_map_rgb(255, 0, 255));
  51.  
  52. al_set_target_bitmap(al_get_backbuffer(display));
  53.  
  54. event_queue = al_create_event_queue();
  55. if (!event_queue) {
  56. fprintf(stderr, "Błąd inicjalizacji event!\n");
  57. al_destroy_bitmap(kwadrat);
  58. al_destroy_display(display);
  59. al_destroy_timer(timer);
  60. return -1;
  61. }
  62.  
  63. al_register_event_source(event_queue, al_get_display_event_source(display));
  64.  
  65. al_register_event_source(event_queue, al_get_timer_event_source(timer));
  66.  
  67. al_clear_to_color(al_map_rgb(0, 0, 0));
  68.  
  69. al_flip_display();
  70.  
  71. al_start_timer(timer);
  72.  
  73. while (1)
  74. {
  75. ALLEGRO_EVENT ev;
  76. al_wait_for_event(event_queue, &ev);
  77.  
  78. if (ev.type == ALLEGRO_EVENT_TIMER) {
  79. if (kwadrat_x < 0 || kwadrat_x > SCREEN_WIDTH - KWADRAT_SIZE) {
  80. kwadrat_dx = -kwadrat_dx;
  81. }
  82.  
  83. if (kwadrat_y < 0 || kwadrat_y > SCREEN_HEIGHT - KWADRAT_SIZE) {
  84. kwadrat_dy = -kwadrat_dy;
  85. }
  86.  
  87. kwadrat_x += kwadrat_dx;
  88. kwadrat_y += kwadrat_dy;
  89.  
  90. redraw = true;
  91. }
  92. else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  93. break;
  94. }
  95.  
  96. if (redraw && al_is_event_queue_empty(event_queue)) {
  97. redraw = false;
  98.  
  99. al_clear_to_color(al_map_rgb(0, 0, 0));
  100.  
  101. al_draw_bitmap(kwadrat, kwadrat_x, kwadrat_y, 0);
  102.  
  103. al_flip_display();
  104. }
  105. }
  106.  
  107. al_destroy_bitmap(kwadrat);
  108. al_destroy_timer(timer);
  109. al_destroy_display(display);
  110. al_destroy_event_queue(event_queue);
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement