Main Menu Very WIP

This commit is contained in:
Dusk 2022-07-13 22:59:54 +02:00
parent 0d7bd044dc
commit 276016cdf7
5 changed files with 59 additions and 15 deletions

View File

@ -5,6 +5,7 @@ const Grid = @import("Grid.zig");
const Piece = @import("Piece.zig"); const Piece = @import("Piece.zig");
const Bag = @import("Bag.zig"); const Bag = @import("Bag.zig");
const Timer = @import("Timer.zig"); const Timer = @import("Timer.zig");
const State = @import("flow.zig").State;
const renderer = @import("renderer.zig"); const renderer = @import("renderer.zig");
const movement = @import("movement.zig"); const movement = @import("movement.zig");
@ -32,6 +33,8 @@ grid_cell_size: i32,
grid_pos_x: i32, grid_pos_x: i32,
grid_pos_y: i32, grid_pos_y: i32,
state: State = State.game,
bag: Bag, bag: Bag,
held: ?Piece.Type, held: ?Piece.Type,
@ -85,7 +88,7 @@ pub fn init(_renderer: *SDL.SDL_Renderer) Self {
return ret; return ret;
} }
pub fn tick(self: *Self) void { pub fn tick(self: *Self) State {
// TIMERS // TIMERS
if (self.piece.timer_dropped.finished()) { if (self.piece.timer_dropped.finished()) {
@ -140,6 +143,8 @@ pub fn tick(self: *Self) void {
self.grid.clearLines(); self.grid.clearLines();
renderer.render(self.*); renderer.render(self.*);
return self.state;
} }
// ACTION CALLBACKS // ACTION CALLBACKS

View File

@ -4,6 +4,8 @@ const SDL = @import("sdl2");
const MenuSelection = @import("MenuSelection.zig"); const MenuSelection = @import("MenuSelection.zig");
const MenuTab = @import("MenuTab.zig"); const MenuTab = @import("MenuTab.zig");
const State = @import("flow.zig").State;
const Self = @This(); const Self = @This();
tab_list: [2]MenuTab = [2]MenuTab{ tab_list: [2]MenuTab = [2]MenuTab{
@ -17,16 +19,30 @@ tab_list: [2]MenuTab = [2]MenuTab{
}), }),
}, },
// Current tab and selection index
tab: usize = 0, tab: usize = 0,
selection: usize = 0, sel: usize = 0,
state: State = State.main_menu,
holding_down: bool = false, 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 { pub fn init() Self {
return 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); var key_state = SDL.SDL_GetKeyboardState(null);
if (key_state[SDL.SDL_SCANCODE_DOWN] == 1 and !self.holding_down) { if (key_state[SDL.SDL_SCANCODE_DOWN] == 1 and !self.holding_down) {
self.holding_down = true; self.holding_down = true;
@ -35,24 +51,36 @@ pub fn tick(self: *Self) void {
if (key_state[SDL.SDL_SCANCODE_DOWN] == 0) { if (key_state[SDL.SDL_SCANCODE_DOWN] == 0) {
self.holding_down = false; 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( std.debug.print(
\\Tab: {s} \\Tab: {s}
\\Selection: {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 { fn actionSelDown(self: *Self) void {
self.selection += 1; self.sel += 1;
self.selection %= self.tab_list[self.tab].contents.len; 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 { fn play(self: *Self) void {
_ = self; self.state = State.game;
std.debug.print("Play", .{});
} }
fn print(self: *Self) void { fn print(self: *Self) void {
_ = self; _ = self;
std.debug.print("Print", .{}); std.debug.print("Print\n", .{});
} }

View File

@ -3,10 +3,10 @@ const MainMenu = @import("MainMenu.zig");
const Self = @This(); const Self = @This();
name: []const u8, name: []const u8,
action: fn(*MainMenu) void, action: fn (*MainMenu) void,
pub fn init(name: []const u8, action: fn(*MainMenu) void) Self { pub fn init(name: []const u8, action: fn (*MainMenu) void) Self {
return Self { return Self{
.name = name, .name = name,
.action = action, .action = action,
}; };

5
src/flow.zig Normal file
View File

@ -0,0 +1,5 @@
// Return values for the main function, allows controlling flow
pub const State = enum {
main_menu,
game,
};

View File

@ -4,6 +4,8 @@ const SDL = @import("sdl2");
const Game = @import("Game.zig"); const Game = @import("Game.zig");
const MainMenu = @import("MainMenu.zig"); const MainMenu = @import("MainMenu.zig");
const State = @import("flow.zig").State;
pub fn main() !void { pub fn main() !void {
const stderr = std.io.getStdErr(); const stderr = std.io.getStdErr();
defer stderr.close(); 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; 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 main_menu = MainMenu.init(); 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); _ = 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_SetRenderDrawColor(renderer, 231, 247, 255, 0xFF);
_ = SDL.SDL_RenderClear(renderer); _ = SDL.SDL_RenderClear(renderer);
//game.tick(); current_state = switch (current_state) {
main_menu.tick(); State.main_menu => main_menu.tick(),
State.game => game.tick(),
};
SDL.SDL_RenderPresent(renderer); SDL.SDL_RenderPresent(renderer);
} }