From 276016cdf76694fbf7a97bebcba5c6c5c15a4183 Mon Sep 17 00:00:00 2001 From: Strangedusk Date: Wed, 13 Jul 2022 22:59:54 +0200 Subject: [PATCH] Main Menu Very WIP --- src/Game.zig | 7 ++++++- src/MainMenu.zig | 44 +++++++++++++++++++++++++++++++++++-------- src/MenuSelection.zig | 6 +++--- src/flow.zig | 5 +++++ src/main.zig | 12 +++++++++--- 5 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 src/flow.zig diff --git a/src/Game.zig b/src/Game.zig index a8f47c4..45713c4 100644 --- a/src/Game.zig +++ b/src/Game.zig @@ -5,6 +5,7 @@ const Grid = @import("Grid.zig"); const Piece = @import("Piece.zig"); const Bag = @import("Bag.zig"); const Timer = @import("Timer.zig"); +const State = @import("flow.zig").State; const renderer = @import("renderer.zig"); const movement = @import("movement.zig"); @@ -32,6 +33,8 @@ grid_cell_size: i32, grid_pos_x: i32, grid_pos_y: i32, +state: State = State.game, + bag: Bag, held: ?Piece.Type, @@ -85,7 +88,7 @@ pub fn init(_renderer: *SDL.SDL_Renderer) Self { return ret; } -pub fn tick(self: *Self) void { +pub fn tick(self: *Self) State { // TIMERS if (self.piece.timer_dropped.finished()) { @@ -140,6 +143,8 @@ pub fn tick(self: *Self) void { self.grid.clearLines(); renderer.render(self.*); + + return self.state; } // ACTION CALLBACKS diff --git a/src/MainMenu.zig b/src/MainMenu.zig index bf724ab..f087d13 100644 --- a/src/MainMenu.zig +++ b/src/MainMenu.zig @@ -4,6 +4,8 @@ const SDL = @import("sdl2"); const MenuSelection = @import("MenuSelection.zig"); const MenuTab = @import("MenuTab.zig"); +const State = @import("flow.zig").State; + const Self = @This(); tab_list: [2]MenuTab = [2]MenuTab{ @@ -17,16 +19,30 @@ tab_list: [2]MenuTab = [2]MenuTab{ }), }, +// Current tab and selection index tab: usize = 0, -selection: usize = 0, +sel: usize = 0, + +state: State = State.main_menu, holding_down: bool = false, +holding_enter: bool = false, + +fn getSel(self: *Self) *MenuSelection { + return &self.tab_list[self.tab].contents[self.sel]; +} + +fn getTab(self: *Self) *MenuTab { + return &self.tab_list[self.tab]; +} pub fn init() Self { return Self{}; } -pub fn tick(self: *Self) void { +pub fn tick(self: *Self) State { + const sel = self.getSel(); + const tab = self.getTab(); var key_state = SDL.SDL_GetKeyboardState(null); if (key_state[SDL.SDL_SCANCODE_DOWN] == 1 and !self.holding_down) { self.holding_down = true; @@ -35,24 +51,36 @@ pub fn tick(self: *Self) void { if (key_state[SDL.SDL_SCANCODE_DOWN] == 0) { self.holding_down = false; } + if (key_state[SDL.SDL_SCANCODE_RETURN] == 1 and !self.holding_enter) { + self.holding_enter = true; + self.actionSelect(); + } + if (key_state[SDL.SDL_SCANCODE_RETURN] == 0) { + self.holding_enter = false; + } std.debug.print( \\Tab: {s} \\Selection: {s} \\ - , .{ self.tab_list[self.tab].name, self.tab_list[self.tab].contents[self.selection].name }); + , .{tab.*.name, sel.*.name}); + return self.state; } fn actionSelDown(self: *Self) void { - self.selection += 1; - self.selection %= self.tab_list[self.tab].contents.len; + self.sel += 1; + self.sel %= self.tab_list[self.tab].contents.len; +} + +fn actionSelect(self: *Self) void { + const action = self.getSel().*.action; + @call(.{}, action, .{self}); } fn play(self: *Self) void { - _ = self; - std.debug.print("Play", .{}); + self.state = State.game; } fn print(self: *Self) void { _ = self; - std.debug.print("Print", .{}); + std.debug.print("Print\n", .{}); } diff --git a/src/MenuSelection.zig b/src/MenuSelection.zig index 466d337..f66c2a0 100644 --- a/src/MenuSelection.zig +++ b/src/MenuSelection.zig @@ -3,10 +3,10 @@ const MainMenu = @import("MainMenu.zig"); const Self = @This(); name: []const u8, -action: fn(*MainMenu) void, +action: fn (*MainMenu) void, -pub fn init(name: []const u8, action: fn(*MainMenu) void) Self { - return Self { +pub fn init(name: []const u8, action: fn (*MainMenu) void) Self { + return Self{ .name = name, .action = action, }; diff --git a/src/flow.zig b/src/flow.zig new file mode 100644 index 0000000..eb208b7 --- /dev/null +++ b/src/flow.zig @@ -0,0 +1,5 @@ +// Return values for the main function, allows controlling flow +pub const State = enum { + main_menu, + game, +}; diff --git a/src/main.zig b/src/main.zig index 58445f7..2b443c2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,6 +4,8 @@ const SDL = @import("sdl2"); const Game = @import("Game.zig"); const MainMenu = @import("MainMenu.zig"); +const State = @import("flow.zig").State; + pub fn main() !void { const stderr = std.io.getStdErr(); defer stderr.close(); @@ -28,8 +30,10 @@ pub fn main() !void { var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_PRESENTVSYNC | SDL.SDL_RENDERER_ACCELERATED) orelse return; defer _ = SDL.SDL_DestroyRenderer(renderer); - //var game = Game.init(renderer); var main_menu = MainMenu.init(); + var game = Game.init(renderer); + + var current_state: State = main_menu.state; _ = SDL.SDL_SetRenderDrawBlendMode(renderer, SDL.SDL_BLENDMODE_BLEND); @@ -44,8 +48,10 @@ pub fn main() !void { _ = SDL.SDL_SetRenderDrawColor(renderer, 231, 247, 255, 0xFF); _ = SDL.SDL_RenderClear(renderer); - //game.tick(); - main_menu.tick(); + current_state = switch (current_state) { + State.main_menu => main_menu.tick(), + State.game => game.tick(), + }; SDL.SDL_RenderPresent(renderer); }