Implement getting all the input names

This commit is contained in:
Dendy 2023-09-30 18:49:14 +02:00
parent 2b4dbc2875
commit e370e2bb66
2 changed files with 49 additions and 2 deletions

View File

@ -1,4 +1,7 @@
const Self = @This();
const rl = @import("raylib.zig"); const rl = @import("raylib.zig");
const std = @import("std");
pub const Input = struct { pub const Input = struct {
name: []const u8, name: []const u8,
@ -16,6 +19,12 @@ pub const Input = struct {
} }
}; };
pub fn loadConfig() void {
for (comptime getInputNames()) |name| {
std.debug.print("{s}\n", .{name});
}
}
fn init(comptime name: []const u8, comptime key_code: []const u8) Input { fn init(comptime name: []const u8, comptime key_code: []const u8) Input {
return Input{ return Input{
.name = name, .name = name,
@ -38,3 +47,40 @@ pub var game_drop = init("Drop Piece", "W");
pub var game_swap_piece = init("Swap Piece", "SPACE"); pub var game_swap_piece = init("Swap Piece", "SPACE");
pub var game_rotate_r = init("Rotate Piece Clockwise", "RIGHT"); pub var game_rotate_r = init("Rotate Piece Clockwise", "RIGHT");
pub var game_rotate_l = init("Rotate Piece Counterclockwise", "LEFT"); pub var game_rotate_l = init("Rotate Piece Counterclockwise", "LEFT");
// Get the names of the Input(s) defined in this file with introspection
fn getInputNames() [getInputCount()][]const u8 {
comptime {
const decls = std.meta.declarations(Self);
var ret: [getInputCount()][]const u8 = undefined;
var i: usize = 0;
inline for (decls) |i_decl| {
const field = @field(Self, i_decl.name);
if (@TypeOf(field) == Input) {
ret[i] = i_decl.name;
i += 1;
}
}
return ret;
}
}
// With reflection count the amount of declarations of type "Input"
fn getInputCount() usize {
comptime {
const decls = std.meta.declarations(Self);
var i: usize = 0;
inline for (decls) |i_decl| {
const field = @field(Self, i_decl.name);
if (@TypeOf(field) == Input) i += 1;
}
return i;
}
}

View File

@ -8,15 +8,16 @@ const MainMenu = @import("MainMenu.zig");
const config = @import("Config/config.zig"); const config = @import("Config/config.zig");
const State = @import("flow.zig").State; const State = @import("flow.zig").State;
const input = @import("input.zig");
pub fn main() !void { pub fn main() !void {
var configIterator = config.getConfigIterator("test") catch unreachable; var configIterator = config.getConfigIterator("test") catch unreachable;
while (configIterator.next()) |option| { while (configIterator.next()) |option| {
std.debug.print("Key: {s}, Option: {s}\n", .{ option.key, option.value }); std.debug.print("Key: {s}, Option: {s}\n", .{ option.key, option.value });
} }
configIterator.deinit(); configIterator.deinit();
input.loadConfig();
var renderer = try Renderer.init(); var renderer = try Renderer.init();
defer renderer.deinit(); defer renderer.deinit();