4

I'm trying to create a window with SDL2. I don't get any error while compiling, but I also don't get any window at runtime.

Here's my code (without error checking for readability).

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

int main(void)
{
    SDL_Window      *win = NULL;
    SDL_Renderer    *ren = NULL;

    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_CreateWindowAndRenderer(320, 640, 0, &win, &ren);

    SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
    SDL_RenderClear(ren);
    SDL_RenderPresent(ren);

    SDL_Delay(3000);

    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();

    return (0);
}

I also tried this code sample from the wiki, same issue.

Compilation

clang -F /Library/Frameworks -framework SDL2 main.c

I'm working on MacOS Big Sur. I installed the SDL2 from the .dmg file downloaded on their website (runtime binaries / Mac OS X), and placed into the /Library/Frameworks folder

Is that a compatibility issue? How can I fix it?

EDIT

I also tried to install the SDL following this tutorial, still same issue.

lfalkau
  • 896
  • 7
  • 21
  • Your code works for me - a black window for 3 seconds spawned. – KamilCuk Dec 05 '20 at 17:44
  • That's why I think there's a compatibility issue with this version of the SDL2 and macOS BigSur, I don't get any window, I tried with several different codes... – lfalkau Dec 05 '20 at 17:48
  • 4
    Add `SDL_PollEvent` and an event loop. You'll need it anyway to process input, and I heard on some platforms windows don't show up otherwise. – HolyBlackCat Dec 05 '20 at 20:44

1 Answers1

4

You need to add an event loop to get the window -

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

int main(void)
{
    SDL_Window      *win = NULL;
    SDL_Renderer    *ren = NULL;

    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_CreateWindowAndRenderer(320, 640, 0, &win, &ren);

    SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
    SDL_RenderClear(ren);
    SDL_RenderPresent(ren);
    bool quit = false;

            //Event handler
            SDL_Event e;

            //While application is running
            while( !quit )
            {
                //Handle events on queue
                while( SDL_PollEvent( &e ) != 0 ) // poll for event
                {
                    //User requests quit
                    if( e.type == SDL_QUIT ) // unless player manually quits
                    {
                        quit = true;
                    }
                }
            }

    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();

    return (0);
}
Shilpi Roy
  • 56
  • 4
  • Are you sure? I'll definitely try it this afternoon, but isnt it supposed to print the window even without event loop? The documentation says it has to work without. – lfalkau Dec 11 '20 at 14:40
  • Yes, I am sure. We need to have a core game loop otherwise the window gets destroyed after the 3s delay. – Shilpi Roy Dec 12 '20 at 10:31
  • But that's what I actually want, I don't want the window to stay open after those 3 secs, I just want it to appear at least once – lfalkau Dec 12 '20 at 11:01
  • Anyway I tried and you're right, I don't understand why but it works – lfalkau Dec 12 '20 at 11:03
  • 1
    It's a platform specific issue I guess. Your code works on windows, on mac os you need to have a game loop. If you want it to close after 3 seconds delay, you can add a break after SDL_Delay(3000) in outer while loop. – Shilpi Roy Dec 12 '20 at 16:18
  • 1
    you can refer to https://stackoverflow.com/questions/34424816/sdl-window-does-not-show for more info – Shilpi Roy Dec 13 '20 at 10:50