50 lines
806 B
Go
50 lines
806 B
Go
package assets
|
|
|
|
import (
|
|
"image"
|
|
_ "image/jpeg"
|
|
_ "image/png"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
)
|
|
|
|
type Manager struct {
|
|
images map[string]*ebiten.Image
|
|
misses map[string]bool
|
|
}
|
|
|
|
func NewManager() *Manager {
|
|
return &Manager{
|
|
images: map[string]*ebiten.Image{},
|
|
misses: map[string]bool{},
|
|
}
|
|
}
|
|
|
|
func (m *Manager) Image(path string) (*ebiten.Image, bool) {
|
|
path = filepath.Clean(path)
|
|
if img, ok := m.images[path]; ok {
|
|
return img, true
|
|
}
|
|
if m.misses[path] {
|
|
return nil, false
|
|
}
|
|
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
m.misses[path] = true
|
|
return nil, false
|
|
}
|
|
defer f.Close()
|
|
|
|
src, _, err := image.Decode(f)
|
|
if err != nil {
|
|
m.misses[path] = true
|
|
return nil, false
|
|
}
|
|
img := ebiten.NewImageFromImage(src)
|
|
m.images[path] = img
|
|
return img, true
|
|
}
|