Can't initialize SDL2, but SDL_GetError() return nothing

I am just trying to launch a simple SDL2 example. Compiling is ok, but in time of executing, in this code:

int main(){

if(SDL_Init(SDL_INIT_EVERYTHING) != 0){
    std::cout << "SDL init error: " << SDL_GetError() << std::endl;
    return false;
}

}

I get SDL init error: without any output from SDL_GetError(). Can you tell me, what is the problem?

IDE: Eclipse, OS: Ubuntu.

If I add some more code:

int main(){
if(SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << “SDL init error: " << SDL_GetError() << " |” << std::endl;
// return false;
}
SDL_Window* window = SDL_CreateWindow(“Hello World”, 200, 200, 460, 200, SDL_WINDOW_SHOWN);
if(window == nullptr){
std::cout << "SDL create window error: " << SDL_GetError() << std::endl;
// return false;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(renderer == nullptr){
std::cout << "SDL create renderer error: " << SDL_GetError() << std::endl;
// return false;
}

If I add more code to the project, I get the same error. That is, during initialization on the first check. Further execution occurs without errors, a window and a renderer are created. But the renderer cannot draw anything, the window is always black and no drawing commands are executed:

SDL_SetRenderDrawColor(renderer, 0xFF, 0xAA, 0xFF, 0xFF);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_Rect actor1 = {10, 10, 550, 50};
SDL_RenderFillRect(renderer, &actor1);

Call SDL_RenderPresent after rendering to update the screen. The wiki explains the function well.

1 Like

Have you tried making your main() look like: int main (int argc, char* argv[])?

From my experience a main method without the arguments always fails to compile while using SDL, so that may not be it, since yours compiles fine…

thanks for answers. Indeed, the rendering problem was resolved after calling the SDL_RenderPresent function. But the initialization problem remained. Also, I found that the problem only appears when the audio system is initialized. If there is no SDL_INIT_AUDIO flag, initialization proceeds without errors.

I’m not sure what problem you’re facing but SDL_SetError is probably not called if there’s no error message. Maybe try configuring pulseaudio or alsa. Or use different audio drivers with setting the SDL_AUDIODRIVER environment variable.

Assuming other programs work fine with audio it might be a SDL problem but since I can’t really reproduce it I’m assuming it has to do something with your configuration (hardware and software).

Thanks for the answer.
Despite the fact that pulseaudio should be installed in Ubuntu (and everything is fine with sound in my system), for some reason this driver is not in the SDL_GetNumAudioDrivers list. The following code:

std::cout << SDL_GetNumAudioDrivers() << std::endl;

for (int i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
const char* driver_name = SDL_GetAudioDriver(i);
if (SDL_AudioInit(driver_name)) {
printf(“Audio driver failed to initialize: %s\n”, driver_name);
continue;
}else{
printf(“Audio driver initialize success: %s\n”, driver_name);
//break;
}
SDL_AudioQuit();
}

gives the result: >

3
Audio driver failed to initialize: dsp
Audio driver initialize success: disk
Audio driver initialize success: dummy

My OS:
|Distributor ID:|Ubuntu|
|Description:|Ubuntu 21.04|
|Release:|21.04|
|Codename:|hirsute|

I couldn’t get any sounds from disk and dummy drivers.