140 lines
4.2 KiB
Zig
140 lines
4.2 KiB
Zig
const ext = @cImport({
|
|
@cInclude("SDL2/SDL.h");
|
|
});
|
|
|
|
const errors = @import("./errors.zig");
|
|
const io = @import("./io.zig");
|
|
const stack = @import("./stack.zig");
|
|
const std = @import("std");
|
|
|
|
const Request = struct {
|
|
next: ?*Request = null,
|
|
frame: anyframe,
|
|
|
|
message: union(enum) {
|
|
close: struct {
|
|
file: *ext.SDL_RWops,
|
|
is_closed: *bool,
|
|
},
|
|
|
|
open_readable: struct {
|
|
uri: *const io.Uri,
|
|
file: ?*ext.SDL_RWops,
|
|
},
|
|
},
|
|
};
|
|
|
|
///
|
|
/// Entry point.
|
|
///
|
|
pub fn main() anyerror!void {
|
|
if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) {
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL2 runtime");
|
|
|
|
return error.InitFailure;
|
|
}
|
|
|
|
defer ext.SDL_Quit();
|
|
|
|
const pref_path = create_pref_path: {
|
|
const path = ext.SDL_GetPrefPath("ona", "ona") orelse {
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load user path");
|
|
|
|
return error.InitFailure;
|
|
};
|
|
|
|
break: create_pref_path path[0 .. std.mem.len(path)];
|
|
};
|
|
|
|
defer ext.SDL_free(pref_path.ptr);
|
|
|
|
const window = create_window: {
|
|
const pos = ext.SDL_WINDOWPOS_UNDEFINED;
|
|
var flags = @as(u32, 0);
|
|
|
|
break: create_window ext.SDL_CreateWindow("Ona", pos, pos, 640, 480, flags) orelse {
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 window");
|
|
|
|
return error.InitFailure;
|
|
};
|
|
};
|
|
|
|
defer ext.SDL_DestroyWindow(window);
|
|
|
|
const renderer = create_renderer: {
|
|
var flags = @as(u32, 0);
|
|
|
|
break: create_renderer ext.SDL_CreateRenderer(window, -1, flags) orelse {
|
|
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 renderer");
|
|
|
|
return error.InitFailure;
|
|
};
|
|
};
|
|
|
|
defer ext.SDL_DestroyRenderer(renderer);
|
|
|
|
var request_chain = @as(?*Request, null);
|
|
var is_running = true;
|
|
|
|
while (is_running) {
|
|
var event = std.mem.zeroes(ext.SDL_Event);
|
|
|
|
while (ext.SDL_PollEvent(&event) != 0) {
|
|
switch (event.type) {
|
|
ext.SDL_QUIT => is_running = false,
|
|
else => {},
|
|
}
|
|
}
|
|
|
|
if (ext.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) != 0) {
|
|
ext.SDL_LogError(ext.SDL_LOG_CATEGORY_VIDEO, ext.SDL_GetError());
|
|
ext.SDL_ClearError();
|
|
}
|
|
|
|
if (ext.SDL_RenderClear(renderer) != 0) {
|
|
ext.SDL_LogError(ext.SDL_LOG_CATEGORY_VIDEO, ext.SDL_GetError());
|
|
ext.SDL_ClearError();
|
|
}
|
|
|
|
ext.SDL_RenderPresent(renderer);
|
|
|
|
while (request_chain) |request_head| {
|
|
const request = request_head;
|
|
|
|
request_chain = request_head.next;
|
|
|
|
switch (request.message) {
|
|
.close => |*close| close.is_closed.* = (ext.SDL_RWclose(close.file) == 0),
|
|
|
|
.open_readable => |*open_readable| {
|
|
if (open_readable.uri.isScheme("data")) {
|
|
var path = stack.Fixed(u8, 4096).init();
|
|
|
|
// These can never fail as the sum of the potential bytes written will
|
|
// always be less than 4096.
|
|
path.pushAll("./") catch unreachable;
|
|
std.debug.assert(open_readable.uri.writePath(path.asWriter()));
|
|
|
|
open_readable.file = ext.SDL_RWFromFile(&path.buffer, "r");
|
|
} else if (open_readable.uri.isScheme("user")) {
|
|
var path = stack.Fixed(u8, 4096).init();
|
|
const isOk = errors.isOk;
|
|
|
|
// Cannot guarantee that the sum of potential bytes written will always be
|
|
// less than path max.
|
|
if (isOk(stack.FinitePushError, path.pushAll(pref_path)) and
|
|
open_readable.uri.writePath(path.asWriter())) {
|
|
|
|
open_readable.file = ext.SDL_RWFromFile(&path.buffer, "r");
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
resume request.frame;
|
|
}
|
|
|
|
ext.SDL_Delay(1);
|
|
}
|
|
}
|