From 540c8ba9d4afa7d80e856125af2d8001c974f374 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Mon, 25 Mar 2024 18:03:02 +0100 Subject: [PATCH] added Zyko0/Ebiary/asset for live reloading --- README.md | 10 ++++++++-- config.go | 2 +- game.go | 50 +++++++++++++++++++++++++++++++++----------------- go.mod | 3 ++- go.sum | 4 ++++ main.go | 9 +-------- 6 files changed, 49 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index cdf1f42..23e3f6a 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,13 @@ [![License](https://img.shields.io/badge/license-GPL-blue.svg)](https://github.com/tlinden/kage-viewer/blob/master/LICENSE) [![Go Report Card](https://goreportcard.com/badge/github.com/tlinden/kage-viewer)](https://goreportcard.com/report/github.com/tlinden/kage-viewer) -This little tool can be used to test shaders written in [Kage](https://ebitengine.org/en/documents/shader.html), a shader meta language for [Ebitengine](https://github.com/hajimehoshi/ebiten). +This little tool can be used to test shaders written in +[Kage](https://ebitengine.org/en/documents/shader.html), a shader meta +language for +[Ebitengine](https://github.com/hajimehoshi/ebiten). kage-viewer +reloads changed assets, which allows you to develop your shader and +see live, how it responds to your changes. If loading fails, an error +will be printed to STDOUT. The same applies for images. ## Installation @@ -129,7 +135,7 @@ Possible parameters equal the long command line options. - [X] Implement loading of images and shader files - [X] Implement basic shader rendering and user input - [ ] Add custom uniforms (maybe using lua code?) -- [ ] Provide a way to respond live to shader code changes (use lua as +- [x] Provide a way to respond live to shader code changes (use lua as well?) # Report bugs diff --git a/config.go b/config.go index 81cb3a2..17fcdd2 100644 --- a/config.go +++ b/config.go @@ -32,7 +32,7 @@ import ( ) const ( - VERSION string = "0.0.2" + VERSION string = "0.0.3" Usage string = `This is kage-viewer, a shader viewer. Usage: kage-viewer [-vd] [-c ] [-g geom] [-p geom] \ diff --git a/game.go b/game.go index a7a0180..2fe6417 100644 --- a/game.go +++ b/game.go @@ -24,14 +24,16 @@ import ( "log/slog" "os" + "github.com/Zyko0/Ebiary/asset" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/inpututil" ) type Game struct { Conf *Config - Images []*ebiten.Image - Shader *ebiten.Shader + Images []*asset.LiveAsset[*ebiten.Image] + Shader *asset.LiveAsset[*ebiten.Shader] + Cursor []float64 Ticks int Slider float64 Flag int @@ -55,24 +57,20 @@ func LoadImage(name string) (*ebiten.Image, error) { func (game *Game) Init() error { for _, image := range game.Conf.Image { slog.Debug("Loading images", "image", image) - img, err := LoadImage(image) + //img, err := LoadImage(image) + img, err := asset.NewLiveAsset[*ebiten.Image](image) if err != nil { - return err + return fmt.Errorf("failed to load image %s: %s", image, err) } game.Images = append(game.Images, img) } - data, err := os.ReadFile(game.Conf.Shader) + shader, err := asset.NewLiveAsset[*ebiten.Shader](game.Conf.Shader) if err != nil { return fmt.Errorf("failed to load shader %s: %s", game.Conf.Shader, err) } - shader, err := ebiten.NewShader(data) - if err != nil { - return fmt.Errorf("failed to create new shader %s: %s", game.Conf.Shader, err) - } - game.Shader = shader return nil @@ -120,10 +118,28 @@ func (g *Game) Down() { } func (game *Game) Update() error { - if game.CheckInput() { - slog.Debug("Key pressed", "Slider", game.Slider, "Flag", game.Flag) + for _, image := range game.Images { + if image.Error() != nil { + fmt.Println("warn: image reloading error:", image.Error()) + } } + if game.Shader.Error() != nil { + fmt.Println("warn: shader reloading error:", game.Shader.Error()) + } + + if game.CheckInput() { + slog.Debug("Key pressed", + game.Conf.Flag, game.Flag, + game.Conf.Slider, game.Slider, + game.Conf.Ticks, fmt.Sprintf("%.02f", float64(game.Ticks)/60), + game.Conf.Mouse, fmt.Sprintf("%.02f, %.02f", game.Cursor[0], game.Cursor[1]), + ) + } + + mousex, mousey := ebiten.CursorPosition() + game.Cursor = []float64{float64(mousex), float64(mousey)} + game.Ticks++ return nil @@ -132,20 +148,20 @@ func (game *Game) Update() error { func (game *Game) Draw(screen *ebiten.Image) { op := &ebiten.DrawRectShaderOptions{} - mousex, mousey := ebiten.CursorPosition() - op.Uniforms = map[string]any{ game.Conf.Flag: game.Flag, game.Conf.Slider: game.Slider, game.Conf.Ticks: float64(game.Ticks) / 60, - game.Conf.Mouse: []float64{float64(mousex), float64(mousey)}, + game.Conf.Mouse: game.Cursor, } - copy(op.Images[:3], game.Images) + for idx, image := range game.Images { + op.Images[idx] = image.Value() + } op.GeoM.Translate(float64(game.Conf.X), float64(game.Conf.Y)) - screen.DrawRectShader(game.Conf.Width, game.Conf.Height, game.Shader, op) + screen.DrawRectShader(game.Conf.Width, game.Conf.Height, game.Shader.Value(), op) } func (game *Game) Layout(outsideWidth, outsideHeight int) (int, int) { diff --git a/go.mod b/go.mod index 7e5ff5d..4991cbf 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,10 @@ module github.com/TLINDEN/kage-viewer go 1.22 require ( + github.com/Zyko0/Ebiary/asset v0.0.0-20240304185439-be56fe8a2a6a // indirect github.com/ebitengine/purego v0.6.0 // indirect github.com/fatih/color v1.16.0 // indirect - github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect github.com/hajimehoshi/ebiten/v2 v2.6.7 // indirect github.com/jezek/xgb v1.1.0 // indirect diff --git a/go.sum b/go.sum index cdbf260..cc99059 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,13 @@ +github.com/Zyko0/Ebiary/asset v0.0.0-20240304185439-be56fe8a2a6a h1:kn4fhGvVA6T1lK7qWujIj3m7e9imCZe4MHBuBeflKgU= +github.com/Zyko0/Ebiary/asset v0.0.0-20240304185439-be56fe8a2a6a/go.mod h1:4CqqwHRUbvGBpBd5ye4MxDA4k/XtZqrAD1sg9uxmcYI= github.com/ebitengine/purego v0.6.0 h1:Yo9uBc1x+ETQbfEaf6wcBsjrQfCEnh/gaGUg7lguEJY= github.com/ebitengine/purego v0.6.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c= github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/hajimehoshi/ebiten/v2 v2.6.7 h1:rxlMxu487wZN/JteykmuGdO1qotOolL8vJDU85lPh7A= diff --git a/main.go b/main.go index 7a6c202..b211bd5 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,6 @@ import ( "fmt" "log" "os" - "runtime/debug" "log/slog" @@ -46,7 +45,6 @@ func main() { if conf.Debug { logLevel := &slog.LevelVar{} // we're using a more verbose logger in debug mode - buildInfo, _ := debug.ReadBuildInfo() opts := &yadu.Options{ Level: logLevel, AddSource: true, @@ -55,12 +53,7 @@ func main() { logLevel.Set(slog.LevelDebug) handler := yadu.NewHandler(os.Stdout, opts) - debuglogger := slog.New(handler).With( - slog.Group("program_info", - slog.Int("pid", os.Getpid()), - slog.String("go_version", buildInfo.GoVersion), - ), - ) + debuglogger := slog.New(handler) slog.SetDefault(debuglogger) }