2022-01-03 00:28:43 +00:00
|
|
|
/*
|
|
|
|
Package sprites manages miscellaneous in-game sprites.
|
|
|
|
|
|
|
|
The sprites are relatively few for UI purposes. Their textures are
|
|
|
|
loaded ONE time and cached in this package for performance.
|
|
|
|
*/
|
2019-07-04 03:24:04 +00:00
|
|
|
package sprites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"image/png"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-12-22 22:11:01 +00:00
|
|
|
"runtime"
|
2019-07-04 03:24:04 +00:00
|
|
|
|
2021-07-14 01:02:57 +00:00
|
|
|
"git.kirsle.net/apps/doodle/assets"
|
2019-07-04 03:24:04 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
2019-12-22 22:11:01 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/wasm"
|
2019-12-31 02:13:28 +00:00
|
|
|
"git.kirsle.net/go/render"
|
|
|
|
"git.kirsle.net/go/ui"
|
2019-07-04 03:24:04 +00:00
|
|
|
)
|
|
|
|
|
2022-01-03 00:28:43 +00:00
|
|
|
// Cache of loaded sprites.
|
|
|
|
var cache = map[string]*ui.Image{}
|
|
|
|
|
|
|
|
// FlushCache clears the sprites cache.
|
|
|
|
func FlushCache() {
|
|
|
|
panic("TODO: free textures")
|
|
|
|
}
|
|
|
|
|
2019-07-04 03:24:04 +00:00
|
|
|
// LoadImage loads a sprite as a ui.Image object. It checks Doodle's embedded
|
|
|
|
// bindata, then the filesystem before erroring out.
|
|
|
|
//
|
|
|
|
// NOTE: only .png images supported as of now. TODO
|
|
|
|
func LoadImage(e render.Engine, filename string) (*ui.Image, error) {
|
2022-01-03 00:28:43 +00:00
|
|
|
if cached, ok := cache[filename]; ok {
|
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
2019-07-04 03:24:04 +00:00
|
|
|
// Try the bindata first.
|
2021-07-14 01:02:57 +00:00
|
|
|
if data, err := assets.Asset(filename); err == nil {
|
2019-07-04 03:24:04 +00:00
|
|
|
log.Debug("sprites.LoadImage: %s from bindata", filename)
|
|
|
|
|
|
|
|
img, err := png.Decode(bytes.NewBuffer(data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-20 00:14:00 +00:00
|
|
|
return ui.ImageFromImage(img)
|
2019-07-04 03:24:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:11:01 +00:00
|
|
|
// WASM: try the file over HTTP ajax request.
|
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
data, err := wasm.HTTPGet(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := png.Decode(bytes.NewBuffer(data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-20 00:14:00 +00:00
|
|
|
return ui.ImageFromImage(img)
|
2019-12-22 22:11:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 03:24:04 +00:00
|
|
|
// Then try the file system.
|
|
|
|
if _, err := os.Stat(filename); !os.IsNotExist(err) {
|
|
|
|
log.Debug("sprites.LoadImage: %s from filesystem", filename)
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := png.Decode(bytes.NewBuffer(data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-20 00:14:00 +00:00
|
|
|
return ui.ImageFromImage(img)
|
2019-07-04 03:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("no such sprite found")
|
|
|
|
}
|