SDL_Init(SDL_INIT_VIDEO) not intializing with SDL_GetError() returns nothing | UBUNTU 24.04.1

I have been using SDL3 for creating basic 2D games before. I have formatted and installed a fresh Ubuntu 24.04.1 today and SDL3 seems to have issues initializing SDL_INIT_VIDEO.


I get the error

Unable to initialize SDL:
SDL_GetError() returns nothing. I am in Ubuntu24.04.1 and using Nvidia 560.35.03 driver.
Any help is greatly appreciated. Thanks in advance.

They recently changed a lot of functions to return bool instead of int. SDL_Init now returns true on success and false on failure.

2 Likes

Awesome. It works. Thanks a ton. I forgot to check the API for SDL_Init, because I have been writing in SDL3 for few months now, I was so confident, I read the SDL_Init thoroughly. Thanks again for taking your time.

This is the danger of using unstable libraries. Once SDL 3.2 is released you should no longer see breaking changes of this magnitude.

To be honest this change is a huge PITA when porting existing SDL2 code - which I just did with dhewm3.

It compiles just fine, but silently breaks… and some of those functions can cause more subtle breakage than “game aborts at start because SDL_Init() appeared to fail”.

It would be nice to have one list with all functions where the return value changed from int to bool, or similar changes to the function signature that are not caught by the compiler (like switched arguments that still have a compatible type) - not sure if such changes exist though, I think so far I’ve only run into the “returns true instead of 0 on success now” issue.

I know that SDL/docs/README-migration.md at main · libsdl-org/SDL · GitHub lists such changes, but that information is spread all over the document (and I don’t know if any functions are missing).

Would be nice to have a simple list that one can use with a script that greps for those functions in your code so one knows where to check. And it should contain both the old and the new names - remember that many codebases will continue to support SDL2 for some time, so people will use SDL_oldnames.h (or similar custom defines).

Would be even nicer if SDL provided not just a list but such a script :wink:

1 Like

Perhaps something like a coccinelle script? :smiley:

1 Like

I haven’t used that coccinelle script because as far as I understood it would rename all the functions etc - I ported manually so I could ensure backwards compatibility.

But yes, the part of the script you posted might be useful to identify the function calls with this particular problem, thanks! :slight_smile:
I guess I’ll toy around with it tomorrow, I’ve never used coccinelle.

oh by the way, that coccinelle script still uses SDL_FALSE and SDL_TRUE - I guess that should be plain false and true now?

They don’t exist in plain C do they, unless #defined?

In C99, true and false are defined as macros inside <stdbool.h>. This is what SDL3 uses now, if available, otherwise it defines them. SDL_bool doesn’t exist anymore.

C23 turns true and false into real keywords.

I tried to turn the coccinelle script into something minimal that only adds a comment to the affected function calls, but it didn’t seem to work too well, missed some cases (maybe it isn’t very good with code hidden behind #if/#else?).

I created a list of all affected functions (based on the list in the coccinelle script, but also added the corresponding SDL2 function names based on SDL_oldnames.h) which can be used with grep to find all potentially affected cases in your code. You’ll have to check each returned line manually, but at least then you know where to look (and can already ignore all the function calls where you don’t check the return value at all ;)).

The file: sdl_boolretfns.txt (7.2 KB)
Use it like:

$ cd /path/to/your/source
$ grep -rn -f /path/to/sdl_boolretfns.txt

You could additionally use greps --exclude, --exclude-dir and/or --include options in case you get too many results from files you don’t care about, see man grep.
One weird thing about --exclude-dir is that it can only be the name of one directory, e.g. --exclude-dir=libs, while --exclude-dir=libs/imgui does not work.