890d7844c5bdbbea6785da24d0a315a13afed92b — Noah Graff 3 months ago 55fde12
Added pause state

This is essentially all I wanted to do for this project, I might come
in and clean all the code, but I think it is kinda readable, and the
functionality is basically what I want it to be.
3 files changed, 31 insertions(+), 5 deletions(-)

M build.zig
M src/game_state.zig
M src/main.zig
M build.zig => build.zig +2 -0
@@ 2,8 2,10 @@   pub fn build(b: *Builder) void {
      const mode = b.standardReleaseOptions();
+     const target = b.standardTargetOptions(null);
      const exe = b.addExecutable("snake", "src/main.zig");
      exe.setBuildMode(mode);
+     exe.setTheTarget(target);
  
      exe.addPackagePath("clap", "vendor/clap/clap.zig");
  

M src/game_state.zig => src/game_state.zig +29 -4
@@ 1,5 1,4 @@ const std = @import("std");
- const pi = std.math.pi;
  const panic = std.debug.panic;
  const root = @import("root");
  const c = @import("c.zig");


@@ 18,7 17,7 @@ pub const State = union(enum) {
      Playing: PlayingState,
      MainMenu: MainMenuState,
-     // Paused: PausedState,
+     Paused: PausedState,
      GameOver: GameOverState,
  
      fn handleKey(state: *State, ctx: *Context, key: Key) ?State {


@@ 29,7 28,7 @@ .Down => s.snake.next_move = .Down,
                      .Left => s.snake.next_move = .Left,
                      .Right => s.snake.next_move = .Right,
-                     .Escape => c.glfwSetWindowShouldClose(ctx.window, c.GL_TRUE),
+                     .Escape => return State{ .Paused = PausedState{ .playing_state = PlayingState.copy(s) } },
                      else => {},
                  }
                  return null;


@@ 47,6 46,11 @@ .Escape => return State{ .MainMenu = MainMenuState{ .time = 0.0 } },
                  else => return null,
              },
+             .Paused => |*s| switch (key) {
+                 .Escape => return State{ .Playing = s.playing_state },
+                 .Space => return State{ .MainMenu = MainMenuState{ .time = 0.0 } },
+                 else => return null,
+             },
          }
      }
  


@@ 61,7 65,7 @@ switch (state.*) {
              .Playing => |*s| s.draw(ctx),
              .MainMenu => |*s| s.draw(ctx),
-             // .Paused => |*s| s.draw(ctx),
+             .Paused => |*s| s.draw(ctx),
              .GameOver => |*s| s.draw(ctx),
          }
      }


@@ 107,6 111,16 @@ };
      }
  
+     // should realloc snake, but not needed for what we are using this for
+     pub fn copy(state: *PlayingState) PlayingState {
+         return PlayingState {
+             .snake = state.snake,
+             .apple_pos = state.apple_pos,
+             .tick_rate = state.tick_rate,
+             .until_tick = state.until_tick,
+         };
+     }
+ 
      pub fn update(self: *PlayingState, ctx: *Context) !?State {
          self.until_tick -= ctx.dt;
          if (self.until_tick <= 0.0) {


@@ 172,6 186,17 @@ }
  };
  
+ pub const PausedState = struct {
+     playing_state: PlayingState,
+ 
+     pub fn draw(state: *PausedState, ctx: *Context) void {
+         state.playing_state.draw(ctx);
+         graphics.drawCenteredText(&ctx.gfx, "Paused", -1, 6.0);
+         graphics.drawCenteredText(&ctx.gfx, "Press escape to resume", 1, 4.0);
+         graphics.drawCenteredText(&ctx.gfx, "Press space to go to the main menu", 2, 4.0);
+     }
+ };
+ 
  pub const SpriteIdx = enum(u4) {
      CornerUR = 0,
      CornerRD = 1,

M src/main.zig => src/main.zig +0 -1
@@ 127,7 127,6 @@ pressed_keys.shrink(0);
  
          if (try game_state.update(&ctx)) |state| {
-             std.debug.warn("moving to state {}\n", state);
              game_state = state;
          }
          game_state.draw(&ctx);