From e370e2bb666e04774f99b940b27a34bae691694a Mon Sep 17 00:00:00 2001 From: Dendy Faist Date: Sat, 30 Sep 2023 18:49:14 +0200 Subject: [PATCH] Implement getting all the input names --- src/input.zig | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.zig | 5 +++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/input.zig b/src/input.zig index 4490a6b..f204b8d 100644 --- a/src/input.zig +++ b/src/input.zig @@ -1,4 +1,7 @@ +const Self = @This(); + const rl = @import("raylib.zig"); +const std = @import("std"); pub const Input = struct { 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 { return Input{ .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_rotate_r = init("Rotate Piece Clockwise", "RIGHT"); 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; + } +} diff --git a/src/main.zig b/src/main.zig index b285b4b..151cd0e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -8,15 +8,16 @@ const MainMenu = @import("MainMenu.zig"); const config = @import("Config/config.zig"); const State = @import("flow.zig").State; +const input = @import("input.zig"); pub fn main() !void { - var configIterator = config.getConfigIterator("test") catch unreachable; 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(); + input.loadConfig(); var renderer = try Renderer.init(); defer renderer.deinit();