Search code examples
c++windowsdl

SDL Window does not show even with Event Loop


I know this question has been asked before but most of the time answer was just to add delay or an event loop. However I have added an event loop and the window ist not showing. Only the console. I am running this program in Visual Studio 2019.

#include <iostream>
#include "GL/glew.h"

#define SDL_MAIN_HANDLED

#include "SDL.h"

int main() {

    SDL_Window* window;
    SDL_Init(SDL_INIT_EVERYTHING);

    //fenster erstellen
    window = SDL_CreateWindow("C++ OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_OPENGL);
    //opengl context setzen
    SDL_GLContext glContext = SDL_GL_CreateContext(window);

    bool close = false;
    while (!close) {

        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                close = true;
            }
        }

        if (close) {
            break;
        }
    }

    return 0;
}


Solution

  • You need to include SDL_MainReady as you are not using SDL_main.

    See here

    So you code would be adjusted like

    int main() {
        SDL_Window* window;
    
        SDL_SetMainReady();
        SDL_Init(SDL_INIT_EVERYTHING);
    
        ...
    
        return 0;
    }