Advertisement
joker546645

Grafika_znaczek

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