summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-10-28 20:56:23 +0100
committerCarlos Maiolino <[email protected]>2025-10-28 20:56:23 +0100
commit5e7ef9236c51593074da22f42d25b1d622939707 (patch)
treeb38977134ca390778b46ad10d880cf57a92e4bf4
Initial drop
Add basic Allegro initialization, draw the display and add sample object Signed-off-by: Carlos Maiolino <[email protected]>
-rw-r--r--Makefile21
-rw-r--r--src/include/rb.h22
-rw-r--r--src/rb.c167
3 files changed, 210 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..ee2e518
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,21 @@
+RB_LIBS_DIR= ./src/include/
+BUILD_DIR= ./build/
+BIN_DIR= ./bin
+
+ALLEGRO_LIBS= -lallegro_font \
+ -lallegro_primitives \
+ -lallegro
+
+INCLUDES= -I$(RB_LIBS_DIR)
+
+OBJS= ./build/*.o
+
+all: $(BUILD_DIR)/rb.o
+ gcc -Wall $(INCLUDES) $(OBJS) $(ALLEGRO_LIBS) -o $(BIN_DIR)/rocksblaster
+
+$(BUILD_DIR)/rb.o: src/rb.c
+ gcc -Wall $(INCLUDES) -c src/rb.c -o $(BUILD_DIR)/rb.o
+
+clean:
+ rm -f $(BUILD_DIR)/*.o
+ rm -f $(BIN_DIR)/rocksblaster
diff --git a/src/include/rb.h b/src/include/rb.h
new file mode 100644
index 0000000..fb3f617
--- /dev/null
+++ b/src/include/rb.h
@@ -0,0 +1,22 @@
+#ifndef RB_H
+#define RB_H
+
+#include <allegro5/allegro5.h>
+
+#define RB_WIDTH 640
+#define RB_HEIGHT 480
+#define RB_FPS (1.0 / 30.0) /* 1 sec divided by 30 frames */
+
+struct rb {
+ float width;
+ float height;
+ float fps;
+ ALLEGRO_TIMER *timer;
+ ALLEGRO_EVENT_QUEUE *event_queue;
+ ALLEGRO_DISPLAY *display;
+ ALLEGRO_FONT *font;
+};
+
+struct display_struct *display;
+
+#endif /* RB_H */
diff --git a/src/rb.c b/src/rb.c
new file mode 100644
index 0000000..d0630ab
--- /dev/null
+++ b/src/rb.c
@@ -0,0 +1,167 @@
+#include <allegro5/allegro5.h>
+#include <allegro5/allegro_font.h>
+#include <allegro5/allegro_primitives.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <rb.h>
+
+ALLEGRO_DISPLAY *
+init_allegro_display(struct rb *rb)
+{
+ if (!rb)
+ return NULL;
+
+ al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
+ al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST);
+ al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR);
+
+ return al_create_display(rb->width, rb->height);
+}
+
+/*
+ * Allegro lib functions return 0 in case of error (or NULL), to make
+ * it compliant to Unix, return 0 if we succeed, non-zero otherwise
+ */
+int
+initialize_allegro(struct rb **p)
+{
+ struct rb *rb;
+
+ if (*p != NULL) {
+ printf("Allegro already initialized\n");
+ return 1;
+ }
+
+ rb = malloc(sizeof(struct rb));
+
+ if (!rb)
+ return 1;
+
+ rb->width = RB_WIDTH;
+ rb->height = RB_HEIGHT;
+ rb->fps = RB_FPS;
+
+ if (!al_init())
+ goto err_alloc;
+
+ if (!al_install_keyboard())
+ goto err_alloc;
+
+ rb->timer = al_create_timer(rb->fps);
+ if (!rb->timer)
+ goto err_alloc;
+
+ rb->event_queue = al_create_event_queue();
+ if (!rb->event_queue)
+ goto err_timer;
+
+ rb->font = al_create_builtin_font();
+ if (!rb->font)
+ goto err_event_queue;
+
+ rb->display = init_allegro_display(rb);
+ if (!rb->display)
+ goto err_font;
+
+ if (!al_init_primitives_addon())
+ goto err_display;
+
+ *p = rb;
+ return 0;
+
+err_display:
+ al_destroy_display(rb->display);
+err_font:
+ al_destroy_font(rb->font);
+err_event_queue:
+ al_destroy_event_queue(rb->event_queue);
+err_timer:
+ al_destroy_timer(rb->timer);
+err_alloc:
+ printf("Couldn't initialize Allegro\n");
+ free(rb);
+ return 1;
+}
+
+void
+shutdown_allegro(struct rb *rb)
+{
+ al_destroy_display(rb->display);
+ al_destroy_font(rb->font);
+ al_destroy_event_queue(rb->event_queue);
+ al_destroy_timer(rb->timer);
+ free(rb);
+}
+
+void
+register_events(struct rb *rb)
+{
+ al_register_event_source(rb->event_queue,
+ al_get_keyboard_event_source());
+ al_register_event_source(rb->event_queue,
+ al_get_display_event_source(rb->display));
+ al_register_event_source(rb->event_queue,
+ al_get_timer_event_source(rb->timer));
+}
+
+void
+start_game(struct rb *rb)
+{
+ bool done = false;
+ bool redraw = true;
+ float x, y;
+ ALLEGRO_EVENT event;
+
+ x = 100;
+ y = 100;
+ al_start_timer(rb->timer);
+
+ while (1) {
+ al_wait_for_event(rb->event_queue, &event);
+
+ switch(event.type) {
+ case ALLEGRO_EVENT_TIMER:
+ redraw = true;
+ break;
+
+ case ALLEGRO_EVENT_KEY_DOWN:
+ /* fallthrough */
+ case ALLEGRO_EVENT_DISPLAY_CLOSE:
+ done = true;
+ break;
+ }
+
+ if (done)
+ break;
+
+ if (redraw && al_is_event_queue_empty(rb->event_queue)) {
+ al_clear_to_color(al_map_rgb(0, 0, 0));
+ al_draw_textf(rb->font, al_map_rgb(255, 255, 255),
+ 0, 0, 0,
+ "X: %.1f Y: %.1f", x, y);
+ al_draw_filled_rectangle(x, y, x+10, y+10,
+ al_map_rgb(255, 0, 0));
+ al_flip_display();
+
+ redraw = false;
+ }
+ }
+}
+
+int
+main(void)
+{
+ int error;
+ struct rb *rb = NULL;
+
+ error = initialize_allegro(&rb);
+ if (error)
+ exit(EXIT_FAILURE);
+
+ register_events(rb);
+ start_game(rb);
+
+ shutdown_allegro(rb);
+
+ return 0;
+}