Window doesn't display

I’m trying to get SDL2 going on OS X (Mojave) without Xcode. I’ve installed the framework and I’m following along the Lazy Foo tutorial (as I’m sure a million others have done) and I can successfully compile 01_hello_SDL but when I run it, there’s no window. It acts as though OS X is preventing it from displaying, even though the program is running. I’ve tried a few other Lazy Foo examples and nothing displays.

I’ve searched various forums, blog posts, SO, etc. and I haven’t found anything that describes this problem. Surely I’m doing something stupid (or more like Mac OS is being stupid).

As a sanity check, I tried a
if (SDL_Init(SDL_INIT_VIDEO) < 0) { cout << "SDL init failed." << endl; }
and that passes.

Any thoughts?

You’ve successfully initialized SDL, but after that you also have to create an SDL window and an SDL renderer. If you follow the very first tutorial on the Lazyfoo site, you’ll learn how to create a window and a renderer.

I believe a ‘feature’ of SDL2 running on MacOS is that you must pump the event queue before the window will display. Try adding:

SDL_PumpEvents();

As you work through the LazyFoo tutorials you will soon find that you need to do that anyway to retrieve the SDL_QUIT event.

1 Like

Thanks for the reply @Daniel1985. If you read my post, you’ll see that I AM working through the first tutorial and even tried subsequent tutorials, none of which worked.

Well, I read your post and checked the pasted code, which missed window/renderer creation code, so I made the guess/assumption that you might had missed that part of the tutorial. Hopefully you’ll get it solved.

@Daniel1985 I’ve tried several of the Lazy Foo tutorials, and while they compile and run, they don’t display a window. It’s very odd. The code I pasted in was just bare-bones to make sure it was initializing properly. If you have any suggestions for debugging this, I’m all ears.

Have you tried what rtrussel suggested?

@rtrussell You’re a genius, thanks for the suggestion. It seems SDL_Delay() is called before the window has a chance to draw. Putting SDL_PumpEvents() in a short loop before SDL_Delay() did the trick. Very strange. Thanks again for your help!

@Daniel1985 I did and it’s working now after a bit of fiddling. Thanks.

MacOS seems to put some additional window events on the queue during window creation before the window will be shown, probably to keep from showing a half-formed window, so yeah, you need to pump the event queue.

Doesn’t matter for a real app, since you’ll need to be processing events anyway, so your window will just show up one frame later.