veandco / go-sdl2

SDL2 binding for Go
https://godoc.org/github.com/veandco/go-sdl2
BSD 3-Clause "New" or "Revised" License
2.21k stars 221 forks source link

Audio messed up #420

Closed Matias-Barrios closed 5 years ago

Matias-Barrios commented 5 years ago

I'm trying to add audio to a game I'm making.

I can see the audio plays ok just the first three times ( .WAV by the way ) and from the 4th time.onwards is messed up ( like it were radio static ).

Here is the repo of my code : https://github.com/Matias-Barrios/SDL_Universe/blob/master/SDL/audio.go

Can someone tell me if I'm doing something wrong that might cause the issue?

veeableful commented 5 years ago

Hi @Matias-Barrios, I have been investigating the issue but I haven't found the real problem that is causing it yet. I did find that the board.Draw function seems to contribute to the problem, at least at a glance. For example, if I call the function three more times, it seems to reproduce the problem as soon as the next piece appears. However, if I play the sound before the next piece appears, it seems to be fine.

It is strange that if I comment the body of SDL.DrawStuff out, the problem seems to be gone even though it doesn't seem to be related. Perhaps memory corruption somewhere?

I will continue to investigate the issue when I can!

Matias-Barrios commented 5 years ago

@veeableful Thanks for checking on this. I'll try to check on that relationship and I'll post here with my findings

Matias-Barrios commented 5 years ago

@veeableful so I tried removing the board.Draw method entirely and it works fine. Then I tried removing only the SDL.Draw from it, and it also works fine.

So the issue is happening only because of the amount of times I call SDL.Draw. (?)

Do you have any idea if this is possible and why? I can help to solve the bug if you give me some hint

veeableful commented 5 years ago

Hi @Matias-Barrios,

I seem to have fixed it by allocating the destination rectangle for Copy() in the native C context. I suppose it has something to do with how Go's memory works (perhaps the rectangle got cleaned up before it can be used by SDL_Renderer?). I have pushed the fix into the master branch so let me know if it works for you!

Matias-Barrios commented 5 years ago

@veeableful Hi ! I have just downloaded the latest version and now it works perfectly fine. thanks so much for your help!! Your work is awesome.

veeableful commented 5 years ago

No problem! Thanks for reporting the issue!