SDL2 sample application hangs on Mac OS Catalina

Hello all,

I think I’ve got a bug report for SDL2 on Mac OS Catalina. Please tell me the right place to post, if it’s not here.

I’ve compiled this sample application (GitHub - JodyAndrews/SDL2_Basic_Setup: A basic setup of SDL2 ( in my case for OSX Mojave and Catalina ) that initialises and demos SDL2, SDL2 Mixer, SDL2 Image, and SDL2 TTF) many times, and although it’s successfully compiled, when I execute it never displays the created window, and hangs on terminal. But, if I hit Ctrl-C on the hanging terminal, it shows correctly for a few seconds and closes itself.

$ ~/Downloads/Pessoal/etudes/etudes_c/SDL2_Basic_Setup/out/bin $ ./SDL2_Basic_Setup
SDL INITIALISED
Display mode is 1440x900px @ 60hz
^C
Created Window and Renderer 640x480

I’ve first installed all necessary libs from brew. With no luck, I’ve deleted every sdl2 lib from brew and downloaded the sources and compiled it… to no avail. Same output: Window freezes, doesn’t show up, and if I hit Ctrl-C on the terminal window, it shows and closes itself.

Please advise. Thanks for reading!

Nilo

Change this line in main.c …

  if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {

…to this…

  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {

…and see if it starts working.

~/Downloads/Pessoal/etudes/etudes_c/SDL2_Basic_Setup/src $ git diff
diff --git a/src/main.c b/src/main.c
index 54bdff1..ff28201 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,4 +1,5 @@
 #include <printf.h>
+#include <stdio.h>
 #include <SDL.h>
 #include <SDL_ttf.h>
 #include <SDL_image.h>
@@ -39,7 +40,7 @@ Mix_Music *_music = NULL;
  */
 void init_sdl()
 {
-  if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
+  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {

It didn’t work.

It hangs in this line:

if (SDL_CreateWindowAndRenderer(_width, _height, SDL_WINDOW_SHOWN, &_window, &_renderer) != 0) {

According to the docs SDL_WINDOW_SHOWN is ignored. It shouldn’t make any difference, but you could perhaps try removing that flag.

Thanks.

I am tracing inside the SDL function source.

The problem is on this inner function:

*renderer = SDL_CreateRenderer(*window, -1, 0);

The problem is the OpenGL driver.

It hangs here:

                renderer = driver->CreateRenderer(window, flags);

Try passing sdl a hint to use metal, as opengl is no longer supported by macOS

OpenGL should work still, though, regardless of how Apple feels about it.

However, SDL shouldn’t be using OpenGL by default on macOS, so a good question is why it didn’t choose Metal in the first place.

Is there anything strange about your setup (is it a Hackintosh, is it using an eGPU, etc)?

Sorry I am away from the notebook.

It’s a macbook pro 2012, running most recent xquartz.

If I change it to favor metal rendering it hangs in the next branch of forementioned function.

even glxinfo hangs in this setup, showing only the initial $DISPLAY value.

I ran the sample with sudo just in case, same issue.

This whole trouble is because kivy uses sdl2, this was my initial try, running a sample python kivy app on python 3.9.5.

I can’t seem to make kivy use other configuration for KIVY_WINDOW, all fail in window creation, only sdl2 passes then hangs. A ctrl-c displays the sdl window and closes it in a second.

To be clear: you’re trying to do graphics over X11 from macOS?

Does SDL work when using macOS’s normal UI interfaces (the “cocoa” driver, not the “x11” driver)?

Yes I’m trying to use the X11 OpenGL driver. How would I use cocoa ?

These are the renderers that are available on my sdl (ifdefs tested true):

metal
opengl
opengles2
software

Why are you trying to use X11? It isn’t needed for SDL to work on macOS.

Also, he means the video driver, not the renderer backend. If you add
printf("Video driver: %s\n", SDL_GetCurrentVideoDriver());
before the window is created, what does it print?

As has been pointed out, OpenGL is supported by MacOS (even on the new ‘Apple Silicon’ M1) which is fortunate for me because my app depends on it. It’s officially deprecated but I think Apple would be crazy to remove support for the one ‘universal’ backend that’s available on virtually every platform.

It seems opengl isn’t on brew, so the only place I found reference to it was from X11.

Video driver: cocoa

https://giovanni.codes/opengl-setup-in-macos/

This was the only reference I found to setup OpenGL without X11 on Mac OS, and it has different headers.

then don’t install SDL from brew? SDL provides a precompiled framework for Mac that works just fine with OpenGL

maybe you have a SDL_VIDEODRIVER=x11 environment variable someplace?

This was the only reference I found to setup OpenGL without X11 on Mac OS, and it has different headers.

there isn’t really a trick to getting OpenGL on Mac except perhaps the headers are under an OpenGL directory (eg #include <OpenGL/gl.h>), but you will be limited to a 4.1 or lower context. Just add the OpenGL framework (ships with macOS) to your project and link it. I don’t use anything like glfw tho.

1 Like

OpenGL isn’t something you need to install; on macOS it’s provided by the operating system. If you aren’t doing anything with OpenGL directly then you don’t need to even worry about it. And if you do want to do stuff with OpenGL, use SDL to set up the OpenGL context instead of doing it yourself.

And since it’s using the Cocoa video driver, I’m guessing there’s some other problem.

What happens if you create the window and SDL_Renderer separately? As in, like

SDL_Window *window = SDL_CreateWindow(blah blah);
if(window == NULL) {
    fprintf(stderr, "ERROR: couldn't create window: %s\n", SDL_GetError());
    exit(EXIT_FAILURE);
}
uint32_t renderFlags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, renderFlags);
if(renderer == NULL) {
    fprintf(stderr, "ERROR: couldn't create SDL Renderer: %s\n", SDL_GetError());
    exit(EXIT_FAILURE);
}