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 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

View File

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

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