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

Fix issue 342 Blocking & non-blocking APIs uniformity #345

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 9 additions & 10 deletions audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ const (

type PlayOptions struct {
Action PlayAction
Wait bool
Loop bool
}

Expand Down Expand Up @@ -155,18 +154,18 @@ func (p *soundMgr) stopAll() {
}
}

func (p *soundMgr) playAction(media Sound, opts *PlayOptions) (err error) {
func (p *soundMgr) playAction(sound Sound, opts *PlayOptions, wait bool) (err error) {
switch opts.Action {
case PlayRewind:
err = p.play(media, opts.Wait, opts.Loop)
err = p.play(sound, wait, opts.Loop)
case PlayContinue:
err = p.playContinue(media, opts.Wait, opts.Loop)
err = p.playContinue(sound, wait, opts.Loop)
case PlayStop:
p.stop(media)
p.stop(sound)
case PlayResume:
p.resume(media)
p.resume(sound)
case PlayPause:
p.pause(media)
p.pause(sound)
}
return
}
Expand All @@ -188,8 +187,8 @@ func (p *soundMgr) playContinue(media Sound, wait, loop bool) (err error) {
return
}

func (p *soundMgr) play(media Sound, wait, loop bool) (err error) {
source, err := p.g.fs.Open(media.Path)
func (p *soundMgr) play(sound Sound, wait, loop bool) (err error) {
source, err := p.g.fs.Open(sound.Path)
if err != nil {
panic(err)
}
Expand All @@ -204,7 +203,7 @@ func (p *soundMgr) play(media Sound, wait, loop bool) (err error) {
d = convert.ToStereo16(d)
d = convert.Resample(d, audioContext.SampleRate())

sp := &soundPlayer{media: media, loop: loop}
sp := &soundPlayer{media: sound, loop: loop}
sp.Player, err = audioContext.NewPlayer(&readCloser{d, source})
if err != nil {
source.Close()
Expand Down
82 changes: 53 additions & 29 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -1271,46 +1271,66 @@ func (p *Game) loadSound(name string) (media Sound, err error) {

// Play func:
//
// Play(sound)
// Play(sound) -- sync
// Play(soundName) -- sync
// Play(sound, options) -- sync
// Play(soundName, options) -- sync
// StartPlay(sound) -- async
// StartPlay(soundName) -- async
// StartPlay(sound, options) -- async
// StartPlay(soundName, options) -- async
// 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{})
func (p *Game) Play__0(sound Sound, options *PlayOptions) {
p.play(sound, options, true)
}

func (p *Game) Play__1(media Sound, wait bool) {
p.Play__2(media, &PlayOptions{Wait: wait})
func (p *Game) Play__1(soundName string, options *PlayOptions) {
Copy link
Collaborator

@nighca nighca Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to restore the original order of overloads after the overload issue fixed by gop. Related PR: goplus/gop#1991

sound, err := p.loadSound(soundName)
if err != nil {
log.Println(err)
return
}
p.play(sound, options, true)
}

func (p *Game) Play__2(media Sound, action *PlayOptions) {
if debugInstr {
log.Println("Play", media.Path)
}
func (p *Game) Play__2(sound Sound) {
p.Play__0(sound, &PlayOptions{})
}

func (p *Game) Play__3(soundName string) {
p.Play__1(soundName, &PlayOptions{})
}

err := p.sounds.playAction(media, action)
func (p *Game) StartPlay__0(sound Sound, options *PlayOptions) {
p.play(sound, options, false)
}

func (p *Game) StartPlay__1(soundName string, options *PlayOptions) {
sound, err := p.loadSound(soundName)
if err != nil {
panic(err)
log.Println(err)
return
}
p.play(sound, options, false)
}
func (p *Game) Play__3(mediaName string) {
p.Play__5(mediaName, &PlayOptions{})

func (p *Game) StartPlay__2(sound Sound) {
p.StartPlay__0(sound, &PlayOptions{})
}

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

func (p *Game) Play__5(mediaName string, action *PlayOptions) {
media, err := p.loadSound(mediaName)
func (p *Game) play(sound Sound, options *PlayOptions, wait bool) {
if debugInstr {
log.Println("play", sound.Path)
}

err := p.sounds.playAction(sound, options, wait)
if err != nil {
log.Println(err)
return
panic(err)
}
p.Play__2(media, action)
}

func (p *Game) StopAllSounds() {
Expand Down Expand Up @@ -1349,12 +1369,16 @@ func (p *Game) Broadcast__0(msg string) {
p.doBroadcast(msg, nil, false)
}

func (p *Game) Broadcast__1(msg string, wait bool) {
p.doBroadcast(msg, nil, wait)
func (p *Game) Broadcast__1(msg string, data interface{}) {
p.doBroadcast(msg, data, false)
}

func (p *Game) BroadcastAndWait__0(msg string, data interface{}) {
p.doBroadcast(msg, data, true)
}

func (p *Game) Broadcast__2(msg string, data interface{}, wait bool) {
p.doBroadcast(msg, data, wait)
func (p *Game) BroadcastAndWait__1(msg string) {
p.doBroadcast(msg, nil, true)
}

// -----------------------------------------------------------------------------
Expand Down
15 changes: 13 additions & 2 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func (p *Sprite) goAnimateInternal(name string, ani *aniConfig, isBlocking bool)
}

if ani.OnStart != nil && ani.OnStart.Play != "" {
p.g.Play__3(ani.OnStart.Play)
p.g.StartPlay__3(ani.OnStart.Play)
}

//anim frame
Expand Down Expand Up @@ -807,7 +807,18 @@ func (p *Sprite) Animate(name string) {
log.Println("==> Animation", name)
}
if ani, ok := p.animations[name]; ok {
p.goAnimate(name, ani)
p.goAnimateInternal(name, ani, true)
} else {
log.Println("Animation not found:", name)
}
}

func (p *Sprite) StartAnimate(name string) {
if debugInstr {
log.Println("==> StartAnimation", name)
}
if ani, ok := p.animations[name]; ok {
p.goAnimateInternal(name, ani, false)
} else {
log.Println("Animation not found:", name)
}
Expand Down
2 changes: 1 addition & 1 deletion tutorial/02-Dragon/Dragon.spx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ onStart => {
step 5
if touching("Shark") {
score++
play chomp, true
play chomp
step -100
}
}
Expand Down
Loading