Skip to content

Commit

Permalink
sdl/render: workaround for audio playback going corrupt from calling …
Browse files Browse the repository at this point in the history
…sdl.Renderer.Copy()

It seems like calling the `Copy()` method with the `dst` parameter being
sdl.Rect from the Go context could make audio playback from the `mix`
package going corrupt (e.g. `mix.Chunk.Play(1, 0)`) especially when
the `sdl.Rect` goes outside the window screen.

This workaround prevents that by allocating `SDL_Rect` in the C context
via a wrapper C function called `RenderCopy()`.

Signed-off-by: Lilis Iskandar <[email protected]>
  • Loading branch information
veeableful committed Aug 17, 2019
1 parent b6ac1ad commit 88c5b0b
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions sdl/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ static inline int SDL_UpdateYUVTexture(SDL_Texture* texture, const SDL_Rect* rec
return -1;
}
#endif
// WORKAROUND: This prevents audio from seemingly going corrupt when drawing outside the screen bounding box?
// It does that by allocating SDL_Rect in the C context instead of Go context.
static inline int RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect *src, int dst_x, int dst_y, int dst_w, int dst_h)
{
SDL_Rect dst = {dst_x, dst_y, dst_w, dst_h};
return SDL_RenderCopy(renderer, texture, src, &dst);
}
*/
import "C"
import (
Expand Down Expand Up @@ -548,11 +556,11 @@ func (renderer *Renderer) FillRects(rects []Rect) error {
// (https://wiki.libsdl.org/SDL_RenderCopy)
func (renderer *Renderer) Copy(texture *Texture, src, dst *Rect) error {
return errorFromInt(int(
C.SDL_RenderCopy(
C.RenderCopy(
renderer.cptr(),
texture.cptr(),
src.cptr(),
dst.cptr())))
C.int(dst.X), C.int(dst.Y), C.int(dst.W), C.int(dst.H))))
}

// CopyEx copies a portion of the texture to the current rendering target, optionally rotating it by angle around the given center and also flipping it top-bottom and/or left-right.
Expand Down

0 comments on commit 88c5b0b

Please sign in to comment.