c++macossdlsdl-image

SDL2 Transparency is super glitchy


(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, 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?


Solution

  • 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...
    }