1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-26 00:55:23 +00:00
OpenDiablo2/d2core/d2audio/ebiten/ebiten_sound_effect.go
Alex Yatskov 8a547ebf0e
Moving files around to make more sense (#279)
* Prevent input fighting between terminal and other input listeners.

* Moving files around, removing exports

* Add missing line
2020-02-01 18:55:56 -05:00

53 lines
1.0 KiB
Go

package ebiten
import (
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
"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 := d2asset.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()
}