49 lines
854 B
Go
49 lines
854 B
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"image"
|
|
_ "image/png"
|
|
"sync"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
)
|
|
|
|
//go:embed assets/image
|
|
var imageAssets embed.FS
|
|
|
|
type imageID int
|
|
|
|
type imageAtlas struct {
|
|
atlas map[imageID]*ebiten.Image
|
|
sync.RWMutex
|
|
}
|
|
|
|
func newImageAtlas() *imageAtlas {
|
|
return &imageAtlas{
|
|
atlas: make(map[imageID]*ebiten.Image),
|
|
}
|
|
}
|
|
|
|
// loadWAV loads a WAV formatted audio file into the atlas.
|
|
func (i *imageAtlas) loadPNG(id imageID, filePath string) error {
|
|
f, err := imageAssets.Open("assets/image/" + filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
img, _, err := image.Decode(f)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
i.atlas[id] = ebiten.NewImageFromImage(img)
|
|
return nil
|
|
}
|
|
|
|
// playAudio rewinds and starts an audio player from the atlas.
|
|
func (i *imageAtlas) getImage(id imageID) *ebiten.Image {
|
|
return i.atlas[id]
|
|
}
|