Something went wrong. Try again.
zag is a game engine written in zig
DEPRECATED. migrated to codeberg at URL: https://codeberg.org/xeno54/zig_zag
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576const std = @import("std");const sdl3 = @import("sdl3");
pub const Context = union(enum) { win: sdl3.video.Window,};
pub const Callback = union(enum) { handler: *const fn() void, system: *const fn(Context) void,};
pub const IO = struct { currentEv: sdl3.events.Event, callbacks: std.ArrayList(Callback), keyboard: Keyboard, mouse: Mouse, allocator: std.mem.Allocator,};
const Keyboard = struct { keys: []const bool, pub fn init(self: *Keyboard) void { self.keys = sdl3.keyboard.getState(); }};const Mouse = struct { buttons: sdl3.mouse.ButtonFlags, x: f32, y: f32, pub fn init(self: *Mouse) void { const state = sdl3.mouse.getState(); self.buttons = state.@"0"; self.x = state.@"1"; self.y = state.@"2"; } pub fn update(self: *Mouse) void { self.init(); }};
pub fn init(io: *IO, allocator: std.mem.Allocator) void { io.callbacks = .empty; io.allocator = allocator;}
pub fn deinit(io: *IO) void { io.callbacks.deinit(io.allocator); }
pub fn addCallback(io: *IO, callback: Callback) !usize { try io.callbacks.append(io.allocator, callback); return (@as(usize, io.callbacks.items.len)-1);}
pub fn onEvent(io: *IO, ctx: ?Context, cID: usize, t: std.meta.Tag(sdl3.events.Event)) !void { if(std.meta.activeTag(io.currentEv) == t) { const callback = io.callbacks.items[cID]; switch(callback) { .handler => callback.handler(), .system => callback.system(ctx.?), } }else return;}
pub fn pump(io: *IO) bool { while(sdl3.events.poll()) |ev| { io.currentEv = ev; switch(ev) { .quit => return false, else => {}, } } return true;}