-2

(Source code and problem line at the bottom)

I made a simple program to load a transparent PNG onto SDL2. However, it pops up as the image, with a very glitchy background that keeps flashing.

enter image description here

I suspect this is a problem with my graphics card (M2 Macbook Air), but I do not know how to fix this. I think this because the issue goes away after I disable hardware acceleration when creating my SDL renderer.

// Issue goes away if I change the '0' in this line to SDL_RENDERER_SOFTWARE
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);

My full source code can be found at https://pastebin.com/XqRxXmyt. My compile command can also be found at https://pastebin.com/a3YbGnLN (I am statically linking my program). How can I fix this transparency issue?

Eric Xue
  • 272
  • 2
  • 11
  • the problem is that you didn't post your code, but a link to your code. You should edit the question removing the links and adding a proper code snippet – mikyll98 Aug 18 '22 at 11:35

1 Answers1

0

Found a solution. Not sure why it solves the problem, but simply clearing the screen before drawing the texture fixes everything:

while (is_running){
    // Game loop stuff

    SDL_RenderClear(renderer);
    // Draw stuff...
}
Eric Xue
  • 272
  • 2
  • 11
  • That's how double buffering works. Basically, since drawing directly on screen is slower will make the flow glitchy, 2 buffers are used: one is currently presented on screen, the other is not. When you use SDL2 (aswell as many other graphics tools), you draw on the buffer that's not getting presented, and then you call a function that swaps the buffers, at the end of the frame. – mikyll98 Aug 18 '22 at 11:44
  • SDL2 handles buffers automatically, but you have to use `SDL_RenderClear()` at the beginning of the frame to clear the buffer, draw your graphics with `SDL_RenderCopy()` and then present them on screen (i.e. swap the buffers) with `SDL_RenderPresent()`. – mikyll98 Aug 18 '22 at 11:45