Skip to content

Commit

Permalink
examples/snake can get key presses
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Berg committed Jan 22, 2025
1 parent bd03960 commit a37e984
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions examples/snake/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const GRID_SIZE = 32;

const square_shader = @embedFile("shaders/square.wgsl");


var previous_key_state = std.EnumMap(glfw.Key, glfw.KeyState).initFull(glfw.KeyState.Released);

pub fn main() !void {

try glfw.init();
Expand Down Expand Up @@ -191,16 +194,27 @@ pub fn main() !void {


var i: usize = 0;
while (!window.ShouldClose()) : (i += 1) {
while (!window.ShouldClose()) {
glfw.pollEvents();

{ // update

if (i == snake.len) i = 0;
if (i == snake.len) {
i = 0; // wouldnt wanna overflow right
@memset(&snake, 0);
}
snake[i] = @intFromBool(true);

queue.WriteBuffer(snake_buffer, 0, u32, snake[0..]);
const just_pressed = glfw.GetKey(window, .D);

if (just_pressed == .Pressed and previous_key_state.get(.D).? != .Pressed) {
i += 1;
}

const prev = previous_key_state.getPtr(.D).?;
prev.* = just_pressed;

queue.WriteBuffer(snake_buffer, 0, u32, snake[0..]);
}

{ // Render
Expand Down

0 comments on commit a37e984

Please sign in to comment.