ebitentutil: Add JoinStringsIntoFilePath

Fixes #428
This commit is contained in:
Hajime Hoshi 2017-12-16 23:17:34 +09:00
parent 2eac4a3b88
commit 10080d83e6
22 changed files with 40 additions and 25 deletions

View File

@ -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...)
}

View File

@ -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...)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}