Created
April 25, 2012 22:35
-
-
Save dradtke/2494024 to your computer and use it in GitHub Desktop.
Hello World example for Allegro.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This program uses the Allegro game library to display a blank window. | |
* | |
* It initializes the display and starts up the main game loop. The | |
* game loop only checks for two events: timer (determined by the FPS) | |
* and display close (when the user tries to close the window). | |
* | |
* http://www.damienradtke.org/building-a-mario-clone-with-allegro | |
*/ | |
#include <stdio.h> | |
#include <allegro5/allegro.h> | |
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CC := gcc | |
CFLAGS := -g -Wall | |
LIBS := -lallegro | |
SOURCES := $(shell find src/ -type f -name "*.c") | |
OBJECTS := $(SOURCES:.c=.o) | |
TARGET := game | |
all: $(SOURCES) $(TARGET) | |
$(TARGET): $(OBJECTS) | |
@echo " Linking..."; $(CC) $^ -o $@ $(LIBS) | |
%.o: %.c | |
@echo " CC $<"; $(CC) $(CFLAGS) -c -o $@ $< | |
clean: | |
@echo " Cleaning..."; $(RM) src/*.o $(TARGET) | |
.PHONY: all clean |
This is not a hello world example as it produces a black window.
thanka lot it help to understand the basic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where would I put the game logic? In the past I usually had like 3 basic functions for the game loop
Then I guess after that it would display the new frame with all the new data.
So where might I add this pattern to your template?
*Edit: Also, if you don't mind, could you explain how this code works? I watched an allegro tutorial video already and it explained most of what you have here, but you did things differently. Like you flip the display before the game loop and again inside the game loop. Why? In the tutorial I saw there was only one flip at the end of the game loop. This is the video: https://www.youtube.com/watch?v=27G-2-wn41w and this is his site with the code: http://www.gamefromscratch.com/page/Allegro-Tutorial-Series.aspx
Any information you're willing to share would be very helpful. I'm new to the world of game frameworks, but very interested. Thanks!