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 (
|
||||
"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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
27
main.go
27
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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue