SDL2 does not render without a delay

Viewed 2257

This simple SDL2 program opens an empty window on my system (ie. it renders nothing) unless I uncomment the SDL_Delay line.

#include <SDL.h>

int main(int argc, char** argv) {

    constexpr unsigned width = 256, height = 256;

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        SDL_Log("Failed to initialize SDL: %s", SDL_GetError());
        return 1;
    }

    SDL_Window* window = SDL_CreateWindow("Test",
                                          SDL_WINDOWPOS_UNDEFINED,
                                          SDL_WINDOWPOS_UNDEFINED,
                                          width, height,
                                          0);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

    SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
    SDL_RenderClear(renderer);
    // SDL_Delay(20);
    SDL_RenderPresent(renderer);

    bool quit{};
    SDL_Event event;
    while (!quit) {
        SDL_WaitEvent(&event);

        switch (event.type) {
        case SDL_QUIT:
            quit = true;
            break;
        }
    }

    SDL_Quit();

    return 0;
}

The delay has to be about 10-20ms or nothing shows up, but it's not consistent - if it's set to 10ms, it works about half of the time.

After reading this answer I added the event loop, but I get the same behavior without it. Users on #sdl on IRC have gotten the expected blue screen from the same code.

Any idea what's going on here/what I've done wrong?

1 Answers
Related