54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package grid
|
|
|
|
import (
|
|
"image/color"
|
|
|
|
"tetris/engine/graphics"
|
|
"tetris/engine/render"
|
|
|
|
rl "github.com/gen2brain/raylib-go/raylib"
|
|
)
|
|
|
|
const (
|
|
// How many pixels wide and tall each cell is.
|
|
CELL_SIZE = 32
|
|
|
|
// Number of pixels between each cell
|
|
CELL_SPACING = 1
|
|
)
|
|
|
|
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(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(pos rl.Vector2, grid Grid) {
|
|
// offset for background.
|
|
pos.X = pos.X + CELL_SPACING
|
|
pos.Y = pos.Y + CELL_SPACING
|
|
|
|
cell := rl.Rectangle{
|
|
X: 0,
|
|
Y: 0,
|
|
Width: float32(CELL_SIZE),
|
|
Height: float32(CELL_SIZE),
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|