13

When I try to execute this SDL3 program:

// g++ -Wall -Ofast main.cpp -Iinclude -Iinclude/SDL3 -Linclude/SDL3 -lSDL3 -o build/program
#define SDL_MAIN_HANDLED
#include <iostream>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL.h>

int main(int argc, char* argv[]) {
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        SDL_Log("SDL_Init failed: %s", SDL_GetError());
        return -1;
    }

    return 0;
}

... I get the following error:

SDL_Init failed:

(Note: SDL_GetError() doesn't return anything).

I'm using Windows 10 Pro.

4
  • 5
    Under SDL2 SDL_Init returns != 0 as error, since SDL3 returns true on success. You want if(!SDL_Init(SDL_INIT_VIDEOS)) Commented May 24 at 10:39
  • 1
    It is preferable to return SDL_APP_CONTINUE instead of 0 in case the actual order of the SDL_AppResult enums change. Commented May 24 at 10:49
  • @cup please add an Answer with a link to the official docs or news/discussion for that! Commented May 24 at 11:13
  • 2
    @cup That only applies to using the new (optional) main callbacks feature; defining main manually works as it used to (note how it returns int and not SDL_AppResult). On a related note, avoid defining SDL_MAIN_HANDLED unless you actually know what that entails! Commented May 25 at 1:21

1 Answer 1

25

There was a change in the API between SDL2 and SDL3.

In SDL2, SDL_Init returns an int which is:

0 on success or a negative error code on failure;

But in SDL3, SDL_Init returns a bool which is:

true on success or false on failure;

I.e. 0 means success in SDL2 but failure in SDL3.

Since you are using SDL3 your call to SDL_Init (which returned a value != 0), actually succeeded.

You should change the calling code to:

//--v---------------------------
if (!SDL_Init(SDL_INIT_VIDEO)) {
     SDL_Log("SDL_Init failed: %s", SDL_GetError());
     return -1;
}

(or compare explicitly against false if you prefer that style).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.