From 1b9ba081e2bf0b7f4be0088ea6d7d6c42294b3ad Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 14 Sep 2025 09:34:51 +0200 Subject: [PATCH] feat: make rendering work with the grid struct. --- game/draw/grid/grid.go | 33 ++++++++++++++++++++------------- game/draw/renderer.go | 6 +++--- main.go | 27 +++++++++++++++++++++------ 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/game/draw/grid/grid.go b/game/draw/grid/grid.go index 5bb25a6..3a6fe83 100644 --- a/game/draw/grid/grid.go +++ b/game/draw/grid/grid.go @@ -3,6 +3,7 @@ package grid import ( "image/color" + "tetris/engine/graphics" "tetris/engine/render" rl "github.com/gen2brain/raylib-go/raylib" @@ -16,19 +17,25 @@ const ( CELL_SPACING = 1 ) -func DrawBackground(rect rl.RectangleInt32, col color.RGBA, border_size int32, border_col color.RGBA) { +type Grid interface { + Width() int32 + Height() int32 + Tile(x, y byte) graphics.Tile +} + +func DrawBackground(pos rl.Vector2, grid Grid, col color.RGBA, border_size int32, border_col color.RGBA) { render.DrawRectBorder(rl.RectangleInt32{ - X: int32(rect.X), - Y: int32(rect.Y), - Width: int32((rect.Width * (CELL_SIZE + CELL_SPACING)) + CELL_SPACING), - Height: int32((rect.Height * (CELL_SIZE + CELL_SPACING)) + CELL_SPACING), + X: int32(pos.X), + Y: int32(pos.Y), + Width: (grid.Width() * (CELL_SIZE + CELL_SPACING)) + CELL_SPACING, + Height: (grid.Height() * (CELL_SIZE + CELL_SPACING)) + CELL_SPACING, }, col, border_size, border_col) } -func Draw(rect rl.RectangleInt32) { +func Draw(pos rl.Vector2, grid Grid) { // offset for background. - rect.X = rect.X + CELL_SPACING - rect.Y = rect.Y + CELL_SPACING + pos.X = pos.X + CELL_SPACING + pos.Y = pos.Y + CELL_SPACING cell := rl.Rectangle{ X: 0, @@ -37,11 +44,11 @@ func Draw(rect rl.RectangleInt32) { Height: float32(CELL_SIZE), } - for y := range rect.Height { - for x := range rect.Width { - cell.X = float32(rect.X + (x * (CELL_SIZE + CELL_SPACING))) - cell.Y = float32(rect.Y + (y * (CELL_SIZE + CELL_SPACING))) - rl.DrawRectangleRec(cell, rl.Black) + for y := range grid.Height() { + for x := range grid.Width() { + cell.X = float32(int32(pos.X) + (x * (CELL_SIZE + CELL_SPACING))) + cell.Y = float32(int32(pos.Y) + (y * (CELL_SIZE + CELL_SPACING))) + render.DrawTextureRec(grid.Tile(byte(x), byte(y)).GetTexRect(), cell) } } } diff --git a/game/draw/renderer.go b/game/draw/renderer.go index b94a427..fcef812 100644 --- a/game/draw/renderer.go +++ b/game/draw/renderer.go @@ -34,7 +34,7 @@ func (r Renderer) DrawFrame(rect rl.RectangleInt32) { render.DrawRectBorder(rect, r.Theme.FrameBG, BORDER_WIDTH, r.Theme.FrameBorder) } -func (r Renderer) DrawGrid(rect rl.RectangleInt32) { - grid.DrawBackground(rect, r.Theme.GridBackground, BORDER_WIDTH, r.Theme.FrameBorder) - grid.Draw(rect) +func (r Renderer) DrawGrid(pos rl.Vector2, g grid.Grid) { + grid.DrawBackground(pos, g, r.Theme.GridBackground, BORDER_WIDTH, r.Theme.FrameBorder) + grid.Draw(pos, g) } diff --git a/main.go b/main.go index 243d8fd..aee9e03 100644 --- a/main.go +++ b/main.go @@ -6,12 +6,32 @@ import ( "tetris/assets" "tetris/engine/graphics" "tetris/engine/render" + "tetris/game" "tetris/game/draw" rl "github.com/gen2brain/raylib-go/raylib" ) +// Create a grid with a block patter to test +// if all the blocks renders correctly +func createGrid() *game.Grid { + grid := &game.Grid{} + + c := 0 + for y := range byte(grid.Height() - 2) { + for x := range byte(grid.Width()) { + grid.Set(x, y, game.Block((c%7)+1)) + c++ + } + } + + return grid +} + func main() { + grid := createGrid() + + // Set random blocks to test render.Init(render.Config{ Title: "Tetris", WindowWidth: 685, @@ -40,12 +60,7 @@ func main() { for !rl.WindowShouldClose() { render.Begin(r.Theme.GridBackground) - r.DrawGrid(rl.RectangleInt32{ - X: 25, - Y: 25, - Width: 10, - Height: 16, - }) + r.DrawGrid(rl.NewVector2(25, 25), grid) r.DrawFrame(rl.RectangleInt32{X: 400, Y: 25, Width: 250, Height: 100}) r.DrawHeaderText(410, 30, "Score") r.DrawText(410, 65, "999999")