From f8c0fff6a77ed7f9fea468fe3fb353a0d62f812f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 19 Jan 2015 22:08:51 +0900 Subject: [PATCH] example: Bug fix: the path from runtime.Caller is not useful on JavaScript (#99) --- ebitenutil/loadimage.go | 3 +++ example/internal/font.go | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ebitenutil/loadimage.go b/ebitenutil/loadimage.go index 6bc8e91e9..4a9408377 100644 --- a/ebitenutil/loadimage.go +++ b/ebitenutil/loadimage.go @@ -23,6 +23,9 @@ import ( ) // NewImageFromFile loads the file path and returns ebiten.Image and image.Image. +// +// The current directory for path depends on your environment. This will vary on your desktop or web browser. +// It'll be safer to embed your resource, e.g., with github.com/jteeuwen/go-bindata instead of using this function. func NewImageFromFile(path string, filter ebiten.Filter) (*ebiten.Image, image.Image, error) { file, err := os.Open(path) if err != nil { diff --git a/example/internal/font.go b/example/internal/font.go index 3951552d6..61e7f48e4 100644 --- a/example/internal/font.go +++ b/example/internal/font.go @@ -42,8 +42,15 @@ func (f *Font) TextWidth(str string) int { } func init() { - _, path, _, _ := runtime.Caller(0) - arcadeFontPath := filepath.Join(filepath.Dir(path), "..", "images", "arcadefont.png") + dir := "" + if runtime.GOARCH != "js" { + // Get the path of this file (font.go). + _, path, _, _ := runtime.Caller(0) + path = filepath.Dir(path) + dir = filepath.Join(path, "..") + } + arcadeFontPath := filepath.Join(dir, "images", "arcadefont.png") + arcadeFontImage, _, err := ebitenutil.NewImageFromFile(arcadeFontPath, ebiten.FilterNearest) if err != nil { panic(err)