app: make apprt agnostic

This commit is contained in:
Mitchell Hashimoto
2022-12-30 15:56:42 -08:00
parent 83f5d29ae2
commit 58218af2b5
2 changed files with 38 additions and 11 deletions

View File

@ -6,7 +6,7 @@ const App = @This();
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const glfw = @import("glfw"); const apprt = @import("apprt.zig");
const Window = @import("Window.zig"); const Window = @import("Window.zig");
const tracy = @import("tracy"); const tracy = @import("tracy");
const Config = @import("config.zig").Config; const Config = @import("config.zig").Config;
@ -27,6 +27,9 @@ pub const Mailbox = BlockingQueue(Message, 64);
/// General purpose allocator /// General purpose allocator
alloc: Allocator, alloc: Allocator,
/// The runtime for this app.
runtime: apprt.runtime.App,
/// The list of windows that are currently open /// The list of windows that are currently open
windows: WindowList, windows: WindowList,
@ -59,9 +62,9 @@ pub const Darwin = struct {
/// up the renderer state, compiles the shaders, etc. This is the primary /// up the renderer state, compiles the shaders, etc. This is the primary
/// "startup" logic. /// "startup" logic.
pub fn create(alloc: Allocator, config: *const Config) !*App { pub fn create(alloc: Allocator, config: *const Config) !*App {
// Initialize glfw // Initialize app runtime
try glfw.init(.{}); var app_backend = try apprt.runtime.App.init();
errdefer glfw.terminate(); errdefer app_backend.terminate();
// The mailbox for messaging this thread // The mailbox for messaging this thread
var mailbox = try Mailbox.create(alloc); var mailbox = try Mailbox.create(alloc);
@ -74,6 +77,7 @@ pub fn create(alloc: Allocator, config: *const Config) !*App {
errdefer alloc.destroy(app); errdefer alloc.destroy(app);
app.* = .{ app.* = .{
.alloc = alloc, .alloc = alloc,
.runtime = app_backend,
.windows = .{}, .windows = .{},
.config = config, .config = config,
.mailbox = mailbox, .mailbox = mailbox,
@ -117,22 +121,21 @@ pub fn destroy(self: *App) void {
self.alloc.destroy(self); self.alloc.destroy(self);
// Close our windowing runtime // Close our windowing runtime
glfw.terminate(); self.runtime.terminate();
} }
/// Wake up the app event loop. This should be called after any messages /// Wake up the app event loop. This should be called after any messages
/// are sent to the mailbox. /// are sent to the mailbox.
pub fn wakeup(self: App) void { pub fn wakeup(self: App) void {
_ = self; self.runtime.wakeup() catch return;
glfw.postEmptyEvent() catch {};
} }
/// Run the main event loop for the application. This blocks until the /// Run the main event loop for the application. This blocks until the
/// application quits or every window is closed. /// application quits or every window is closed.
pub fn run(self: *App) !void { pub fn run(self: *App) !void {
while (!self.quit and self.windows.items.len > 0) { while (!self.quit and self.windows.items.len > 0) {
// Block for any glfw events. // Block for any events.
try glfw.waitEvents(); try self.runtime.wait();
// If any windows are closing, destroy them // If any windows are closing, destroy them
var i: usize = 0; var i: usize = 0;

View File

@ -10,12 +10,12 @@ const Allocator = std.mem.Allocator;
const trace = @import("tracy").trace; const trace = @import("tracy").trace;
const glfw = @import("glfw"); const glfw = @import("glfw");
const objc = @import("objc"); const objc = @import("objc");
const App = @import("../App.zig");
const input = @import("../input.zig"); const input = @import("../input.zig");
const internal_os = @import("../os/main.zig"); const internal_os = @import("../os/main.zig");
const renderer = @import("../renderer.zig"); const renderer = @import("../renderer.zig");
const Renderer = renderer.Renderer; const Renderer = renderer.Renderer;
const apprt = @import("../apprt.zig"); const apprt = @import("../apprt.zig");
const CoreApp = @import("../App.zig");
const CoreWindow = @import("../Window.zig"); const CoreWindow = @import("../Window.zig");
// Get native API access on certain platforms so we can do more customization. // Get native API access on certain platforms so we can do more customization.
@ -25,6 +25,30 @@ const glfwNative = glfw.Native(.{
const log = std.log.scoped(.glfw); const log = std.log.scoped(.glfw);
pub const App = struct {
pub fn init() !App {
try glfw.init(.{});
return .{};
}
pub fn terminate(self: App) void {
_ = self;
glfw.terminate();
}
/// Wakeup the event loop. This should be able to be called from any thread.
pub fn wakeup(self: App) !void {
_ = self;
try glfw.postEmptyEvent();
}
/// Wait for events in the event loop to process.
pub fn wait(self: App) !void {
_ = self;
try glfw.waitEvents();
}
};
pub const Window = struct { pub const Window = struct {
/// The glfw window handle /// The glfw window handle
window: glfw.Window, window: glfw.Window,
@ -32,7 +56,7 @@ pub const Window = struct {
/// The glfw mouse cursor handle. /// The glfw mouse cursor handle.
cursor: glfw.Cursor, cursor: glfw.Cursor,
pub fn init(app: *const App, core_win: *CoreWindow) !Window { pub fn init(app: *const CoreApp, core_win: *CoreWindow) !Window {
// Create our window // Create our window
const win = try glfw.Window.create( const win = try glfw.Window.create(
640, 640,