render-zig

A 3D rendering engine written in Zig
git clone git://git.christianermann.dev/render-zig
Log | Files | Refs

input.zig (1265B)


      1 const std = @import("std");
      2 const glfw = @import("mach_glfw");
      3 
      4 pub const UserInput = struct {
      5     ptr: *anyopaque,
      6     keyCallbackFn: KeyCallbackFn,
      7 
      8     pub fn keyCallback(self: UserInput, args: KeyCallbackArgs) void {
      9         return self.keyCallbackFn(self.ptr, args);
     10     }
     11 };
     12 
     13 pub const KeyCallbackArgs = struct {
     14     window: glfw.Window,
     15     key: glfw.Key,
     16     scancode: i32,
     17     action: glfw.Action,
     18     mods: glfw.Mods,
     19 };
     20 
     21 const KeyCallbackFn = *const fn (
     22     ptr: *anyopaque,
     23     args: KeyCallbackArgs,
     24 ) void;
     25 
     26 pub const WASDInput = struct {
     27     w: bool = false,
     28     a: bool = false,
     29     s: bool = false,
     30     d: bool = false,
     31 
     32     fn keyCallback(ptr: *anyopaque, args: KeyCallbackArgs) void {
     33         const self: *WASDInput = @ptrCast(@alignCast(ptr));
     34         if ((args.action != glfw.Action.press) and (args.action != glfw.Action.release)) {
     35             return;
     36         }
     37         switch (args.key) {
     38             .w => self.w = !self.w,
     39             .a => self.a = !self.a,
     40             .s => self.s = !self.s,
     41             .d => self.d = !self.d,
     42             else => {},
     43         }
     44     }
     45 
     46     pub fn userInput(self: *WASDInput) UserInput {
     47         return .{
     48             .ptr = self,
     49             .keyCallbackFn = WASDInput.keyCallback,
     50         };
     51     }
     52 };