using resources so it might run as a plugin

This commit is contained in:
Jeff Carr 2024-12-05 18:49:47 -06:00
parent 4a72be2044
commit 3b22b50a2b
7 changed files with 48 additions and 35 deletions

View File

@ -1,11 +1,12 @@
all: plugin all: goimports plugin
ldd pixelgl.so #ldd pixelgl.so
plugin: plugin:
go build -v -x -buildmode=plugin -o pixelgl.so GO111MODULE=off go build -v -x -buildmode=plugin -o pixelgl.so
install: 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: full-plugin:
GO111MODULE=off go build -v -x -buildmode=plugin -o pixelgl.so 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 GO111MODULE=off go build -v -x -buildmode=plugin -o pixelgl.so
standalone: standalone:
go build -v -x GO111MODULE=off go build -v -x
./pixelgl
check-git-clean: check-git-clean:
@git diff-index --quiet HEAD -- || (echo "Git repository is dirty, please commit your changes first"; exit 1) @git diff-index --quiet HEAD -- || (echo "Git repository is dirty, please commit your changes first"; exit 1)

42
main.go
View File

@ -9,6 +9,8 @@ package main
import ( import (
"embed" "embed"
"io/fs"
"os"
"go.wit.com/dev/alexflint/arg" "go.wit.com/dev/alexflint/arg"
"go.wit.com/log" "go.wit.com/log"
@ -22,10 +24,14 @@ import (
var VERSION string var VERSION string
var BUILDTIME string var BUILDTIME string
//go:embed *.glsl //go:embed resources/*
var glFile embed.FS var resources embed.FS
var pp *arg.Parser var pp *arg.Parser
// the glsl file
var glslFile string
func init() { func init() {
pp = arg.MustParse(&argv) pp = arg.MustParse(&argv)
@ -50,6 +56,7 @@ func init() {
go simpleStdin() go simpleStdin()
glslFile = loadGLSL(argv.Filename)
// I think this doesn't work as a goroutine because // I think this doesn't work as a goroutine because
// opengl closes. This plugin probably has to wait // opengl closes. This plugin probably has to wait
// until there is some sort of protobuf + socket interface // until there is some sort of protobuf + socket interface
@ -66,3 +73,34 @@ func main() {
// parseConfig() // parseConfig()
pixelgl.Run(run) 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)
}

View File

@ -1,12 +1,8 @@
package main package main
import ( import (
"io/fs"
"io/ioutil"
"github.com/faiface/pixel" "github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl" "github.com/faiface/pixel/pixelgl"
"go.wit.com/log"
) )
// Pixel Shader utility functions // 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
}

View File

@ -32,12 +32,6 @@ func run() {
// I am putting all shader example initializing stuff here for // I am putting all shader example initializing stuff here for
// easier reference to those learning to use this functionality // easier reference to those learning to use this functionality
fragSource, err := LoadFileToString(argv.Filename)
if err != nil {
panic(err)
}
var uMouse mgl32.Vec4 var uMouse mgl32.Vec4
var uTime float32 var uTime float32
var glDrift float32 = argv.GLdrift var glDrift float32 = argv.GLdrift
@ -52,7 +46,7 @@ func run() {
"uDrift", &glDrift, "uDrift", &glDrift,
) )
canvas.SetFragmentShader(fragSource) canvas.SetFragmentShader(glslFile)
start := time.Now() start := time.Now()