added implementation in GO lang
This commit is contained in:
49
internal/assets/manager.go
Normal file
49
internal/assets/manager.go
Normal file
@ -0,0 +1,49 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user