From 3b22b50a2b9312a27218f2096d6d0a2dae2c115d Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 5 Dec 2024 18:49:47 -0600 Subject: [PATCH] using resources so it might run as a plugin --- Makefile | 12 +++--- main.go | 42 ++++++++++++++++++- psutil.go | 21 ---------- planetfall.glsl => resources/planetfall.glsl | 0 seascape.glsl => resources/seascape.glsl | 0 .../seascape_original.glsl | 0 seascape.go | 8 +--- 7 files changed, 48 insertions(+), 35 deletions(-) rename planetfall.glsl => resources/planetfall.glsl (100%) rename seascape.glsl => resources/seascape.glsl (100%) rename seascape_original.glsl => resources/seascape_original.glsl (100%) diff --git a/Makefile b/Makefile index edc1823..8fa7f4e 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,12 @@ -all: plugin - ldd pixelgl.so +all: goimports plugin + #ldd pixelgl.so plugin: - go build -v -x -buildmode=plugin -o pixelgl.so + GO111MODULE=off go build -v -x -buildmode=plugin -o pixelgl.so install: - go build -v -x -buildmode=plugin -o pixelgl.so + rm -f pixelgo.so + go build -v -x -buildmode=plugin -o ~/go/lib/pixelgl.so full-plugin: GO111MODULE=off go build -v -x -buildmode=plugin -o pixelgl.so @@ -14,7 +15,8 @@ full-install: GO111MODULE=off go build -v -x -buildmode=plugin -o pixelgl.so standalone: - go build -v -x + GO111MODULE=off go build -v -x + ./pixelgl check-git-clean: @git diff-index --quiet HEAD -- || (echo "Git repository is dirty, please commit your changes first"; exit 1) diff --git a/main.go b/main.go index d92b0b4..27e62cd 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ package main import ( "embed" + "io/fs" + "os" "go.wit.com/dev/alexflint/arg" "go.wit.com/log" @@ -22,10 +24,14 @@ import ( var VERSION string var BUILDTIME string -//go:embed *.glsl -var glFile embed.FS +//go:embed resources/* +var resources embed.FS + var pp *arg.Parser +// the glsl file +var glslFile string + func init() { pp = arg.MustParse(&argv) @@ -50,6 +56,7 @@ func init() { go simpleStdin() + glslFile = loadGLSL(argv.Filename) // I think this doesn't work as a goroutine because // opengl closes. This plugin probably has to wait // until there is some sort of protobuf + socket interface @@ -66,3 +73,34 @@ func main() { // parseConfig() pixelgl.Run(run) } + +// LoadFileToString loads the contents of a file into a string +func loadGLSL(filename string) string { + var err error + var data []byte + + // + data, err = os.ReadFile(filename) + if err == nil { + log.Println("found embedded file:", filename) + return string(data) + } + data, err = fs.ReadFile(resources, filename) + if len(data) == 0 { + log.Info("still could not find file", filename, err) + } else { + return string(data) + } + + filename = "resources/seascape.glsl" + log.Println("did not find embedded file:", filename, err) + data, err = fs.ReadFile(resources, filename) + if len(data) == 0 { + log.Info("still could not find file", filename) + os.Exit(-1) + } + + // return a string of the data to feed into + // canvas.SetFragmentShader(file) + return string(data) +} diff --git a/psutil.go b/psutil.go index 957e8dc..f7dbe4a 100644 --- a/psutil.go +++ b/psutil.go @@ -1,12 +1,8 @@ package main import ( - "io/fs" - "io/ioutil" - "github.com/faiface/pixel" "github.com/faiface/pixel/pixelgl" - "go.wit.com/log" ) // Pixel Shader utility functions @@ -44,20 +40,3 @@ func CenterWindow(win *pixelgl.Window) { ), ) } - -// LoadFileToString loads the contents of a file into a string -func LoadFileToString(filename string) (string, error) { - embedf, err1 := fs.ReadFile(glFile, filename) - if err1 == nil { - log.Println("found embedded file:", filename) - return string(embedf), nil - } else { - log.Println("did not find embedded file:", filename) - log.Println("err", err1) - } - b, err := ioutil.ReadFile("/tmp/" + filename) - if err != nil { - return "", err - } - return string(b), nil -} diff --git a/planetfall.glsl b/resources/planetfall.glsl similarity index 100% rename from planetfall.glsl rename to resources/planetfall.glsl diff --git a/seascape.glsl b/resources/seascape.glsl similarity index 100% rename from seascape.glsl rename to resources/seascape.glsl diff --git a/seascape_original.glsl b/resources/seascape_original.glsl similarity index 100% rename from seascape_original.glsl rename to resources/seascape_original.glsl diff --git a/seascape.go b/seascape.go index 5e93a27..ac7151c 100644 --- a/seascape.go +++ b/seascape.go @@ -32,12 +32,6 @@ func run() { // I am putting all shader example initializing stuff here for // easier reference to those learning to use this functionality - fragSource, err := LoadFileToString(argv.Filename) - - if err != nil { - panic(err) - } - var uMouse mgl32.Vec4 var uTime float32 var glDrift float32 = argv.GLdrift @@ -52,7 +46,7 @@ func run() { "uDrift", &glDrift, ) - canvas.SetFragmentShader(fragSource) + canvas.SetFragmentShader(glslFile) start := time.Now()