example: Bug fix: the path from runtime.Caller is not useful on JavaScript (#99)

This commit is contained in:
Hajime Hoshi 2015-01-19 22:08:51 +09:00
parent d12477fb2a
commit f8c0fff6a7
2 changed files with 12 additions and 2 deletions

View File

@ -23,6 +23,9 @@ import (
) )
// NewImageFromFile loads the file path and returns ebiten.Image and image.Image. // 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) { func NewImageFromFile(path string, filter ebiten.Filter) (*ebiten.Image, image.Image, error) {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {

View File

@ -42,8 +42,15 @@ func (f *Font) TextWidth(str string) int {
} }
func init() { func init() {
dir := ""
if runtime.GOARCH != "js" {
// Get the path of this file (font.go).
_, path, _, _ := runtime.Caller(0) _, path, _, _ := runtime.Caller(0)
arcadeFontPath := filepath.Join(filepath.Dir(path), "..", "images", "arcadefont.png") path = filepath.Dir(path)
dir = filepath.Join(path, "..")
}
arcadeFontPath := filepath.Join(dir, "images", "arcadefont.png")
arcadeFontImage, _, err := ebitenutil.NewImageFromFile(arcadeFontPath, ebiten.FilterNearest) arcadeFontImage, _, err := ebitenutil.NewImageFromFile(arcadeFontPath, ebiten.FilterNearest)
if err != nil { if err != nil {
panic(err) panic(err)