How to create a window with SDL2 on MacOS

Viewed 934

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.

1 Answers

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);
}
Related