#include #include #include #include // Dołącz definicje biblioteki Allegro #include #include const float FPS = 60; //obraz będzie aktualizowany co 1/FPS sekundy const int SCREEN_W = 640; //szerokość okna const int SCREEN_H = 480; //wysokość okna float* hak; void draw_shape(int x, int y, int n, float R, ALLEGRO_COLOR kolor) { float zmiana_alfa = (2 * ALLEGRO_PI) / n; float alfa = 0; float *wsp = new float[n*2]; for (int i = 0; i < n; i++) { wsp[2*i] = x+R * cos(alfa); wsp[2 * i + 1] = y+R * sin(alfa); alfa += zmiana_alfa; } al_draw_polygon(wsp, n ,ALLEGRO_LINE_JOIN_BEVEL, kolor, 5, 6); } int rc() { return rand()% 255; } // Funkcja główna int main() { srand(time(NULL)); 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; float tablica_wielokatow[50][20];// max 50 wielokatw, ni ewiecej niz 20 bokow if (!al_init()) { //inicjalizacja biblioteki Allegro fprintf(stderr, "Nie zainicjalizowano allegro!n"); return -1; } 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(); 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.) //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_mouse_event_source()); al_register_event_source(event_queue, al_get_keyboard_event_source()); //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 = 7; float dx[N] = { 0.0, -30.0, -10.0, -10.0, 10.0, 10.0, 30.0 }; float dy[N] = { -60.0, -30.0,-30.0, 60.0, 60.0,-30.0,-30.0 }; //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. 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_MOUSE_BUTTON_DOWN) { int random_n = rand() % 20 + 1; int random_size = rand() % 200 + 50; draw_shape(event.mouse.x, event.mouse.y, random_n, random_size, al_map_rgb(rc(), rc(), rc())); } 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; } 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; } fi += dfi; ////Narysuj wypełniony okrąg //al_draw_filled_circle(xm, ym, 100, blue); //al_draw_circle(xm, ym, 100, yellow, 2); ////Narysuj wypełniony wielokat //al_draw_filled_polygon(points, N, white); //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; }