From 4f459e51255b83fef7f69d7046f08c7d8482be0f Mon Sep 17 00:00:00 2001 From: dusk Date: Mon, 25 Sep 2023 15:09:32 +0200 Subject: [PATCH] Score rendering --- src/Game.zig | 8 ++++++-- src/Game/Grid.zig | 5 ++++- src/Game/Renderer.zig | 16 ++++++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Game.zig b/src/Game.zig index ddb7fc6..88e1118 100644 --- a/src/Game.zig +++ b/src/Game.zig @@ -11,6 +11,8 @@ const input = @import("input.zig"); const Renderer = @import("Game/Renderer.zig"); const movement = @import("Game/movement.zig"); +pub const Score = u64; + const Self = @This(); // Codes to access action list @@ -36,6 +38,8 @@ held: ?Piece.Type, piece: Piece, shadow: Piece, +score: Score = 0, + action_list: ActionList = .{ .right = Action.init(input.game_right, actionRight), // Right .left = Action.init(input.game_left, actionLeft), // Left @@ -70,7 +74,7 @@ pub fn init(renderer: *Renderer.Renderer) Self { .timer_left_das = Timer.init(0.2), .timer_right_arr = Timer.init(0.05), .timer_right_das = Timer.init(0.2), - .timer_gravity = Timer.init(0.2), + .timer_gravity = Timer.init(0.3), }; // Get a piece from the bag @@ -205,7 +209,7 @@ pub fn tick(self: *Self) State { } } - self.grid.clearLines(); + self.score += self.grid.clearLines(); self.renderer.render(self.*); diff --git a/src/Game/Grid.zig b/src/Game/Grid.zig index 8ce1a9f..e71f1a4 100644 --- a/src/Game/Grid.zig +++ b/src/Game/Grid.zig @@ -30,7 +30,8 @@ pub fn init() Self { return self; } -pub fn clearLines(self: *Self) void { +pub fn clearLines(self: *Self) u8 { + var ncleared: u8 = 0; for (self.cells, 0..) |_, y| { for (self.cells[y], 0..) |cell_x, x| { if (cell_x.free) { @@ -42,8 +43,10 @@ pub fn clearLines(self: *Self) void { for (self.cells[1..y], 0..) |_, i| { self.cells[y - i] = self.cells[y - i - 1]; } + ncleared += 1; } } } } + return ncleared; } diff --git a/src/Game/Renderer.zig b/src/Game/Renderer.zig index 516177e..dacef5d 100644 --- a/src/Game/Renderer.zig +++ b/src/Game/Renderer.zig @@ -1,5 +1,4 @@ -const SDL = @import("sdl2"); - +const std = @import("std"); const Game = @import("../Game.zig"); const Bag = @import("Bag.zig"); const Grid = @import("Grid.zig"); @@ -105,6 +104,7 @@ pub fn render(self: *Self, game: Game) void { renderPiece(self, game.shadow); renderBag(self, game); renderHeld(self, game); + renderScore(self, game.score); } pub fn renderPiece(self: *Self, piece: Piece) void { @@ -130,6 +130,7 @@ pub fn renderGrid(self: *Self, grid: Grid) void { const pos_x = self.grid_pos_x; const pos_y = self.grid_pos_y; + var visible = grid.cells[Grid.buffer..]; for (visible, 0..) |_, y| { for (visible[y], 0..) |_, x| { @@ -151,3 +152,14 @@ pub fn renderGrid(self: *Self, grid: Grid) void { } } } + +pub fn renderScore(self: *Self, score: Game.Score) void { + + const s_size = self.renderer.getOutputSize(); + + var buffer: [32]u8 = undefined; + var score_string = std.fmt.bufPrintZ(&buffer, "Score: {d:0>16}", .{score}) catch "Score: " ++ "9" ** 16; + + self.renderer.setColor(.{0,0,0,255}); + self.renderer.drawText(score_string, s_size.width - 400, 50, 20); +}