From 27270e183245f50afbd3c29c9ad5b9f2afcaea36 Mon Sep 17 00:00:00 2001 From: Strangedusk Date: Thu, 21 Jul 2022 13:14:16 +0200 Subject: [PATCH] Change src structure, separate game renderer --- src/Game.zig | 39 ++++--------- src/{ => Game}/Bag.zig | 0 src/{ => Game}/Cell.zig | 2 +- src/{ => Game}/Grid.zig | 4 +- src/{ => Game}/Piece.zig | 4 +- src/{renderer.zig => Game/Renderer.zig} | 74 +++++++++++++++++-------- src/{ => Game}/movement.zig | 0 src/MainMenu.zig | 10 +--- src/{ => MainMenu}/MenuSelection.zig | 2 +- src/{ => MainMenu}/MenuTab.zig | 0 10 files changed, 69 insertions(+), 66 deletions(-) rename src/{ => Game}/Bag.zig (100%) rename src/{ => Game}/Cell.zig (84%) rename src/{ => Game}/Grid.zig (96%) rename src/{ => Game}/Piece.zig (98%) rename src/{renderer.zig => Game/Renderer.zig} (51%) rename src/{ => Game}/movement.zig (100%) rename src/{ => MainMenu}/MenuSelection.zig (82%) rename src/{ => MainMenu}/MenuTab.zig (100%) diff --git a/src/Game.zig b/src/Game.zig index a8ff3c0..2f3b8a7 100644 --- a/src/Game.zig +++ b/src/Game.zig @@ -1,15 +1,15 @@ const std = @import("std"); const SDL = @import("sdl2"); -const Grid = @import("Grid.zig"); -const Piece = @import("Piece.zig"); -const Bag = @import("Bag.zig"); +const Grid = @import("Game/Grid.zig"); +const Piece = @import("Game/Piece.zig"); +const Bag = @import("Game/Bag.zig"); const Timer = @import("Timer.zig"); const State = @import("flow.zig").State; const Action = @import("Action.zig"); -const renderer = @import("renderer.zig"); -const movement = @import("movement.zig"); +const Renderer = @import("Game/Renderer.zig"); +const movement = @import("Game/movement.zig"); const Self = @This(); @@ -25,9 +25,8 @@ pub const ActionCode = enum(usize) { }; grid: Grid, -grid_cell_size: i32, -grid_pos_x: i32, -grid_pos_y: i32, + +renderer: Renderer, state: State = State.game, @@ -37,8 +36,6 @@ held: ?Piece.Type, piece: Piece, shadow: Piece, -renderer: *SDL.SDL_Renderer, - action_list: [7]Action = .{ Action.init(SDL.SDL_SCANCODE_D, actionRight), // Right Action.init(SDL.SDL_SCANCODE_A, actionLeft), // Left @@ -55,18 +52,15 @@ timer_left_das: Timer, timer_right_arr: Timer, timer_right_das: Timer, -pub fn init(_renderer: *SDL.SDL_Renderer) Self { +pub fn init(renderer: *SDL.SDL_Renderer) Self { var ret = Self{ .grid = Grid.init(), - .grid_cell_size = undefined, - .grid_pos_x = undefined, - .grid_pos_y = undefined, .bag = Bag.init(), .piece = undefined, .shadow = undefined, .held = null, - .renderer = _renderer, + .renderer = Renderer.init(renderer), .timer_down = 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), }; - // 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 ret.piece = ret.bag.pop(); @@ -194,7 +175,7 @@ pub fn tick(self: *Self) State { self.grid.clearLines(); - renderer.render(self.*); + self.renderer.render(self.*); return self.state; } diff --git a/src/Bag.zig b/src/Game/Bag.zig similarity index 100% rename from src/Bag.zig rename to src/Game/Bag.zig diff --git a/src/Cell.zig b/src/Game/Cell.zig similarity index 84% rename from src/Cell.zig rename to src/Game/Cell.zig index 71c8aab..14d60da 100644 --- a/src/Cell.zig +++ b/src/Game/Cell.zig @@ -1,7 +1,7 @@ const std = @import("std"); const SDL = @import("sdl2"); -const Color = @import("color.zig"); +const Color = @import("../color.zig"); const Self = @This(); diff --git a/src/Grid.zig b/src/Game/Grid.zig similarity index 96% rename from src/Grid.zig rename to src/Game/Grid.zig index 58d6622..c81c4fc 100644 --- a/src/Grid.zig +++ b/src/Game/Grid.zig @@ -1,9 +1,9 @@ const std = @import("std"); const SDL = @import("sdl2"); -const color = @import("color.zig"); +const color = @import("../color.zig"); const Cell = @import("Cell.zig"); -const utils = @import("utils.zig"); +const utils = @import("../utils.zig"); const range = utils.range; diff --git a/src/Piece.zig b/src/Game/Piece.zig similarity index 98% rename from src/Piece.zig rename to src/Game/Piece.zig index e1178d9..c345498 100644 --- a/src/Piece.zig +++ b/src/Game/Piece.zig @@ -2,9 +2,9 @@ const SDL = @import("sdl2"); const std = @import("std"); 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(); diff --git a/src/renderer.zig b/src/Game/Renderer.zig similarity index 51% rename from src/renderer.zig rename to src/Game/Renderer.zig index feed79d..e3e41ac 100644 --- a/src/renderer.zig +++ b/src/Game/Renderer.zig @@ -1,16 +1,42 @@ const SDL = @import("sdl2"); -const Game = @import("Game.zig"); +const Game = @import("../Game.zig"); const Bag = @import("Bag.zig"); const Grid = @import("Grid.zig"); const Piece = @import("Piece.zig"); -const color = @import("color.zig"); +const color = @import("../color.zig"); -pub fn renderBag(game: Game) void { - 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); - const cell_size = game.grid_cell_size; +const Self = @This(); + +renderer: *SDL.SDL_Renderer, +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{ .x = pos_x, @@ -46,20 +72,20 @@ pub fn renderBag(game: Game) void { rect.x = pos_x + @intCast(i32, x) * 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_RenderFillRect(game.renderer, &rect); - _ = SDL.SDL_SetRenderDrawColor(game.renderer, r -| 30, g -| 30, b -| 30, a); - _ = SDL.SDL_RenderDrawRect(game.renderer, &rect); + _ = SDL.SDL_SetRenderDrawColor(self.renderer, r, g, b, a); + _ = SDL.SDL_RenderFillRect(self.renderer, &rect); + _ = SDL.SDL_SetRenderDrawColor(self.renderer, r -| 30, g -| 30, b -| 30, a); + _ = SDL.SDL_RenderDrawRect(self.renderer, &rect); } } } } } -pub fn renderHeld(game: Game) void { - const pos_x = game.grid_pos_x - (5 * game.grid_cell_size); - const pos_y = game.grid_pos_y + (Grid.buffer * game.grid_cell_size); - const cell_size = game.grid_cell_size; +pub fn renderHeld(self: *Self, game: Game) void { + const pos_x = self.grid_pos_x - (5 * 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{ .x = pos_x, @@ -94,19 +120,19 @@ pub fn renderHeld(game: Game) void { rect.x = pos_x + @intCast(i32, x) * cell_size; rect.y = pos_y + @intCast(i32, y) * cell_size; - _ = SDL.SDL_SetRenderDrawColor(game.renderer, r, g, b, a); - _ = SDL.SDL_RenderFillRect(game.renderer, &rect); - _ = SDL.SDL_SetRenderDrawColor(game.renderer, r -| 30, g -| 30, b -| 30, a); - _ = SDL.SDL_RenderDrawRect(game.renderer, &rect); + _ = SDL.SDL_SetRenderDrawColor(self.renderer, r, g, b, a); + _ = SDL.SDL_RenderFillRect(self.renderer, &rect); + _ = SDL.SDL_SetRenderDrawColor(self.renderer, r -| 30, g -| 30, b -| 30, a); + _ = SDL.SDL_RenderDrawRect(self.renderer, &rect); } } } } -pub fn render(game: Game) void { - game.grid.render(game.renderer, game.grid_cell_size, game.grid_pos_x, game.grid_pos_y); - game.piece.render(game.renderer, game.grid_cell_size, game.grid_pos_x, game.grid_pos_y); - game.shadow.render(game.renderer, game.grid_cell_size, game.grid_pos_x, game.grid_pos_y); - renderBag(game); - renderHeld(game); +pub fn render(self: *Self, game: Game) void { + game.grid.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y); + game.piece.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y); + game.shadow.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y); + renderBag(self, game); + renderHeld(self, game); } diff --git a/src/movement.zig b/src/Game/movement.zig similarity index 100% rename from src/movement.zig rename to src/Game/movement.zig diff --git a/src/MainMenu.zig b/src/MainMenu.zig index 2d68c82..7e4f136 100644 --- a/src/MainMenu.zig +++ b/src/MainMenu.zig @@ -1,8 +1,8 @@ const std = @import("std"); const SDL = @import("sdl2"); -const MenuSelection = @import("MenuSelection.zig"); -const MenuTab = @import("MenuTab.zig"); +const MenuSelection = @import("MainMenu/MenuSelection.zig"); +const MenuTab = @import("MainMenu/MenuTab.zig"); const State = @import("flow.zig").State; const Action = @import("Action.zig"); @@ -37,9 +37,6 @@ state: State = State.main_menu, holding_down: bool = false, holding_enter: bool = false, -pos_x: i32, -pos_i: i32, - pub fn init() Self { return Self{}; } @@ -86,8 +83,7 @@ fn actionSelDown(self: *Self) void { } fn actionSelUp(self: *Self) void { - if (self.sel == 0) self.sel = self.tab_list[self.tab].contents.len - 1 - else self.sel -= 1; + if (self.sel == 0) self.sel = self.tab_list[self.tab].contents.len - 1 else self.sel -= 1; } fn actionSelect(self: *Self) void { diff --git a/src/MenuSelection.zig b/src/MainMenu/MenuSelection.zig similarity index 82% rename from src/MenuSelection.zig rename to src/MainMenu/MenuSelection.zig index f66c2a0..bc70aff 100644 --- a/src/MenuSelection.zig +++ b/src/MainMenu/MenuSelection.zig @@ -1,4 +1,4 @@ -const MainMenu = @import("MainMenu.zig"); +const MainMenu = @import("../MainMenu.zig"); const Self = @This(); diff --git a/src/MenuTab.zig b/src/MainMenu/MenuTab.zig similarity index 100% rename from src/MenuTab.zig rename to src/MainMenu/MenuTab.zig