From db9ec53d354d746748aba10be37bed53d79ecd4b Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 18 Oct 2025 11:28:26 +0200 Subject: [PATCH 01/21] feat: add line clear animation --- README.md | 2 +- game/grid.go | 23 +++++++++++++ game/line_clear_animation.go | 57 +++++++++++++++++++++++++++++++++ game/state/handlers/gameplay.go | 50 ++++++++++++++++++----------- 4 files changed, 112 insertions(+), 20 deletions(-) create mode 100644 game/line_clear_animation.go diff --git a/README.md b/README.md index 35e6bb5..a1ff326 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ go build tetris.go ## Roadmap -- [ ] Line clear animations +- [x] Line clear animations - [ ] Hard drop and ghost piece - [ ] Level/speed progression and pause - [ ] High score persistence diff --git a/game/grid.go b/game/grid.go index d951d2a..3cf9b6b 100644 --- a/game/grid.go +++ b/game/grid.go @@ -1,6 +1,8 @@ package game import ( + "slices" + "tetris/engine/graphics" ) @@ -44,6 +46,16 @@ func (g *Grid) ClearFullRows() byte { return completed } +func (g *Grid) FullRows() []byte { + rows := []byte{} + for y := byte(0); y < byte(g.Height()); y++ { + if g.IsRowFull(y) { + rows = append(rows, y) + } + } + return rows +} + func (g *Grid) IsRowFull(y byte) bool { for x := range byte(g.Width()) { if g.At(x, y) == BLOCK_EMPTY { @@ -53,6 +65,17 @@ func (g *Grid) IsRowFull(y byte) bool { return true } +func (g *Grid) MoveRowsDown(rows ...byte) { + completed := byte(0) + for y := int(g.Height() - 1); y >= 0; y-- { + if slices.Contains(rows, byte(y)) { + completed++ + } else if completed > 0 { + g.MoveRowDown(byte(y), completed) + } + } +} + func (g *Grid) MoveRowDown(y, num_rows byte) { w := uint16(g.Width()) src := uint16(y) * w diff --git a/game/line_clear_animation.go b/game/line_clear_animation.go new file mode 100644 index 0000000..b5885e9 --- /dev/null +++ b/game/line_clear_animation.go @@ -0,0 +1,57 @@ +package game + +// LineClearAnimation clears completed lines cell-by-cell to create +// a visual wipe effect from the center outward. +// +// Once the lines are cleared, the remaining lines are moved down. +type LineClearAnimation struct { + // lines to clear. + lines []byte + // next index to clear to the left + leftIndex int8 + // next index to clear to the right + rightIndex int8 +} + +func (lca *LineClearAnimation) Reset() { + lca.lines = []byte{} +} + +func (lca LineClearAnimation) NumLines() int { + return len(lca.lines) +} + +func (lca *LineClearAnimation) SetLines(rows []byte) { + lca.lines = rows + + lca.leftIndex = (GRID_WIDTH - 1) / 2 + if (GRID_WIDTH-1)%2 != 0 { + lca.rightIndex = lca.leftIndex + 1 + } else { + lca.rightIndex = lca.leftIndex + } +} + +func (lca *LineClearAnimation) Update(grid *Grid) { + if lca.Completed() { + return + } + + if lca.leftIndex < 0 || lca.rightIndex > GRID_WIDTH { + grid.MoveRowsDown(lca.lines...) + lca.Reset() + return + } + + for _, lineIndex := range lca.lines { + grid.Set(byte(lca.leftIndex), lineIndex, BLOCK_EMPTY) + grid.Set(byte(lca.rightIndex), lineIndex, BLOCK_EMPTY) + } + + lca.leftIndex-- + lca.rightIndex++ +} + +func (lca LineClearAnimation) Completed() bool { + return lca.NumLines() == 0 +} diff --git a/game/state/handlers/gameplay.go b/game/state/handlers/gameplay.go index 0027b0a..19ff3b3 100644 --- a/game/state/handlers/gameplay.go +++ b/game/state/handlers/gameplay.go @@ -16,21 +16,24 @@ import ( ) type GamePlay struct { - shape game.Shape - shape_pos core.Vec2i8 - score game.Score - dropTimer core.IntervalTimer - moveTimer core.IntervalTimer - grid game.Grid - nextShape game.Shape - shapeQueue *game.ShapeQueue - r draw.Renderer + shape game.Shape + shape_pos core.Vec2i8 + score game.Score + dropTimer core.IntervalTimer + moveTimer core.IntervalTimer + lineClearTimer core.IntervalTimer + grid game.Grid + nextShape game.Shape + shapeQueue *game.ShapeQueue + r draw.Renderer + lineClearAnimation game.LineClearAnimation } func NewGamePlay() *GamePlay { return &GamePlay{ - dropTimer: core.NewIntervalTimer(0.3), - moveTimer: core.NewIntervalTimer(0.1), + dropTimer: core.NewIntervalTimer(0.3), + moveTimer: core.NewIntervalTimer(0.1), + lineClearTimer: core.NewIntervalTimer(0.05), r: draw.Renderer{ Theme: &draw.Theme{ FrameBG: color.RGBA{R: 30, G: 30, B: 46, A: 255}, @@ -50,6 +53,7 @@ func (gp *GamePlay) Enter() { gp.shapeQueue = game.NewShapeQueue() gp.nextShape = gp.shapeQueue.Next() gp.SpawnShape() + gp.lineClearAnimation.Reset() } func (GamePlay) Exit() { @@ -81,6 +85,13 @@ func (gp *GamePlay) Update(fsm state.Transitioner, delta float32) { gp.dropTimer.SetInterval(0.3) } + if !gp.lineClearAnimation.Completed() { + if gp.lineClearTimer.UpdateReset(delta) { + gp.lineClearAnimation.Update(&gp.grid) + } + return + } + if rl.IsKeyPressed(rl.KeyUp) { rotated := gp.shape.RotateCW() if !game.CheckShapeCollision(gp.shape_pos, &rotated, &gp.grid) { @@ -107,15 +118,16 @@ func (gp *GamePlay) Update(fsm state.Transitioner, delta float32) { // Update position if it does not collide if game.CheckShapeCollision(new_pos, &gp.shape, &gp.grid) { gp.LockShape() - num_rows := gp.grid.ClearFullRows() - if num_rows > 0 { - audio.Play(assets.SFX_ROW_CLEARED) - gp.score.Lines(num_rows) - } gp.SpawnShape() - - if game.CheckShapeCollision(gp.shape_pos, &gp.shape, &gp.grid) { - fsm.Switch("gameover") + lines := gp.grid.FullRows() + if len(lines) > 0 { + gp.lineClearAnimation.SetLines(lines) + gp.score.Lines(byte(len(lines))) + audio.Play(assets.SFX_ROW_CLEARED) + } else { + if game.CheckShapeCollision(gp.shape_pos, &gp.shape, &gp.grid) { + fsm.Switch("gameover") + } } } else { gp.shape_pos = new_pos From 190c6ad914430edbbedfa29612b6700071e9ab1e Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 18 Oct 2025 11:32:26 +0200 Subject: [PATCH 02/21] chore: rename Rows to Lines in Grid as Lines are more common in tetris. --- game/grid.go | 30 +++++++++++++++--------------- game/line_clear_animation.go | 2 +- game/state/handlers/gameplay.go | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/game/grid.go b/game/grid.go index 3cf9b6b..d166cd0 100644 --- a/game/grid.go +++ b/game/grid.go @@ -33,30 +33,30 @@ func (g *Grid) Set(x, y byte, c Block) { (*g)[uint16(x)+(uint16(y)*GRID_WIDTH)] = c } -func (g *Grid) ClearFullRows() byte { +func (g *Grid) ClearFullLines() byte { completed := byte(0) for y := int(g.Height() - 1); y >= 0; y-- { - if g.IsRowFull(byte(y)) { + if g.IsLineFull(byte(y)) { completed++ - g.ClearRow(byte(y)) + g.ClearLine(byte(y)) } else if completed > 0 { - g.MoveRowDown(byte(y), completed) + g.MoveLineDown(byte(y), completed) } } return completed } -func (g *Grid) FullRows() []byte { - rows := []byte{} +func (g *Grid) FullLines() []byte { + lines := []byte{} for y := byte(0); y < byte(g.Height()); y++ { - if g.IsRowFull(y) { - rows = append(rows, y) + if g.IsLineFull(y) { + lines = append(lines, y) } } - return rows + return lines } -func (g *Grid) IsRowFull(y byte) bool { +func (g *Grid) IsLineFull(y byte) bool { for x := range byte(g.Width()) { if g.At(x, y) == BLOCK_EMPTY { return false @@ -65,26 +65,26 @@ func (g *Grid) IsRowFull(y byte) bool { return true } -func (g *Grid) MoveRowsDown(rows ...byte) { +func (g *Grid) MoveLinesDown(rows ...byte) { completed := byte(0) for y := int(g.Height() - 1); y >= 0; y-- { if slices.Contains(rows, byte(y)) { completed++ } else if completed > 0 { - g.MoveRowDown(byte(y), completed) + g.MoveLineDown(byte(y), completed) } } } -func (g *Grid) MoveRowDown(y, num_rows byte) { +func (g *Grid) MoveLineDown(y, num_lines byte) { w := uint16(g.Width()) src := uint16(y) * w - dst := uint16(y+num_rows) * w + dst := uint16(y+num_lines) * w copy(g[dst:dst+w], g[src:src+w]) clear(g[src : src+w]) } -func (g *Grid) ClearRow(y byte) { +func (g *Grid) ClearLine(y byte) { w := uint16(g.Width()) n := uint16(y) * w clear(g[n : n+w]) diff --git a/game/line_clear_animation.go b/game/line_clear_animation.go index b5885e9..5c650d5 100644 --- a/game/line_clear_animation.go +++ b/game/line_clear_animation.go @@ -38,7 +38,7 @@ func (lca *LineClearAnimation) Update(grid *Grid) { } if lca.leftIndex < 0 || lca.rightIndex > GRID_WIDTH { - grid.MoveRowsDown(lca.lines...) + grid.MoveLinesDown(lca.lines...) lca.Reset() return } diff --git a/game/state/handlers/gameplay.go b/game/state/handlers/gameplay.go index 19ff3b3..48a64f8 100644 --- a/game/state/handlers/gameplay.go +++ b/game/state/handlers/gameplay.go @@ -119,7 +119,7 @@ func (gp *GamePlay) Update(fsm state.Transitioner, delta float32) { if game.CheckShapeCollision(new_pos, &gp.shape, &gp.grid) { gp.LockShape() gp.SpawnShape() - lines := gp.grid.FullRows() + lines := gp.grid.FullLines() if len(lines) > 0 { gp.lineClearAnimation.SetLines(lines) gp.score.Lines(byte(len(lines))) From bcd6025aa371904d8d56069ce936d72142d10082 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 19 Oct 2025 11:36:12 +0200 Subject: [PATCH 03/21] refactor: move ui code from game/state/handlers/menu.go to its own package in game/ui --- game/state/handlers/menu.go | 63 +++++++++++-------------------------- game/ui/button.go | 33 +++++++++++++++++++ game/ui/menu.go | 59 ++++++++++++++++++++++++++++++++++ game/ui/widget.go | 6 ++++ tetris.go | 2 +- 5 files changed, 118 insertions(+), 45 deletions(-) create mode 100644 game/ui/button.go create mode 100644 game/ui/menu.go create mode 100644 game/ui/widget.go diff --git a/game/state/handlers/menu.go b/game/state/handlers/menu.go index 53e38a9..e544d76 100644 --- a/game/state/handlers/menu.go +++ b/game/state/handlers/menu.go @@ -2,59 +2,39 @@ package handlers import ( "tetris/assets" - "tetris/engine/audio" "tetris/engine/render" "tetris/game" "tetris/game/state" + "tetris/game/ui" rl "github.com/gen2brain/raylib-go/raylib" ) -type entry struct { - label string - state string +type MainMenu struct { + menu ui.Menu } -type Menu struct { - selected int - entries []entry -} - -func NewMenu() *Menu { - return &Menu{ - selected: 0, - entries: []entry{ - {"Start", "gameplay"}, - {"Quit", "quit"}, - }, +func NewMainMenu(fsm state.Transitioner) *MainMenu { + return &MainMenu{ + menu: ui.NewMenu([]ui.Widget{ + ui.NewButton("Start", func() { fsm.Switch("gameplay") }), + ui.NewButton("Quit", func() { fsm.Switch("quit") }), + }), } } -func (menu *Menu) Enter() { - menu.selected = 0 +func (main *MainMenu) Enter() { + main.menu.Select(0) } -func (Menu) Exit() { +func (MainMenu) Exit() { } -func (menu *Menu) Update(fsm state.Transitioner, delta float32) { - if rl.IsKeyPressed(rl.KeyEnter) { - fsm.Switch(menu.entries[menu.selected].state) - audio.Play(assets.SFX_MENU_ENTER) - } else if rl.IsKeyPressed(rl.KeyDown) { - if menu.selected+1 < len(menu.entries) { - menu.selected = menu.selected + 1 - audio.Play(assets.SFX_MENU_SELECT) - } - } else if rl.IsKeyPressed(rl.KeyUp) { - if menu.selected-1 >= 0 { - menu.selected = menu.selected - 1 - audio.Play(assets.SFX_MENU_SELECT) - } - } +func (menu *MainMenu) Update(fsm state.Transitioner, delta float32) { + menu.menu.HandleInput() } -func (Menu) renderLogo(offset_x, offset_y int32) { +func (MainMenu) renderLogo(offset_x, offset_y int32) { for y := range assets.LOGO_HEIGHT { for x := range assets.LOGO_STRIDE { index := assets.Logo[x+(y*assets.LOGO_STRIDE)] @@ -76,20 +56,15 @@ func (Menu) renderLogo(offset_x, offset_y int32) { } } -func (menu Menu) renderEntries(offset_x, offset_y int32) { +func (menu MainMenu) renderEntries(offset_x, offset_y int32) { y := offset_y - for i, entry := range menu.entries { - - col := rl.White - if i == menu.selected { - col = rl.Red - } - render.DrawTextCenter(offset_x, y, 32, entry.label, col) + for i, entry := range menu.menu.Entries() { + entry.Draw(offset_x, y, menu.menu.IsSelected(i)) y += 40 } } -func (menu Menu) Render() { +func (menu MainMenu) Render() { render.Begin(rl.Black) menu.renderLogo(20, 150) diff --git a/game/ui/button.go b/game/ui/button.go new file mode 100644 index 0000000..5677142 --- /dev/null +++ b/game/ui/button.go @@ -0,0 +1,33 @@ +package ui + +import ( + "tetris/engine/render" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +type Button struct { + Text string + Action func() +} + +func NewButton(text string, action func()) Button { + return Button{ + Text: text, + Action: action, + } +} + +func (b Button) HandleInput() { + if rl.IsKeyPressed(rl.KeyEnter) { + b.Action() + } +} + +func (b Button) Draw(x, y int32, selected bool) { + col := rl.White + if selected { + col = rl.Red + } + render.DrawTextCenter(x, y, 32, b.Text, col) +} diff --git a/game/ui/menu.go b/game/ui/menu.go new file mode 100644 index 0000000..a6c61e2 --- /dev/null +++ b/game/ui/menu.go @@ -0,0 +1,59 @@ +package ui + +import ( + "tetris/assets" + "tetris/engine/audio" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +type Menu struct { + selected int + entries []Widget +} + +func NewMenu(entries []Widget) Menu { + return Menu{ + entries: entries, + } +} + +func (menu Menu) Entries() []Widget { + return menu.entries +} + +func (menu *Menu) Select(index int) { + menu.selected = min(index, len(menu.entries)-1) +} + +func (menu Menu) Selected() Widget { + return menu.entries[menu.selected] +} + +func (menu Menu) IsSelected(index int) bool { + return menu.selected == index +} + +func (menu *Menu) Next() { + if menu.selected+1 < len(menu.entries) { + menu.selected = menu.selected + 1 + audio.Play(assets.SFX_MENU_SELECT) + } +} + +func (menu *Menu) Previous() { + if menu.selected-1 >= 0 { + menu.selected = menu.selected - 1 + audio.Play(assets.SFX_MENU_SELECT) + } +} + +func (menu *Menu) HandleInput() { + if rl.IsKeyPressed(rl.KeyDown) { + menu.Next() + } else if rl.IsKeyPressed(rl.KeyUp) { + menu.Previous() + } else { + menu.Selected().HandleInput() + } +} diff --git a/game/ui/widget.go b/game/ui/widget.go new file mode 100644 index 0000000..3f03541 --- /dev/null +++ b/game/ui/widget.go @@ -0,0 +1,6 @@ +package ui + +type Widget interface { + HandleInput() + Draw(x, y int32, selected bool) +} diff --git a/tetris.go b/tetris.go index 43830ac..61a9ce6 100644 --- a/tetris.go +++ b/tetris.go @@ -41,7 +41,7 @@ func main() { // Setup state machine. fsm := machine.New() - fsm.Register("menu", handlers.NewMenu()) + fsm.Register("menu", handlers.NewMainMenu(fsm)) fsm.Register("gameover", &handlers.GameOver{}) fsm.Register("gameplay", handlers.NewGamePlay()) fsm.Start("menu") From a1756549f9dbefbfcda259f6839dab6159fd5b2a Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 19 Oct 2025 11:56:13 +0200 Subject: [PATCH 04/21] feat(ui): menu should not call audio module directly, make it more modular with and onSelect event callback --- game/state/handlers/menu.go | 5 +++-- game/ui/menu.go | 22 ++++++++++++++-------- game/uievents/select.go | 10 ++++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 game/uievents/select.go diff --git a/game/state/handlers/menu.go b/game/state/handlers/menu.go index e544d76..e026e84 100644 --- a/game/state/handlers/menu.go +++ b/game/state/handlers/menu.go @@ -6,12 +6,13 @@ import ( "tetris/game" "tetris/game/state" "tetris/game/ui" + "tetris/game/uievents" rl "github.com/gen2brain/raylib-go/raylib" ) type MainMenu struct { - menu ui.Menu + menu *ui.Menu } func NewMainMenu(fsm state.Transitioner) *MainMenu { @@ -19,7 +20,7 @@ func NewMainMenu(fsm state.Transitioner) *MainMenu { menu: ui.NewMenu([]ui.Widget{ ui.NewButton("Start", func() { fsm.Switch("gameplay") }), ui.NewButton("Quit", func() { fsm.Switch("quit") }), - }), + }).OnSelect(uievents.MenuSelect), } } diff --git a/game/ui/menu.go b/game/ui/menu.go index a6c61e2..d456b1f 100644 --- a/game/ui/menu.go +++ b/game/ui/menu.go @@ -1,23 +1,29 @@ package ui import ( - "tetris/assets" - "tetris/engine/audio" - rl "github.com/gen2brain/raylib-go/raylib" ) +type OnSelectCallback func() + type Menu struct { selected int entries []Widget + onSelect OnSelectCallback } -func NewMenu(entries []Widget) Menu { - return Menu{ - entries: entries, +func NewMenu(entries []Widget) *Menu { + return &Menu{ + entries: entries, + onSelect: func() {}, } } +func (menu *Menu) OnSelect(callback OnSelectCallback) *Menu { + menu.onSelect = callback + return menu +} + func (menu Menu) Entries() []Widget { return menu.entries } @@ -37,14 +43,14 @@ func (menu Menu) IsSelected(index int) bool { func (menu *Menu) Next() { if menu.selected+1 < len(menu.entries) { menu.selected = menu.selected + 1 - audio.Play(assets.SFX_MENU_SELECT) + menu.onSelect() } } func (menu *Menu) Previous() { if menu.selected-1 >= 0 { menu.selected = menu.selected - 1 - audio.Play(assets.SFX_MENU_SELECT) + menu.onSelect() } } diff --git a/game/uievents/select.go b/game/uievents/select.go new file mode 100644 index 0000000..7fa6d69 --- /dev/null +++ b/game/uievents/select.go @@ -0,0 +1,10 @@ +package uievents + +import ( + "tetris/assets" + "tetris/engine/audio" +) + +func MenuSelect() { + audio.Play(assets.SFX_MENU_SELECT) +} From 0bc5863284bc09bf43c9a3782d4826c576e7fa9b Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 19 Oct 2025 12:06:10 +0200 Subject: [PATCH 05/21] feat(ui): call onSelect in Select() function and make Next()/Previous() use that function --- game/ui/menu.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/game/ui/menu.go b/game/ui/menu.go index d456b1f..9a5bc83 100644 --- a/game/ui/menu.go +++ b/game/ui/menu.go @@ -29,7 +29,10 @@ func (menu Menu) Entries() []Widget { } func (menu *Menu) Select(index int) { - menu.selected = min(index, len(menu.entries)-1) + if index >= 0 && index < len(menu.entries) { + menu.selected = index + menu.onSelect() + } } func (menu Menu) Selected() Widget { @@ -41,17 +44,11 @@ func (menu Menu) IsSelected(index int) bool { } func (menu *Menu) Next() { - if menu.selected+1 < len(menu.entries) { - menu.selected = menu.selected + 1 - menu.onSelect() - } + menu.Select(menu.selected + 1) } func (menu *Menu) Previous() { - if menu.selected-1 >= 0 { - menu.selected = menu.selected - 1 - menu.onSelect() - } + menu.Select(menu.selected - 1) } func (menu *Menu) HandleInput() { From d1ed55677e2750867150ec4911b6b79bab1fb401 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 19 Oct 2025 22:58:21 +0200 Subject: [PATCH 06/21] feat: dont close application when user presses Esc key --- tetris.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tetris.go b/tetris.go index 61a9ce6..3cb4040 100644 --- a/tetris.go +++ b/tetris.go @@ -22,6 +22,9 @@ func main() { }) defer render.Exit() + // Dont close application when user presses escape + rl.SetExitKey(rl.KeyNull) + // Set window icon if icon := graphics.LoadImageFromMemory(".png", assets.Icon); icon != nil { rl.SetWindowIcon(*icon) From 678670fd376d2098f4ad5ef1491f716545f907c1 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 27 Oct 2025 20:13:34 +0100 Subject: [PATCH 07/21] feat(audio): add SetVolume() and Volume() --- engine/audio/default_manger.go | 8 ++++++++ engine/audio/manager.go | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/engine/audio/default_manger.go b/engine/audio/default_manger.go index 21a3921..ae0a31a 100644 --- a/engine/audio/default_manger.go +++ b/engine/audio/default_manger.go @@ -6,6 +6,14 @@ func LoadLibrary(library *Library) { defaultManager.Load(library) } +func SetVolume(value float32) { + defaultManager.SetVolume(value) +} + +func Volume() float32 { + return defaultManager.Volume() +} + func Play(id SoundID) { defaultManager.Play(id) } diff --git a/engine/audio/manager.go b/engine/audio/manager.go index 36fdd71..63300f6 100644 --- a/engine/audio/manager.go +++ b/engine/audio/manager.go @@ -21,6 +21,14 @@ func (sm *Manager) Load(library *Library) { sm.library = library } +func (sm Manager) SetVolume(value float32) { + rl.SetMasterVolume(value) +} + +func (sm Manager) Volume() float32 { + return rl.GetMasterVolume() +} + func (sm *Manager) play(id SoundID, looping bool) { snd := sm.library.Get(id) From 3d3e8d7a445e3f33902006b194b1aa7712009bbe Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 27 Oct 2025 20:13:44 +0100 Subject: [PATCH 08/21] feat(input): add KeyPressedWithRepeat() --- engine/input/keyboard.go | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 engine/input/keyboard.go diff --git a/engine/input/keyboard.go b/engine/input/keyboard.go new file mode 100644 index 0000000..89134d5 --- /dev/null +++ b/engine/input/keyboard.go @@ -0,0 +1,8 @@ +package input + +import rl "github.com/gen2brain/raylib-go/raylib" + +func KeyPressedWithRepeat(key int32) bool { + return rl.IsKeyPressed(key) || rl.IsKeyPressedRepeat(key) +} + From d407eaad8b3f68b2ae1ab6a068b6ca576f0d83b2 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 27 Oct 2025 20:15:05 +0100 Subject: [PATCH 09/21] feat(render): adding DrawRectOutlineBorder() --- engine/render/rect.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/engine/render/rect.go b/engine/render/rect.go index 3ad993b..399c741 100644 --- a/engine/render/rect.go +++ b/engine/render/rect.go @@ -17,10 +17,15 @@ func DrawRectBorder(rect rl.RectangleInt32, col color.RGBA, border_size int32, b Height: float32(rect.Height), }, col) - rl.DrawRectangleLinesEx(rl.Rectangle{ - X: float32(rect.X - border_size), - Y: float32(rect.Y - border_size), - Width: float32(rect.Width + (border_size * 2)), - Height: float32(rect.Height + (border_size * 2)), - }, float32(border_size), border_col) + DrawRectOutlineBorder(rect, border_size, border_col) +} + +// DrawRectOutlineBorder draws a border (outer) around the rectangle. +func DrawRectOutlineBorder(rect rl.RectangleInt32, size int32, col color.RGBA) { + rl.DrawRectangleLinesEx(rl.Rectangle{ + X: float32(rect.X - size), + Y: float32(rect.Y - size), + Width: float32(rect.Width + (size * 2)), + Height: float32(rect.Height + (size * 2)), + }, float32(size), col) } From 6cf0e4abb4ae67ac5ad1f1ec833caf2abd73b4c5 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 27 Oct 2025 20:17:13 +0100 Subject: [PATCH 10/21] feat(ui): implement new ui system --- game/state/handlers/menu.go | 28 +++---- game/ui/base/focus.go | 26 ++++++ game/ui/button.go | 33 -------- game/ui/component.go | 14 ++++ game/ui/layouts/input_control.go | 57 +++++++++++++ game/ui/layouts/list_box.go | 84 +++++++++++++++++++ game/ui/menu.go | 62 -------------- game/ui/widget.go | 16 +++- game/ui/widgets/button.go | 48 +++++++++++ game/ui/widgets/label.go | 39 +++++++++ game/ui/widgets/slider.go | 136 +++++++++++++++++++++++++++++++ 11 files changed, 430 insertions(+), 113 deletions(-) create mode 100644 game/ui/base/focus.go delete mode 100644 game/ui/button.go create mode 100644 game/ui/component.go create mode 100644 game/ui/layouts/input_control.go create mode 100644 game/ui/layouts/list_box.go delete mode 100644 game/ui/menu.go create mode 100644 game/ui/widgets/button.go create mode 100644 game/ui/widgets/label.go create mode 100644 game/ui/widgets/slider.go diff --git a/game/state/handlers/menu.go b/game/state/handlers/menu.go index e026e84..ef5b389 100644 --- a/game/state/handlers/menu.go +++ b/game/state/handlers/menu.go @@ -6,33 +6,37 @@ import ( "tetris/game" "tetris/game/state" "tetris/game/ui" + "tetris/game/ui/layouts" + "tetris/game/ui/widgets" "tetris/game/uievents" rl "github.com/gen2brain/raylib-go/raylib" ) type MainMenu struct { - menu *ui.Menu + list *layouts.ListBox } func NewMainMenu(fsm state.Transitioner) *MainMenu { return &MainMenu{ - menu: ui.NewMenu([]ui.Widget{ - ui.NewButton("Start", func() { fsm.Switch("gameplay") }), - ui.NewButton("Quit", func() { fsm.Switch("quit") }), - }).OnSelect(uievents.MenuSelect), + list: layouts.NewListBox([]ui.InputWidget{ + widgets.NewButton("Start", 32, func() { fsm.Switch("gameplay") }), + widgets.NewButton("Options", 32, func() { fsm.Switch("options") }), + widgets.NewButton("Quit", 32, func() { fsm.Switch("quit") }), + }).Spacing(10). + OnSelect(uievents.MenuSelect), } } func (main *MainMenu) Enter() { - main.menu.Select(0) + main.list.Select(0) } func (MainMenu) Exit() { } func (menu *MainMenu) Update(fsm state.Transitioner, delta float32) { - menu.menu.HandleInput() + menu.list.HandleInput() } func (MainMenu) renderLogo(offset_x, offset_y int32) { @@ -57,19 +61,11 @@ func (MainMenu) renderLogo(offset_x, offset_y int32) { } } -func (menu MainMenu) renderEntries(offset_x, offset_y int32) { - y := offset_y - for i, entry := range menu.menu.Entries() { - entry.Draw(offset_x, y, menu.menu.IsSelected(i)) - y += 40 - } -} - func (menu MainMenu) Render() { render.Begin(rl.Black) menu.renderLogo(20, 150) - menu.renderEntries(340, 400) + menu.list.Draw(340, 400) render.End() } diff --git a/game/ui/base/focus.go b/game/ui/base/focus.go new file mode 100644 index 0000000..e0e0b10 --- /dev/null +++ b/game/ui/base/focus.go @@ -0,0 +1,26 @@ +package base + +// WithFocus is a base component that implements the Focusable interface +type WithFocus struct { + focus bool +} + +// SetFocus - Set the focus value on this component +func (f *WithFocus) SetFocus(value bool) { + f.focus = value +} + +// Focus - focus this component, syntactic sugar for f.SetFocus(true) +func (f *WithFocus) Focus() { + f.SetFocus(true) +} + +// Unfocus - unfocus this component, syntactic sugar for f.SetFocus(false) +func (f *WithFocus) Unfocus() { + f.SetFocus(false) +} + +// HasFocus - Returns true if this component has focus. +func (f WithFocus) HasFocus() bool { + return f.focus +} diff --git a/game/ui/button.go b/game/ui/button.go deleted file mode 100644 index 5677142..0000000 --- a/game/ui/button.go +++ /dev/null @@ -1,33 +0,0 @@ -package ui - -import ( - "tetris/engine/render" - - rl "github.com/gen2brain/raylib-go/raylib" -) - -type Button struct { - Text string - Action func() -} - -func NewButton(text string, action func()) Button { - return Button{ - Text: text, - Action: action, - } -} - -func (b Button) HandleInput() { - if rl.IsKeyPressed(rl.KeyEnter) { - b.Action() - } -} - -func (b Button) Draw(x, y int32, selected bool) { - col := rl.White - if selected { - col = rl.Red - } - render.DrawTextCenter(x, y, 32, b.Text, col) -} diff --git a/game/ui/component.go b/game/ui/component.go new file mode 100644 index 0000000..083c732 --- /dev/null +++ b/game/ui/component.go @@ -0,0 +1,14 @@ +package ui + +// Focusable is a component that can have focus +type Focusable interface { + SetFocus(value bool) + Focus() + Unfocus() + HasFocus() bool +} + +// ReceivesInput is a component that can receive user input. +type ReceivesInput interface { + HandleInput() +} diff --git a/game/ui/layouts/input_control.go b/game/ui/layouts/input_control.go new file mode 100644 index 0000000..ca256a6 --- /dev/null +++ b/game/ui/layouts/input_control.go @@ -0,0 +1,57 @@ +package layouts + +import ( + "tetris/engine/core" + "tetris/game/ui" + "tetris/game/ui/base" + "tetris/game/ui/widgets" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +// InputControl is a layout that has a label and an a widget associated with it. +type InputControl struct { + *base.WithFocus + label *widgets.Label + widget ui.InputWidget + spacing int +} + +func NewInputControl(label *widgets.Label, wiget ui.InputWidget) *InputControl { + return &InputControl{ + WithFocus: &base.WithFocus{}, + label: label, + widget: wiget, + spacing: 10, + } +} + +func (ic InputControl) Size() core.Vec2i { + return core.Vec2i{ + X: ic.label.Size().X + ic.widget.Size().X + ic.spacing, + Y: max(ic.label.Size().Y, ic.widget.Size().Y), + } +} + +func (ic *InputControl) SetFocus(value bool) { + ic.WithFocus.SetFocus(value) + ic.widget.SetFocus(value) + + // TODO: use a theme system here so colors are not hardcoded. + if value { + ic.label.SetColor(rl.Red) + } else { + ic.label.SetColor(rl.White) + } +} + +func (ic InputControl) HandleInput() { + ic.widget.HandleInput() +} + +func (ic InputControl) Draw(x, y int32) { + size := ic.Size() + label_y := (int32(size.Y) - int32(ic.label.Size().Y)) / 2 + ic.label.Draw(x, y+label_y) + ic.widget.Draw(x+int32(ic.label.Size().X+ic.spacing), y) +} diff --git a/game/ui/layouts/list_box.go b/game/ui/layouts/list_box.go new file mode 100644 index 0000000..4a058cf --- /dev/null +++ b/game/ui/layouts/list_box.go @@ -0,0 +1,84 @@ +package layouts + +import ( + "tetris/game/ui" + "tetris/game/ui/base" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +type OnSelectCallback func() + +type ListBox struct { + *base.WithFocus + selected int + entries []ui.InputWidget + onSelect OnSelectCallback + spacing int +} + +func NewListBox(entries []ui.InputWidget) *ListBox { + lb := &ListBox{ + WithFocus: &base.WithFocus{}, + entries: entries, + onSelect: func() {}, + } + lb.entries[0].SetFocus(true) + return lb +} + +func (lb *ListBox) Spacing(value int) *ListBox { + lb.spacing = value + return lb +} + +func (lb *ListBox) OnSelect(callback OnSelectCallback) *ListBox { + lb.onSelect = callback + return lb +} + +func (lb ListBox) Entries() []ui.InputWidget { + return lb.entries +} + +func (lb *ListBox) Select(index int) { + if index >= 0 && index < len(lb.entries) { + lb.entries[lb.selected].SetFocus(false) + lb.selected = index + lb.entries[lb.selected].SetFocus(true) + lb.onSelect() + } +} + +func (lb ListBox) Selected() ui.InputWidget { + return lb.entries[lb.selected] +} + +func (lb ListBox) IsSelected(index int) bool { + return lb.selected == index +} + +func (lb *ListBox) Next() { + lb.Select(lb.selected + 1) +} + +func (lb *ListBox) Previous() { + lb.Select(lb.selected - 1) +} + +func (lb *ListBox) HandleInput() { + if rl.IsKeyPressed(rl.KeyDown) { + lb.Next() + } else if rl.IsKeyPressed(rl.KeyUp) { + lb.Previous() + } else { + lb.Selected().HandleInput() + } +} + +func (lb *ListBox) Draw(x, y int32) { + for _, ent := range lb.entries { + ent.Draw(x, y) + y += int32(ent.Size().Y + lb.spacing) + } +} diff --git a/game/ui/menu.go b/game/ui/menu.go deleted file mode 100644 index 9a5bc83..0000000 --- a/game/ui/menu.go +++ /dev/null @@ -1,62 +0,0 @@ -package ui - -import ( - rl "github.com/gen2brain/raylib-go/raylib" -) - -type OnSelectCallback func() - -type Menu struct { - selected int - entries []Widget - onSelect OnSelectCallback -} - -func NewMenu(entries []Widget) *Menu { - return &Menu{ - entries: entries, - onSelect: func() {}, - } -} - -func (menu *Menu) OnSelect(callback OnSelectCallback) *Menu { - menu.onSelect = callback - return menu -} - -func (menu Menu) Entries() []Widget { - return menu.entries -} - -func (menu *Menu) Select(index int) { - if index >= 0 && index < len(menu.entries) { - menu.selected = index - menu.onSelect() - } -} - -func (menu Menu) Selected() Widget { - return menu.entries[menu.selected] -} - -func (menu Menu) IsSelected(index int) bool { - return menu.selected == index -} - -func (menu *Menu) Next() { - menu.Select(menu.selected + 1) -} - -func (menu *Menu) Previous() { - menu.Select(menu.selected - 1) -} - -func (menu *Menu) HandleInput() { - if rl.IsKeyPressed(rl.KeyDown) { - menu.Next() - } else if rl.IsKeyPressed(rl.KeyUp) { - menu.Previous() - } else { - menu.Selected().HandleInput() - } -} diff --git a/game/ui/widget.go b/game/ui/widget.go index 3f03541..da9cd17 100644 --- a/game/ui/widget.go +++ b/game/ui/widget.go @@ -1,6 +1,18 @@ package ui +import ( + "tetris/engine/core" +) + +// Widget is a base widget (Can be drawn on screen) type Widget interface { - HandleInput() - Draw(x, y int32, selected bool) + Size() core.Vec2i + Draw(x, y int32) +} + +// InputWidget is a widget that also handles user input and can have focus. +type InputWidget interface { + Widget + Focusable + ReceivesInput } diff --git a/game/ui/widgets/button.go b/game/ui/widgets/button.go new file mode 100644 index 0000000..62efd5d --- /dev/null +++ b/game/ui/widgets/button.go @@ -0,0 +1,48 @@ +package widgets + +import ( + "tetris/engine/core" + "tetris/engine/render" + "tetris/game/ui/base" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +type Button struct { + *base.WithFocus + Text string + TextSize int32 + Action func() +} + +func NewButton(text string, size int32, action func()) Button { + return Button{ + WithFocus: &base.WithFocus{}, + Text: text, + TextSize: size, + Action: action, + } +} + +func (b Button) Size() core.Vec2i { + s := int(b.TextSize) + return core.Vec2i{ + X: len(b.Text) * s, + Y: s, + } +} + +func (b Button) HandleInput() { + if rl.IsKeyPressed(rl.KeyEnter) { + b.Action() + } +} + +func (b Button) Draw(x, y int32) { + // TODO: use a theme system here so colors are not hardcoded. + col := rl.White + if b.HasFocus() { + col = rl.Red + } + render.DrawTextCenter(x, y, b.TextSize, b.Text, col) +} diff --git a/game/ui/widgets/label.go b/game/ui/widgets/label.go new file mode 100644 index 0000000..027e3d5 --- /dev/null +++ b/game/ui/widgets/label.go @@ -0,0 +1,39 @@ +package widgets + +import ( + "image/color" + + "tetris/engine/core" + "tetris/engine/render" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +type Label struct { + text string + size int32 + color color.RGBA +} + +func NewLabel(text string, size int32) *Label { + return &Label{ + text: text, + size: size, + color: rl.White, + } +} + +func (label Label) Size() core.Vec2i { + return core.Vec2i{ + X: int(label.size) * len(label.text), + Y: int(label.size), + } +} + +func (label *Label) SetColor(col color.RGBA) { + label.color = col +} + +func (label Label) Draw(x, y int32) { + render.DrawText(x, y, label.size, label.text, label.color) +} diff --git a/game/ui/widgets/slider.go b/game/ui/widgets/slider.go new file mode 100644 index 0000000..1ae833f --- /dev/null +++ b/game/ui/widgets/slider.go @@ -0,0 +1,136 @@ +package widgets + +import ( + "fmt" + "image/color" + + "tetris/engine/core" + "tetris/engine/input" + "tetris/engine/render" + "tetris/game/ui/base" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +const ( + sliderBorderSize = 4 + sliderWidth = 200 + sliderHeight = 30 + sliderTextSize = 12 + sliderTextOffset = 6 +) + +type SliderOnChange func(this *Slider) + +type Slider struct { + *base.WithFocus + Min int + Max int + Step int + Value int + borderSize int32 + onChange SliderOnChange +} + +func NewSlider(min, max, step int) *Slider { + return &Slider{ + WithFocus: &base.WithFocus{}, + Min: min, + Max: max, + Step: step, + borderSize: 4, + } +} + +func (slider *Slider) WithOnChange(callback SliderOnChange) *Slider { + slider.onChange = callback + return slider +} + +func (slider Slider) Size() core.Vec2i { + w := sliderWidth + (sliderBorderSize * 2) + return core.Vec2i{ + X: w + sliderTextOffset + (sliderTextSize * len(slider.ValueText())), + Y: sliderHeight + (sliderBorderSize * 2), + } +} + +func (slider *Slider) HandleInput() { + if input.KeyPressedWithRepeat(rl.KeyLeft) { + slider.Decrement() + } else if input.KeyPressedWithRepeat(rl.KeyRight) { + slider.Increment() + } +} + +func (slider *Slider) SetValue(value int) { + if value != slider.Value { + slider.Value = value + slider.onChange(slider) + } +} + +func (slider *Slider) Increment() { + slider.SetValue(min(slider.Max, slider.Value+slider.Step)) +} + +func (slider *Slider) Decrement() { + slider.SetValue(max(slider.Min, slider.Value-slider.Step)) +} + +func (slider Slider) Percent() float32 { + return float32(slider.Min+slider.Value) / float32(slider.Max) +} + +func (slider Slider) drawTrack(rect rl.RectangleInt32) { + spacing := int32(2) + num_marks := int32(10) + mark_width := float32(rect.Width-((num_marks+1)*spacing)) / float32(num_marks) + + markRect := rl.Rectangle{ + X: float32(rect.X + spacing), + Y: float32(rect.Y + spacing), + Width: mark_width, + Height: float32(rect.Height - (spacing * 2)), + } + + for range num_marks { + rl.DrawRectangleRec(markRect, rl.DarkGray) + markRect.X += mark_width + float32(spacing) + } +} + +func (slider Slider) drawHandle(x, y, width int32, height int32, col color.RGBA) { + handleWidth := int32(10) + x = x + int32(float32(width-handleWidth-2)*slider.Percent()) + rl.DrawRectangle(x, y, handleWidth, height, col) +} + +func (slider Slider) ValueText() string { + return fmt.Sprintf("%d", int(slider.Percent()*100)) +} + +func (slider Slider) Draw(x, y int32) { + // rl.DrawRectangle(x, y, int32(slider.Size().X), int32(slider.Size().Y), rl.Green) + + // TODO: use a theme system here so colors are not hardcoded. + handleColor := rl.White + if slider.HasFocus() { + handleColor = rl.Red + } + + rect := rl.RectangleInt32{ + X: x + sliderBorderSize, + Y: y + sliderBorderSize, + Width: sliderWidth, + Height: sliderHeight, + } + + render.DrawRectOutlineBorder(rect, slider.borderSize, handleColor) + slider.drawTrack(rect) + slider.drawHandle(rect.X+1, rect.Y+1, int32(rect.Width), sliderHeight-2, handleColor) + + textOffset := x + int32(rect.Width) + (sliderBorderSize * 2) + sliderTextOffset + + render.DrawText(textOffset, rect.Y+8, sliderTextSize, slider.ValueText(), rl.White) +} From 7d5d5b9a54b92e00fa159bef64cc8e096c626726 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 27 Oct 2025 20:19:22 +0100 Subject: [PATCH 11/21] feat(engine): add number clamping functions --- engine/core/num_clamp.go | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 engine/core/num_clamp.go diff --git a/engine/core/num_clamp.go b/engine/core/num_clamp.go new file mode 100644 index 0000000..5ebb6c6 --- /dev/null +++ b/engine/core/num_clamp.go @@ -0,0 +1,9 @@ +package core + +func ByteToClampedFloat32(v byte) float32 { + return min(float32(v)/255.0, 1.0) +} + +func ClampedFloat32ToByte(v float32) byte { + return byte(min(v, 1.0) * 255) +} From 30b194619ff76136bb3f5ec68127c8c4bac445a5 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 27 Oct 2025 20:21:18 +0100 Subject: [PATCH 12/21] feat(ui): add options menu --- assets/sound.go | 11 +++--- game/state/handlers/options.go | 62 ++++++++++++++++++++++++++++++++++ tetris.go | 1 + 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 game/state/handlers/options.go diff --git a/assets/sound.go b/assets/sound.go index 09503d4..9bc2365 100644 --- a/assets/sound.go +++ b/assets/sound.go @@ -5,11 +5,12 @@ import ( ) const ( - SFX_SHAPE_LOCKED audio.SoundID = 0 - SFX_ROW_CLEARED audio.SoundID = 1 - SFX_MENU_SELECT audio.SoundID = 0 - SFX_MENU_ENTER audio.SoundID = 1 - SFX_GAME_OVER audio.SoundID = 2 + SFX_SHAPE_LOCKED audio.SoundID = 0 + SFX_ROW_CLEARED audio.SoundID = 1 + SFX_MENU_SELECT audio.SoundID = 0 + SFX_MENU_ENTER audio.SoundID = 1 + SFX_MENU_SOUND_VOLUME_SELECT audio.SoundID = 1 + SFX_GAME_OVER audio.SoundID = 2 ) func LoadSound() *audio.Library { diff --git a/game/state/handlers/options.go b/game/state/handlers/options.go new file mode 100644 index 0000000..7627964 --- /dev/null +++ b/game/state/handlers/options.go @@ -0,0 +1,62 @@ +package handlers + +import ( + "tetris/assets" + "tetris/engine/audio" + "tetris/engine/core" + "tetris/engine/render" + "tetris/game/state" + "tetris/game/ui" + "tetris/game/ui/layouts" + "tetris/game/ui/widgets" + "tetris/game/uievents" + + rl "github.com/gen2brain/raylib-go/raylib" +) + +type OptionsMenu struct { + sndVolume *widgets.Slider + list *layouts.ListBox +} + +func soundVolumeChanged(widget *widgets.Slider) { + value := core.ByteToClampedFloat32(byte(widget.Value)) + + audio.SetVolume(value) + audio.Play(assets.SFX_MENU_SOUND_VOLUME_SELECT) +} + +func NewOptionsMenu() *OptionsMenu { + sndVolume := widgets.NewSlider(0, 255, 10).WithOnChange(soundVolumeChanged) + return &OptionsMenu{ + sndVolume: sndVolume, + list: layouts.NewListBox([]ui.InputWidget{ + layouts.NewInputControl(widgets.NewLabel("Sound Volume", 16), sndVolume), + }).Spacing(10).OnSelect(uievents.MenuSelect), + } +} + +func (menu *OptionsMenu) Enter() { + vol := core.ClampedFloat32ToByte(audio.Volume()) + menu.sndVolume.SetValue(int(vol)) +} + +func (OptionsMenu) Exit() { +} + +func (menu *OptionsMenu) Update(fsm state.Transitioner, delta float32) { + if rl.IsKeyPressed(rl.KeyEscape) { + fsm.Switch("menu") + } else { + menu.list.HandleInput() + } +} + +func (opt OptionsMenu) Render() { + render.Begin(rl.Black) + + render.DrawTextCenter(340, 100, 32, "Options", rl.White) + opt.list.Draw(150, 200) + + render.End() +} diff --git a/tetris.go b/tetris.go index 3cb4040..40ae3eb 100644 --- a/tetris.go +++ b/tetris.go @@ -45,6 +45,7 @@ func main() { // Setup state machine. fsm := machine.New() fsm.Register("menu", handlers.NewMainMenu(fsm)) + fsm.Register("options", handlers.NewOptionsMenu()) fsm.Register("gameover", &handlers.GameOver{}) fsm.Register("gameplay", handlers.NewGamePlay()) fsm.Start("menu") From 8a0224984572b8c167d5298a35823b6b2daaabe5 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 28 Oct 2025 06:35:14 +0100 Subject: [PATCH 13/21] feat: implement persistant configuration --- .gitignore | 1 + game/config.go | 21 ++++++++++++++++ game/config/config.go | 45 ++++++++++++++++++++++++++++++++++ game/state/handlers/options.go | 5 ++++ tetris.go | 10 ++++++++ 5 files changed, 82 insertions(+) create mode 100644 game/config.go create mode 100644 game/config/config.go diff --git a/.gitignore b/.gitignore index e0cc394..d46f6d4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /tetris /tetris.exe +/config.json diff --git a/game/config.go b/game/config.go new file mode 100644 index 0000000..710fb0c --- /dev/null +++ b/game/config.go @@ -0,0 +1,21 @@ +package game + +import ( + "io" + + "tetris/game/config" +) + +// Global config variable with default values. +var Config = &config.Config{ + SoundVolume: 255, +} + +func LoadConfig(configPath string) error { + cfg, err := config.LoadFromFile(configPath) + if err != nil && err != io.EOF { + return err + } + Config = cfg + return nil +} diff --git a/game/config/config.go b/game/config/config.go new file mode 100644 index 0000000..82ad3d8 --- /dev/null +++ b/game/config/config.go @@ -0,0 +1,45 @@ +package config + +import ( + "encoding/json" + "io" + "os" +) + +type Config struct { + fd *os.File + SoundVolume byte `json:"sound_volume"` +} + +func DefaultConfig(fd *os.File) *Config { + cfg := Config{ + SoundVolume: 255, + } + cfg.fd = fd + return &cfg +} + +func LoadFromFile(filename string) (*Config, error) { + fd, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0o644) + if err != nil { + return nil, err + } + + cfg := DefaultConfig(fd) + err = json.NewDecoder(fd).Decode(cfg) + return cfg, err +} + +func (c Config) Save() error { + if err := c.fd.Truncate(0); err != nil { + return err + } + if _, err := c.fd.Seek(0, io.SeekStart); err != nil { + return err + } + return json.NewEncoder(c.fd).Encode(c) +} + +func (c Config) Close() error { + return c.fd.Close() +} diff --git a/game/state/handlers/options.go b/game/state/handlers/options.go index 7627964..8fabd6d 100644 --- a/game/state/handlers/options.go +++ b/game/state/handlers/options.go @@ -5,6 +5,7 @@ import ( "tetris/engine/audio" "tetris/engine/core" "tetris/engine/render" + "tetris/game" "tetris/game/state" "tetris/game/ui" "tetris/game/ui/layouts" @@ -24,6 +25,10 @@ func soundVolumeChanged(widget *widgets.Slider) { audio.SetVolume(value) audio.Play(assets.SFX_MENU_SOUND_VOLUME_SELECT) + + // Store in config and save + game.Config.SoundVolume = byte(widget.Value) + game.Config.Save() } func NewOptionsMenu() *OptionsMenu { diff --git a/tetris.go b/tetris.go index 40ae3eb..c5dfcbf 100644 --- a/tetris.go +++ b/tetris.go @@ -1,10 +1,14 @@ package main import ( + "fmt" + "tetris/assets" "tetris/engine/audio" + "tetris/engine/core" "tetris/engine/graphics" "tetris/engine/render" + "tetris/game" "tetris/game/state/handlers" "tetris/game/state/machine" @@ -12,6 +16,10 @@ import ( ) func main() { + if err := game.LoadConfig("./config.json"); err != nil { + fmt.Println("Failed to load config:", err) + } + render.Init(render.Config{ Title: "Tetris", WindowWidth: 685, @@ -34,6 +42,8 @@ func main() { audio.Init() defer audio.Exit() + // Set volume from config + audio.SetVolume(core.ByteToClampedFloat32(game.Config.SoundVolume)) audio.LoadLibrary(assets.LoadSound()) // Load texture From e5c86a1528100e7f3f4018bfc2739ba95eb9472e Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 28 Oct 2025 06:36:15 +0100 Subject: [PATCH 14/21] chore: add trimpath and ldflags to build command --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 672e158..42604a9 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ GO = go +GOFLAGS = -trimpath -ldflags="-s -w" .PHONY: tetris tetris : - $(GO) build tetris.go + $(GO) build $(GOFLAGS) tetris.go From fd15ed0b11b6db29ebf84e3ed5f20790abef334a Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 28 Oct 2025 07:43:17 +0100 Subject: [PATCH 15/21] chore: setup git-cliff --- Makefile | 7 +++++ cliff.toml | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 cliff.toml diff --git a/Makefile b/Makefile index 42604a9..87a9e30 100644 --- a/Makefile +++ b/Makefile @@ -5,3 +5,10 @@ GOFLAGS = -trimpath -ldflags="-s -w" tetris : $(GO) build $(GOFLAGS) tetris.go + +release : NEXT_VER = $(shell git-cliff --bumped-version) +release : + git-cliff --bump -o CHANGELOG.md + git add CHANGELOG.md + git commit -m "chore(version): Bump to $(NEXT_VER)" + git tag $(NEXT_VER) diff --git a/cliff.toml b/cliff.toml new file mode 100644 index 0000000..3153cb1 --- /dev/null +++ b/cliff.toml @@ -0,0 +1,86 @@ +# git-cliff ~ configuration file +# https://git-cliff.org/docs/configuration + +[changelog] +# A Tera template to be rendered as the changelog's footer. +# See https://keats.github.io/tera/docs/#introduction +header = """ +# Changelog\n +All notable changes to this project will be documented in this file. See [conventional commits](https://www.conventionalcommits.org/) for commit guidelines.\n +""" +# A Tera template to be rendered for each release in the changelog. +# See https://keats.github.io/tera/docs/#introduction +body = """ +--- +{% if version %}\ + ## [{{ version | trim_start_matches(pat="v") }}]($REPO/src/{{ version }}) - {{ timestamp | date(format="%Y-%m-%d") }} +{% else %}\ + ## [unreleased] +{% endif %}\ +{% for group, commits in commits | group_by(attribute="group") %} + ### {{ group | striptags | trim | upper_first }} + {% for commit in commits + | filter(attribute="scope") + | sort(attribute="scope") %} + - **({{commit.scope}})**{% if commit.breaking %} [**breaking**]{% endif %} \ + {{ commit.message }} ([{{ commit.id | truncate(length=7, end="") }}]($REPO/commit/{{ commit.id }})) + {%- endfor -%} + {% raw %}\n{% endraw %}\ + {%- for commit in commits %} + {%- if commit.scope -%} + {% else -%} + - {% if commit.breaking %} [**breaking**]{% endif %}\ + {{ commit.message }} ([{{ commit.id | truncate(length=7, end="") }}]($REPO/commit/{{ commit.id }})) + {% endif -%} + {% endfor -%} +{% endfor %}\n +""" +# A Tera template to be rendered as the changelog's footer. +# See https://keats.github.io/tera/docs/#introduction +footer = """ + +""" +# Remove leading and trailing whitespaces from the changelog's body. +trim = true +# postprocessors +postprocessors = [ + # Replace the placeholder `` with a URL. + { pattern = '\$REPO', replace = "http://git.sargeras.net/pnx/tetris-go" }, +] + +[git] +# Parse commits according to the conventional commits specification. +# See https://www.conventionalcommits.org +conventional_commits = true +# Exclude commits that do not match the conventional commits specification. +filter_unconventional = true +# Split commits on newlines, treating each line as an individual commit. +split_commits = false +# An array of regex based parsers to modify commit messages prior to further processing. +commit_preprocessors = [ + # Replace issue numbers with link templates to be updated in `changelog.postprocessors`. + #{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "([#${2}](https://github.com/orhun/git-cliff/issues/${2}))"}, +] +# An array of regex based parsers for extracting data from the commit message. +# Assigns commits to groups. +# Optionally sets the commit's scope and can decide to exclude commits from further processing. +commit_parsers = [ + { message = "^feat", group = "Features" }, + { message = "^fix", group = "Bug Fixes" }, + { message = "^doc", group = "Documentation" }, + { message = "^perf", group = "Performance" }, + { message = "^refactor", group = "Refactoring" }, + { message = "^style", group = "Style" }, + { message = "^revert", group = "Revert" }, + { message = "^test", group = "Tests" }, + { message = "^chore\\(version\\):", skip = true }, + { message = "^chore", group = "Miscellaneous Chores" }, + { body = ".*security", group = "Security" }, +] +# Exclude commits that are not matched by any commit parser. +filter_commits = false +# Order releases topologically instead of chronologically. +topo_order = false +# Order of commits in each group/release within the changelog. +# Allowed values: newest, oldest +sort_commits = "oldest" From 46552f0ed257f7ae74751588a28a3ac3a51e4434 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 28 Oct 2025 07:44:12 +0100 Subject: [PATCH 16/21] chore(version): Bump to 1.1.0 --- CHANGELOG.md | 262 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 161 insertions(+), 101 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62163f1..fbe4ac7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,141 +1,201 @@ -## [1.0] - 2025-10-09 +# Changelog -### 🚀 Features +All notable changes to this project will be documented in this file. See [conventional commits](https://www.conventionalcommits.org/) for commit guidelines. -- *(assets)* Add icon -- *(engine)* Add LoadImageFromMemory() -- Set window icon -- *(gameover)* Only allow the user to switch state after the gameover sound has been played. -- *(gameover)* Make gameover state abit more interesting. -- *(assets)* Adding logo data -- *(menu)* Draw logo -- *(menu)* Put entries rendering into its own function +--- +## [1.1.0](http://git.sargeras.net/pnx/tetris-go/src/1.1.0) - 2025-10-28 -### 🐛 Bug Fixes +### Features -- *(audio)* Implement IsPlaying() correctly +- **(audio)** add SetVolume() and Volume() ([678670f](http://git.sargeras.net/pnx/tetris-go/commit/678670fd376d2098f4ad5ef1491f716545f907c1)) +- **(engine)** add number clamping functions ([7d5d5b9](http://git.sargeras.net/pnx/tetris-go/commit/7d5d5b9a54b92e00fa159bef64cc8e096c626726)) +- **(input)** add KeyPressedWithRepeat() ([3d3e8d7](http://git.sargeras.net/pnx/tetris-go/commit/3d3e8d7a445e3f33902006b194b1aa7712009bbe)) +- **(render)** adding DrawRectOutlineBorder() ([d407eaa](http://git.sargeras.net/pnx/tetris-go/commit/d407eaad8b3f68b2ae1ab6a068b6ca576f0d83b2)) +- **(ui)** menu should not call audio module directly, make it more modular with and onSelect event callback ([a175654](http://git.sargeras.net/pnx/tetris-go/commit/a1756549f9dbefbfcda259f6839dab6159fd5b2a)) +- **(ui)** call onSelect in Select() function and make Next()/Previous() use that function ([0bc5863](http://git.sargeras.net/pnx/tetris-go/commit/0bc5863284bc09bf43c9a3782d4826c576e7fa9b)) +- **(ui)** implement new ui system ([6cf0e4a](http://git.sargeras.net/pnx/tetris-go/commit/6cf0e4abb4ae67ac5ad1f1ec833caf2abd73b4c5)) +- **(ui)** add options menu ([30b1946](http://git.sargeras.net/pnx/tetris-go/commit/30b194619ff76136bb3f5ec68127c8c4bac445a5)) +- add line clear animation ([db9ec53](http://git.sargeras.net/pnx/tetris-go/commit/db9ec53d354d746748aba10be37bed53d79ecd4b)) +- dont close application when user presses Esc key ([d1ed556](http://git.sargeras.net/pnx/tetris-go/commit/d1ed55677e2750867150ec4911b6b79bab1fb401)) +- implement persistant configuration ([8a02249](http://git.sargeras.net/pnx/tetris-go/commit/8a0224984572b8c167d5298a35823b6b2daaabe5)) -### ⚙️ Miscellaneous Tasks +### Miscellaneous Chores -- Fix build for windows -- *(makefile)* Add .PHONY target again -- Add editorconfig -- Update readme and add license -## [0.13.0] - 2025-09-24 +- rename Rows to Lines in Grid as Lines are more common in tetris. ([190c6ad](http://git.sargeras.net/pnx/tetris-go/commit/190c6ad914430edbbedfa29612b6700071e9ab1e)) +- add trimpath and ldflags to build command ([e5c86a1](http://git.sargeras.net/pnx/tetris-go/commit/e5c86a1528100e7f3f4018bfc2739ba95eb9472e)) +- setup git-cliff ([fd15ed0](http://git.sargeras.net/pnx/tetris-go/commit/fd15ed0b11b6db29ebf84e3ed5f20790abef334a)) -### 🚀 Features +### Refactoring -- Add basic state machine implementation -- Refactor gameplay logic from main.go to a state machine handler. -- Add basic menu state -- Add basic game over state -- Register menu and gameover states. -- Start the machine in the menu state -- Detect game over condition and switch state. -- *(render)* Add DrawTextCenter() -- *(fsm)* Make the special state "quit" exit the fsm. -- Improve menu -- *(assets)* Add menu sound effects -- *(menu)* Play sound effects. -- *(assets)* Add gameover sound -- *(gameover)* Play gameover sound -- *(gameover)* Center text +- move ui code from game/state/handlers/menu.go to its own package in game/ui ([bcd6025](http://git.sargeras.net/pnx/tetris-go/commit/bcd6025aa371904d8d56069ce936d72142d10082)) -### 🐛 Bug Fixes +--- +## [1.0.0](http://git.sargeras.net/pnx/tetris-go/src/1.0.0) - 2025-10-28 -- Reset state on Enter() -## [0.12.0] - 2025-09-21 +### Bug Fixes -### 🚀 Features +- **(audio)** implement IsPlaying() correctly ([1b187d7](http://git.sargeras.net/pnx/tetris-go/commit/1b187d74120b15b1eb2c044bada468ff293e5319)) -- *(assets)* Add sound files -- Engine audio code -- *(assets)* Add sound library -- Initialize and use audio subsystem. -- Add sound on shape locked and row clear -## [0.11.0] - 2025-09-17 +### Features -### 🚀 Features +- **(assets)** add icon ([5b54466](http://git.sargeras.net/pnx/tetris-go/commit/5b54466018962e8ab02c8f34893bb66c03769910)) +- **(assets)** adding logo data ([dc83316](http://git.sargeras.net/pnx/tetris-go/commit/dc833168865024d01092a240be02b3bbddfbd129)) +- **(engine)** add LoadImageFromMemory() ([93736e0](http://git.sargeras.net/pnx/tetris-go/commit/93736e060ef02b0c26cdcaf22f104b3e62e17507)) +- **(gameover)** only allow the user to switch state after the gameover sound has been played. ([53ac384](http://git.sargeras.net/pnx/tetris-go/commit/53ac3840baf207b5c983535a3fbf69a11f4b7a52)) +- **(gameover)** make gameover state abit more interesting. ([4319ec5](http://git.sargeras.net/pnx/tetris-go/commit/4319ec55e5b3b101aea56972b91fec3e8d9ee69f)) +- **(menu)** draw logo ([9b11f3b](http://git.sargeras.net/pnx/tetris-go/commit/9b11f3b2200983d36dff0e0d5ef18619e608619d)) +- **(menu)** put entries rendering into its own function ([ca481ad](http://git.sargeras.net/pnx/tetris-go/commit/ca481ad7c7ae0be652d12552128d0724351e2d6c)) +- set window icon ([57e57f3](http://git.sargeras.net/pnx/tetris-go/commit/57e57f3037ac026ec24c264617f648a234c59862)) -- Add game/shape_queue.go -- Use ShapeQueue to implement shape RNG -## [0.10.0] - 2025-09-17 +### Miscellaneous Chores -### 🚀 Features +- **(makefile)** add .PHONY target again ([634fd7c](http://git.sargeras.net/pnx/tetris-go/commit/634fd7ce408cf4c8fd36db9bd974da95269e3080)) +- fix build for windows ([213b9a0](http://git.sargeras.net/pnx/tetris-go/commit/213b9a0d9eb4d0871566d549ece09128b97976bc)) +- add editorconfig ([cb6e66b](http://git.sargeras.net/pnx/tetris-go/commit/cb6e66bc45fe35cf209f780700cd424108fa759c)) +- update readme and add license ([02a915b](http://git.sargeras.net/pnx/tetris-go/commit/02a915b8373f9ace1ae9702b565f82065b4289ed)) -- Draw next shape -## [0.9.0] - 2025-09-15 +--- +## [0.13.0](http://git.sargeras.net/pnx/tetris-go/src/0.13.0) - 2025-09-24 -### 🚀 Features +### Bug Fixes -- Add score type -- Show score and increment when lines are cleared -## [0.8.0] - 2025-09-15 +- reset state on Enter() ([cd750a5](http://git.sargeras.net/pnx/tetris-go/commit/cd750a55083f056ccc06b67d7193288c9ec2b291)) -### 🚀 Features +### Features -- *(grid)* Add IsRowFull() -- *(grid)* Add ClearRow() -- *(grid)* Add MoveRowDown() -- *(grid)* Add ClearFullRows() -- Clear full lines after locking shape -## [0.7.0] - 2025-09-15 +- **(assets)** add menu sound effects ([7c02075](http://git.sargeras.net/pnx/tetris-go/commit/7c020750755fbf016c48ba7c6607ecde1da2c444)) +- **(assets)** add gameover sound ([9f055c6](http://git.sargeras.net/pnx/tetris-go/commit/9f055c6bb82d583c36289cf510f7200d0382a6a3)) +- **(fsm)** make the special state "quit" exit the fsm. ([35e73f2](http://git.sargeras.net/pnx/tetris-go/commit/35e73f2b37e75c0af270c11f04b28de27e08f383)) +- **(gameover)** play gameover sound ([dcad4ad](http://git.sargeras.net/pnx/tetris-go/commit/dcad4ad0a5f973a7bf95f63277c591c79956ac68)) +- **(gameover)** center text ([8f8be3d](http://git.sargeras.net/pnx/tetris-go/commit/8f8be3d6f7906a84b077e71082ba3bcb73180c51)) +- **(menu)** play sound effects. ([68b67e4](http://git.sargeras.net/pnx/tetris-go/commit/68b67e4585a4f38974b5246089b675f92d40d96b)) +- **(render)** add DrawTextCenter() ([346cf35](http://git.sargeras.net/pnx/tetris-go/commit/346cf350d8375df56ca2fbf6044d7600d03ccdf1)) +- add basic state machine implementation ([75b4981](http://git.sargeras.net/pnx/tetris-go/commit/75b498156667fb7569d3e08dbb00a9b60e69c679)) +- refactor gameplay logic from main.go to a state machine handler. ([29225b7](http://git.sargeras.net/pnx/tetris-go/commit/29225b7007cd154243c48bad8c321b6c4633f323)) +- add basic menu state ([53a31ea](http://git.sargeras.net/pnx/tetris-go/commit/53a31ea8ade6a63ecf3c56c3b061f4715859e18f)) +- add basic game over state ([6d15a80](http://git.sargeras.net/pnx/tetris-go/commit/6d15a806f6172b674617511ab57fc258e55e2a77)) +- register menu and gameover states. ([b8397ae](http://git.sargeras.net/pnx/tetris-go/commit/b8397aefdb0e653266b3851f81708dea28ac7063)) +- start the machine in the menu state ([cae34a5](http://git.sargeras.net/pnx/tetris-go/commit/cae34a52aa3c4d78607065feb976b98e53a5475c)) +- detect game over condition and switch state. ([54a7821](http://git.sargeras.net/pnx/tetris-go/commit/54a7821df0016eb0f1deeeb7c8a55af121d3e76a)) +- improve menu ([288e710](http://git.sargeras.net/pnx/tetris-go/commit/288e710b9f298de25d0a16564287c54e788fb551)) -### 🚀 Features +--- +## [0.12.0](http://git.sargeras.net/pnx/tetris-go/src/0.12.0) - 2025-09-21 -- Soft drop -## [0.6.0] - 2025-09-15 +### Features -### 🚀 Features +- **(assets)** add sound files ([1799422](http://git.sargeras.net/pnx/tetris-go/commit/17994225917cfe0f5085b1f968deec55273482b2)) +- **(assets)** add sound library ([1c31fd4](http://git.sargeras.net/pnx/tetris-go/commit/1c31fd43ac99239db184d3144383efb03e59ad90)) +- engine audio code ([5860aa6](http://git.sargeras.net/pnx/tetris-go/commit/5860aa69d6cea02e321d6de768adfddc39cc3b52)) +- initialize and use audio subsystem. ([173adad](http://git.sargeras.net/pnx/tetris-go/commit/173adadb8405804e641f5bd56e666d2b0a1a3861)) +- add sound on shape locked and row clear ([a5c9e68](http://git.sargeras.net/pnx/tetris-go/commit/a5c9e687ee604a5c783c7873ffa0d5a5e5f6813c)) -- *(shape)* Add RotateCW() and RotateCCW() -- Rotate shape when UP is pressed. -## [0.5.0] - 2025-09-15 +--- +## [0.11.0](http://git.sargeras.net/pnx/tetris-go/src/0.11.0) - 2025-09-17 -### 🚀 Features +### Features -- *(collision)* Check grid bounds in x axis. -- Add shape movement +- add game/shape_queue.go ([5c45032](http://git.sargeras.net/pnx/tetris-go/commit/5c4503268f62c30d814db74dde56bfdbf5aa9293)) +- use ShapeQueue to implement shape RNG ([d6c9389](http://git.sargeras.net/pnx/tetris-go/commit/d6c9389e60252048c80f44572b024607d19c8814)) -### ⚙️ Miscellaneous Tasks +--- +## [0.10.0](http://git.sargeras.net/pnx/tetris-go/src/0.10.0) - 2025-09-17 -- Update go.mod -## [0.4.0] - 2025-09-14 +### Features -### 🚀 Features +- draw next shape ([12712e0](http://git.sargeras.net/pnx/tetris-go/commit/12712e0172071013f323d87c75d573645d7f2195)) -- *(collision)* Check for collision against blocks in the grid. +--- +## [0.9.0](http://git.sargeras.net/pnx/tetris-go/src/0.9.0) - 2025-09-15 -### 🐛 Bug Fixes +### Features -- Check bound before placing a block in LockShape() -## [0.3.0] - 2025-09-14 +- add score type ([14520e0](http://git.sargeras.net/pnx/tetris-go/commit/14520e047c2fdff69a5489ba1dbfd565f09826d8)) +- show score and increment when lines are cleared ([b6a33d0](http://git.sargeras.net/pnx/tetris-go/commit/b6a33d017f718659a5b21f2d80b8c706d4ee28be)) -### 🚀 Features +--- +## [0.8.0](http://git.sargeras.net/pnx/tetris-go/src/0.8.0) - 2025-09-15 -- Lock and spawn shapes +### Features -### 🚜 Refactor +- **(grid)** add IsRowFull() ([eb31bdf](http://git.sargeras.net/pnx/tetris-go/commit/eb31bdf50cfb30f2b3ee4e3f3771b938330735f7)) +- **(grid)** add ClearRow() ([8903319](http://git.sargeras.net/pnx/tetris-go/commit/890331991ffd12c0bc41ba9ba329fcc661e3feef)) +- **(grid)** add MoveRowDown() ([1d7423d](http://git.sargeras.net/pnx/tetris-go/commit/1d7423d8867a802f442b18143824a4cffa8fb9d0)) +- **(grid)** add ClearFullRows() ([2a7f394](http://git.sargeras.net/pnx/tetris-go/commit/2a7f394b7f129b8a8d3a44ea314ba7c654688abc)) +- clear full lines after locking shape ([ac4d911](http://git.sargeras.net/pnx/tetris-go/commit/ac4d911efd0bfa7aeff57acf8535c916dc6a22ca)) -- Cleanup main loop code by moving related code into update/render functions respectively. -## [0.2.0] - 2025-09-14 +--- +## [0.7.0](http://git.sargeras.net/pnx/tetris-go/src/0.7.0) - 2025-09-15 -### 🚀 Features +### Features -- Add engine/core/vec2.go -- Add shape struct -- *(draw)* Add grid.DrawBlock() -- *(draw)* Add DrawShape() -- Draw shape on grid. -- Add engine/core/interval_timer.go -- Implement shape drop -- Add CheckShapeCollision() -- Add collision check on shape drop. -## [0.1.0] - 2025-09-14 +- soft drop ([8f11a99](http://git.sargeras.net/pnx/tetris-go/commit/8f11a99e08edc0473458f28adb4db87a75092a04)) -### 🚀 Features +--- +## [0.6.0](http://git.sargeras.net/pnx/tetris-go/src/0.6.0) - 2025-09-15 -- *(assets)* Add constant with the size of blocks in the sprite texture. -- Add game/block.go -- Add game/grid.go -- Make rendering work with the grid struct. +### Features + +- **(shape)** add RotateCW() and RotateCCW() ([6131b75](http://git.sargeras.net/pnx/tetris-go/commit/6131b751e568c7a48500428b639457d2cd426eb3)) +- rotate shape when UP is pressed. ([4ce5b82](http://git.sargeras.net/pnx/tetris-go/commit/4ce5b82b25acd93ce7cfaab18b300b6c25f2ba30)) + +--- +## [0.5.0](http://git.sargeras.net/pnx/tetris-go/src/0.5.0) - 2025-09-15 + +### Features + +- **(collision)** check grid bounds in x axis. ([aeaf6cb](http://git.sargeras.net/pnx/tetris-go/commit/aeaf6cb5c47027c07958f7a7c2c07decb1ed49e7)) +- add shape movement ([ac14dfe](http://git.sargeras.net/pnx/tetris-go/commit/ac14dfe8b3b2a60dcb3bf71bbf89a0b42c99e389)) + +### Miscellaneous Chores + +- update go.mod ([d6a9e80](http://git.sargeras.net/pnx/tetris-go/commit/d6a9e80fbb79532a2f63d88b126b2fd448de3c7f)) + +--- +## [0.4.0](http://git.sargeras.net/pnx/tetris-go/src/0.4.0) - 2025-09-14 + +### Bug Fixes + +- check bound before placing a block in LockShape() ([ca6497d](http://git.sargeras.net/pnx/tetris-go/commit/ca6497d36223508a717177bc8f21d8238aa6eae5)) + +### Features + +- **(collision)** check for collision against blocks in the grid. ([dab2570](http://git.sargeras.net/pnx/tetris-go/commit/dab2570b76c224ad3b6b3741c3cb0381d403fa79)) + +--- +## [0.3.0](http://git.sargeras.net/pnx/tetris-go/src/0.3.0) - 2025-09-14 + +### Features + +- lock and spawn shapes ([a6072e5](http://git.sargeras.net/pnx/tetris-go/commit/a6072e5b05a265bfd38c3b784731bfaed165e2d7)) + +### Refactoring + +- cleanup main loop code by moving related code into update/render functions respectively. ([3c84709](http://git.sargeras.net/pnx/tetris-go/commit/3c847094a060caec81dd805bc58f2ab1b93b2289)) + +--- +## [0.2.0](http://git.sargeras.net/pnx/tetris-go/src/0.2.0) - 2025-09-14 + +### Features + +- **(draw)** add grid.DrawBlock() ([4dd6d93](http://git.sargeras.net/pnx/tetris-go/commit/4dd6d936535e9e39d09b365d9ae005c4a9ddefb5)) +- **(draw)** add DrawShape() ([02157a1](http://git.sargeras.net/pnx/tetris-go/commit/02157a12eadc96a6be8e6212c7fe8357a290c432)) +- add engine/core/vec2.go ([9387402](http://git.sargeras.net/pnx/tetris-go/commit/93874028b07c879a718d9dc41a0c98a17fd55bbc)) +- add shape struct ([6564879](http://git.sargeras.net/pnx/tetris-go/commit/656487903e282507c0bfabac5657c124a63c4d7f)) +- draw shape on grid. ([63a3662](http://git.sargeras.net/pnx/tetris-go/commit/63a3662dbe6f6a801fdbcbc07e99f24f1f0afbb6)) +- add engine/core/interval_timer.go ([ffbf133](http://git.sargeras.net/pnx/tetris-go/commit/ffbf1332c5c0f1035a58c9b1d98c12c16c76f66b)) +- implement shape drop ([ab69d50](http://git.sargeras.net/pnx/tetris-go/commit/ab69d501b9e0e82358873e431cc934ad74821361)) +- add CheckShapeCollision() ([27c10af](http://git.sargeras.net/pnx/tetris-go/commit/27c10af4248bfd3150850006ecdbd6833c7c27c9)) +- add collision check on shape drop. ([50a7666](http://git.sargeras.net/pnx/tetris-go/commit/50a76662469874ea48b32e7dc03fcbf75977e4a6)) + +--- +## [0.1.0](http://git.sargeras.net/pnx/tetris-go/src/0.1.0) - 2025-09-14 + +### Features + +- **(assets)** add constant with the size of blocks in the sprite texture. ([93a660e](http://git.sargeras.net/pnx/tetris-go/commit/93a660e8d3f2fe7fa06d4066b2a74bc84347978b)) +- add game/block.go ([6e21b1f](http://git.sargeras.net/pnx/tetris-go/commit/6e21b1fcb2cd0f6554e5af3b52222f31c461915c)) +- add game/grid.go ([2a84c7b](http://git.sargeras.net/pnx/tetris-go/commit/2a84c7bf6a8a4faee67c5b813faca587621aa6aa)) +- make rendering work with the grid struct. ([1b9ba08](http://git.sargeras.net/pnx/tetris-go/commit/1b9ba081e2bf0b7f4be0088ea6d7d6c42294b3ad)) + + From f3d7995bc25b8da9055f6a7ead404c3ad21df852 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 28 Oct 2025 09:47:19 +0100 Subject: [PATCH 17/21] feat(ui): add ListBox.SetSelected() this function will set the selected widget without triggering a onSelect event. --- game/ui/layouts/list_box.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/game/ui/layouts/list_box.go b/game/ui/layouts/list_box.go index 4a058cf..bcbcd08 100644 --- a/game/ui/layouts/list_box.go +++ b/game/ui/layouts/list_box.go @@ -42,12 +42,19 @@ func (lb ListBox) Entries() []ui.InputWidget { } func (lb *ListBox) Select(index int) { + if lb.SetSelected(index) { + lb.onSelect() + } +} + +func (lb *ListBox) SetSelected(index int) bool { if index >= 0 && index < len(lb.entries) { lb.entries[lb.selected].SetFocus(false) lb.selected = index lb.entries[lb.selected].SetFocus(true) - lb.onSelect() + return true } + return false } func (lb ListBox) Selected() ui.InputWidget { From ef798896c3493d67b823ae762bb0c8a14c6a2f45 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 28 Oct 2025 09:47:50 +0100 Subject: [PATCH 18/21] feat(menu): use list.SetSelected() on Enter() so that no audio is played. --- game/state/handlers/menu.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/state/handlers/menu.go b/game/state/handlers/menu.go index ef5b389..40b767e 100644 --- a/game/state/handlers/menu.go +++ b/game/state/handlers/menu.go @@ -29,7 +29,7 @@ func NewMainMenu(fsm state.Transitioner) *MainMenu { } func (main *MainMenu) Enter() { - main.list.Select(0) + main.list.SetSelected(0) } func (MainMenu) Exit() { From eb7486f57cf9878cd626fcbf39417b714e9acaef Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 28 Oct 2025 09:52:58 +0100 Subject: [PATCH 19/21] feat(menu): play "menu enter" sound when button action is triggered --- game/state/handlers/menu.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/game/state/handlers/menu.go b/game/state/handlers/menu.go index 40b767e..c8b51ca 100644 --- a/game/state/handlers/menu.go +++ b/game/state/handlers/menu.go @@ -2,6 +2,7 @@ package handlers import ( "tetris/assets" + "tetris/engine/audio" "tetris/engine/render" "tetris/game" "tetris/game/state" @@ -17,12 +18,19 @@ type MainMenu struct { list *layouts.ListBox } +func enterState(fsm state.Transitioner, state string) func() { + return func() { + audio.Play(assets.SFX_MENU_ENTER) + fsm.Switch(state) + } +} + func NewMainMenu(fsm state.Transitioner) *MainMenu { return &MainMenu{ list: layouts.NewListBox([]ui.InputWidget{ - widgets.NewButton("Start", 32, func() { fsm.Switch("gameplay") }), - widgets.NewButton("Options", 32, func() { fsm.Switch("options") }), - widgets.NewButton("Quit", 32, func() { fsm.Switch("quit") }), + widgets.NewButton("Start", 32, enterState(fsm, "gameplay")), + widgets.NewButton("Options", 32, enterState(fsm, "options")), + widgets.NewButton("Quit", 32, enterState(fsm, "quit")), }).Spacing(10). OnSelect(uievents.MenuSelect), } From 562d47a72366eeaca88a0eeb3c3ee5f75bed36f8 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 9 Nov 2025 06:58:26 +0100 Subject: [PATCH 20/21] feat(music): add gameplay background music --- assets/sound.go | 1 + game/state/handlers/gameplay.go | 3 +++ 2 files changed, 4 insertions(+) diff --git a/assets/sound.go b/assets/sound.go index 9bc2365..bd12a39 100644 --- a/assets/sound.go +++ b/assets/sound.go @@ -7,6 +7,7 @@ import ( const ( SFX_SHAPE_LOCKED audio.SoundID = 0 SFX_ROW_CLEARED audio.SoundID = 1 + SFX_MUSIC audio.SoundID = 3 SFX_MENU_SELECT audio.SoundID = 0 SFX_MENU_ENTER audio.SoundID = 1 SFX_MENU_SOUND_VOLUME_SELECT audio.SoundID = 1 diff --git a/game/state/handlers/gameplay.go b/game/state/handlers/gameplay.go index 48a64f8..b65da11 100644 --- a/game/state/handlers/gameplay.go +++ b/game/state/handlers/gameplay.go @@ -54,9 +54,12 @@ func (gp *GamePlay) Enter() { gp.nextShape = gp.shapeQueue.Next() gp.SpawnShape() gp.lineClearAnimation.Reset() + + audio.PlayLooped(assets.SFX_MUSIC) } func (GamePlay) Exit() { + audio.Stop(assets.SFX_MUSIC) } func (gp *GamePlay) SpawnShape() { From defb0f56d173056e9394c811c2fd686228c79f89 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 4 Apr 2026 14:46:13 +0200 Subject: [PATCH 21/21] chore(version): Bump to 1.2.0 --- CHANGELOG.md | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbe4ac7..c009c82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ All notable changes to this project will be documented in this file. See [conventional commits](https://www.conventionalcommits.org/) for commit guidelines. +--- +## [1.2.0](http://git.sargeras.net/pnx/tetris-go/src/1.2.0) - 2026-04-04 + +### Features + +- **(menu)** use list.SetSelected() on Enter() so that no audio is played. ([ef79889](http://git.sargeras.net/pnx/tetris-go/commit/ef798896c3493d67b823ae762bb0c8a14c6a2f45)) +- **(menu)** play "menu enter" sound when button action is triggered ([eb7486f](http://git.sargeras.net/pnx/tetris-go/commit/eb7486f57cf9878cd626fcbf39417b714e9acaef)) +- **(music)** add gameplay background music ([562d47a](http://git.sargeras.net/pnx/tetris-go/commit/562d47a72366eeaca88a0eeb3c3ee5f75bed36f8)) +- **(ui)** add ListBox.SetSelected() ([f3d7995](http://git.sargeras.net/pnx/tetris-go/commit/f3d7995bc25b8da9055f6a7ead404c3ad21df852)) + --- ## [1.1.0](http://git.sargeras.net/pnx/tetris-go/src/1.1.0) - 2025-10-28 @@ -55,7 +65,7 @@ All notable changes to this project will be documented in this file. See [conven - update readme and add license ([02a915b](http://git.sargeras.net/pnx/tetris-go/commit/02a915b8373f9ace1ae9702b565f82065b4289ed)) --- -## [0.13.0](http://git.sargeras.net/pnx/tetris-go/src/0.13.0) - 2025-09-24 +## [0.13.0](http://git.sargeras.net/pnx/tetris-go/src/v0.13.0) - 2025-09-24 ### Bug Fixes @@ -80,7 +90,7 @@ All notable changes to this project will be documented in this file. See [conven - improve menu ([288e710](http://git.sargeras.net/pnx/tetris-go/commit/288e710b9f298de25d0a16564287c54e788fb551)) --- -## [0.12.0](http://git.sargeras.net/pnx/tetris-go/src/0.12.0) - 2025-09-21 +## [0.12.0](http://git.sargeras.net/pnx/tetris-go/src/v0.12.0) - 2025-09-21 ### Features @@ -91,7 +101,7 @@ All notable changes to this project will be documented in this file. See [conven - add sound on shape locked and row clear ([a5c9e68](http://git.sargeras.net/pnx/tetris-go/commit/a5c9e687ee604a5c783c7873ffa0d5a5e5f6813c)) --- -## [0.11.0](http://git.sargeras.net/pnx/tetris-go/src/0.11.0) - 2025-09-17 +## [0.11.0](http://git.sargeras.net/pnx/tetris-go/src/v0.11.0) - 2025-09-17 ### Features @@ -99,14 +109,14 @@ All notable changes to this project will be documented in this file. See [conven - use ShapeQueue to implement shape RNG ([d6c9389](http://git.sargeras.net/pnx/tetris-go/commit/d6c9389e60252048c80f44572b024607d19c8814)) --- -## [0.10.0](http://git.sargeras.net/pnx/tetris-go/src/0.10.0) - 2025-09-17 +## [0.10.0](http://git.sargeras.net/pnx/tetris-go/src/v0.10.0) - 2025-09-17 ### Features - draw next shape ([12712e0](http://git.sargeras.net/pnx/tetris-go/commit/12712e0172071013f323d87c75d573645d7f2195)) --- -## [0.9.0](http://git.sargeras.net/pnx/tetris-go/src/0.9.0) - 2025-09-15 +## [0.9.0](http://git.sargeras.net/pnx/tetris-go/src/v0.9.0) - 2025-09-15 ### Features @@ -114,7 +124,7 @@ All notable changes to this project will be documented in this file. See [conven - show score and increment when lines are cleared ([b6a33d0](http://git.sargeras.net/pnx/tetris-go/commit/b6a33d017f718659a5b21f2d80b8c706d4ee28be)) --- -## [0.8.0](http://git.sargeras.net/pnx/tetris-go/src/0.8.0) - 2025-09-15 +## [0.8.0](http://git.sargeras.net/pnx/tetris-go/src/v0.8.0) - 2025-09-15 ### Features @@ -125,14 +135,14 @@ All notable changes to this project will be documented in this file. See [conven - clear full lines after locking shape ([ac4d911](http://git.sargeras.net/pnx/tetris-go/commit/ac4d911efd0bfa7aeff57acf8535c916dc6a22ca)) --- -## [0.7.0](http://git.sargeras.net/pnx/tetris-go/src/0.7.0) - 2025-09-15 +## [0.7.0](http://git.sargeras.net/pnx/tetris-go/src/v0.7.0) - 2025-09-15 ### Features - soft drop ([8f11a99](http://git.sargeras.net/pnx/tetris-go/commit/8f11a99e08edc0473458f28adb4db87a75092a04)) --- -## [0.6.0](http://git.sargeras.net/pnx/tetris-go/src/0.6.0) - 2025-09-15 +## [0.6.0](http://git.sargeras.net/pnx/tetris-go/src/v0.6.0) - 2025-09-15 ### Features @@ -140,7 +150,7 @@ All notable changes to this project will be documented in this file. See [conven - rotate shape when UP is pressed. ([4ce5b82](http://git.sargeras.net/pnx/tetris-go/commit/4ce5b82b25acd93ce7cfaab18b300b6c25f2ba30)) --- -## [0.5.0](http://git.sargeras.net/pnx/tetris-go/src/0.5.0) - 2025-09-15 +## [0.5.0](http://git.sargeras.net/pnx/tetris-go/src/v0.5.0) - 2025-09-15 ### Features @@ -152,7 +162,7 @@ All notable changes to this project will be documented in this file. See [conven - update go.mod ([d6a9e80](http://git.sargeras.net/pnx/tetris-go/commit/d6a9e80fbb79532a2f63d88b126b2fd448de3c7f)) --- -## [0.4.0](http://git.sargeras.net/pnx/tetris-go/src/0.4.0) - 2025-09-14 +## [0.4.0](http://git.sargeras.net/pnx/tetris-go/src/v0.4.0) - 2025-09-14 ### Bug Fixes @@ -163,7 +173,7 @@ All notable changes to this project will be documented in this file. See [conven - **(collision)** check for collision against blocks in the grid. ([dab2570](http://git.sargeras.net/pnx/tetris-go/commit/dab2570b76c224ad3b6b3741c3cb0381d403fa79)) --- -## [0.3.0](http://git.sargeras.net/pnx/tetris-go/src/0.3.0) - 2025-09-14 +## [0.3.0](http://git.sargeras.net/pnx/tetris-go/src/v0.3.0) - 2025-09-14 ### Features @@ -174,7 +184,7 @@ All notable changes to this project will be documented in this file. See [conven - cleanup main loop code by moving related code into update/render functions respectively. ([3c84709](http://git.sargeras.net/pnx/tetris-go/commit/3c847094a060caec81dd805bc58f2ab1b93b2289)) --- -## [0.2.0](http://git.sargeras.net/pnx/tetris-go/src/0.2.0) - 2025-09-14 +## [0.2.0](http://git.sargeras.net/pnx/tetris-go/src/v0.2.0) - 2025-09-14 ### Features