Skip to content

Commit 7dda5e1

Browse files
committed
all: embed texture file
This makes the result binary more portable. It can be copied to another machine where the source code isn't present and still run okay. Relying on the go/build package and GOPATH to find assets was the best we could do before the module mode. Fixes #85.
1 parent f8ff288 commit 7dda5e1

File tree

2 files changed

+12
-54
lines changed

2 files changed

+12
-54
lines changed

gl21-cube/cube.go

+6-27
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// Renders a textured spinning cube using GLFW 3 and OpenGL 2.1.
6-
package main // import "github.com/go-gl/example/gl21-cube"
5+
// gl21-cube renders a textured spinning cube using GLFW 3 and OpenGL 2.1.
6+
package main
77

88
import (
9-
"go/build"
9+
"embed"
1010
"image"
1111
"image/draw"
1212
_ "image/png"
1313
"log"
14-
"os"
1514
"runtime"
1615

1716
"github.com/go-gl/gl/v2.1/gl"
@@ -62,7 +61,7 @@ func main() {
6261
}
6362

6463
func newTexture(file string) uint32 {
65-
imgFile, err := os.Open(file)
64+
imgFile, err := assetFS.Open(file)
6665
if err != nil {
6766
log.Fatalf("texture %q not found on disk: %v\n", file, err)
6867
}
@@ -204,25 +203,5 @@ func drawScene() {
204203
gl.End()
205204
}
206205

207-
// Set the working directory to the root of Go package, so that its assets can be accessed.
208-
func init() {
209-
dir, err := importPathToDir("github.com/go-gl/example/gl21-cube")
210-
if err != nil {
211-
log.Fatalln("Unable to find Go package in your GOPATH, it's needed to load assets:", err)
212-
}
213-
err = os.Chdir(dir)
214-
if err != nil {
215-
log.Panicln("os.Chdir:", err)
216-
}
217-
}
218-
219-
// importPathToDir resolves the absolute path from importPath.
220-
// There doesn't need to be a valid Go package inside that import path,
221-
// but the directory must exist.
222-
func importPathToDir(importPath string) (string, error) {
223-
p, err := build.Import(importPath, "", build.FindOnly)
224-
if err != nil {
225-
return "", err
226-
}
227-
return p.Dir, nil
228-
}
206+
//go:embed square.png
207+
var assetFS embed.FS

gl41core-cube/cube.go

+6-27
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// Renders a textured spinning cube using GLFW 3 and OpenGL 4.1 core forward-compatible profile.
6-
package main // import "github.com/go-gl/example/gl41core-cube"
5+
// gl41core-cube renders a textured spinning cube using GLFW 3 and OpenGL 4.1 core forward-compatible profile.
6+
package main
77

88
import (
9+
"embed"
910
"fmt"
10-
"go/build"
1111
"image"
1212
"image/draw"
1313
_ "image/png"
1414
"log"
15-
"os"
1615
"runtime"
1716
"strings"
1817

@@ -198,7 +197,7 @@ func compileShader(source string, shaderType uint32) (uint32, error) {
198197
}
199198

200199
func newTexture(file string) (uint32, error) {
201-
imgFile, err := os.Open(file)
200+
imgFile, err := assetFS.Open(file)
202201
if err != nil {
203202
return 0, fmt.Errorf("texture %q not found on disk: %v", file, err)
204203
}
@@ -318,25 +317,5 @@ var cubeVertices = []float32{
318317
1.0, 1.0, 1.0, 0.0, 1.0,
319318
}
320319

321-
// Set the working directory to the root of Go package, so that its assets can be accessed.
322-
func init() {
323-
dir, err := importPathToDir("github.com/go-gl/example/gl41core-cube")
324-
if err != nil {
325-
log.Fatalln("Unable to find Go package in your GOPATH, it's needed to load assets:", err)
326-
}
327-
err = os.Chdir(dir)
328-
if err != nil {
329-
log.Panicln("os.Chdir:", err)
330-
}
331-
}
332-
333-
// importPathToDir resolves the absolute path from importPath.
334-
// There doesn't need to be a valid Go package inside that import path,
335-
// but the directory must exist.
336-
func importPathToDir(importPath string) (string, error) {
337-
p, err := build.Import(importPath, "", build.FindOnly)
338-
if err != nil {
339-
return "", err
340-
}
341-
return p.Dir, nil
342-
}
320+
//go:embed square.png
321+
var assetFS embed.FS

0 commit comments

Comments
 (0)