diff --git a/ebitenutil/file_js.go b/ebitenutil/file_js.go index 943669e47..820edf6b4 100644 --- a/ebitenutil/file_js.go +++ b/ebitenutil/file_js.go @@ -20,6 +20,7 @@ import ( "bytes" "errors" "fmt" + "path" "github.com/gopherjs/gopherjs/js" ) @@ -62,3 +63,7 @@ func OpenFile(path string) (ReadSeekCloser, error) { f := &file{bytes.NewReader(data)} return f, nil } + +func JoinStringsIntoFilePath(elems ...string) string { + return path.Join(elems...) +} diff --git a/ebitenutil/file_notjs.go b/ebitenutil/file_notjs.go index 339fc2295..b9d7279c3 100644 --- a/ebitenutil/file_notjs.go +++ b/ebitenutil/file_notjs.go @@ -21,6 +21,7 @@ package ebitenutil import ( "os" + "path/filepath" ) // OpenFile opens a file and returns a stream for its data. @@ -30,3 +31,12 @@ import ( func OpenFile(path string) (ReadSeekCloser, error) { return os.Open(path) } + +// JoinFilePath joins any number of path elements into a single path, +// adding a Separator if necessary. +// +// This is basically same as filepath.Join, but the behavior is different on JavaScript. +// For browsers, path.Join is called instead of filepath.Join. +func JoinStringsIntoFilePath(elems ...string) string { + return filepath.Join(elems...) +} diff --git a/examples/additive/main.go b/examples/additive/main.go index 5284ab332..cd5b73029 100644 --- a/examples/additive/main.go +++ b/examples/additive/main.go @@ -54,7 +54,7 @@ func update(screen *ebiten.Image) error { func main() { var err error - ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterNearest) + ebitenImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "ebiten.png"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/airship/main.go b/examples/airship/main.go index fbd695186..a14cf3565 100644 --- a/examples/airship/main.go +++ b/examples/airship/main.go @@ -51,7 +51,7 @@ var ( func init() { var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + gophersImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "gophers.jpg"), ebiten.FilterNearest) if err != nil { panic(err) } diff --git a/examples/alphablending/main.go b/examples/alphablending/main.go index 90ebf3bf1..36c74157e 100644 --- a/examples/alphablending/main.go +++ b/examples/alphablending/main.go @@ -63,7 +63,7 @@ func update(screen *ebiten.Image) error { func main() { var err error - ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterNearest) + ebitenImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "ebiten.png"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/audio/main.go b/examples/audio/main.go index 4de199006..62b1ba271 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -103,11 +103,11 @@ func playerBarRect() (x, y, w, h int) { func NewPlayer(audioContext *audio.Context) (*Player, error) { const bytesPerSample = 4 // TODO: This should be defined in audio package - wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav") + wavF, err := ebitenutil.OpenFile(ebitenutil.JoinStringsIntoFilePath("_resources", "audio", "jab.wav")) if err != nil { return nil, err } - mp3F, err := ebitenutil.OpenFile("_resources/audio/classic.mp3") + mp3F, err := ebitenutil.OpenFile(ebitenutil.JoinStringsIntoFilePath("_resources", "audio", "classic.mp3")) if err != nil { return nil, err } diff --git a/examples/blocks/blocks/gamescene.go b/examples/blocks/blocks/gamescene.go index 5fa9aff85..cadfc38d4 100644 --- a/examples/blocks/blocks/gamescene.go +++ b/examples/blocks/blocks/gamescene.go @@ -71,7 +71,7 @@ func linesTextBoxPosition() (x, y int) { func init() { // Background var err error - imageGameBG, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterLinear) + imageGameBG, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "gophers.jpg"), ebiten.FilterLinear) if err != nil { panic(err) } diff --git a/examples/blocks/blocks/piece.go b/examples/blocks/blocks/piece.go index d6f755547..d191cf38a 100644 --- a/examples/blocks/blocks/piece.go +++ b/examples/blocks/blocks/piece.go @@ -27,7 +27,7 @@ var imageBlocks *ebiten.Image func init() { var err error - imageBlocks, _, err = ebitenutil.NewImageFromFile("_resources/images/blocks/blocks.png", ebiten.FilterNearest) + imageBlocks, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "blocks", "blocks.png"), ebiten.FilterNearest) if err != nil { panic(err) } diff --git a/examples/blocks/blocks/titlescene.go b/examples/blocks/blocks/titlescene.go index e0b3bcb37..8120eee7d 100644 --- a/examples/blocks/blocks/titlescene.go +++ b/examples/blocks/blocks/titlescene.go @@ -28,7 +28,7 @@ var imageBackground *ebiten.Image func init() { var err error - imageBackground, _, err = ebitenutil.NewImageFromFile("_resources/images/blocks/background.png", ebiten.FilterNearest) + imageBackground, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "blocks", "background.png"), ebiten.FilterNearest) if err != nil { panic(err) } diff --git a/examples/hsv/main.go b/examples/hsv/main.go index cb9b2ca75..7c1f57547 100644 --- a/examples/hsv/main.go +++ b/examples/hsv/main.go @@ -116,7 +116,7 @@ Inverted: %s [I]`, hue, saturation, value, msgInverted) func main() { var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + gophersImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "gophers.jpg"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/hue/main.go b/examples/hue/main.go index 9261241d5..614e3adc0 100644 --- a/examples/hue/main.go +++ b/examples/hue/main.go @@ -50,7 +50,7 @@ func update(screen *ebiten.Image) error { func main() { var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + gophersImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "gophers.jpg"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/infinitescroll/main.go b/examples/infinitescroll/main.go index 640b6eeaf..1d6cee5f2 100644 --- a/examples/infinitescroll/main.go +++ b/examples/infinitescroll/main.go @@ -101,7 +101,7 @@ func update(screen *ebiten.Image) error { func main() { var err error - bgImage, _, err = ebitenutil.NewImageFromFile("_resources/images/tile.png", ebiten.FilterNearest) + bgImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "tile.png"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/keyboard/main.go b/examples/keyboard/main.go index 2747fb0ac..beb508ab1 100644 --- a/examples/keyboard/main.go +++ b/examples/keyboard/main.go @@ -34,7 +34,7 @@ var keyboardImage *ebiten.Image func init() { var err error - keyboardImage, _, err = ebitenutil.NewImageFromFile("_resources/images/keyboard/keyboard.png", ebiten.FilterNearest) + keyboardImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "keyboard", "keyboard.png"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/masking/main.go b/examples/masking/main.go index 7dcebb10f..eddc93b11 100644 --- a/examples/masking/main.go +++ b/examples/masking/main.go @@ -100,11 +100,11 @@ func min(a, b int) int { func main() { var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + gophersImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "gophers.jpg"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } - fiveyearsImage, _, err = ebitenutil.NewImageFromFile("_resources/images/fiveyears.jpg", ebiten.FilterNearest) + fiveyearsImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "fiveyears.jpg"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/mosaic/main.go b/examples/mosaic/main.go index 5985f026f..ea40260a9 100644 --- a/examples/mosaic/main.go +++ b/examples/mosaic/main.go @@ -51,7 +51,7 @@ func update(screen *ebiten.Image) error { func main() { var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + gophersImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "gophers.jpg"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/perspective/main.go b/examples/perspective/main.go index c0376008d..58db910eb 100644 --- a/examples/perspective/main.go +++ b/examples/perspective/main.go @@ -58,7 +58,7 @@ func update(screen *ebiten.Image) error { func main() { var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + gophersImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "gophers.jpg"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/platformer/main.go b/examples/platformer/main.go index 77ca3316b..dc270f8ab 100644 --- a/examples/platformer/main.go +++ b/examples/platformer/main.go @@ -81,19 +81,19 @@ func update(screen *ebiten.Image) error { func main() { // Preload images var err error - rightSprite, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/right.png", ebiten.FilterNearest) + rightSprite, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "platformer", "right.png"), ebiten.FilterNearest) if err != nil { panic(err) } - leftSprite, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/left.png", ebiten.FilterNearest) + leftSprite, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "platformer", "left.png"), ebiten.FilterNearest) if err != nil { panic(err) } - idleSprite, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/mainchar.png", ebiten.FilterNearest) + idleSprite, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "platformer", "mainchar.png"), ebiten.FilterNearest) if err != nil { panic(err) } - backgroundImage, _, err = ebitenutil.NewImageFromFile("_resources/images/platformer/background.png", ebiten.FilterNearest) + backgroundImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "platformer", "background.png"), ebiten.FilterNearest) if err != nil { panic(err) } diff --git a/examples/rotate/main.go b/examples/rotate/main.go index 61e407dc6..991824dae 100644 --- a/examples/rotate/main.go +++ b/examples/rotate/main.go @@ -51,7 +51,7 @@ func update(screen *ebiten.Image) error { func main() { var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + gophersImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "gophers.jpg"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/sprites/main.go b/examples/sprites/main.go index d4580c3ea..f614f447b 100644 --- a/examples/sprites/main.go +++ b/examples/sprites/main.go @@ -126,7 +126,7 @@ Press <- or -> to change the number of sprites`, ebiten.CurrentFPS(), sprites.nu func main() { var err error - img, _, err := ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterNearest) + img, _, err := ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "ebiten.png"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/ui/main.go b/examples/ui/main.go index ccb2fdb7c..6ba5b32a9 100644 --- a/examples/ui/main.go +++ b/examples/ui/main.go @@ -43,7 +43,7 @@ var ( func init() { var err error - uiImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ui.png", ebiten.FilterNearest) + uiImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "ui.png"), ebiten.FilterNearest) if err != nil { log.Fatal(err) } diff --git a/examples/wav/main.go b/examples/wav/main.go index fbf21c31b..9456b3187 100644 --- a/examples/wav/main.go +++ b/examples/wav/main.go @@ -43,7 +43,7 @@ func init() { log.Fatal(err) } - f, err := ebitenutil.OpenFile("_resources/audio/jab.wav") + f, err := ebitenutil.OpenFile(ebitenutil.JoinStringsIntoFilePath("_resources", "audio", "jab.wav")) if err != nil { log.Fatal(err) } diff --git a/examples/windowsize/main.go b/examples/windowsize/main.go index 06f93e5f0..596d82561 100644 --- a/examples/windowsize/main.go +++ b/examples/windowsize/main.go @@ -168,7 +168,7 @@ FPS: %0.2f`, x, y, ebiten.CurrentFPS()) func main() { var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + gophersImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "gophers.jpg"), ebiten.FilterNearest) if err != nil { log.Fatal(err) }