1
0
Fork 0

Compare commits

..

No commits in common. "main" and "1.0.0" have entirely different histories.
main ... 1.0.0

28 changed files with 195 additions and 1061 deletions

1
.gitignore vendored
View file

@ -1,3 +1,2 @@
/tetris
/tetris.exe
/config.json

View file

@ -1,211 +1,141 @@
# Changelog
## [1.0] - 2025-10-09
All notable changes to this project will be documented in this file. See [conventional commits](https://www.conventionalcommits.org/) for commit guidelines.
### 🚀 Features
---
## [1.2.0](http://git.sargeras.net/pnx/tetris-go/src/1.2.0) - 2026-04-04
- *(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
### Features
### 🐛 Bug Fixes
- **(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))
- *(audio)* Implement IsPlaying() correctly
---
## [1.1.0](http://git.sargeras.net/pnx/tetris-go/src/1.1.0) - 2025-10-28
### ⚙️ Miscellaneous Tasks
### Features
- Fix build for windows
- *(makefile)* Add .PHONY target again
- Add editorconfig
- Update readme and add license
## [0.13.0] - 2025-09-24
- **(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))
### 🚀 Features
### Miscellaneous Chores
- 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
- 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))
### 🐛 Bug Fixes
### Refactoring
- Reset state on Enter()
## [0.12.0] - 2025-09-21
- 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))
### 🚀 Features
---
## [1.0.0](http://git.sargeras.net/pnx/tetris-go/src/1.0.0) - 2025-10-28
- *(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
### Bug Fixes
### 🚀 Features
- **(audio)** implement IsPlaying() correctly ([1b187d7](http://git.sargeras.net/pnx/tetris-go/commit/1b187d74120b15b1eb2c044bada468ff293e5319))
- Add game/shape_queue.go
- Use ShapeQueue to implement shape RNG
## [0.10.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))
- Draw next shape
## [0.9.0] - 2025-09-15
### 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))
- Add score type
- Show score and increment when lines are cleared
## [0.8.0] - 2025-09-15
---
## [0.13.0](http://git.sargeras.net/pnx/tetris-go/src/v0.13.0) - 2025-09-24
### 🚀 Features
### Bug Fixes
- *(grid)* Add IsRowFull()
- *(grid)* Add ClearRow()
- *(grid)* Add MoveRowDown()
- *(grid)* Add ClearFullRows()
- Clear full lines after locking shape
## [0.7.0] - 2025-09-15
- reset state on Enter() ([cd750a5](http://git.sargeras.net/pnx/tetris-go/commit/cd750a55083f056ccc06b67d7193288c9ec2b291))
### 🚀 Features
### Features
- Soft drop
## [0.6.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/v0.12.0) - 2025-09-21
- *(shape)* Add RotateCW() and RotateCCW()
- Rotate shape when UP is pressed.
## [0.5.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))
- *(collision)* Check grid bounds in x axis.
- Add shape movement
---
## [0.11.0](http://git.sargeras.net/pnx/tetris-go/src/v0.11.0) - 2025-09-17
### ⚙️ Miscellaneous Tasks
### Features
- Update go.mod
## [0.4.0] - 2025-09-14
- 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))
### 🚀 Features
---
## [0.10.0](http://git.sargeras.net/pnx/tetris-go/src/v0.10.0) - 2025-09-17
- *(collision)* Check for collision against blocks in the grid.
### Features
### 🐛 Bug Fixes
- draw next shape ([12712e0](http://git.sargeras.net/pnx/tetris-go/commit/12712e0172071013f323d87c75d573645d7f2195))
- Check bound before placing a block in LockShape()
## [0.3.0] - 2025-09-14
---
## [0.9.0](http://git.sargeras.net/pnx/tetris-go/src/v0.9.0) - 2025-09-15
### 🚀 Features
### Features
- Lock and spawn shapes
- 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))
### 🚜 Refactor
---
## [0.8.0](http://git.sargeras.net/pnx/tetris-go/src/v0.8.0) - 2025-09-15
- Cleanup main loop code by moving related code into update/render functions respectively.
## [0.2.0] - 2025-09-14
### Features
### 🚀 Features
- **(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))
- 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
---
## [0.7.0](http://git.sargeras.net/pnx/tetris-go/src/v0.7.0) - 2025-09-15
### 🚀 Features
### Features
- soft drop ([8f11a99](http://git.sargeras.net/pnx/tetris-go/commit/8f11a99e08edc0473458f28adb4db87a75092a04))
---
## [0.6.0](http://git.sargeras.net/pnx/tetris-go/src/v0.6.0) - 2025-09-15
### 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/v0.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/v0.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/v0.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/v0.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))
<!-- generated by git-cliff -->
- *(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.

View file

@ -1,14 +1,6 @@
GO = go
GOFLAGS = -trimpath -ldflags="-s -w"
.PHONY: tetris
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)
$(GO) build tetris.go

View file

@ -64,7 +64,7 @@ go build tetris.go
## Roadmap
- [x] Line clear animations
- [ ] Line clear animations
- [ ] Hard drop and ghost piece
- [ ] Level/speed progression and pause
- [ ] High score persistence

View file

@ -5,13 +5,11 @@ 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
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_GAME_OVER audio.SoundID = 2
)
func LoadSound() *audio.Library {

View file

@ -1,86 +0,0 @@
# 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 = """
<!-- generated by git-cliff -->
"""
# Remove leading and trailing whitespaces from the changelog's body.
trim = true
# postprocessors
postprocessors = [
# Replace the placeholder `<REPO>` 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"

View file

@ -6,14 +6,6 @@ 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)
}

View file

@ -21,14 +21,6 @@ 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)

View file

@ -1,9 +0,0 @@
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)
}

View file

@ -1,8 +0,0 @@
package input
import rl "github.com/gen2brain/raylib-go/raylib"
func KeyPressedWithRepeat(key int32) bool {
return rl.IsKeyPressed(key) || rl.IsKeyPressedRepeat(key)
}

View file

@ -17,15 +17,10 @@ func DrawRectBorder(rect rl.RectangleInt32, col color.RGBA, border_size int32, b
Height: float32(rect.Height),
}, 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)
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)
}

View file

@ -1,21 +0,0 @@
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
}

View file

@ -1,45 +0,0 @@
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()
}

View file

@ -1,8 +1,6 @@
package game
import (
"slices"
"tetris/engine/graphics"
)
@ -33,30 +31,20 @@ func (g *Grid) Set(x, y byte, c Block) {
(*g)[uint16(x)+(uint16(y)*GRID_WIDTH)] = c
}
func (g *Grid) ClearFullLines() byte {
func (g *Grid) ClearFullRows() byte {
completed := byte(0)
for y := int(g.Height() - 1); y >= 0; y-- {
if g.IsLineFull(byte(y)) {
if g.IsRowFull(byte(y)) {
completed++
g.ClearLine(byte(y))
g.ClearRow(byte(y))
} else if completed > 0 {
g.MoveLineDown(byte(y), completed)
g.MoveRowDown(byte(y), completed)
}
}
return completed
}
func (g *Grid) FullLines() []byte {
lines := []byte{}
for y := byte(0); y < byte(g.Height()); y++ {
if g.IsLineFull(y) {
lines = append(lines, y)
}
}
return lines
}
func (g *Grid) IsLineFull(y byte) bool {
func (g *Grid) IsRowFull(y byte) bool {
for x := range byte(g.Width()) {
if g.At(x, y) == BLOCK_EMPTY {
return false
@ -65,26 +53,15 @@ func (g *Grid) IsLineFull(y byte) bool {
return true
}
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.MoveLineDown(byte(y), completed)
}
}
}
func (g *Grid) MoveLineDown(y, num_lines byte) {
func (g *Grid) MoveRowDown(y, num_rows byte) {
w := uint16(g.Width())
src := uint16(y) * w
dst := uint16(y+num_lines) * w
dst := uint16(y+num_rows) * w
copy(g[dst:dst+w], g[src:src+w])
clear(g[src : src+w])
}
func (g *Grid) ClearLine(y byte) {
func (g *Grid) ClearRow(y byte) {
w := uint16(g.Width())
n := uint16(y) * w
clear(g[n : n+w])

View file

@ -1,57 +0,0 @@
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.MoveLinesDown(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
}

View file

@ -16,24 +16,21 @@ import (
)
type GamePlay struct {
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
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
}
func NewGamePlay() *GamePlay {
return &GamePlay{
dropTimer: core.NewIntervalTimer(0.3),
moveTimer: core.NewIntervalTimer(0.1),
lineClearTimer: core.NewIntervalTimer(0.05),
dropTimer: core.NewIntervalTimer(0.3),
moveTimer: core.NewIntervalTimer(0.1),
r: draw.Renderer{
Theme: &draw.Theme{
FrameBG: color.RGBA{R: 30, G: 30, B: 46, A: 255},
@ -53,13 +50,9 @@ func (gp *GamePlay) Enter() {
gp.shapeQueue = game.NewShapeQueue()
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() {
@ -88,13 +81,6 @@ 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) {
@ -121,16 +107,15 @@ 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()
gp.SpawnShape()
lines := gp.grid.FullLines()
if len(lines) > 0 {
gp.lineClearAnimation.SetLines(lines)
gp.score.Lines(byte(len(lines)))
num_rows := gp.grid.ClearFullRows()
if num_rows > 0 {
audio.Play(assets.SFX_ROW_CLEARED)
} else {
if game.CheckShapeCollision(gp.shape_pos, &gp.shape, &gp.grid) {
fsm.Switch("gameover")
}
gp.score.Lines(num_rows)
}
gp.SpawnShape()
if game.CheckShapeCollision(gp.shape_pos, &gp.shape, &gp.grid) {
fsm.Switch("gameover")
}
} else {
gp.shape_pos = new_pos

View file

@ -6,48 +6,55 @@ import (
"tetris/engine/render"
"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 {
list *layouts.ListBox
type entry struct {
label string
state string
}
func enterState(fsm state.Transitioner, state string) func() {
return func() {
type Menu struct {
selected int
entries []entry
}
func NewMenu() *Menu {
return &Menu{
selected: 0,
entries: []entry{
{"Start", "gameplay"},
{"Quit", "quit"},
},
}
}
func (menu *Menu) Enter() {
menu.selected = 0
}
func (Menu) 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)
fsm.Switch(state)
} 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 NewMainMenu(fsm state.Transitioner) *MainMenu {
return &MainMenu{
list: layouts.NewListBox([]ui.InputWidget{
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),
}
}
func (main *MainMenu) Enter() {
main.list.SetSelected(0)
}
func (MainMenu) Exit() {
}
func (menu *MainMenu) Update(fsm state.Transitioner, delta float32) {
menu.list.HandleInput()
}
func (MainMenu) renderLogo(offset_x, offset_y int32) {
func (Menu) 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)]
@ -69,11 +76,24 @@ func (MainMenu) renderLogo(offset_x, offset_y int32) {
}
}
func (menu MainMenu) Render() {
func (menu Menu) 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)
y += 40
}
}
func (menu Menu) Render() {
render.Begin(rl.Black)
menu.renderLogo(20, 150)
menu.list.Draw(340, 400)
menu.renderEntries(340, 400)
render.End()
}

View file

@ -1,67 +0,0 @@
package handlers
import (
"tetris/assets"
"tetris/engine/audio"
"tetris/engine/core"
"tetris/engine/render"
"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 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)
// Store in config and save
game.Config.SoundVolume = byte(widget.Value)
game.Config.Save()
}
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()
}

View file

@ -1,26 +0,0 @@
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
}

View file

@ -1,14 +0,0 @@
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()
}

View file

@ -1,57 +0,0 @@
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)
}

View file

@ -1,91 +0,0 @@
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 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)
return true
}
return false
}
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)
}
}

View file

@ -1,18 +0,0 @@
package ui
import (
"tetris/engine/core"
)
// Widget is a base widget (Can be drawn on screen)
type Widget interface {
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
}

View file

@ -1,48 +0,0 @@
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)
}

View file

@ -1,39 +0,0 @@
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)
}

View file

@ -1,136 +0,0 @@
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)
}

View file

@ -1,10 +0,0 @@
package uievents
import (
"tetris/assets"
"tetris/engine/audio"
)
func MenuSelect() {
audio.Play(assets.SFX_MENU_SELECT)
}

View file

@ -1,14 +1,10 @@
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"
@ -16,10 +12,6 @@ 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,
@ -30,9 +22,6 @@ 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)
@ -42,8 +31,6 @@ func main() {
audio.Init()
defer audio.Exit()
// Set volume from config
audio.SetVolume(core.ByteToClampedFloat32(game.Config.SoundVolume))
audio.LoadLibrary(assets.LoadSound())
// Load texture
@ -54,8 +41,7 @@ func main() {
// Setup state machine.
fsm := machine.New()
fsm.Register("menu", handlers.NewMainMenu(fsm))
fsm.Register("options", handlers.NewOptionsMenu())
fsm.Register("menu", handlers.NewMenu())
fsm.Register("gameover", &handlers.GameOver{})
fsm.Register("gameplay", handlers.NewGamePlay())
fsm.Start("menu")