Main Menu Structure WIP

This commit is contained in:
Dusk 2022-07-12 20:27:40 +02:00
parent df495eb608
commit 0d7bd044dc
4 changed files with 96 additions and 2 deletions

View File

@ -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", .{});
}

13
src/MenuSelection.zig Normal file
View File

@ -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,
};
}

20
src/MenuTab.zig Normal file
View File

@ -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;
}

View File

@ -2,6 +2,7 @@ const std = @import("std");
const SDL = @import("sdl2"); const SDL = @import("sdl2");
const Game = @import("Game.zig"); const Game = @import("Game.zig");
const MainMenu = @import("MainMenu.zig");
pub fn main() !void { pub fn main() !void {
const stderr = std.io.getStdErr(); 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; var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_PRESENTVSYNC | SDL.SDL_RENDERER_ACCELERATED) orelse return;
defer _ = SDL.SDL_DestroyRenderer(renderer); 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); _ = 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_SetRenderDrawColor(renderer, 231, 247, 255, 0xFF);
_ = SDL.SDL_RenderClear(renderer); _ = SDL.SDL_RenderClear(renderer);
game.tick(); //game.tick();
main_menu.tick();
SDL.SDL_RenderPresent(renderer); SDL.SDL_RenderPresent(renderer);
} }