feat: make rendering work with the grid struct.
This commit is contained in:
parent
2a84c7bf6a
commit
1b9ba081e2
3 changed files with 44 additions and 22 deletions
|
|
@ -3,6 +3,7 @@ package grid
|
||||||
import (
|
import (
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
|
"tetris/engine/graphics"
|
||||||
"tetris/engine/render"
|
"tetris/engine/render"
|
||||||
|
|
||||||
rl "github.com/gen2brain/raylib-go/raylib"
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
|
@ -16,19 +17,25 @@ const (
|
||||||
CELL_SPACING = 1
|
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{
|
render.DrawRectBorder(rl.RectangleInt32{
|
||||||
X: int32(rect.X),
|
X: int32(pos.X),
|
||||||
Y: int32(rect.Y),
|
Y: int32(pos.Y),
|
||||||
Width: int32((rect.Width * (CELL_SIZE + CELL_SPACING)) + CELL_SPACING),
|
Width: (grid.Width() * (CELL_SIZE + CELL_SPACING)) + CELL_SPACING,
|
||||||
Height: int32((rect.Height * (CELL_SIZE + CELL_SPACING)) + CELL_SPACING),
|
Height: (grid.Height() * (CELL_SIZE + CELL_SPACING)) + CELL_SPACING,
|
||||||
}, col, border_size, border_col)
|
}, col, border_size, border_col)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Draw(rect rl.RectangleInt32) {
|
func Draw(pos rl.Vector2, grid Grid) {
|
||||||
// offset for background.
|
// offset for background.
|
||||||
rect.X = rect.X + CELL_SPACING
|
pos.X = pos.X + CELL_SPACING
|
||||||
rect.Y = rect.Y + CELL_SPACING
|
pos.Y = pos.Y + CELL_SPACING
|
||||||
|
|
||||||
cell := rl.Rectangle{
|
cell := rl.Rectangle{
|
||||||
X: 0,
|
X: 0,
|
||||||
|
|
@ -37,11 +44,11 @@ func Draw(rect rl.RectangleInt32) {
|
||||||
Height: float32(CELL_SIZE),
|
Height: float32(CELL_SIZE),
|
||||||
}
|
}
|
||||||
|
|
||||||
for y := range rect.Height {
|
for y := range grid.Height() {
|
||||||
for x := range rect.Width {
|
for x := range grid.Width() {
|
||||||
cell.X = float32(rect.X + (x * (CELL_SIZE + CELL_SPACING)))
|
cell.X = float32(int32(pos.X) + (x * (CELL_SIZE + CELL_SPACING)))
|
||||||
cell.Y = float32(rect.Y + (y * (CELL_SIZE + CELL_SPACING)))
|
cell.Y = float32(int32(pos.Y) + (y * (CELL_SIZE + CELL_SPACING)))
|
||||||
rl.DrawRectangleRec(cell, rl.Black)
|
render.DrawTextureRec(grid.Tile(byte(x), byte(y)).GetTexRect(), cell)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ func (r Renderer) DrawFrame(rect rl.RectangleInt32) {
|
||||||
render.DrawRectBorder(rect, r.Theme.FrameBG, BORDER_WIDTH, r.Theme.FrameBorder)
|
render.DrawRectBorder(rect, r.Theme.FrameBG, BORDER_WIDTH, r.Theme.FrameBorder)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Renderer) DrawGrid(rect rl.RectangleInt32) {
|
func (r Renderer) DrawGrid(pos rl.Vector2, g grid.Grid) {
|
||||||
grid.DrawBackground(rect, r.Theme.GridBackground, BORDER_WIDTH, r.Theme.FrameBorder)
|
grid.DrawBackground(pos, g, r.Theme.GridBackground, BORDER_WIDTH, r.Theme.FrameBorder)
|
||||||
grid.Draw(rect)
|
grid.Draw(pos, g)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
27
main.go
27
main.go
|
|
@ -6,12 +6,32 @@ import (
|
||||||
"tetris/assets"
|
"tetris/assets"
|
||||||
"tetris/engine/graphics"
|
"tetris/engine/graphics"
|
||||||
"tetris/engine/render"
|
"tetris/engine/render"
|
||||||
|
"tetris/game"
|
||||||
"tetris/game/draw"
|
"tetris/game/draw"
|
||||||
|
|
||||||
rl "github.com/gen2brain/raylib-go/raylib"
|
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() {
|
func main() {
|
||||||
|
grid := createGrid()
|
||||||
|
|
||||||
|
// Set random blocks to test
|
||||||
render.Init(render.Config{
|
render.Init(render.Config{
|
||||||
Title: "Tetris",
|
Title: "Tetris",
|
||||||
WindowWidth: 685,
|
WindowWidth: 685,
|
||||||
|
|
@ -40,12 +60,7 @@ func main() {
|
||||||
|
|
||||||
for !rl.WindowShouldClose() {
|
for !rl.WindowShouldClose() {
|
||||||
render.Begin(r.Theme.GridBackground)
|
render.Begin(r.Theme.GridBackground)
|
||||||
r.DrawGrid(rl.RectangleInt32{
|
r.DrawGrid(rl.NewVector2(25, 25), grid)
|
||||||
X: 25,
|
|
||||||
Y: 25,
|
|
||||||
Width: 10,
|
|
||||||
Height: 16,
|
|
||||||
})
|
|
||||||
r.DrawFrame(rl.RectangleInt32{X: 400, Y: 25, Width: 250, Height: 100})
|
r.DrawFrame(rl.RectangleInt32{X: 400, Y: 25, Width: 250, Height: 100})
|
||||||
r.DrawHeaderText(410, 30, "Score")
|
r.DrawHeaderText(410, 30, "Score")
|
||||||
r.DrawText(410, 65, "999999")
|
r.DrawText(410, 65, "999999")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue