It works!

This commit is contained in:
2025-11-26 15:47:02 +01:00
parent 9f2d1e4608
commit b9a804ead6
8 changed files with 150 additions and 88 deletions

View File

@@ -11,20 +11,11 @@ const Engine = @import("engine/Engine.zig");
const Swapchain = @import("engine/Swapchain.zig");
const Game = @import("Game.zig");
pub var allocator: std.mem.Allocator = undefined;
pub var temp_allocator: std.mem.Allocator = undefined;
pub var window: *glfw.Window = undefined;
pub fn main() !void {
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
defer _ = gpa.deinit();
var fba: std.heap.FixedBufferAllocator = .init(&struct {
pub var buffer: [c.temp_allocator_capacity]u8 = undefined;
}.buffer);
allocator = gpa.allocator();
temp_allocator = fba.threadSafeAllocator();
const allocator = gpa.allocator();
atoms.init(allocator);
defer atoms.deinit();
@@ -44,13 +35,20 @@ pub fn main() !void {
}
glfw.windowHint(.client_api, .no_api);
window = glfw.Window.create(c.default_window_width, c.default_window_height, c.window_title, null) catch |err| {
var window = glfw.Window.create(c.default_window_width, c.default_window_height, c.window_title, null) catch |err| {
std.log.err("Could not create window", .{});
return err;
};
defer window.destroy();
window.setSizeLimits(c.min_window_width, c.min_window_height, -1, -1);
window.setInputMode(.cursor, .disabled) catch {};
if (glfw.rawMouseMotionSupported()) {
window.setInputMode(.raw_mouse_motion, true) catch {};
}
_ = window.setKeyCallback(keyCallback);
_ = window.setCursorPosCallback(cursorPosCallback);
var engine = try Engine.init(allocator, window);
defer engine.deinit();
@@ -59,7 +57,19 @@ pub fn main() !void {
defer swapchain.deinit(&engine);
var game = try Game.init(allocator, &engine, &swapchain);
defer game.deinit();
var callback_context: CallbackContext = blk: {
const cursor_last_xpos, const cursor_last_ypos = window.getCursorPos();
break :blk .{
.game = &game,
.cursor_last_xpos = cursor_last_xpos,
.cursor_last_ypos = cursor_last_ypos,
};
};
window.setUserPointer(&callback_context);
defer {
window.setUserPointer(null);
game.deinit();
}
var t1 = glfw.getTime();
while (!window.shouldClose()) {
@@ -73,3 +83,42 @@ pub fn main() !void {
std.Thread.sleep(1 * std.time.ns_per_ms);
}
}
const CallbackContext = struct {
game: *Game,
cursor_last_xpos: f64,
cursor_last_ypos: f64,
};
fn keyCallback(window: *glfw.Window, key_code: glfw.Key, _: c_int, action: glfw.Action, mods: glfw.Mods) callconv(.c) void {
const maybe_ctx = window.getUserPointer(CallbackContext);
if (key_code == .escape and action == .press and (window.getInputMode(.cursor) catch .normal) == .disabled) {
window.setInputMode(.cursor, .normal) catch {};
if (maybe_ctx) |ctx| {
const cursor_last_xpos, const cursor_last_ypos = window.getCursorPos();
ctx.cursor_last_xpos = cursor_last_xpos;
ctx.cursor_last_ypos = cursor_last_ypos;
}
return;
}
if (maybe_ctx) |ctx| {
switch (action) {
.press => ctx.game.onKeyDown(key_code, mods),
.release => ctx.game.onKeyUp(key_code, mods),
.repeat => {},
}
}
}
fn cursorPosCallback(window: *glfw.Window, cursor_xpos: f64, cursor_ypos: f64) callconv(.c) void {
const maybe_ctx = window.getUserPointer(CallbackContext);
if (maybe_ctx) |ctx| {
const dx: f32 = @floatCast(cursor_xpos - ctx.cursor_last_xpos);
const dy: f32 = @floatCast(cursor_ypos - ctx.cursor_last_ypos);
ctx.game.onMouseMove(dx, dy);
ctx.cursor_last_xpos = cursor_xpos;
ctx.cursor_last_ypos = cursor_ypos;
}
}