Skip to content

Instantly share code, notes, and snippets.

@ryonagana
Last active April 25, 2025 01:08
Show Gist options
  • Select an option

  • Save ryonagana/4bf1fded015c7902b81f1b34c4261d90 to your computer and use it in GitHub Desktop.

Select an option

Save ryonagana/4bf1fded015c7902b81f1b34c4261d90 to your computer and use it in GitHub Desktop.
Simple Allegro Application Using Zig 0.14.0
const std = @import("std");
const LazyPath = std.Build.LazyPath;
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "game_test",
.target = target,
.optimize = optimize,
.root_source_file = b.path("main.zig"),
});
exe.linkSystemLibrary("c");
exe.linkSystemLibrary("allegro_main");
exe.linkSystemLibrary("allegro-5");
exe.linkSystemLibrary("allegro_image-5");
b.installArtifact(exe);
}
const std = @import("std");
const allegro = @cImport({
@cInclude("allegro5/allegro.h");
@cInclude("allegro5/allegro_image.h");
});
pub fn main() !void {
if(!allegro.al_init()){
std.debug.print("Erro!",.{});
return;
}
defer allegro.al_uninstall_system();
if(!allegro.al_init_image_addon()){
std.debug.print("Image Addon",.{});
return;
}
defer allegro.al_shutdown_image_addon();
var display : ?*allegro.ALLEGRO_DISPLAY = null;
const queue : ?*allegro.ALLEGRO_EVENT_QUEUE = allegro.al_create_event_queue();
defer allegro.al_destroy_event_queue(queue);
const timer : ?*allegro.ALLEGRO_TIMER = allegro.al_create_timer(1.0/60.0);
defer allegro.al_destroy_timer(timer);
display = allegro.al_create_display(800, 600);
if(display == null){
std.debug.print("Display Failed!",.{});
}
defer allegro.al_destroy_display(display);
allegro.al_register_event_source(queue, allegro.al_get_display_event_source(display));
//allegro.al_register_event_source(queue, allegro.al_get_display_event_source(display));
//allegro.al_register_event_source(queue, allegro.al_get_display_event_source(display));
allegro.al_register_event_source(queue, allegro.al_get_timer_event_source(timer));
allegro.al_start_timer(timer);
var closed : bool = false;
var redraw : bool = false;
while(!closed){
var e : allegro.ALLEGRO_EVENT = undefined;
if(redraw and allegro.al_event_queue_is_empty(queue)){
redraw = false;
allegro.al_clear_to_color(allegro.al_map_rgb(255,0, 0));
allegro.al_flip_display();
}
while(!allegro.al_is_event_queue_empty(queue)){
allegro.al_wait_for_event(queue, &e);
if(e.type == allegro.ALLEGRO_EVENT_DISPLAY_CLOSE){
closed = true;
break;
}
if(e.type == allegro.ALLEGRO_EVENT_TIMER){
redraw = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment