sdl-gpu icon indicating copy to clipboard operation
sdl-gpu copied to clipboard

A single GPU_Flip() isn't updating the screen

Open ramirez7 opened this issue 2 years ago • 7 comments

I've been writing a little tool to visualize hitbox collisions. It's a function that takes two hitboxes, draws them + their collision manifold (if any), and then waits for Quit/Esc.

The code is basically

  • GPU_Init() with default flags
  • Draw shapes
  • Visualize manifold with circles and lines
  • GPU_Flip()
  • Loop over SDL events until Quit/Esc
  • GPU_Quit()

For some reason, the screen isn't being updated on that GPU_Flip() for me (it's just showing the pixels that were there if that makes sense - the way it behaves when you just spawn a window).

When I put GPU_Flip() in my wait loop and call it a bunch of times, everything does work. But I'm surprised a single one isn't working.

There is some indirection here: I am calling sdl-gpu from Haskell in its repl using my bindings. That said, I think I've done everything I need to for that to work (RTS is threaded, I'm using more than one native thread to back the green threads, the loop is running in a bound thread and not being shifted around OS thread in the b/g, etc). It is possible this is something on my end of the Haskell/C divide, but I figured I'd ask (and I'd like to fix it on my side anyways ofc).

ramirez7 avatar Jan 24 '23 22:01 ramirez7

I've noticed that if I do a certain amount of GPU_Flips in a row before my wait loop, it works. On my machine, it's about 20. Which makes it feel like some sort of race condition to me.

ramirez7 avatar Jan 24 '23 22:01 ramirez7

I've refactored the code to be more like my game loops:

  • GPU_Init() with default flags
  • Loop:
    • GPU_Clear()
    • Draw shapes and manifolds
    • SDL_PollEvent() and see if any Quit/Esc events are in there
      • If no, GPU_Flip() and SDL_Delay(100)
      • If so, GPU_Quit()

And it works fine. So maybe my understanding of how sdl-gpu works is off.

ramirez7 avatar Jan 24 '23 23:01 ramirez7

I am not at a computer to test anything, but you could try GPU_FlushBlitBuffer() before the flip. SDL_gpu does hold on to data in order to render it all in one go and double-buffering could also be involved (so front and back buffers might appear out of sync if you aren't clearing and flipping them every frame).

grimfang4 avatar Jan 24 '23 23:01 grimfang4

GPU_FlushBlitBuffer() didn't make a difference either.

ramirez7 avatar Jan 26 '23 00:01 ramirez7

I'll confirm I can make a minimal repro in C.

ramirez7 avatar Jan 26 '23 00:01 ramirez7

Can you share it with us?

albertvaka avatar Jan 27 '23 15:01 albertvaka

Here's a C repro:

https://gitlab.com/macaroni.dev/sdl-gpu-hs/-/merge_requests/11/diffs

ramirez7 avatar Feb 16 '23 23:02 ramirez7