core: refactor code structure
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
e3393abbc1
commit
de1275dc0a
6 changed files with 50 additions and 34 deletions
50
src/Core.zig
50
src/Core.zig
|
|
@ -388,6 +388,31 @@ pub fn deinit(entities: *mach.Entities.Mod, core: *Mod) !void {
|
||||||
state.instance.release();
|
state.instance.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const InputState = struct {
|
||||||
|
const KeyBitSet = std.StaticBitSet(@intFromEnum(Key.max) + 1);
|
||||||
|
const MouseButtonSet = std.StaticBitSet(@as(u4, @intFromEnum(MouseButton.max)) + 1);
|
||||||
|
|
||||||
|
keys: KeyBitSet = KeyBitSet.initEmpty(),
|
||||||
|
mouse_buttons: MouseButtonSet = MouseButtonSet.initEmpty(),
|
||||||
|
mouse_position: Position = .{ .x = 0, .y = 0 },
|
||||||
|
|
||||||
|
pub inline fn isKeyPressed(input: InputState, key: Key) bool {
|
||||||
|
return input.keys.isSet(@intFromEnum(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub inline fn isKeyReleased(input: InputState, key: Key) bool {
|
||||||
|
return !input.isKeyPressed(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub inline fn isMouseButtonPressed(input: InputState, button: MouseButton) bool {
|
||||||
|
return input.mouse_buttons.isSet(@intFromEnum(button));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub inline fn isMouseButtonReleased(input: InputState, button: MouseButton) bool {
|
||||||
|
return !input.isMouseButtonPressed(button);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
pub const Event = union(enum) {
|
pub const Event = union(enum) {
|
||||||
key_press: KeyEvent,
|
key_press: KeyEvent,
|
||||||
key_repeat: KeyEvent,
|
key_repeat: KeyEvent,
|
||||||
|
|
@ -1050,6 +1075,31 @@ pub fn deinitLinuxGamemode() void {
|
||||||
gamemode_log.info("gamemode: deactivated", .{});
|
gamemode_log.info("gamemode: deactivated", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn detectBackendType(allocator: std.mem.Allocator) !gpu.BackendType {
|
||||||
|
const backend = std.process.getEnvVarOwned(
|
||||||
|
allocator,
|
||||||
|
"MACH_GPU_BACKEND",
|
||||||
|
) catch |err| switch (err) {
|
||||||
|
error.EnvironmentVariableNotFound => {
|
||||||
|
if (builtin.target.isDarwin()) return .metal;
|
||||||
|
if (builtin.target.os.tag == .windows) return .d3d12;
|
||||||
|
return .vulkan;
|
||||||
|
},
|
||||||
|
else => return err,
|
||||||
|
};
|
||||||
|
defer allocator.free(backend);
|
||||||
|
|
||||||
|
if (std.ascii.eqlIgnoreCase(backend, "null")) return .null;
|
||||||
|
if (std.ascii.eqlIgnoreCase(backend, "d3d11")) return .d3d11;
|
||||||
|
if (std.ascii.eqlIgnoreCase(backend, "d3d12")) return .d3d12;
|
||||||
|
if (std.ascii.eqlIgnoreCase(backend, "metal")) return .metal;
|
||||||
|
if (std.ascii.eqlIgnoreCase(backend, "vulkan")) return .vulkan;
|
||||||
|
if (std.ascii.eqlIgnoreCase(backend, "opengl")) return .opengl;
|
||||||
|
if (std.ascii.eqlIgnoreCase(backend, "opengles")) return .opengles;
|
||||||
|
|
||||||
|
@panic("unknown MACH_GPU_BACKEND type");
|
||||||
|
}
|
||||||
|
|
||||||
// Verifies that a platform implementation exposes the expected function declarations.
|
// Verifies that a platform implementation exposes the expected function declarations.
|
||||||
comptime {
|
comptime {
|
||||||
// Core
|
// Core
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ const std = @import("std");
|
||||||
const mach = @import("../main.zig");
|
const mach = @import("../main.zig");
|
||||||
const Core = @import("../Core.zig");
|
const Core = @import("../Core.zig");
|
||||||
const InputState = @import("InputState.zig");
|
const InputState = @import("InputState.zig");
|
||||||
const unicode = @import("unicode.zig");
|
|
||||||
const detectBackendType = @import("common.zig").detectBackendType;
|
|
||||||
const gpu = mach.gpu;
|
const gpu = mach.gpu;
|
||||||
const InitOptions = Core.InitOptions;
|
const InitOptions = Core.InitOptions;
|
||||||
const Event = Core.Event;
|
const Event = Core.Event;
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,6 @@ const std = @import("std");
|
||||||
const mach = @import("../main.zig");
|
const mach = @import("../main.zig");
|
||||||
const Core = @import("../Core.zig");
|
const Core = @import("../Core.zig");
|
||||||
const InputState = @import("InputState.zig");
|
const InputState = @import("InputState.zig");
|
||||||
const unicode = @import("unicode.zig");
|
|
||||||
const detectBackendType = @import("common.zig").detectBackendType;
|
|
||||||
const gpu = mach.gpu;
|
const gpu = mach.gpu;
|
||||||
const InitOptions = Core.InitOptions;
|
const InitOptions = Core.InitOptions;
|
||||||
const Event = Core.Event;
|
const Event = Core.Event;
|
||||||
|
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
const std = @import("std");
|
|
||||||
const builtin = @import("builtin");
|
|
||||||
const mach = @import("../main.zig");
|
|
||||||
const gpu = mach.gpu;
|
|
||||||
|
|
||||||
pub fn detectBackendType(allocator: std.mem.Allocator) !gpu.BackendType {
|
|
||||||
const backend = std.process.getEnvVarOwned(
|
|
||||||
allocator,
|
|
||||||
"MACH_GPU_BACKEND",
|
|
||||||
) catch |err| switch (err) {
|
|
||||||
error.EnvironmentVariableNotFound => {
|
|
||||||
if (builtin.target.isDarwin()) return .metal;
|
|
||||||
if (builtin.target.os.tag == .windows) return .d3d12;
|
|
||||||
return .vulkan;
|
|
||||||
},
|
|
||||||
else => return err,
|
|
||||||
};
|
|
||||||
defer allocator.free(backend);
|
|
||||||
|
|
||||||
if (std.ascii.eqlIgnoreCase(backend, "null")) return .null;
|
|
||||||
if (std.ascii.eqlIgnoreCase(backend, "d3d11")) return .d3d11;
|
|
||||||
if (std.ascii.eqlIgnoreCase(backend, "d3d12")) return .d3d12;
|
|
||||||
if (std.ascii.eqlIgnoreCase(backend, "metal")) return .metal;
|
|
||||||
if (std.ascii.eqlIgnoreCase(backend, "vulkan")) return .vulkan;
|
|
||||||
if (std.ascii.eqlIgnoreCase(backend, "opengl")) return .opengl;
|
|
||||||
if (std.ascii.eqlIgnoreCase(backend, "opengles")) return .opengles;
|
|
||||||
|
|
||||||
@panic("unknown MACH_GPU_BACKEND type");
|
|
||||||
}
|
|
||||||
|
|
@ -3,7 +3,6 @@ const w = @import("../win32.zig");
|
||||||
const mach = @import("../main.zig");
|
const mach = @import("../main.zig");
|
||||||
const Core = @import("../Core.zig");
|
const Core = @import("../Core.zig");
|
||||||
const InputState = @import("InputState.zig");
|
const InputState = @import("InputState.zig");
|
||||||
const unicode = @import("unicode.zig");
|
|
||||||
|
|
||||||
const gpu = mach.gpu;
|
const gpu = mach.gpu;
|
||||||
const InitOptions = Core.InitOptions;
|
const InitOptions = Core.InitOptions;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue