Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1.  
  2. #include <math.h>
  3. #include <stdio.h>
  4.  
  5. // Dołącz definicje biblioteki Allegro
  6. #include <allegro5/allegro.h>
  7. #include <allegro5/allegro_primitives.h>
  8. #include <allegro5/allegro_image.h>
  9. #include "allegro5/allegro_native_dialog.h"
  10.  
  11. const float FPS = 60;       //obraz będzie aktualizowany co 1/FPS sekundy
  12. const int SCREEN_W = 1280;  //szerokość okna
  13. const int SCREEN_H = 720;   //wysokość okna
  14.  
  15. // Funkcja główna
  16. int main()
  17. {
  18.     ALLEGRO_DISPLAY *display = NULL;            //okno
  19.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;    //kolejka zdarzen
  20.     ALLEGRO_TIMER *timer = NULL;                //timer, od ktorego będziemy odbierac zdarzenia (potrzebny do animacji)
  21.     bool redraw = true;
  22.  
  23.     if (!al_init()) {                           //inicjalizacja biblioteki Allegro
  24.         fprintf(stderr, "Nie zainicjalizowano allegro!\n");
  25.         return -1;
  26.     }
  27.     al_init_image_addon();
  28.     display = al_create_display(SCREEN_W, SCREEN_H);    //utworznie okna
  29.     timer = al_create_timer(1.0 / FPS);                 //utworzenie timera
  30.     al_install_keyboard();                              //inicjalizacja obsługi klawiatury
  31.     al_install_mouse(); //inicjalizacja myszy TUTAJ JOKER
  32.     event_queue = al_create_event_queue();              //utworzenie kolejki zdarzeń
  33.    
  34.     al_init_primitives_addon();                         //inicjalizacja obsługi prostych elementów (punkty, linie, prostokąty, elipsy itd.)
  35.  
  36.  
  37.     //ALLEGRO_BITMAP *znak = al_load_bitmap("znak.bmp");
  38.  
  39.     //Rejestracja żródeł zdarzeń (okno, timer, klawiatura ...)
  40.     al_register_event_source(event_queue, al_get_display_event_source(display));
  41.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  42.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  43.     al_register_event_source(event_queue, al_get_mouse_event_source()); // TUTAJ JOKER
  44.    
  45.     //Kolory rysowania
  46.     ALLEGRO_COLOR yellow = al_map_rgb(255, 255, 0);
  47.     ALLEGRO_COLOR white = al_map_rgb(255, 255, 255);
  48.     ALLEGRO_COLOR blue = al_map_rgb(0, 0, 255);
  49.     ALLEGRO_COLOR black = al_map_rgb(0, 0, 0);
  50.  
  51.     //Definicja wielokąta
  52.     const int N = 12;
  53.     double dx[N] = { -60, -25, -25, 25, 25, 60, 60, 25, 25, -25, -25, -60 };
  54.     double dy[N] = { -25, -25, -60, -60, -25, -25, 25, 25, 60, 60, 25, 25 };
  55.  
  56.     //Tablice na przetworzone współrzędna punktów
  57.     float points[2*N];
  58.  
  59.     //Zmienne na potrzeby obracania figury
  60.     double fi=0.0, dfi=0.1, sinfi, cosfi;
  61.  
  62.     //Uruchamiamy timer, który będzie z zadaną częstotliwością wysyłał zdarzenia
  63.     al_start_timer(timer);
  64.    
  65.     //Pętla główna programu - obsługa zdarzeń.
  66.     //Działamy, dopóki użytkownik nie wciśnie Esc.
  67.     double pos_x = 0;
  68.     double pos_y = 0;
  69.     while(true)
  70.     {
  71.         ALLEGRO_EVENT event;
  72.         al_wait_for_event(event_queue, &event);
  73.  
  74.         if (event.type == ALLEGRO_EVENT_TIMER) {    //zdarzenie timera -> odświeżenie obrazu
  75.             redraw = true;
  76.         }
  77.         else if (event.type == ALLEGRO_EVENT_KEY_DOWN) {    //zdarzenie klawiatury -> jeśli Esc to kończymy
  78.             if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
  79.             break;
  80.         }
  81.         else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { //zdarzenie zamknięcia okna
  82.             break;
  83.         }
  84.         else if (event.type == ALLEGRO_EVENT_MOUSE_AXES) //TUTAJ JOKER
  85.         {
  86.             pos_x = event.mouse.x;
  87.             pos_y = event.mouse.y;
  88.         }
  89.  
  90.         if (redraw && al_is_event_queue_empty(event_queue))
  91.         {
  92.             redraw = false;
  93.             al_clear_to_color(black); //czyszczenie okna na zadany kolor
  94.  
  95.             //Wyznacz środek ekranu
  96.             int xm = SCREEN_W / 2;
  97.             int ym = SCREEN_H / 2;
  98.  
  99.             //Obrót figury
  100.             sinfi = sin(fi);
  101.             cosfi = cos(fi);
  102.             for (int i = 0; i < N; i++)
  103.             {
  104.                 points[2 * i] = (dx[i] * cosfi - dy[i] * sinfi + 0.5) +xm;
  105.                 points[2 * i + 1] = (dx[i] * sinfi + dy[i] * cosfi + 0.5) + ym + 30;
  106.             }
  107.             //fi += dfi;
  108.  
  109.             //Narysuj wypełniony okrąg
  110.             al_draw_filled_circle(xm, ym, 100, blue);
  111.             al_draw_filled_circle(pos_x, pos_y, 30, white);
  112.             al_draw_circle(xm, ym, 100, yellow, 20);
  113.  
  114.             //Narysuj wypełniony wielokat
  115.             al_draw_filled_polygon(points, N, yellow);
  116.  
  117.             //al_draw_bitmap(znak, 0, 0, 0);
  118.  
  119.             //Wyświetl w oknie to, co narysowano w buforze
  120.             al_flip_display();
  121.         }
  122.     }
  123.  
  124.     al_destroy_display(display);
  125.     al_destroy_timer(timer);
  126.     al_destroy_event_queue(event_queue);
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement