Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builder#443 Simplify process to add & play sound #279

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type soundMgr struct {
audioContext *audio.Context
players map[*soundPlayer]chan bool
playersM sync.Mutex
audios map[string]Sound
}

const (
Expand All @@ -109,6 +110,7 @@ func (p *soundMgr) init(g *Game) {
p.audioContext = audioContext
p.players = make(map[*soundPlayer]chan bool)
p.g = g
p.audios = make(map[string]Sound)
}

func (p *soundMgr) update() {
Expand Down
33 changes: 24 additions & 9 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,6 @@ func instance(gamer reflect.Value) *Game {
return fld.Addr().Interface().(*Game)
}

func lookupSound(gamer reflect.Value, name string) (Sound, bool) {
if val := findFieldPtr(gamer, name, 0); val != nil {
if m, ok := val.(*Sound); ok {
return *m, true
}
}
return nil, false
}

func getFieldPtrOrAlloc(v reflect.Value, i int) (name string, val interface{}) {
tFld := v.Type().Field(i)
vFld := v.Field(i)
Expand Down Expand Up @@ -1179,6 +1170,10 @@ func (p *Game) ClearSoundEffects() {
type Sound *soundConfig

func (p *Game) loadSound(name string) (media Sound, err error) {
if media, ok := p.sounds.audios[name]; ok {
return media, nil
}

if debugLoad {
log.Println("==> LoadSound", name)
}
Expand All @@ -1188,6 +1183,7 @@ func (p *Game) loadSound(name string) (media Sound, err error) {
return
}
media.Path = prefix + "/" + media.Path
p.sounds.audios[name] = media
return
}

Expand All @@ -1197,6 +1193,9 @@ func (p *Game) loadSound(name string) (media Sound, err error) {
// Play(video) -- maybe
// Play(media, wait) -- sync
// Play(media, opts)
// Play(mediaName)
// Play(mediaName, wait) -- sync
// Play(mediaName, opts)
func (p *Game) Play__0(media Sound) {
p.Play__2(media, &PlayOptions{})
}
Expand All @@ -1215,6 +1214,22 @@ func (p *Game) Play__2(media Sound, action *PlayOptions) {
panic(err)
}
}
func (p *Game) Play__3(mediaName string) {
p.Play__5(mediaName, &PlayOptions{})
}

func (p *Game) Play__4(mediaName string, wait bool) {
p.Play__5(mediaName, &PlayOptions{Wait: wait})
}

func (p *Game) Play__5(mediaName string, action *PlayOptions) {
media, err := p.loadSound(mediaName)
if err != nil {
log.Println(err)
return
}
p.Play__2(media, action)
}

func (p *Game) StopAllSounds() {
p.sounds.stopAll()
Expand Down
6 changes: 1 addition & 5 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,7 @@ func (p *Sprite) goAnimate(name string, ani *aniConfig) {

if ani.OnStart != nil {
if ani.OnStart.Play != "" {
media, playSound := lookupSound(p.gamer, ani.OnStart.Play)
if !playSound {
panic("lookupSound: media not found")
}
p.g.Play__0(media)
p.g.Play__3(ani.OnStart.Play)
}
}

Expand Down
Loading