Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <allegro5/allegro.h>
- const int SCREEN_WIDTH = 1024;
- const int SCREEN_HEIGHT = 768;
- const int KWADRAT_SIZE = 32;
- const float FPS = 60;
- int main(int argc, char** argv) {
- ALLEGRO_DISPLAY* display;
- ALLEGRO_BITMAP* kwadrat;
- ALLEGRO_EVENT_QUEUE* event_queue;
- ALLEGRO_TIMER* timer;
- float kwadrat_x = SCREEN_WIDTH / 2.0 - KWADRAT_SIZE / 2.0;
- float kwadrat_y = SCREEN_HEIGHT / 2.0 - KWADRAT_SIZE / 2.0;
- float kwadrat_dx = -4.0, kwadrat_dy = 4.0;
- bool redraw = true;
- if (!al_init()) {
- fprintf(stderr, "Błąd inicjalizacji allegro!\n");
- return -1;
- }
- timer = al_create_timer(1.0 / FPS);
- if (!timer) {
- fprintf(stderr, "Błąd inicjalizacji timerów!\n");
- return -1;
- }
- display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
- if (!display) {
- fprintf(stderr, "Błąd inicjalizacji ekranu!\n");
- al_destroy_timer(timer);
- return -1;
- }
- kwadrat = al_create_bitmap(KWADRAT_SIZE, KWADRAT_SIZE);
- if (!kwadrat) {
- fprintf(stderr, "Błąd inicjalizacji bitmapy!\n");
- al_destroy_display(display);
- al_destroy_timer(timer);
- return -1;
- }
- al_set_target_bitmap(kwadrat);
- al_clear_to_color(al_map_rgb(255, 0, 255));
- al_set_target_bitmap(al_get_backbuffer(display));
- event_queue = al_create_event_queue();
- if (!event_queue) {
- fprintf(stderr, "Błąd inicjalizacji event!\n");
- al_destroy_bitmap(kwadrat);
- 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_clear_to_color(al_map_rgb(0, 0, 0));
- al_flip_display();
- al_start_timer(timer);
- while (1)
- {
- ALLEGRO_EVENT ev;
- al_wait_for_event(event_queue, &ev);
- if (ev.type == ALLEGRO_EVENT_TIMER) {
- if (kwadrat_x < 0 || kwadrat_x > SCREEN_WIDTH - KWADRAT_SIZE) {
- kwadrat_dx = -kwadrat_dx;
- }
- if (kwadrat_y < 0 || kwadrat_y > SCREEN_HEIGHT - KWADRAT_SIZE) {
- kwadrat_dy = -kwadrat_dy;
- }
- kwadrat_x += kwadrat_dx;
- kwadrat_y += kwadrat_dy;
- redraw = true;
- }
- else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
- break;
- }
- if (redraw && al_is_event_queue_empty(event_queue)) {
- redraw = false;
- al_clear_to_color(al_map_rgb(0, 0, 0));
- al_draw_bitmap(kwadrat, kwadrat_x, kwadrat_y, 0);
- al_flip_display();
- }
- }
- al_destroy_bitmap(kwadrat);
- al_destroy_timer(timer);
- al_destroy_display(display);
- al_destroy_event_queue(event_queue);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement