110 lines
2.7 KiB
Zig
110 lines
2.7 KiB
Zig
const coral = @import("coral");
|
|
|
|
const ext = @import("./ext.zig");
|
|
|
|
pub const files = @import("./files.zig");
|
|
|
|
pub const gfx = @import("./gfx.zig");
|
|
|
|
pub const App = opaque {
|
|
pub fn Starter(comptime errors: type) type {
|
|
return fn (app_state: *App) errors!void;
|
|
}
|
|
|
|
const State = struct {
|
|
last_event: ext.SDL_Event,
|
|
base_file_sandbox: files.FileSandbox,
|
|
canvas: gfx.Canvas,
|
|
|
|
fn cast(self: *App) *State {
|
|
return @ptrCast(*State, @alignCast(@alignOf(State), self));
|
|
}
|
|
};
|
|
|
|
pub fn canvas(self: *App) *gfx.Canvas {
|
|
return &State.cast(self).canvas;
|
|
}
|
|
|
|
pub fn data_fs(self: *App) files.FileAccessor {
|
|
return files.FileAccessor.bind(files.FileSandbox, &State.cast(self).base_file_sandbox);
|
|
}
|
|
|
|
pub fn poll(self: *App) bool {
|
|
const state = State.cast(self);
|
|
|
|
while (ext.SDL_PollEvent(&state.last_event) != 0) switch (state.last_event.type) {
|
|
ext.SDL_QUIT => return false,
|
|
else => {},
|
|
};
|
|
|
|
return true;
|
|
}
|
|
|
|
pub fn run(comptime errors: type, start: *const Starter(errors)) errors!void {
|
|
const base_prefix = ext.SDL_GetBasePath() orelse {
|
|
return log_error(&.{.{.string = coral.io.slice_sentineled(u8, 0, ext.SDL_GetError())}});
|
|
};
|
|
|
|
defer ext.SDL_free(base_prefix);
|
|
|
|
var state = App.State{
|
|
.last_event = undefined,
|
|
|
|
.base_file_sandbox = .{
|
|
.prefix = coral.io.slice_sentineled(u8, 0, base_prefix),
|
|
|
|
.flags = .{
|
|
.is_readable = true,
|
|
.is_queryable = true,
|
|
}
|
|
},
|
|
|
|
.canvas = .{},
|
|
};
|
|
|
|
return start(@ptrCast(*App, &state));
|
|
}
|
|
};
|
|
|
|
pub const allocator = coral.io.MemoryAllocator.bind(&heap, @TypeOf(heap));
|
|
|
|
var heap = struct {
|
|
live_allocations: usize = 0,
|
|
|
|
const Self = @This();
|
|
|
|
pub fn reallocate(self: *Self, maybe_allocation: ?*anyopaque, size: usize) ?[*]u8 {
|
|
if (size == 0) {
|
|
ext.SDL_free(maybe_allocation);
|
|
|
|
self.live_allocations -= 1;
|
|
|
|
return null;
|
|
}
|
|
|
|
if (ext.SDL_realloc(maybe_allocation, size)) |allocation| {
|
|
self.live_allocations += 1;
|
|
|
|
return @ptrCast([*]u8, allocation);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}{};
|
|
|
|
pub fn log_debug(values: []const coral.format.Value) void {
|
|
var message_memory = [_]u8{0} ** 4096;
|
|
var message_buffer = coral.buffer.Fixed{.data = &message_memory};
|
|
const message_length = coral.format.print(message_buffer.as_writer(), values) catch return;
|
|
|
|
ext.SDL_LogDebug(ext.SDL_LOG_CATEGORY_APPLICATION, "%.*s\n", message_length, &message_buffer);
|
|
}
|
|
|
|
pub fn log_error(values: []const coral.format.Value) void {
|
|
var message_memory = [_]u8{0} ** 4096;
|
|
var message_buffer = coral.buffer.Fixed{.data = &message_memory};
|
|
const message_length = coral.format.print(message_buffer.as_writer(), values) catch return;
|
|
|
|
ext.SDL_LogError(ext.SDL_LOG_CATEGORY_APPLICATION, "%.*s\n", message_length, &message_buffer);
|
|
}
|