Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- // Dołącz definicje biblioteki Allegro
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_primitives.h>
- #include <allegro5/allegro_image.h>
- #include "allegro5/allegro_native_dialog.h"
- const float FPS = 60; //obraz będzie aktualizowany co 1/FPS sekundy
- const int SCREEN_W = 1280; //szerokość okna
- const int SCREEN_H = 720; //wysokość okna
- // Funkcja główna
- int main()
- {
- ALLEGRO_DISPLAY *display = NULL; //okno
- ALLEGRO_EVENT_QUEUE *event_queue = NULL; //kolejka zdarzen
- ALLEGRO_TIMER *timer = NULL; //timer, od ktorego będziemy odbierac zdarzenia (potrzebny do animacji)
- bool redraw = true;
- if (!al_init()) { //inicjalizacja biblioteki Allegro
- fprintf(stderr, "Nie zainicjalizowano allegro!\n");
- return -1;
- }
- al_init_image_addon();
- display = al_create_display(SCREEN_W, SCREEN_H); //utworznie okna
- timer = al_create_timer(1.0 / FPS); //utworzenie timera
- al_install_keyboard(); //inicjalizacja obsługi klawiatury
- al_install_mouse(); //inicjalizacja myszy TUTAJ JOKER
- event_queue = al_create_event_queue(); //utworzenie kolejki zdarzeń
- al_init_primitives_addon(); //inicjalizacja obsługi prostych elementów (punkty, linie, prostokąty, elipsy itd.)
- //ALLEGRO_BITMAP *znak = al_load_bitmap("znak.bmp");
- //Rejestracja żródeł zdarzeń (okno, timer, klawiatura ...)
- al_register_event_source(event_queue, al_get_display_event_source(display));
- al_register_event_source(event_queue, al_get_timer_event_source(timer));
- al_register_event_source(event_queue, al_get_keyboard_event_source());
- al_register_event_source(event_queue, al_get_mouse_event_source()); // TUTAJ JOKER
- //Kolory rysowania
- ALLEGRO_COLOR yellow = al_map_rgb(255, 255, 0);
- ALLEGRO_COLOR white = al_map_rgb(255, 255, 255);
- ALLEGRO_COLOR blue = al_map_rgb(0, 0, 255);
- ALLEGRO_COLOR black = al_map_rgb(0, 0, 0);
- //Definicja wielokąta
- const int N = 12;
- double dx[N] = { -60, -25, -25, 25, 25, 60, 60, 25, 25, -25, -25, -60 };
- double dy[N] = { -25, -25, -60, -60, -25, -25, 25, 25, 60, 60, 25, 25 };
- //Tablice na przetworzone współrzędna punktów
- float points[2*N];
- //Zmienne na potrzeby obracania figury
- double fi=0.0, dfi=0.1, sinfi, cosfi;
- //Uruchamiamy timer, który będzie z zadaną częstotliwością wysyłał zdarzenia
- al_start_timer(timer);
- //Pętla główna programu - obsługa zdarzeń.
- //Działamy, dopóki użytkownik nie wciśnie Esc.
- double pos_x = 0;
- double pos_y = 0;
- while(true)
- {
- ALLEGRO_EVENT event;
- al_wait_for_event(event_queue, &event);
- if (event.type == ALLEGRO_EVENT_TIMER) { //zdarzenie timera -> odświeżenie obrazu
- redraw = true;
- }
- else if (event.type == ALLEGRO_EVENT_KEY_DOWN) { //zdarzenie klawiatury -> jeśli Esc to kończymy
- if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
- break;
- }
- else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { //zdarzenie zamknięcia okna
- break;
- }
- else if (event.type == ALLEGRO_EVENT_MOUSE_AXES) //TUTAJ JOKER
- {
- pos_x = event.mouse.x;
- pos_y = event.mouse.y;
- }
- if (redraw && al_is_event_queue_empty(event_queue))
- {
- redraw = false;
- al_clear_to_color(black); //czyszczenie okna na zadany kolor
- //Wyznacz środek ekranu
- int xm = SCREEN_W / 2;
- int ym = SCREEN_H / 2;
- //Obrót figury
- sinfi = sin(fi);
- cosfi = cos(fi);
- for (int i = 0; i < N; i++)
- {
- points[2 * i] = (dx[i] * cosfi - dy[i] * sinfi + 0.5) +xm;
- points[2 * i + 1] = (dx[i] * sinfi + dy[i] * cosfi + 0.5) + ym + 30;
- }
- //fi += dfi;
- //Narysuj wypełniony okrąg
- al_draw_filled_circle(xm, ym, 100, blue);
- al_draw_filled_circle(pos_x, pos_y, 30, white);
- al_draw_circle(xm, ym, 100, yellow, 20);
- //Narysuj wypełniony wielokat
- al_draw_filled_polygon(points, N, yellow);
- //al_draw_bitmap(znak, 0, 0, 0);
- //Wyświetl w oknie to, co narysowano w buforze
- al_flip_display();
- }
- }
- al_destroy_display(display);
- al_destroy_timer(timer);
- al_destroy_event_queue(event_queue);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement