From 13461aa45577938444600f780360bd24b27442a9 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 7 Mar 2016 01:29:12 +0900 Subject: [PATCH] ebitenutil: Add OpenFile --- ebitenutil/file.go | 29 ++++++++++++++++++++++ ebitenutil/{loadimage_js.go => file_js.go} | 28 ++++++++++----------- ebitenutil/loadimage.go | 8 +++--- 3 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 ebitenutil/file.go rename ebitenutil/{loadimage_js.go => file_js.go} (78%) diff --git a/ebitenutil/file.go b/ebitenutil/file.go new file mode 100644 index 000000000..04c75c967 --- /dev/null +++ b/ebitenutil/file.go @@ -0,0 +1,29 @@ +// Copyright 2016 Hajime Hoshi +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !js + +package ebitenutil + +import ( + "io" + "os" +) + +// OpenFile opens a file and returns a stream for its data. +// +// This function is available both on desktops and browsers. +func OpenFile(path string) (io.ReadCloser, error) { + return os.Open(path) +} diff --git a/ebitenutil/loadimage_js.go b/ebitenutil/file_js.go similarity index 78% rename from ebitenutil/loadimage_js.go rename to ebitenutil/file_js.go index 364d9b079..20b9ff04b 100644 --- a/ebitenutil/loadimage_js.go +++ b/ebitenutil/file_js.go @@ -20,12 +20,20 @@ import ( "bytes" "errors" "fmt" + "io" + "github.com/gopherjs/gopherjs/js" - "github.com/hajimehoshi/ebiten" - "image" ) -func NewImageFromFile(path string, filter ebiten.Filter) (*ebiten.Image, image.Image, error) { +type file struct { + *bytes.Reader +} + +func (f *file) Close() error { + return nil +} + +func OpenFile(path string) (io.ReadCloser, error) { var err error var content *js.Object ch := make(chan struct{}) @@ -48,18 +56,10 @@ func NewImageFromFile(path string, filter ebiten.Filter) (*ebiten.Image, image.I req.Call("send") <-ch if err != nil { - return nil, nil, err + return nil, err } data := js.Global.Get("Uint8Array").New(content).Interface().([]uint8) - b := bytes.NewBuffer(data) - img, _, err := image.Decode(b) - if err != nil { - return nil, nil, err - } - img2, err := ebiten.NewImageFromImage(img, filter) - if err != nil { - return nil, nil, err - } - return img2, img, nil + f := &file{bytes.NewReader(data)} + return f, nil } diff --git a/ebitenutil/loadimage.go b/ebitenutil/loadimage.go index 4a9408377..a58ce64a5 100644 --- a/ebitenutil/loadimage.go +++ b/ebitenutil/loadimage.go @@ -12,14 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -// +build !js - package ebitenutil import ( - "github.com/hajimehoshi/ebiten" "image" - "os" + + "github.com/hajimehoshi/ebiten" ) // NewImageFromFile loads the file path and returns ebiten.Image and image.Image. @@ -27,7 +25,7 @@ import ( // 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) + file, err := OpenFile(path) if err != nil { return nil, nil, err }