1
0
Fork 0

feat: implement persistant configuration

This commit is contained in:
Henrik Hautakoski 2025-10-28 06:35:14 +01:00
parent 30b194619f
commit 8a02249845
5 changed files with 82 additions and 0 deletions

21
game/config.go Normal file
View file

@ -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
}

45
game/config/config.go Normal file
View file

@ -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()
}

View file

@ -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 {