SDL2 compiles but doen't open a window when run

I try to setup a SDL2 project on Eclipse (on mac) for a class project.

I followed this video tutorial “https://www.youtube.com/watch?v=EQyAjNMjMkM”.

I tried the following code and I have no errors but the window doesn’t open, there is just the icon of a “ghost” program that opens (as in the picture below). I searched for a solution on the internet but impossible to find something about it …

Someone would have already encountered this problem ?

[Testing code]

#include <stdio.h>
#include <SDL2/SDL.h>

int main(int argc, char** argv)
{
    if (SDL_Init(SDL_INIT_VIDEO) != 0 )
    {
        fprintf(stdout,"Failed to initialize the SDL (%s)\n",SDL_GetError());
        return -1;
    }

    {
        SDL_Window* pWindow = NULL;
        pWindow = SDL_CreateWindow("My first SDL2 application",SDL_WINDOWPOS_UNDEFINED,
                                                                  SDL_WINDOWPOS_UNDEFINED,
                                                                  640,
                                                                  480,
                                                                  SDL_WINDOW_SHOWN);

        if( pWindow )
        {
            SDL_Delay(3000);

            SDL_DestroyWindow(pWindow);
        }
        else
        {
            fprintf(stderr,"Error creating the window: %s\n",SDL_GetError());
        }
    }

    SDL_Quit();

    return 0;
}

[The “ghost” program]

On MacOS, you have to pump the message/event queue before the window will be shown. In an actual application this isn’t a problem, since you’re going to have to do it to get input etc., but in a toy application where you’re just trying to show a window, you’ll need to add something like

if(pWindow) {
    // Go through the pending event queue once
    SDL_Event event;
    while(SDL_PollEvent(&event) {
        // do nothing
    }

    SDL_Delay(3000);

    ...

1 Like

Thank you very much for the explanation! Now it works perfectly :pray: