commit b1dd4a1febacbf44bfa95ee2c71448aadfa52a16 Author: Alexander Zhirov Date: Fri May 5 09:19:22 2023 +0300 first diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cfdf2cf --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +CXX = g++ +FLAGS = -Wall +LDFLAGS = -lallegro-static +LIBS = -L./ +OTHER_FLAGS = -lwinmm -lopengl32 -lgdi32 -lole32 -lshlwapi +WINFLAG = -mwindows +BIN = main +SOURCE = main.c + +all: + $(CXX) $(SOURCE) $(LIBS) $(LDFLAGS) $(FLAGS) $(OTHER_FLAGS) $(WINFLAG) -o $(BIN) diff --git a/main.c b/main.c new file mode 100644 index 0000000..a3839ed --- /dev/null +++ b/main.c @@ -0,0 +1,101 @@ +#include +#include + +const float FPS = 60; + +int main(int argc, char *argv[]) +{ + ALLEGRO_DISPLAY *display = NULL; + ALLEGRO_EVENT_QUEUE *event_queue = NULL; + ALLEGRO_TIMER *timer = NULL; + + bool running = true; + bool redraw = true; + + // Initialize allegro + if (!al_init()) + { + fprintf(stderr, "Failed to initialize allegro.\n"); + return 1; + } + + // Initialize the timer + timer = al_create_timer(1.0 / FPS); + if (!timer) + { + fprintf(stderr, "Failed to create timer.\n"); + return 1; + } + + // Create the display + display = al_create_display(640, 480); + if (!display) + { + fprintf(stderr, "Failed to create display.\n"); + return 1; + } + + // Create the event queue + event_queue = al_create_event_queue(); + if (!event_queue) + { + fprintf(stderr, "Failed to create event queue."); + return 1; + } + + // Register event sources + al_register_event_source(event_queue, al_get_display_event_source(display)); + al_register_event_source(event_queue, al_get_timer_event_source(timer)); + + // Display a black screen + al_clear_to_color(al_map_rgb(0, 0, 0)); + al_flip_display(); + + // Start the timer + al_start_timer(timer); + + // Game loop + while (running) + { + ALLEGRO_EVENT event; + ALLEGRO_TIMEOUT timeout; + + // Initialize timeout + al_init_timeout(&timeout, 0.06); + + // Fetch the event (if one exists) + bool get_event = al_wait_for_event_until(event_queue, &event, &timeout); + + // Handle the event + if (get_event) + { + switch (event.type) + { + case ALLEGRO_EVENT_TIMER: + redraw = true; + break; + case ALLEGRO_EVENT_DISPLAY_CLOSE: + running = false; + break; + default: + fprintf(stderr, "Unsupported event received: %d\n", event.type); + break; + } + } + + // Check if we need to redraw + if (redraw && al_is_event_queue_empty(event_queue)) + { + // Redraw + al_clear_to_color(al_map_rgb(0, 0, 0)); + al_flip_display(); + redraw = false; + } + } + + // Clean up + al_destroy_display(display); + al_destroy_event_queue(event_queue); + + return 0; +} \ No newline at end of file