Skip to content

Commit

Permalink
all: embed texture file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dmitshur committed Nov 3, 2024
1 parent f8ff288 commit 7dda5e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 54 deletions.
33 changes: 6 additions & 27 deletions gl21-cube/cube.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

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

import (
"go/build"
"embed"
"image"
"image/draw"
_ "image/png"
"log"
"os"
"runtime"

"github.com/go-gl/gl/v2.1/gl"
Expand Down Expand Up @@ -62,7 +61,7 @@ func main() {
}

func newTexture(file string) uint32 {
imgFile, err := os.Open(file)
imgFile, err := assetFS.Open(file)
if err != nil {
log.Fatalf("texture %q not found on disk: %v\n", file, err)
}
Expand Down Expand Up @@ -204,25 +203,5 @@ func drawScene() {
gl.End()
}

// Set the working directory to the root of Go package, so that its assets can be accessed.
func init() {
dir, err := importPathToDir("github.com/go-gl/example/gl21-cube")
if err != nil {
log.Fatalln("Unable to find Go package in your GOPATH, it's needed to load assets:", err)
}
err = os.Chdir(dir)
if err != nil {
log.Panicln("os.Chdir:", err)
}
}

// importPathToDir resolves the absolute path from importPath.
// There doesn't need to be a valid Go package inside that import path,
// but the directory must exist.
func importPathToDir(importPath string) (string, error) {
p, err := build.Import(importPath, "", build.FindOnly)
if err != nil {
return "", err
}
return p.Dir, nil
}
//go:embed square.png
var assetFS embed.FS
33 changes: 6 additions & 27 deletions gl41core-cube/cube.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

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

import (
"embed"
"fmt"
"go/build"
"image"
"image/draw"
_ "image/png"
"log"
"os"
"runtime"
"strings"

Expand Down Expand Up @@ -198,7 +197,7 @@ func compileShader(source string, shaderType uint32) (uint32, error) {
}

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

// Set the working directory to the root of Go package, so that its assets can be accessed.
func init() {
dir, err := importPathToDir("github.com/go-gl/example/gl41core-cube")
if err != nil {
log.Fatalln("Unable to find Go package in your GOPATH, it's needed to load assets:", err)
}
err = os.Chdir(dir)
if err != nil {
log.Panicln("os.Chdir:", err)
}
}

// importPathToDir resolves the absolute path from importPath.
// There doesn't need to be a valid Go package inside that import path,
// but the directory must exist.
func importPathToDir(importPath string) (string, error) {
p, err := build.Import(importPath, "", build.FindOnly)
if err != nil {
return "", err
}
return p.Dir, nil
}
//go:embed square.png
var assetFS embed.FS

0 comments on commit 7dda5e1

Please sign in to comment.