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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue