SDL window does not show SDL window does not show xcode xcode

SDL window does not show


You have to give the system a chance to have it's event loop run.

easiest is to poll for events yourself:

SDL_Event e;bool quit = false;while (!quit){    while (SDL_PollEvent(&e)){        if (e.type == SDL_QUIT){            quit = true;        }        if (e.type == SDL_KEYDOWN){            quit = true;        }        if (e.type == SDL_MOUSEBUTTONDOWN){            quit = true;        }    }}

instead of the wait loop

--- Addendum

Since this answer is still helping people maybe it's nice if I also add a bit more info on why this works instead of just posting the solution.

When on the Mac (same for Windows actually) a program starts, it starts with just the 'main thread'. This is the thread which is used to set up UI stuff. The 'main thead' differs from other threads in that it comes with an event handling system. This system catches events like mouse moves, key presses, button clicks and then queues these and lets your code respond to it. All the UI things on Mac (and Windows) rely on this event pump being there and running. This is the reason why if you do anything UI related in your code you need to make sure you are not on a different thread.

Now, in your code you initialise the window and the UI, but then you do a SDL_Delay. This just blocks the thread and halts it for 20 seconds so nothing is done. And since you do that on the main thread, even the handling of the queue with the events is blocked. So on the Mac that shows as the spinning macwheel.

So the solution I posted actually keeps on polling for events and handles them. This way you are effectively also 'idling', but the moment events are posted (like mouse clicks and keys) the thread will wake up again and stuff will be processed.


You have to load a bitmap image, or display something on the window, for Xcode to start displaying the window.

#include <SDL2/SDL.h>#include <iostream>using namespace std;int main() {    SDL_Window * window = nullptr;    SDL_Surface * window_surface = nullptr;    SDL_Surface * image_surface = nullptr;    SDL_Init(SDL_INIT_VIDEO);    window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);    window_surface = SDL_GetWindowSurface(window);    image_surface = SDL_LoadBMP("image.bmp");    SDL_BlitSurface(image_surface, NULL, window_surface, NULL);    SDL_UpdateWindowSurface(window);    SDL_Delay(5000);    SDL_DestroyWindow(window);    SDL_FreeSurface(image_surface);    SDL_Quit();}


You need to initialize SDL with SDL_Init(SDL_INIT_VIDEO) before creating the window.