[SDL3.1.1] Window briefly wrong

When I start this demo, the window briefly appears in black at the top left, then it centers itself. Then wait 2 seconds. Then it disappears again briefly and only then does it turn yellow.

Shouldn’t it appear centered from the start and then fade gently to yellow after 2 seconds?

Is this a bug or is it intentional?

Linux 64bit.

#include <SDL3/SDL.h>

int main()
{
        SDL_Init(SDL_INIT_VIDEO);

        SDL_Window * win = SDL_CreateWindow("Shapes", 800, 600, 0);
        SDL_Delay(2000);
        SDL_Renderer * screen = SDL_CreateRenderer(win, 0, SDL_RENDERER_PRESENTVSYNC);

        SDL_SetRenderDrawColor(screen, 255, 255, 25, SDL_ALPHA_OPAQUE);
        SDL_RenderClear(screen);
        SDL_RenderPresent(screen);

        SDL_Delay(5000);
        SDL_DestroyWindow(win);
        SDL_Quit();
}

That can be resolved by starting the window in a hidden state, it’s trying to show content before the window is fully initialized.
SDL_RenderClear/SDL_RenderPresent doesn’t work on a hidden window, but starting in a hidden state does at least stop the flickering window position.
The window then gets to start with that little “grow” animation on gnome in which it is given a couple of frames worth of time to draw the screen content so that it looks correct in time.


#include <SDL3/SDL.h>

int main()
{
        uint32_t initFlags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS;
        SDL_Init(SDL_INIT_VIDEO);

        SDL_Window * win = SDL_CreateWindow("Shapes", 800, 600, SDL_WINDOW_HIDDEN);
        SDL_Renderer * screen = SDL_CreateRenderer(win, 0, SDL_RENDERER_PRESENTVSYNC);
        SDL_ShowWindow(win);

        SDL_SetRenderDrawColor(screen, 255, 255, 25, SDL_ALPHA_OPAQUE);
        SDL_RenderClear(screen);
        SDL_RenderPresent(screen);

        SDL_Delay(3000);
        SDL_DestroyWindow(win);
        SDL_Quit();
}

Side note: I’m using ctrl-e to wrap code in tags, it puts 3 back-ticks (```) instead of just one, that’s my guess why my post has different syntax highlighting…?

I don’t think it should “fade gently to yellow”. When you use SDL_RenderPresent it should show what you have drawn immediately.

You can specify the language by putting it after the opening backticks.

default:

#include <SDL3/SDL.h>
int main() { SDL_Init(SDL_INIT_VIDEO);

``` c

#include <SDL3/SDL.h>
int main() { SDL_Init(SDL_INIT_VIDEO);

``` java

#include <SDL3/SDL.h>
int main() { SDL_Init(SDL_INIT_VIDEO);

``` text

#include <SDL3/SDL.h>
int main() { SDL_Init(SDL_INIT_VIDEO);

Oh, that is a cool feature.
Thank you!

I changed it a little with the SDL_WINDOW_HIDDEN. I also removed the first delay.
Now the window still flashes black briefly before turning yellow. But when recoloring the window changes position.

I still remember when I was tinkering with native X11. The window manager positions the window in the position that suits it.

#include <SDL3/SDL.h>

int main()
{
        SDL_Init(SDL_INIT_VIDEO);

        SDL_Window * win = SDL_CreateWindow("Shapes", 800, 600, SDL_WINDOW_HIDDEN);
        //SDL_Delay(2000);
        SDL_ShowWindow(win);
        SDL_Renderer * screen = SDL_CreateRenderer(win, 0, SDL_RENDERER_PRESENTVSYNC);

        SDL_SetRenderDrawColor(screen, 255, 255, 25, SDL_ALPHA_OPAQUE);
        SDL_RenderClear(screen);
        SDL_RenderPresent(screen);

        SDL_Delay(5000);
        SDL_DestroyWindow(win);
        SDL_Quit();
}

I’m using Linux Mint with Cinnamon

I was trying to say something that Peter stated much more clearly then edited out from his own post; the renderer should be created before the window is shown. Creating the renderer is part of the initialization that I was trying to refer to.

Please see my post above, where I edited in an example.

I was trying to explain my thoughts on the order of functions that I think you would probably want to follow (like don’t render to the screen before it is shown because it won’t draw on a hidden window).
I kind of fumbled that explanation, sorry.

Try updating SDL to the latest code and using this:

#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>

int main(int argc, char *argv[])
{
        SDL_Init(SDL_INIT_VIDEO);

        SDL_Window * win;
        SDL_Renderer * screen;
        SDL_CreateWindowAndRenderer("Shapes", 800, 600, 0, &win, &screen);

        SDL_SetRenderDrawColor(screen, 255, 255, 25, SDL_ALPHA_OPAQUE);
        SDL_RenderClear(screen);
        SDL_RenderPresent(screen);

        SDL_Delay(5000);
        SDL_DestroyWindow(win);
        SDL_DestroyRenderer(screen);
        SDL_Quit();
        return 0;
}

There was an error in your code. I corrected it, but the error with the window movement is still there.

// SDL_CreateWindowAndRenderer("Shapes", 800, 600, 0, &win, &screen);
SDL_CreateWindowAndRenderer(800, 600, 0, &win, &screen); // new

In the Wiki it is also wrong in the description above. But I have just corrected this. The demo below is ok, except for the window pos shifting problem.

The demo is not ok because the main loop does not process all pending events in a single iteration, but only pops one event from the queue. There should be two while loops — the main one and the nested one, emptying the entire event queue before rendering the frame.

Do you mean something like this?
But nothing changes.

#include <SDL3/SDL.h>

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window * win;
    win = SDL_CreateWindow("Hello World", 640, 480, SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN) ;

    SDL_Renderer  * renderer;
    renderer = SDL_CreateRenderer(win, NULL, SDL_RENDERER_PRESENTVSYNC);

    SDL_bool quit = SDL_FALSE;

    SDL_ShowWindow(win);
    while (!quit)
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_EVENT_QUIT:
                    quit = SDL_TRUE;
                    break;
            }
        }
        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(win);
    SDL_Quit();
    return 0;
}

Yes — this is a correct way to process SDL events queue in the game loop. Maybe it does not fix your problem but the SDL wiki should contain correct examples. If someone C-oriented have a minute, please update the SDL wiki.