From b1bc503bd52c7da80141c6fd92ed99d4002b59e1 Mon Sep 17 00:00:00 2001 From: Tim Wundenberg Date: Sun, 22 Feb 2026 00:15:34 +0100 Subject: [PATCH] extract render method --- src/main.zig | 70 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/src/main.zig b/src/main.zig index e5e7484..9762425 100644 --- a/src/main.zig +++ b/src/main.zig @@ -33,11 +33,6 @@ pub fn main() !void { const deltaTime = rl.getFrameTime(); tickTimer += deltaTime; - rl.beginDrawing(); - defer rl.endDrawing(); - - rl.clearBackground(.white); - if (rl.isKeyPressed(.up)) { game.setDirection(g.Direction.up); } @@ -57,28 +52,47 @@ pub fn main() !void { try game.tick(); } - // Border - const fieldWidth = game.playFieldSize[0] * scaleFactor + scaleFactor; - const fieldHeight = game.playFieldSize[1] * scaleFactor + scaleFactor; - rl.drawRectangle(0, 0, fieldWidth, scaleFactor, .black); // top - rl.drawRectangle(0, 0, scaleFactor, fieldHeight, .black); // left - rl.drawRectangle(0, game.playFieldSize[1] * scaleFactor, fieldWidth, scaleFactor, .black); // bottom - rl.drawRectangle(game.playFieldSize[0] * scaleFactor, 0, scaleFactor, fieldHeight, .black); // right - - // Food - rl.drawRectangle(game.food[0] * scaleFactor, game.food[1] * scaleFactor, scaleFactor, scaleFactor, .red); - - // Snake - for (game.body.items, 0..) |part, i| { - const color: rl.Color = if (i == 0) .green else .dark_green; - rl.drawRectangle(part[0] * scaleFactor, part[1] * scaleFactor, scaleFactor, scaleFactor, color); - } - - if (game.state == g.State.lost) { - rl.drawText(gameOverText, @divTrunc(screenWidth - gameOverTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray); - } - if (game.state == g.State.won) { - rl.drawText(youWinText, @divTrunc(screenWidth - youWinTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray); - } + render( + game, + scaleFactor, + gameOverText, + screenWidth, + gameOverTextWidth, + screenHeight, + fontSize, + youWinText, + youWinTextWidth, + ); + } +} + +fn render(game: g.Game, scaleFactor: comptime_int, gameOverText: [:0]const u8, screenWidth: i32, gameOverTextWidth: i32, screenHeight: i32, fontSize: comptime_int, youWinText: [:0]const u8, youWinTextWidth: i32) void { + rl.beginDrawing(); + defer rl.endDrawing(); + + rl.clearBackground(.white); + + // Border + const fieldWidth = game.playFieldSize[0] * scaleFactor + scaleFactor; + const fieldHeight = game.playFieldSize[1] * scaleFactor + scaleFactor; + rl.drawRectangle(0, 0, fieldWidth, scaleFactor, .black); // top + rl.drawRectangle(0, 0, scaleFactor, fieldHeight, .black); // left + rl.drawRectangle(0, game.playFieldSize[1] * scaleFactor, fieldWidth, scaleFactor, .black); // bottom + rl.drawRectangle(game.playFieldSize[0] * scaleFactor, 0, scaleFactor, fieldHeight, .black); // right + + // Food + rl.drawRectangle(game.food[0] * scaleFactor, game.food[1] * scaleFactor, scaleFactor, scaleFactor, .red); + + // Snake + for (game.body.items, 0..) |part, i| { + const color: rl.Color = if (i == 0) .green else .dark_green; + rl.drawRectangle(part[0] * scaleFactor, part[1] * scaleFactor, scaleFactor, scaleFactor, color); + } + + if (game.state == g.State.lost) { + rl.drawText(gameOverText, @divTrunc(screenWidth - gameOverTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray); + } + if (game.state == g.State.won) { + rl.drawText(youWinText, @divTrunc(screenWidth - youWinTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray); } }