1
0
Fork 0

refactor: cleanup main loop code by moving related code into update/render functions respectively.

This commit is contained in:
Henrik Hautakoski 2025-09-14 22:14:05 +02:00
parent 6cf1c9f6f7
commit 3c847094a0

76
main.go
View file

@ -13,12 +13,47 @@ import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
shape := game.NewShape(game.SHAPE_O)
shape_pos := core.Vec2i8{X: 4, Y: 0}
dropTimer := core.NewIntervalTimer(0.3)
grid := game.Grid{}
var (
shape = game.NewShape(game.SHAPE_O)
shape_pos = core.Vec2i8{X: 4, Y: 0}
dropTimer = core.NewIntervalTimer(0.3)
grid = game.Grid{}
r = draw.Renderer{
Theme: &draw.Theme{
FrameBG: color.RGBA{R: 30, G: 30, B: 46, A: 255},
FrameBorder: color.RGBA{R: 242, G: 205, B: 205, A: 255},
TextHeader: color.RGBA{R: 242, G: 205, B: 205, A: 255},
Text: color.RGBA{R: 205, G: 214, B: 244, A: 255},
GridBackground: color.RGBA{R: 17, G: 17, B: 27, A: 255},
},
}
)
func Update(delta float32) {
if dropTimer.UpdateReset(delta) {
new_pos := shape_pos
new_pos.Y += 1
// Update position if it does not collide
if !game.CheckShapeCollision(new_pos, &shape, &grid) {
shape_pos = new_pos
}
}
}
func Render() {
render.Begin(r.Theme.GridBackground)
r.DrawGrid(rl.NewVector2(25, 25), grid)
draw.DrawShape(rl.NewVector2(25, 25), shape_pos, shape)
r.DrawFrame(rl.RectangleInt32{X: 400, Y: 25, Width: 250, Height: 100})
r.DrawHeaderText(410, 30, "Score")
r.DrawText(410, 65, "999999")
r.DrawFrame(rl.RectangleInt32{X: 400, Y: 150, Width: 250, Height: 200})
render.End()
}
func main() {
// Set random blocks to test
render.Init(render.Config{
Title: "Tetris",
@ -36,35 +71,8 @@ func main() {
render.SetTexture(texture)
render.SetFont(&assets.Font)
r := draw.Renderer{
Theme: &draw.Theme{
FrameBG: color.RGBA{R: 30, G: 30, B: 46, A: 255},
FrameBorder: color.RGBA{R: 242, G: 205, B: 205, A: 255},
TextHeader: color.RGBA{R: 242, G: 205, B: 205, A: 255},
Text: color.RGBA{R: 205, G: 214, B: 244, A: 255},
GridBackground: color.RGBA{R: 17, G: 17, B: 27, A: 255},
},
}
for !rl.WindowShouldClose() {
if dropTimer.UpdateReset(rl.GetFrameTime()) {
new_pos := shape_pos
new_pos.Y += 1
// Update position if it does not collide
if !game.CheckShapeCollision(new_pos, &shape, &grid) {
shape_pos = new_pos
}
}
render.Begin(r.Theme.GridBackground)
r.DrawGrid(rl.NewVector2(25, 25), grid)
draw.DrawShape(rl.NewVector2(25, 25), shape_pos, shape)
r.DrawFrame(rl.RectangleInt32{X: 400, Y: 25, Width: 250, Height: 100})
r.DrawHeaderText(410, 30, "Score")
r.DrawText(410, 65, "999999")
r.DrawFrame(rl.RectangleInt32{X: 400, Y: 150, Width: 250, Height: 200})
render.End()
Update(rl.GetFrameTime())
Render()
}
}