Change src structure, separate game renderer
This commit is contained in:
parent
9ba1ca0216
commit
27270e1832
39
src/Game.zig
39
src/Game.zig
|
@ -1,15 +1,15 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const SDL = @import("sdl2");
|
const SDL = @import("sdl2");
|
||||||
|
|
||||||
const Grid = @import("Grid.zig");
|
const Grid = @import("Game/Grid.zig");
|
||||||
const Piece = @import("Piece.zig");
|
const Piece = @import("Game/Piece.zig");
|
||||||
const Bag = @import("Bag.zig");
|
const Bag = @import("Game/Bag.zig");
|
||||||
const Timer = @import("Timer.zig");
|
const Timer = @import("Timer.zig");
|
||||||
const State = @import("flow.zig").State;
|
const State = @import("flow.zig").State;
|
||||||
const Action = @import("Action.zig");
|
const Action = @import("Action.zig");
|
||||||
|
|
||||||
const renderer = @import("renderer.zig");
|
const Renderer = @import("Game/Renderer.zig");
|
||||||
const movement = @import("movement.zig");
|
const movement = @import("Game/movement.zig");
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
|
@ -25,9 +25,8 @@ pub const ActionCode = enum(usize) {
|
||||||
};
|
};
|
||||||
|
|
||||||
grid: Grid,
|
grid: Grid,
|
||||||
grid_cell_size: i32,
|
|
||||||
grid_pos_x: i32,
|
renderer: Renderer,
|
||||||
grid_pos_y: i32,
|
|
||||||
|
|
||||||
state: State = State.game,
|
state: State = State.game,
|
||||||
|
|
||||||
|
@ -37,8 +36,6 @@ held: ?Piece.Type,
|
||||||
piece: Piece,
|
piece: Piece,
|
||||||
shadow: Piece,
|
shadow: Piece,
|
||||||
|
|
||||||
renderer: *SDL.SDL_Renderer,
|
|
||||||
|
|
||||||
action_list: [7]Action = .{
|
action_list: [7]Action = .{
|
||||||
Action.init(SDL.SDL_SCANCODE_D, actionRight), // Right
|
Action.init(SDL.SDL_SCANCODE_D, actionRight), // Right
|
||||||
Action.init(SDL.SDL_SCANCODE_A, actionLeft), // Left
|
Action.init(SDL.SDL_SCANCODE_A, actionLeft), // Left
|
||||||
|
@ -55,18 +52,15 @@ timer_left_das: Timer,
|
||||||
timer_right_arr: Timer,
|
timer_right_arr: Timer,
|
||||||
timer_right_das: Timer,
|
timer_right_das: Timer,
|
||||||
|
|
||||||
pub fn init(_renderer: *SDL.SDL_Renderer) Self {
|
pub fn init(renderer: *SDL.SDL_Renderer) Self {
|
||||||
var ret = Self{
|
var ret = Self{
|
||||||
.grid = Grid.init(),
|
.grid = Grid.init(),
|
||||||
.grid_cell_size = undefined,
|
|
||||||
.grid_pos_x = undefined,
|
|
||||||
.grid_pos_y = undefined,
|
|
||||||
|
|
||||||
.bag = Bag.init(),
|
.bag = Bag.init(),
|
||||||
.piece = undefined,
|
.piece = undefined,
|
||||||
.shadow = undefined,
|
.shadow = undefined,
|
||||||
.held = null,
|
.held = null,
|
||||||
.renderer = _renderer,
|
.renderer = Renderer.init(renderer),
|
||||||
|
|
||||||
.timer_down = Timer.init(50),
|
.timer_down = Timer.init(50),
|
||||||
.timer_left_arr = Timer.init(50),
|
.timer_left_arr = Timer.init(50),
|
||||||
|
@ -75,19 +69,6 @@ pub fn init(_renderer: *SDL.SDL_Renderer) Self {
|
||||||
.timer_right_das = Timer.init(200),
|
.timer_right_das = Timer.init(200),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Calculate positions
|
|
||||||
{
|
|
||||||
var aux_w: i32 = undefined;
|
|
||||||
var aux_h: i32 = undefined;
|
|
||||||
|
|
||||||
_ = SDL.SDL_GetRendererOutputSize(_renderer, &aux_w, &aux_h);
|
|
||||||
|
|
||||||
ret.grid_cell_size = @divFloor(@minimum(aux_w, aux_h), 32);
|
|
||||||
|
|
||||||
ret.grid_pos_x = @divFloor(aux_w, 2) - (ret.grid_cell_size * @divFloor(Grid.ncolumns, 2));
|
|
||||||
ret.grid_pos_y = @divFloor(aux_h, 2) - (ret.grid_cell_size * @divFloor(Grid.nrows + Grid.buffer, 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a piece from the bag
|
// Get a piece from the bag
|
||||||
ret.piece = ret.bag.pop();
|
ret.piece = ret.bag.pop();
|
||||||
|
|
||||||
|
@ -194,7 +175,7 @@ pub fn tick(self: *Self) State {
|
||||||
|
|
||||||
self.grid.clearLines();
|
self.grid.clearLines();
|
||||||
|
|
||||||
renderer.render(self.*);
|
self.renderer.render(self.*);
|
||||||
|
|
||||||
return self.state;
|
return self.state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const SDL = @import("sdl2");
|
const SDL = @import("sdl2");
|
||||||
|
|
||||||
const Color = @import("color.zig");
|
const Color = @import("../color.zig");
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const SDL = @import("sdl2");
|
const SDL = @import("sdl2");
|
||||||
|
|
||||||
const color = @import("color.zig");
|
const color = @import("../color.zig");
|
||||||
const Cell = @import("Cell.zig");
|
const Cell = @import("Cell.zig");
|
||||||
const utils = @import("utils.zig");
|
const utils = @import("../utils.zig");
|
||||||
|
|
||||||
const range = utils.range;
|
const range = utils.range;
|
||||||
|
|
|
@ -2,9 +2,9 @@ const SDL = @import("sdl2");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const Grid = @import("Grid.zig");
|
const Grid = @import("Grid.zig");
|
||||||
const Timer = @import("Timer.zig");
|
const Timer = @import("../Timer.zig");
|
||||||
|
|
||||||
const color = @import("color.zig");
|
const color = @import("../color.zig");
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
|
@ -1,16 +1,42 @@
|
||||||
const SDL = @import("sdl2");
|
const SDL = @import("sdl2");
|
||||||
|
|
||||||
const Game = @import("Game.zig");
|
const Game = @import("../Game.zig");
|
||||||
const Bag = @import("Bag.zig");
|
const Bag = @import("Bag.zig");
|
||||||
const Grid = @import("Grid.zig");
|
const Grid = @import("Grid.zig");
|
||||||
const Piece = @import("Piece.zig");
|
const Piece = @import("Piece.zig");
|
||||||
|
|
||||||
const color = @import("color.zig");
|
const color = @import("../color.zig");
|
||||||
|
|
||||||
pub fn renderBag(game: Game) void {
|
const Self = @This();
|
||||||
const pos_x = game.grid_pos_x + ((Grid.ncolumns + 1) * game.grid_cell_size);
|
|
||||||
const pos_y = game.grid_pos_y + (Grid.buffer * game.grid_cell_size);
|
renderer: *SDL.SDL_Renderer,
|
||||||
const cell_size = game.grid_cell_size;
|
grid_cell_size: i32,
|
||||||
|
grid_pos_x: i32,
|
||||||
|
grid_pos_y: i32,
|
||||||
|
|
||||||
|
pub fn init(renderer: *SDL.SDL_Renderer) Self {
|
||||||
|
// Calculate positions
|
||||||
|
var aux_w: i32 = undefined;
|
||||||
|
var aux_h: i32 = undefined;
|
||||||
|
|
||||||
|
// TODO: have in mind the return value
|
||||||
|
_ = SDL.SDL_GetRendererOutputSize(renderer, &aux_w, &aux_h);
|
||||||
|
|
||||||
|
const grid_cell_size = @divFloor(@minimum(aux_w, aux_h), 32);
|
||||||
|
const grid_pos_x = @divFloor(aux_w, 2) - (grid_cell_size * @divFloor(Grid.ncolumns, 2));
|
||||||
|
const grid_pos_y = @divFloor(aux_h, 2) - (grid_cell_size * @divFloor(Grid.nrows + Grid.buffer, 2));
|
||||||
|
return Self{
|
||||||
|
.renderer = renderer,
|
||||||
|
.grid_pos_x = grid_pos_x,
|
||||||
|
.grid_pos_y = grid_pos_y,
|
||||||
|
.grid_cell_size = grid_cell_size,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn renderBag(self: *Self, game: Game) void {
|
||||||
|
const pos_x = self.grid_pos_x + ((Grid.ncolumns + 1) * self.grid_cell_size);
|
||||||
|
const pos_y = self.grid_pos_y + (Grid.buffer * self.grid_cell_size);
|
||||||
|
const cell_size = self.grid_cell_size;
|
||||||
|
|
||||||
var rect = SDL.SDL_Rect{
|
var rect = SDL.SDL_Rect{
|
||||||
.x = pos_x,
|
.x = pos_x,
|
||||||
|
@ -46,20 +72,20 @@ pub fn renderBag(game: Game) void {
|
||||||
rect.x = pos_x + @intCast(i32, x) * cell_size;
|
rect.x = pos_x + @intCast(i32, x) * cell_size;
|
||||||
rect.y = pos_y + (@intCast(i32, y) + @intCast(i32, i * piece.structure.len)) * cell_size;
|
rect.y = pos_y + (@intCast(i32, y) + @intCast(i32, i * piece.structure.len)) * cell_size;
|
||||||
|
|
||||||
_ = SDL.SDL_SetRenderDrawColor(game.renderer, r, g, b, a);
|
_ = SDL.SDL_SetRenderDrawColor(self.renderer, r, g, b, a);
|
||||||
_ = SDL.SDL_RenderFillRect(game.renderer, &rect);
|
_ = SDL.SDL_RenderFillRect(self.renderer, &rect);
|
||||||
_ = SDL.SDL_SetRenderDrawColor(game.renderer, r -| 30, g -| 30, b -| 30, a);
|
_ = SDL.SDL_SetRenderDrawColor(self.renderer, r -| 30, g -| 30, b -| 30, a);
|
||||||
_ = SDL.SDL_RenderDrawRect(game.renderer, &rect);
|
_ = SDL.SDL_RenderDrawRect(self.renderer, &rect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn renderHeld(game: Game) void {
|
pub fn renderHeld(self: *Self, game: Game) void {
|
||||||
const pos_x = game.grid_pos_x - (5 * game.grid_cell_size);
|
const pos_x = self.grid_pos_x - (5 * self.grid_cell_size);
|
||||||
const pos_y = game.grid_pos_y + (Grid.buffer * game.grid_cell_size);
|
const pos_y = self.grid_pos_y + (Grid.buffer * self.grid_cell_size);
|
||||||
const cell_size = game.grid_cell_size;
|
const cell_size = self.grid_cell_size;
|
||||||
|
|
||||||
var rect = SDL.SDL_Rect{
|
var rect = SDL.SDL_Rect{
|
||||||
.x = pos_x,
|
.x = pos_x,
|
||||||
|
@ -94,19 +120,19 @@ pub fn renderHeld(game: Game) void {
|
||||||
rect.x = pos_x + @intCast(i32, x) * cell_size;
|
rect.x = pos_x + @intCast(i32, x) * cell_size;
|
||||||
rect.y = pos_y + @intCast(i32, y) * cell_size;
|
rect.y = pos_y + @intCast(i32, y) * cell_size;
|
||||||
|
|
||||||
_ = SDL.SDL_SetRenderDrawColor(game.renderer, r, g, b, a);
|
_ = SDL.SDL_SetRenderDrawColor(self.renderer, r, g, b, a);
|
||||||
_ = SDL.SDL_RenderFillRect(game.renderer, &rect);
|
_ = SDL.SDL_RenderFillRect(self.renderer, &rect);
|
||||||
_ = SDL.SDL_SetRenderDrawColor(game.renderer, r -| 30, g -| 30, b -| 30, a);
|
_ = SDL.SDL_SetRenderDrawColor(self.renderer, r -| 30, g -| 30, b -| 30, a);
|
||||||
_ = SDL.SDL_RenderDrawRect(game.renderer, &rect);
|
_ = SDL.SDL_RenderDrawRect(self.renderer, &rect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(game: Game) void {
|
pub fn render(self: *Self, game: Game) void {
|
||||||
game.grid.render(game.renderer, game.grid_cell_size, game.grid_pos_x, game.grid_pos_y);
|
game.grid.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y);
|
||||||
game.piece.render(game.renderer, game.grid_cell_size, game.grid_pos_x, game.grid_pos_y);
|
game.piece.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y);
|
||||||
game.shadow.render(game.renderer, game.grid_cell_size, game.grid_pos_x, game.grid_pos_y);
|
game.shadow.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y);
|
||||||
renderBag(game);
|
renderBag(self, game);
|
||||||
renderHeld(game);
|
renderHeld(self, game);
|
||||||
}
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const SDL = @import("sdl2");
|
const SDL = @import("sdl2");
|
||||||
|
|
||||||
const MenuSelection = @import("MenuSelection.zig");
|
const MenuSelection = @import("MainMenu/MenuSelection.zig");
|
||||||
const MenuTab = @import("MenuTab.zig");
|
const MenuTab = @import("MainMenu/MenuTab.zig");
|
||||||
|
|
||||||
const State = @import("flow.zig").State;
|
const State = @import("flow.zig").State;
|
||||||
const Action = @import("Action.zig");
|
const Action = @import("Action.zig");
|
||||||
|
@ -37,9 +37,6 @@ state: State = State.main_menu,
|
||||||
holding_down: bool = false,
|
holding_down: bool = false,
|
||||||
holding_enter: bool = false,
|
holding_enter: bool = false,
|
||||||
|
|
||||||
pos_x: i32,
|
|
||||||
pos_i: i32,
|
|
||||||
|
|
||||||
pub fn init() Self {
|
pub fn init() Self {
|
||||||
return Self{};
|
return Self{};
|
||||||
}
|
}
|
||||||
|
@ -86,8 +83,7 @@ fn actionSelDown(self: *Self) void {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn actionSelUp(self: *Self) void {
|
fn actionSelUp(self: *Self) void {
|
||||||
if (self.sel == 0) self.sel = self.tab_list[self.tab].contents.len - 1
|
if (self.sel == 0) self.sel = self.tab_list[self.tab].contents.len - 1 else self.sel -= 1;
|
||||||
else self.sel -= 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn actionSelect(self: *Self) void {
|
fn actionSelect(self: *Self) void {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const MainMenu = @import("MainMenu.zig");
|
const MainMenu = @import("../MainMenu.zig");
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
Loading…
Reference in New Issue