Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_primitives.h>
- #include <iostream>
- const int screen_width = 1024;
- const int screen_height = 766;
- typedef struct
- {
- float x;
- float y;
- float dx;
- float dy;
- int size;
- int life;
- } odlamek;
- const int default_life = 40;
- const int max_n = 10000;
- const int fragments = 3;
- const float g = 0.01;
- int n;
- odlamek o[max_n];
- void rysuj_plansze()
- {
- al_clear_to_color(al_map_rgb(0, 0, 0));
- for (int i = 0; i < n; i++) {
- if (o[i].life == 0) continue;
- al_draw_filled_circle(o[i].x, o[i].y, 3, al_map_rgb(o[i].life * (255 / default_life), o[i].life * (255 / default_life), 10 + 2 * o[i].life));
- }
- }
- void aktualizuj_plansze()
- {
- for (int i = 0; i < n; i++) {
- if (o[i].life == 0) continue;
- o[i].life--;
- if (o[i].life == 0 && o[i].size > 0) {
- for (int j = 0; j < fragments; j++) {
- o[n].x = o[i].x;
- o[n].y = o[i].y;
- o[n].dx = o[i].dx * (0.5 + (rand() % 100) / 100.0);
- o[n].dy = o[i].dy * (0.5 + (rand() % 100) / 100.0);
- o[n].size = o[i].size - 1;
- o[n].life = default_life;
- n++;
- }
- }
- o[i].x = o[i].x + o[i].dx;
- o[i].y = o[i].y + o[i].dy;
- o[i].dy = o[i].dy + g;
- }
- }
- void co_robia_gracze()
- {
- }
- void inicjalizacja()
- {
- n = 0;
- o[n].x = screen_width / 2;
- o[n].y = screen_height - 20;
- o[n].dx = (float)(rand() % 100) / 50.0 - 1.0;
- o[n].dy = -2 - (float)(rand() % 100) / 100.0;
- o[n].size = 4;
- o[n].life = default_life;
- n++;
- o[n].x = screen_width / 2;
- o[n].y = screen_height - 20;
- o[n].dx = (float)(rand() % 100) / 50.0 - 1.0;
- o[n].dy = -2 - (float)(rand() % 100) / 100.0;
- o[n].size = 4;
- o[n].life = default_life;
- n++;
- }
- const float FPS = 60;
- bool key[ALLEGRO_KEY_MAX];
- ALLEGRO_DISPLAY* display = NULL;
- ALLEGRO_EVENT_QUEUE* event_queue = NULL;
- ALLEGRO_TIMER* timer = NULL;
- int init()
- {
- if (!al_init()) {
- std::cout << "Błąd inicjalizacji allegro." << std::endl;
- return -1;
- }
- if (!al_init_primitives_addon()) {
- std::cout << "Błąd inicjalizacji dodatku 'primitives'." << std::endl;
- return -1;
- }
- if (!al_install_keyboard()) {
- std::cout << "Błąd inicjalizacji klawiatury." << std::endl;
- return -1;
- }
- if (!al_install_mouse()) {
- std::cout << "Błąd inicjalizacji myszy." << std::endl;
- return -1;
- }
- timer = al_create_timer(1.0 / FPS);
- if (!timer) {
- std::cout << "Błąd inicjalizacji zegara." << std::endl;
- return -1;
- }
- display = al_create_display(screen_width, screen_height);
- if (!display) {
- std::cout << "Błąd inicjalizacji ekranu." << std::endl;
- al_destroy_timer(timer);
- return -1;
- }
- event_queue = al_create_event_queue();
- if (!event_queue) {
- std::cout << "Błąd inicjalizacji kolejki zdarzeń." << std::endl;
- al_destroy_display(display);
- al_destroy_timer(timer);
- return -1;
- }
- 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_clear_to_color(al_map_rgb(0, 0, 0));
- al_flip_display();
- al_start_timer(timer);
- return 0;
- }
- int main(int argc, char** argv)
- {
- if (init() != 0) {
- std::cout << "Inicjalizacja nie powiodła się." << std::endl;
- return -1;
- }
- inicjalizacja();
- bool przerysuj = true;
- bool wyjdz = false;
- while (!wyjdz)
- {
- ALLEGRO_EVENT ev;
- al_wait_for_event(event_queue, &ev);
- if (ev.type == ALLEGRO_EVENT_TIMER) {
- przerysuj = true;
- co_robia_gracze();
- aktualizuj_plansze();
- }
- else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
- key[ev.keyboard.keycode] = true;
- }
- else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
- key[ev.keyboard.keycode] = false;
- if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
- wyjdz = true;
- }
- }
- if (przerysuj && al_is_event_queue_empty(event_queue)) {
- przerysuj = false;
- rysuj_plansze();
- al_flip_display();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement