Complete renderer file structure

This commit is contained in:
Dusk 2022-07-23 19:33:43 +02:00
parent 65c1f5eb34
commit d740e72544
8 changed files with 103 additions and 68 deletions

Binary file not shown.

View File

@ -147,7 +147,7 @@ pub fn tick(self: *Self) State {
if (!self.piece.timer_dropped.started) {
if (!self.timer_gravity.started) {
self.timer_gravity.start();
} else if (self.timer_gravity.finished()){
} else if (self.timer_gravity.finished()) {
self.action_list[@enumToInt(ActionCode.down)].activate = true;
self.timer_gravity.start();
}

View File

@ -48,29 +48,3 @@ pub fn clearLines(self: *Self) void {
}
}
pub fn render(self: Self, renderer: *SDL.SDL_Renderer, cell_size: i32, pos_x: i32, pos_y: i32) void {
var rect = SDL.SDL_Rect{
.x = pos_x,
.y = pos_y,
.w = cell_size,
.h = cell_size,
};
var visible = self.cells[buffer..];
for (visible) |_, y| {
for (visible[y]) |_, x| {
var r = visible[y][x].color.r;
var g = visible[y][x].color.g;
var b = visible[y][x].color.b;
var a = visible[y][x].color.a;
rect.x = pos_x + @intCast(i32, x) * cell_size;
rect.y = pos_y + (@intCast(i32, y) + buffer) * cell_size;
_ = SDL.SDL_SetRenderDrawColor(renderer, r, g, b, a);
_ = SDL.SDL_RenderFillRect(renderer, &rect);
_ = SDL.SDL_SetRenderDrawColor(renderer, color.light_grey.r, color.light_grey.g, color.light_grey.b, color.light_grey.a);
_ = SDL.SDL_RenderDrawRect(renderer, &rect);
}
}
}

View File

@ -185,33 +185,3 @@ pub fn rotate(self: Self, dir: Rot) Self {
return new_piece;
}
pub fn render(self: Self, renderer: *SDL.SDL_Renderer, cell_size: i32, pos_x: i32, pos_y: i32) void {
var rect = SDL.SDL_Rect{
.x = pos_x,
.y = pos_y,
.w = cell_size,
.h = cell_size,
};
for (self.structure) |_, y| {
for (self.structure[y]) |_, x| {
// We don't want to paint void cells
if (self.structure[y][x] == false) {
continue;
}
var r = self.color.r;
var g = self.color.g;
var b = self.color.b;
var a = self.color.a;
rect.x = pos_x + (@intCast(i32, x) + self.col) * cell_size;
rect.y = pos_y + (@intCast(i32, y) + self.row) * cell_size;
_ = SDL.SDL_SetRenderDrawColor(renderer, r, g, b, a);
_ = SDL.SDL_RenderFillRect(renderer, &rect);
_ = SDL.SDL_SetRenderDrawColor(renderer, r -| 30, g -| 30, b -| 30, a +| 30);
_ = SDL.SDL_RenderDrawRect(renderer, &rect);
}
}
}

View File

@ -130,9 +130,82 @@ pub fn renderHeld(self: *Self, game: Game) void {
}
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);
renderGrid(self, game.grid);
renderPiece(self, game.piece);
renderPiece(self, game.shadow);
renderBag(self, game);
renderHeld(self, game);
}
pub fn renderPiece(self: *Self, piece: Piece) void {
const cell_size = self.grid_cell_size;
const pos_x = self.grid_pos_x;
const pos_y = self.grid_pos_y;
const renderer = self.renderer;
var rect = SDL.SDL_Rect{
.x = pos_x,
.y = pos_y,
.w = cell_size,
.h = cell_size,
};
for (piece.structure) |_, y| {
for (piece.structure[y]) |_, x| {
// We don't want to paint void cells
if (piece.structure[y][x] == false) {
continue;
}
var r = piece.color.r;
var g = piece.color.g;
var b = piece.color.b;
var a = piece.color.a;
rect.x = pos_x + (@intCast(i32, x) + piece.col) * cell_size;
rect.y = pos_y + (@intCast(i32, y) + piece.row) * cell_size;
_ = SDL.SDL_SetRenderDrawColor(renderer, r, g, b, a);
_ = SDL.SDL_RenderFillRect(renderer, &rect);
_ = SDL.SDL_SetRenderDrawColor(renderer, r -| 30, g -| 30, b -| 30, a +| 30);
_ = SDL.SDL_RenderDrawRect(renderer, &rect);
}
}
}
pub fn renderGrid(self: *Self, grid: Grid) void {
const cell_size = self.grid_cell_size;
const pos_x = self.grid_pos_x;
const pos_y = self.grid_pos_y;
const renderer = self.renderer;
var rect = SDL.SDL_Rect{
.x = pos_x,
.y = pos_y,
.w = cell_size,
.h = cell_size,
};
const buffer = Grid.buffer;
var visible = grid.cells[buffer..];
for (visible) |_, y| {
for (visible[y]) |_, x| {
var r = visible[y][x].color.r;
var g = visible[y][x].color.g;
var b = visible[y][x].color.b;
var a = visible[y][x].color.a;
rect.x = pos_x + @intCast(i32, x) * cell_size;
rect.y = pos_y + (@intCast(i32, y) + buffer) * cell_size;
_ = SDL.SDL_SetRenderDrawColor(renderer, r, g, b, a);
_ = SDL.SDL_RenderFillRect(renderer, &rect);
_ = SDL.SDL_SetRenderDrawColor(renderer, color.light_grey.r, color.light_grey.g, color.light_grey.b, color.light_grey.a);
_ = SDL.SDL_RenderDrawRect(renderer, &rect);
}
}
}

