SDL_Init
Use this function to initialize the SDL library. This must be called before using any other SDL function.
Syntax
int SDL_Init(Uint32 flags)
Function Parameters
flags |
subsystem initialization flags; see Remarks for details |
Return Value
Returns 0 on success or a negative error code on failure; call SDL_GetError() for more information.
Code Examples
C
#include "SDL.h"
int main(int, char**) {
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
fprintf(stderr,
"\nUnable to initialize SDL: %s\n",
SDL_GetError()
);
return 1;
}
atexit(SDL_Quit);
/* ... */
return 0;
}
C++
#include <exception>
#include <string>
#include "SDL.h"
class InitError: public std::exception {
public:
InitError();
InitError(const std::string&);
virtual ~InitError() throw();
virtual const char* what() const throw();
private:
std::string msg;
};
InitError::InitError():
exception(), msg(SDL_GetError()) {}
InitError::InitError(const std::string& m):
exception(), msg(m) {}
InitError::~InitError() throw() {}
const char* InitError::what() const throw() {
return msg.c_str();
}
class SDL {
public:
SDL(Uint32 flags = 0) throw(InitError);
virtual ~SDL();
};
SDL::SDL(Uint32 flags) throw(InitError) {
if (SDL_Init(flags) != 0)
throw InitError();
}
SDL::~SDL() {
SDL_Quit();
}
/* ... */
#include <iostream>
int main(int argc, char **argv) {
try {
SDL sdl(SDL_INIT_VIDEO|SDL_INIT_TIMER);
/* ... */
return 0;
}
catch (const InitError& err) {
std::cerr
<< "Error while initializing SDL: "
<< err.what() << std::endl;
}
return 1;
}
Remarks
The Event Handling, File I/O, and Threading subsystems are initialized by default. You must specifically initialize other subsystems if you use them in your application.
flags may be any of the following OR'd together:
SDL_INIT_TIMER |
timer subsystem |
SDL_INIT_AUDIO |
audio subsystem |
SDL_INIT_VIDEO |
video subsystem |
SDL_INIT_JOYSTICK |
joystick subsystem |
SDL_INIT_HAPTIC |
haptic (force feedback) subsystem |
SDL_INIT_EVERYTHING |
all of the above subsystems |
SDL_INIT_NOPARACHUTE |
don't catch fatal signals |
Unless the SDL_INIT_NOPARACHUTE flag is set, it will install cleanup signal handlers for some commonly ignored fatal signals (like SIGSEGV).
If you want to initialize subsystems separately you would call SDL_Init(0) followed by SDL_InitSubSystem() with the desired subsystem flag.