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
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)

42
main.go
View File

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

View File

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

View File

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