From 0d7bd044dc7d738074495419ffdf78e97c88b50b Mon Sep 17 00:00:00 2001 From: Strangedusk Date: Tue, 12 Jul 2022 20:27:40 +0200 Subject: [PATCH] Main Menu Structure WIP --- src/MainMenu.zig | 58 +++++++++++++++++++++++++++++++++++++++++++ src/MenuSelection.zig | 13 ++++++++++ src/MenuTab.zig | 20 +++++++++++++++ src/main.zig | 7 ++++-- 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/MenuSelection.zig create mode 100644 src/MenuTab.zig diff --git a/src/MainMenu.zig b/src/MainMenu.zig index e69de29..bf724ab 100644 --- a/src/MainMenu.zig +++ b/src/MainMenu.zig @@ -0,0 +1,58 @@ +const std = @import("std"); +const SDL = @import("sdl2"); + +const MenuSelection = @import("MenuSelection.zig"); +const MenuTab = @import("MenuTab.zig"); + +const Self = @This(); + +tab_list: [2]MenuTab = [2]MenuTab{ + MenuTab.init("1P", &.{ + MenuSelection.init("Play", play), + MenuSelection.init("Print", print), + }), + MenuTab.init("Options", &.{ + MenuSelection.init("Play", play), + MenuSelection.init("Print", print), + }), +}, + +tab: usize = 0, +selection: usize = 0, + +holding_down: bool = false, + +pub fn init() Self { + return Self{}; +} + +pub fn tick(self: *Self) void { + var key_state = SDL.SDL_GetKeyboardState(null); + if (key_state[SDL.SDL_SCANCODE_DOWN] == 1 and !self.holding_down) { + self.holding_down = true; + self.actionSelDown(); + } + if (key_state[SDL.SDL_SCANCODE_DOWN] == 0) { + self.holding_down = false; + } + std.debug.print( + \\Tab: {s} + \\Selection: {s} + \\ + , .{ self.tab_list[self.tab].name, self.tab_list[self.tab].contents[self.selection].name }); +} + +fn actionSelDown(self: *Self) void { + self.selection += 1; + self.selection %= self.tab_list[self.tab].contents.len; +} + +fn play(self: *Self) void { + _ = self; + std.debug.print("Play", .{}); +} + +fn print(self: *Self) void { + _ = self; + std.debug.print("Print", .{}); +} diff --git a/src/MenuSelection.zig b/src/MenuSelection.zig new file mode 100644 index 0000000..466d337 --- /dev/null +++ b/src/MenuSelection.zig @@ -0,0 +1,13 @@ +const MainMenu = @import("MainMenu.zig"); + +const Self = @This(); + +name: []const u8, +action: fn(*MainMenu) void, + +pub fn init(name: []const u8, action: fn(*MainMenu) void) Self { + return Self { + .name = name, + .action = action, + }; +} diff --git a/src/MenuTab.zig b/src/MenuTab.zig new file mode 100644 index 0000000..9c70628 --- /dev/null +++ b/src/MenuTab.zig @@ -0,0 +1,20 @@ +const std = @import("std"); + +const MenuSelection = @import("MenuSelection.zig"); + +const Self = @This(); + +name: []const u8, +contents: []MenuSelection, +contents_buffer: [50]MenuSelection, + +pub fn init(name: []const u8, contents: []const MenuSelection) Self { + var ret = Self { + .name = name, + .contents = undefined, + .contents_buffer = undefined, + }; + std.mem.copy(MenuSelection, &ret.contents_buffer, contents); + ret.contents = ret.contents_buffer[0..contents.len]; + return ret; +} diff --git a/src/main.zig b/src/main.zig index 2314476..58445f7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,7 @@ const std = @import("std"); const SDL = @import("sdl2"); const Game = @import("Game.zig"); +const MainMenu = @import("MainMenu.zig"); pub fn main() !void { const stderr = std.io.getStdErr(); @@ -27,7 +28,8 @@ 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 game = Game.init(renderer); + var main_menu = MainMenu.init(); _ = SDL.SDL_SetRenderDrawBlendMode(renderer, SDL.SDL_BLENDMODE_BLEND); @@ -42,7 +44,8 @@ pub fn main() !void { _ = SDL.SDL_SetRenderDrawColor(renderer, 231, 247, 255, 0xFF); _ = SDL.SDL_RenderClear(renderer); - game.tick(); + //game.tick(); + main_menu.tick(); SDL.SDL_RenderPresent(renderer); }