1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-10 14:26:15 -05:00
OpenDiablo2/d2core/d2audio/ebiten/ebiten_sound_effect.go
Tim Sarbin 2461142fbd
Minor changes to project layout (#276)
* Minor changes to reduce interdependencies on modules.
2020-01-31 23:18:11 -05:00

53 lines
1.1 KiB
Go

package ebiten
import (
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2assetmanager"
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/wav"
)
type EbitenSoundEffect struct {
player *audio.Player
}
func CreateSoundEffect(sfx string, context *audio.Context, volume float64) *EbitenSoundEffect {
result := &EbitenSoundEffect{}
var soundFile string
if _, exists := d2datadict.Sounds[sfx]; exists {
soundEntry := d2datadict.Sounds[sfx]
soundFile = soundEntry.FileName
} else {
soundFile = sfx
}
audioData, err := d2assetmanager.LoadFile(soundFile)
if err != nil {
panic(err)
}
d, err := wav.Decode(context, audio.BytesReadSeekCloser(audioData))
if err != nil {
log.Fatal(err)
}
player, err := audio.NewPlayer(context, d)
if err != nil {
log.Fatal(err)
}
player.SetVolume(volume)
result.player = player
return result
}
func (v *EbitenSoundEffect) Play() {
v.player.Rewind()
v.player.Play()
}
func (v *EbitenSoundEffect) Stop() {
v.player.Pause()
}