View File

@ -3,6 +3,7 @@ const SDL = @import("sdl2");
const MenuSelection = @import("MainMenu/MenuSelection.zig");
const MenuTab = @import("MainMenu/MenuTab.zig");
const Renderer = @import("MainMenu/Renderer.zig");
const State = @import("flow.zig").State;
const Action = @import("Action.zig");
@ -10,11 +11,11 @@ const Action = @import("Action.zig");
const Self = @This();
action_list: [3]Action = .{
// Action.init(SDL.SDL_SCANCODE_RIGHT, actionRight), // Right
// Action.init(SDL.SDL_SCANCODE_LEFT, actionLeft), // Left
Action.init(SDL.SDL_SCANCODE_DOWN, actionSelDown), // Down
Action.init(SDL.SDL_SCANCODE_UP, actionSelUp), // Down
Action.init(SDL.SDL_SCANCODE_RETURN, actionSelect), // Down
// Action.init(SDL.SDL_SCANCODE_RIGHT, actionRight), // Tab Right
// Action.init(SDL.SDL_SCANCODE_LEFT, actionLeft), // Tab left
Action.init(SDL.SDL_SCANCODE_DOWN, actionSelDown), // Go down
Action.init(SDL.SDL_SCANCODE_UP, actionSelUp), // Go up
Action.init(SDL.SDL_SCANCODE_RETURN, actionSelect), // Select
},
tab_list: [2]MenuTab = [2]MenuTab{
@ -28,6 +29,8 @@ tab_list: [2]MenuTab = [2]MenuTab{
}),
},
renderer: Renderer,
// Current tab and selection index
tab: usize = 0,
sel: usize = 0,
@ -37,8 +40,8 @@ state: State = State.main_menu,
holding_down: bool = false,
holding_enter: bool = false,
pub fn init() Self {
return Self{};
pub fn init(renderer: *SDL.SDL_Renderer) Self {
return Self{.renderer = Renderer.init(renderer)};
}
fn getSel(self: *Self) MenuSelection {

15
src/MainMenu/Renderer.zig Normal file
View File

@ -0,0 +1,15 @@
const SDL = @import("sdl2");
const MainMenu = @import("../MainMenu.zig");
const color = @import("../color.zig");
const Self = @This();
renderer: *SDL.SDL_Renderer,
pub fn init(renderer: *SDL.SDL_Renderer) Self {
return Self {
.renderer = renderer,
};
}

View File

@ -30,7 +30,7 @@ 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 main_menu = MainMenu.init();
var main_menu = MainMenu.init(renderer);
var game = Game.init(renderer);
var current_state: State = main_menu.state;