SDL events input routing

This commit is contained in:
Peter Li 2025-04-14 16:09:50 -07:00
parent 9679514ff2
commit 86d56cf5e6
9 changed files with 569 additions and 150 deletions

View File

@ -32,6 +32,7 @@ pub fn getArgs() !NwArgs {
// to conditionally start up feature modules within the engine
var shutdownList: std.ArrayListUnmanaged(*const fn (std.mem.Allocator) void) = .{};
var shutdownModuleNames: std.ArrayListUnmanaged([]const u8) = .{};
pub fn start_modules(comptime programSpec: anytype, maybeArgs: ?NwArgs, allocator: std.mem.Allocator) !void {
const Backlog = @This();
@ -46,6 +47,7 @@ pub fn start_modules(comptime programSpec: anytype, maybeArgs: ?NwArgs, allocato
try Struct.start_module(programSpec, NwArgs{}, allocator);
}
try shutdownList.append(allocator, Struct.shutdown_module);
try shutdownModuleNames.append(allocator, feature);
core.engine_logs("module started >>>> " ++ feature ++ " <<<<");
}
}
@ -55,9 +57,11 @@ pub fn start_modules(comptime programSpec: anytype, maybeArgs: ?NwArgs, allocato
pub fn shutdown_modules(allocator: std.mem.Allocator) void {
var i: isize = @intCast(shutdownList.items.len - 1);
while (i >= 0) : (i -= 1) {
core.engine_log("module shutting down >>>> {s} <<<<", .{shutdownModuleNames.items[@intCast(i)]});
shutdownList.items[@intCast(i)](allocator);
}
shutdownList.deinit(allocator);
shutdownModuleNames.deinit(allocator);
}
pub fn start_everything(comptime spec: anytype, allocator: std.mem.Allocator, maybeArgs: ?NwArgs) !void {

View File

@ -139,13 +139,10 @@ pub fn start_module(comptime programSpec: anytype, args: anytype, allocator: std
_ = try inputs.initInputStack();
logs("core module starting up... ");
return;
}
pub fn shutdown_module(_: std.mem.Allocator) void {
logs("core module shutting down...");
MemoryTracker.MTPrintStatsDelta();
logging.shutdownLogging();

View File

@ -6,12 +6,45 @@ pub const BindingType = enum(u8) {
const glfw_release = 0;
pub fn convertEvent(event: *const sdl3.Event) ?IOEvent {
switch (event.type) {
sdl3.events.key_down, sdl3.events.key_up => {
const scan: Key = @enumFromInt(event.key.scancode);
return IOEvent{
.key = .{
.key = scan,
.scancode = scan,
.action = if (event.key.down) .keyDown else .keyUp,
.mods = 0,
},
};
},
sdl3.events.mouse_button_down, sdl3.events.mouse_button_up => {
const scan: Key = @enumFromInt(@as(u32, @intCast(event.button.button)) + 10000);
return IOEvent{
.key = .{
.key = scan,
.scancode = scan,
.action = if (event.key.down) .keyDown else .keyUp,
.mods = 0,
},
};
},
else => {
return null;
},
}
return null;
}
pub const IOEvent = union(enum(u8)) {
windowFocused: struct { focused: c_int },
mousePosition: struct { x: f64, y: f64 },
mouseButton: struct { button: c_int, action: c_int, mods: c_int },
scroll: struct { xoffset: f64, yoffset: f64 },
key: struct { key: c_int, scancode: c_int, action: c_int, mods: c_int },
key: struct { key: Key, scancode: Key, action: ActionEvent, mods: c_int },
windowResize: struct { newSize: core.Vector2f },
codepoint: c_uint,
};
@ -562,15 +595,9 @@ pub const InputStack = struct {
pub fn routeEvent(self: *@This(), eventToRoute: IOEvent) void {
switch (eventToRoute) {
.key => |key| {
const keyEvent: ActionEvent = @enumFromInt(@as(u8, @intCast(key.action)));
const keyEvent: ActionEvent = key.action; // @enumFromInt(@as(u8, @intCast(key.action)));
if (keyEvent != .keyHeld) {
self.routeKeyEvent(@enumFromInt(key.key), keyEvent);
}
},
.mouseButton => |button| {
const keyEvent: ActionEvent = @enumFromInt(@as(u8, @intCast(button.action)));
if (keyEvent != .keyHeld) {
self.routeKeyEvent(@enumFromInt(button.button + 10001), keyEvent);
self.routeKeyEvent(key.key, keyEvent);
}
},
else => {},
@ -614,6 +641,7 @@ pub fn initInputStack() !void {
const lua = core.lua;
const pod = lua.pod;
const Key = @import("keys.zig").Key;
pub const Key = @import("keys.zig").Key;
const sdl3 = @import("sdl3");
const std = @import("std");
const core = @import("../core.zig");

View File

@ -1,128 +1,509 @@
pub const Key = enum(i32) {
Unknown = -1,
Space = 32,
Apostrophe = 39,
Comma = 44,
Period = 45,
Slash = 47,
@"0" = 48,
@"1" = 49,
@"2" = 50,
@"3" = 51,
@"4" = 52,
@"5" = 53,
@"6" = 54,
@"7" = 55,
@"8" = 56,
@"9" = 57,
Semicolon = 59,
Equal = 61,
A = 65,
B = 66,
C = 67,
D = 68,
E = 69,
F = 70,
G = 71,
H = 72,
I = 73,
J = 74,
K = 75,
L = 76,
M = 77,
N = 78,
O = 79,
P = 80,
Q = 81,
R = 82,
S = 83,
T = 84,
U = 85,
V = 86,
W = 87,
X = 88,
Y = 89,
Z = 90,
LeftBrackSq = 91,
BackSlash = 92,
RightBrackSq = 93,
Grave = 96,
World1 = 161,
World2 = 162,
Escape = 256,
Enter = 257,
Tab = 258,
Backspace = 259,
Insert = 260,
Delete = 261,
Right = 262,
Left = 263,
Down = 264,
Up = 265,
PageUp = 266,
PageDown = 267,
Home = 268,
End = 269,
CapsLock = 280,
ScrollLock = 281,
NumLock = 282,
PrintScreen = 283,
Pause = 284,
F1 = 290,
F2 = 291,
F3 = 292,
F4 = 293,
F5 = 294,
F6 = 295,
F7 = 296,
F8 = 297,
F9 = 298,
F10 = 299,
F11 = 300,
F12 = 301,
F13 = 302,
F14 = 303,
F15 = 304,
F16 = 305,
F17 = 306,
F18 = 307,
F19 = 308,
F20 = 309,
F21 = 310,
F22 = 311,
F23 = 312,
F24 = 313,
F25 = 314,
Kp0 = 320,
Kp1 = 321,
Kp2 = 322,
Kp3 = 323,
Kp4 = 324,
Kp5 = 325,
Kp6 = 326,
Kp7 = 327,
Kp8 = 328,
Kp9 = 329,
KpDecimal = 330,
KpDivide = 331,
KpMultiply = 332,
KpSubtract = 333,
KpAdd = 334,
KpEnter = 335,
KpEqual = 336,
LeftShift = 340,
LeftControl = 341,
LeftAlt = 342,
LeftSuper = 343,
RightShift = 344,
RightControl = 345,
RightAlt = 346,
RightSuper = 347,
Menu = 348,
Mouse1 = 10001,
Mouse2 = 10002,
Mouse3 = 10003,
Mouse4 = 10004,
Mouse5 = 10005,
_,
pub const sdl3 = @import("sdl3");
pub const Key = enum(c_uint) {
unknown,
//**
//* \name Usage page 0x07
//*
//* These values are from usage page 0x07 (USB keyboard page).
//*/
//* @{ */
a = 4,
b = 5,
c = 6,
d = 7,
e = 8,
f = 9,
g = 10,
h = 11,
i = 12,
j = 13,
k = 14,
l = 15,
m = 16,
n = 17,
o = 18,
p = 19,
q = 20,
r = 21,
s = 22,
t = 23,
u = 24,
v = 25,
w = 26,
x = 27,
y = 28,
z = 29,
@"1" = 30,
@"2" = 31,
@"3" = 32,
@"4" = 33,
@"5" = 34,
@"6" = 35,
@"7" = 36,
@"8" = 37,
@"9" = 38,
@"0" = 39,
@"return" = 40,
escape = 41,
backspace = 42,
tab = 43,
space = 44,
minus = 45,
equals = 46,
leftbracket = 47,
rightbracket = 48,
backslash = 49, //**< Located at the lower left of the return
// * key on ISO keyboards and at the right end
// * of the QWERTY row on ANSI keyboards.
// * Produces REVERSE SOLIDUS (backslash) and
// * VERTICAL LINE in a US layout, REVERSE
// * SOLIDUS and VERTICAL LINE in a UK Mac
// * layout, NUMBER SIGN and TILDE in a UK
// * Windows layout, DOLLAR SIGN and POUND SIGN
// * in a Swiss German layout, NUMBER SIGN and
// * APOSTROPHE in a German layout, GRAVE
// * ACCENT and POUND SIGN in a French Mac
// * layout, and ASTERISK and MICRO SIGN in a
// * French Windows layout.
// */
nonushash = 50, //**< iso usb keyboards actually use this code
//* instead of 49 for the same key, but all
//* OSes I've seen treat the two codes
//* identically. So, as an implementor, unless
//* your keyboard generates both of those
//* codes and your OS treats them differently,
//* you should generate SDL_SCANCODE_BACKSLASH
//* instead of this code. As a user, you
//* should not rely on this code because SDL
//* will never generate it with most (all?)
//* keyboards.
//*/
semicolon = 51,
apostrophe = 52,
grave = 53, //**< located in the top left corner (on both ansi
//* and ISO keyboards). Produces GRAVE ACCENT and
//* TILDE in a US Windows layout and in US and UK
//* Mac layouts on ANSI keyboards, GRAVE ACCENT
//* and NOT SIGN in a UK Windows layout, SECTION
//* SIGN and PLUS-MINUS SIGN in US and UK Mac
//* layouts on ISO keyboards, SECTION SIGN and
//* DEGREE SIGN in a Swiss German layout (Mac:
//* only on ISO keyboards), CIRCUMFLEX ACCENT and
//* DEGREE SIGN in a German layout (Mac: only on
//* ISO keyboards), SUPERSCRIPT TWO and TILDE in a
//* French Windows layout, COMMERCIAL AT and
//* NUMBER SIGN in a French Mac layout on ISO
//* keyboards, and LESS-THAN SIGN and GREATER-THAN
//* SIGN in a Swiss German, German, or French Mac
//* layout on ANSI keyboards.
//*/
comma = 54,
period = 55,
slash = 56,
capslock = 57,
f1 = 58,
f2 = 59,
f3 = 60,
f4 = 61,
f5 = 62,
f6 = 63,
f7 = 64,
f8 = 65,
f9 = 66,
f10 = 67,
f11 = 68,
f12 = 69,
printscreen = 70,
scrolllock = 71,
pause = 72,
insert = 73, //**< insert on pc, help on some mac keyboards (but
// does send code 73, not 117) */
home = 74,
pageup = 75,
delete = 76,
end = 77,
pagedown = 78,
right = 79,
left = 80,
down = 81,
up = 82,
numlockclear = 83, //**< num lock on pc, clear on mac keyboards
//*/
kp_divide = 84,
kp_multiply = 85,
kp_minus = 86,
kp_plus = 87,
kp_enter = 88,
kp_1 = 89,
kp_2 = 90,
kp_3 = 91,
kp_4 = 92,
kp_5 = 93,
kp_6 = 94,
kp_7 = 95,
kp_8 = 96,
kp_9 = 97,
kp_0 = 98,
kp_period = 99,
nonusbackslash = 100, //**< this is the additional key that iso
//* keyboards have over ANSI ones,
//* located between left shift and Y.
//* Produces GRAVE ACCENT and TILDE in a
//* US or UK Mac layout, REVERSE SOLIDUS
//* (backslash) and VERTICAL LINE in a
//* US or UK Windows layout, and
//* LESS-THAN SIGN and GREATER-THAN SIGN
//* in a Swiss German, German, or French
//* layout. */
application = 101, //**< windows contextual menu, compose */
power = 102, //**< the usb document says this is a status flag,
// * not a physical key - but some Mac keyboards
// * do have a power key. */
kp_equals = 103,
f13 = 104,
f14 = 105,
f15 = 106,
f16 = 107,
f17 = 108,
f18 = 109,
f19 = 110,
f20 = 111,
f21 = 112,
f22 = 113,
f23 = 114,
f24 = 115,
execute = 116,
help = 117, //**< al integrated help center */
menu = 118, //**< menu (show menu) */
select = 119,
stop = 120, //**< ac stop */
again = 121, //**< ac redo/repeat */
undo = 122, //**< ac undo */
cut = 123, //**< ac cut */
copy = 124, //**< ac copy */
paste = 125, //**< ac paste */
find = 126, //**< ac find */
mute = 127,
volumeup = 128,
volumedown = 129,
///* not sure whether there's a reason to enable these */
///* sdl_scancode_lockingcapslock = 130, */
///* sdl_scancode_lockingnumlock = 131, */
///* sdl_scancode_lockingscrolllock = 132, */
kp_comma = 133,
kp_equalsas400 = 134,
international1 = 135, // **< used on asian keyboards, see
// footnotes in usb doc */
international2 = 136,
international3 = 137, //**< yen */
international4 = 138,
international5 = 139,
international6 = 140,
international7 = 141,
international8 = 142,
international9 = 143,
lang1 = 144, //**< hangul/english toggle */
lang2 = 145, //**< hanja conversion */
lang3 = 146, //**< katakana */
lang4 = 147, //**< hiragana */
lang5 = 148, //**< zenkaku/hankaku */
lang6 = 149, //**< reserved */
lang7 = 150, //**< reserved */
lang8 = 151, //**< reserved */
lang9 = 152, //**< reserved */
alterase = 153, //**< erase-eaze */
sysreq = 154,
cancel = 155, //**< ac cancel */
clear = 156,
prior = 157,
return2 = 158,
separator = 159,
out = 160,
oper = 161,
clearagain = 162,
crsel = 163,
exsel = 164,
kp_00 = 176,
kp_000 = 177,
thousandsseparator = 178,
decimalseparator = 179,
currencyunit = 180,
currencysubunit = 181,
kp_leftparen = 182,
kp_rightparen = 183,
kp_leftbrace = 184,
kp_rightbrace = 185,
kp_tab = 186,
kp_backspace = 187,
kp_a = 188,
kp_b = 189,
kp_c = 190,
kp_d = 191,
kp_e = 192,
kp_f = 193,
kp_xor = 194,
kp_power = 195,
kp_percent = 196,
kp_less = 197,
kp_greater = 198,
kp_ampersand = 199,
kp_dblampersand = 200,
kp_verticalbar = 201,
kp_dblverticalbar = 202,
kp_colon = 203,
kp_hash = 204,
kp_space = 205,
kp_at = 206,
kp_exclam = 207,
kp_memstore = 208,
kp_memrecall = 209,
kp_memclear = 210,
kp_memadd = 211,
kp_memsubtract = 212,
kp_memmultiply = 213,
kp_memdivide = 214,
kp_plusminus = 215,
kp_clear = 216,
kp_clearentry = 217,
kp_binary = 218,
kp_octal = 219,
kp_decimal = 220,
kp_hexadecimal = 221,
lctrl = 224,
lshift = 225,
lalt = 226, //**< alt, option */
lgui = 227, //**< windows, command (apple), meta */
rctrl = 228,
rshift = 229,
ralt = 230, //**< alt gr, option */
rgui = 231, //**< windows, command (apple), meta */
mode = 257, //**< i'm not sure if this is really not covered
//* by any of the above, but since there's a
//* special sdl_kmod_mode for it i'm adding it here
//*/
//* @} *//* Usage page 0x07 */
///**
// * \name Usage page 0x0C
// *
// * These values are mapped from usage page 0x0C (USB consumer page).
// *
// * There are way more keys in the spec than we can represent in the
// * current scancode range, so pick the ones that commonly come up in
// * real world usage.
// */
///* @{ */
sleep = 258, //**< sleep */
wake = 259, //**< wake */
channel_increment = 260, //**< channel increment */
channel_decrement = 261, //**< channel decrement */
media_play = 262, //**< play */
media_pause = 263, //**< pause */
media_record = 264, //**< record */
media_fast_forward = 265, //**< fast forward */
media_rewind = 266, //**< rewind */
media_next_track = 267, //**< next track */
media_previous_track = 268, //**< previous track */
media_stop = 269, //**< stop */
media_eject = 270, //**< eject */
media_play_pause = 271, //**< play / pause */
media_select = 272, //* media select */
ac_new = 273, //**< ac new */
ac_open = 274, //**< ac open */
ac_close = 275, //**< ac close */
ac_exit = 276, //**< ac exit */
ac_save = 277, //**< ac save */
ac_print = 278, //**< ac print */
ac_properties = 279, //**< ac properties */
ac_search = 280, //**< ac search */
ac_home = 281, //**< ac home */
ac_back = 282, //**< ac back */
ac_forward = 283, //**< ac forward */
ac_stop = 284, //**< ac stop */
ac_refresh = 285, //**< ac refresh */
ac_bookmarks = 286, //**< ac bookmarks */
//* @} *//* Usage page 0x0C */
//**
//* \name Mobile keys
//*
//* These are values that are often used on mobile phones.
//*/
//* @{ */
softleft = 287, //**< usually situated below the display on phones and
// used as a multi-function feature key for selecting
// a software defined function shown on the bottom left
// of the display. */
softright = 288, //**< usually situated below the display on phones and
// used as a multi-function feature key for selecting
// a software defined function shown on the bottom right
// of the display. */
call = 289, //**< used for accepting phone calls. */
endcall = 290, //**< used for rejecting phone calls. */
//* @} *//* Mobile keys */
//* Add any other keys here. */
reserved = 400, //**< 400-500 reserved for dynamic keycodes */
count = 512, //**< not a key, just marks the number of scancodes for array bounds */
// BacklogEngine extensions.. will treat mouse button events as a key much like a scancode.
Mouse1 = 10001, // Left
Mouse2 = 10002, // Middle
Mouse3 = 10003, // Right
Mouse4 = 10004, // Mouse 4
Mouse5 = 10005, // Mouse 5
};
// pub const Key = enum(i32) {
// Unknown = -1,
// Space = 32,
// Apostrophe = 39,
// Comma = 44,
// Period = 45,
// Slash = 47,
// @"0" = 48,
// @"1" = 49,
// @"2" = 50,
// @"3" = 51,
// @"4" = 52,
// @"5" = 53,
// @"6" = 54,
// @"7" = 55,
// @"8" = 56,
// @"9" = 57,
// Semicolon = 59,
// Equal = 61,
// A = 65,
// B = 66,
// C = 67,
// D = 68,
// E = 69,
// F = 70,
// G = 71,
// H = 72,
// I = 73,
// J = 74,
// K = 75,
// L = 76,
// M = 77,
// N = 78,
// O = 79,
// P = 80,
// Q = 81,
// R = 82,
// S = 83,
// T = 84,
// U = 85,
// V = 86,
// W = 87,
// X = 88,
// Y = 89,
// Z = 90,
// LeftBrackSq = 91,
// BackSlash = 92,
// RightBrackSq = 93,
// Grave = 96,
// World1 = 161,
// World2 = 162,
// Escape = 256,
// Enter = 257,
// Tab = 258,
// Backspace = 259,
// Insert = 260,
// Delete = 261,
// Right = 262,
// Left = 263,
// Down = 264,
// Up = 265,
// PageUp = 266,
// PageDown = 267,
// Home = 268,
// End = 269,
// CapsLock = 280,
// ScrollLock = 281,
// NumLock = 282,
// PrintScreen = 283,
// Pause = 284,
// F1 = 290,
// F2 = 291,
// F3 = 292,
// F4 = 293,
// F5 = 294,
// F6 = 295,
// F7 = 296,
// F8 = 297,
// F9 = 298,
// F10 = 299,
// F11 = 300,
// F12 = 301,
// F13 = 302,
// F14 = 303,
// F15 = 304,
// F16 = 305,
// F17 = 306,
// F18 = 307,
// F19 = 308,
// F20 = 309,
// F21 = 310,
// F22 = 311,
// F23 = 312,
// F24 = 313,
// F25 = 314,
// Kp0 = 320,
// Kp1 = 321,
// Kp2 = 322,
// Kp3 = 323,
// Kp4 = 324,
// Kp5 = 325,
// Kp6 = 326,
// Kp7 = 327,
// Kp8 = 328,
// Kp9 = 329,
// KpDecimal = 330,
// KpDivide = 331,
// KpMultiply = 332,
// KpSubtract = 333,
// KpAdd = 334,
// KpEnter = 335,
// KpEqual = 336,
// LeftShift = 340,
// LeftControl = 341,
// LeftAlt = 342,
// LeftSuper = 343,
// RightShift = 344,
// RightControl = 345,
// RightAlt = 346,
// RightSuper = 347,
// Menu = 348,
// Mouse1 = 10001,
// Mouse2 = 10002,
// Mouse3 = 10003,
// Mouse4 = 10004,
// Mouse5 = 10005,
// _,
// };

View File

@ -22,7 +22,6 @@ pub fn setWindowSettings(params: windowing.PlatformParams) void {
pub fn start_module(comptime programSpec: anytype, args: anytype, allocator: std.mem.Allocator) !void {
_ = args;
_ = programSpec;
core.engine_log("shader compile test sizeof Scene = {d}", .{@sizeOf(test_vert.Scene)});
if (core.isUtility()) {
return;
}
@ -42,7 +41,6 @@ pub fn shutdown_module(allocator: std.mem.Allocator) void {
return;
}
core.engine_logs("Shutting down platform");
gPlatformInstance.deinit();
// I have a bug somewhere. need to find out where it is

View File

@ -119,8 +119,11 @@ pub const PlatformInstance = struct {
core.setupEnginePlatform(self, setupWindow, enginePoll, processEvents);
}
// runs pinned on io thread.
// old glfw needed to do it's polling here.
pub fn enginePoll(opaqueSelf: *anyopaque) core.EngineDataEventError!void {
_ = opaqueSelf;
// this polling event should probably be used for IO, now that we have a free thread for doing w.e
// var self: *@This() = @alignCast(@ptrCast(opaqueSelf));
// self.pollEvents();
}
@ -129,11 +132,6 @@ pub const PlatformInstance = struct {
try self.listeners.append(RawInputObjectRef.from(listener));
}
fn convertEvent(event: *const sdl3.Event) ?IOEvent {
_ = event;
return null;
}
pub fn processEvents(ptr: *anyopaque, frameNumber: u64) core.EngineDataEventError!void {
_ = frameNumber;
@ -145,7 +143,7 @@ pub const PlatformInstance = struct {
const inputStack = core.getInputStack();
inputStack.updatePreviousInputs();
while (sdl3.pollEvent()) |event| {
if (convertEvent(event)) |converted| {
if (core.inputs.convertEvent(event)) |converted| {
inputStack.routeEvent(converted);
}

View File

2
lib/tracy/build.zig vendored
View File

@ -9,7 +9,7 @@ pub fn build(b: *std.Build) void {
//const tracy_enabled = b.option(bool, "tracy", "Enables tracy integration") orelse false;
const tracy_enabled: bool = true; //if (b.graph.env_map.hash_map.get("WITH_TRACY") != null) true else false;
const tracy_enabled: bool = false; //if (b.graph.env_map.hash_map.get("WITH_TRACY") != null) true else false;
// if (b.graph.env_map.hash_map.get("WITH_TRACY")) |with_tracy| {
// tracy_enabled = with_tracy;
// }

View File

@ -15,10 +15,23 @@ pub fn prepare_game(self: *@This()) !void {
try core.fs().addContentPath("sampleGame");
try script.loadTypes("scripts");
try script.runScriptFile("scripts/prepare.lua");
const exitInput = try core.ActionBinding.create(core.MakeName("exit"));
exitInput.addKey(.escape, .keyDown);
_ = exitInput.data.addListener(null, onExit);
exitInput.activate();
}
pub fn onExit(ctx: ?*anyopaque, action: core.ActionEvent) void {
_ = ctx;
_ = action;
core.exitNow();
}
pub fn tick(self: *@This(), _: f64) void {
_ = self;
std.time.sleep(6000 * 1000); // just gonna put this here...
}
pub fn deinit(self: *@This()) void {