From b384795e535c6c9ca931b43bc9f44a1b8f944d5d Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Mon, 25 Mar 2024 20:32:09 +0100 Subject: [PATCH] added --tps and --background --- README.md | 2 ++ config.go | 22 ++++++++++++++-------- game.go | 39 ++++++++++++++++++++++++++++++++------- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d0a50c6..998a0e1 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,8 @@ Options: -s --shader Shader to run -g --geometry Window size -p --position Position of image0 +-b --background Image to load as background +-t --tps At how many ticks per second to run --map-flag Map Flag uniform to --map-ticks Map Flag uniform to --map-slider Map Flag uniform to diff --git a/config.go b/config.go index 17fcdd2..86bfe7c 100644 --- a/config.go +++ b/config.go @@ -32,7 +32,7 @@ import ( ) const ( - VERSION string = "0.0.3" + VERSION string = "0.0.4" Usage string = `This is kage-viewer, a shader viewer. Usage: kage-viewer [-vd] [-c ] [-g geom] [-p geom] \ @@ -44,6 +44,8 @@ Options: -s --shader Shader to run -g --geometry Window size -p --position Position of image0 +-b --background Image to load as background +-t --tps At how many ticks per second to run --map-flag Map Flag uniform to --map-ticks Map Flag uniform to --map-slider Map Flag uniform to @@ -54,13 +56,15 @@ Options: ) type Config struct { - Showversion bool `koanf:"version"` // -v - Debug bool `koanf:"debug"` // -d - Config string `koanf:"config"` // -c - Image []string `koanf:"image"` // -i - Shader string `koanf:"shader"` // -s - Geo string `koanf:"geometry"` // -g - Posision string `koanf:"position"` // -p + Showversion bool `koanf:"version"` // -v + Debug bool `koanf:"debug"` // -d + Config string `koanf:"config"` // -c + Image []string `koanf:"image"` // -i + Shader string `koanf:"shader"` // -s + Background string `koanf:"background"` // -b + TPS int `koanf:"tps"` // -t + Geo string `koanf:"geometry"` // -g + Posision string `koanf:"position"` // -p Flag string `koanf:"map-flag"` Ticks string `koanf:"map-ticks"` Mouse string `koanf:"map-mouse"` @@ -91,6 +95,8 @@ func InitConfig() (*Config, error) { flagset.StringP("map-ticks", "", "Ticks", "map ticks uniform") flagset.StringP("map-mouse", "", "Mouse", "map mouse uniform") flagset.StringP("map-slider", "", "Slider", "map slider uniform") + flagset.StringP("background", "b", "", "background image") + flagset.IntP("tps", "t", 60, "ticks per second") if err := flagset.Parse(os.Args[1:]); err != nil { return nil, fmt.Errorf("failed to parse program arguments: %w", err) diff --git a/game.go b/game.go index 2fe6417..ca3e5ac 100644 --- a/game.go +++ b/game.go @@ -30,13 +30,14 @@ import ( ) type Game struct { - Conf *Config - Images []*asset.LiveAsset[*ebiten.Image] - Shader *asset.LiveAsset[*ebiten.Shader] - Cursor []float64 - Ticks int - Slider float64 - Flag int + Conf *Config + Images []*asset.LiveAsset[*ebiten.Image] + Shader *asset.LiveAsset[*ebiten.Shader] + Cursor []float64 + Ticks int + Slider float64 + Flag int + Background *asset.LiveAsset[*ebiten.Image] } func LoadImage(name string) (*ebiten.Image, error) { @@ -71,8 +72,21 @@ func (game *Game) Init() error { return fmt.Errorf("failed to load shader %s: %s", game.Conf.Shader, err) } + if game.Conf.Background != "" { + slog.Debug("Loading background", "image", game.Conf.Background) + + img, err := asset.NewLiveAsset[*ebiten.Image](game.Conf.Background) + if err != nil { + return fmt.Errorf("failed to load image %s: %s", game.Conf.Background, err) + } + + game.Background = img + } + game.Shader = shader + ebiten.SetMaxTPS(game.Conf.TPS) + return nil } @@ -128,6 +142,12 @@ func (game *Game) Update() error { fmt.Println("warn: shader reloading error:", game.Shader.Error()) } + if game.Background != nil { + if game.Background.Error() != nil { + fmt.Println("warn: background image reloading error:", game.Background.Error()) + } + } + if game.CheckInput() { slog.Debug("Key pressed", game.Conf.Flag, game.Flag, @@ -146,6 +166,11 @@ func (game *Game) Update() error { } func (game *Game) Draw(screen *ebiten.Image) { + if game.Background != nil { + op := &ebiten.DrawImageOptions{} + screen.DrawImage(game.Background.Value(), op) + } + op := &ebiten.DrawRectShaderOptions{} op.Uniforms = map[string]any{