SDL_Init

Name

SDL_Init -- Initializes SDL

Synopsis

#include "SDL.h"

int SDL_Init (Uint32 flags);

Description

This function loads the SDL dynamically linked library and initializes the subsystems specified by 'flags' (and those satisfying dependencies). Unless the SDL_INIT_NOPARACHUTE flag is set, it will install cleanup signal handlers for some commonly ignored fatal signals (like SIGSEGV).

These are the flags which may be passed to SDL_Init() -- you should specify the subsystems which you will be using in your application:

SDL_INIT_TIMER

Initialize timer subsystem

SDL_INIT_AUDIO

Initialize audio subsystem

SDL_INIT_VIDEO

Initialize video subsystem

SDL_INIT_CDROM

Initialize CD-ROM subsystem

SDL_INIT_JOYSTICK

Initialize joystick subsystem

SDL_INIT_HAPTIC

Initialize haptic (force feedback) subsystem

SDL_INIT_EVERYTHING

Initialize all of the above subsystems

SDL_INIT_NOPARACHUTE

Don't catch fatal signals

SDL_INIT_EVENTTHREAD

Run the event loop in a separate thread (not supported by all OSs)

SDL_Init initializes the SDL library. This must be called before using any other SDL function.

You can call SDL_Init(0) or SDL_Init(SDL_INIT_NOPARACHUTE) to initialize SDL without initializing any subsystems.

Note that the event system is actually started by the video subsystem, so using SDL_INIT_EVENTTHREAD without SDL_INIT_VIDEO is pointless.

Return Value

Returns 0 if successful, -1 otherwise.

If this function fails, you can usually get an error message through SDL_GetError(), but not always. Specifically, if pth_init() fails, no error message is set.

Example

   1 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
   2     fprintf(stderr, "\nUnable to initialize SDL:  %s\n", SDL_GetError());
   3     return 1;
   4 }

   1 #include <exception>
   2 #include <string>
   3 #include <SDL.h>
   4 
   5 class InitError: public std::exception {
   6     public:
   7         InitError();
   8         InitError(const std::string&);
   9         virtual ~InitError() throw();
  10         virtual const char* what() const throw();
  11     private:
  12         std::string msg;
  13 };
  14 
  15 InitError::InitError():
  16   exception(), msg(SDL_GetError()) {}
  17 InitError::InitError(const std::string& m):
  18   exception(), msg(m) {}
  19 InitError::~InitError() throw() {}
  20 const char* InitError::what() const throw() {
  21     return msg.c_str();
  22 }
  23 
  24 class SDL {
  25     public:
  26         SDL(Uint32 flags = 0) throw(InitError);
  27         virtual ~SDL();
  28 };
  29 
  30 SDL::SDL(Uint32 flags) throw(InitError) {
  31     if (SDL_Init(flags) != 0)
  32         throw InitError();
  33 }
  34 
  35 SDL::~SDL() {
  36     SDL_Quit();
  37 }
  38 
  39 /* ... */
  40 
  41 #include <iostream>
  42 
  43 int main(int argc, char **argv) {
  44     try {
  45         SDL sdl(SDL_INIT_VIDEO|SDL_INIT_TIMER);
  46 
  47         /* ... */
  48 
  49         return 0;
  50     }
  51 
  52     catch (const InitError& err) {
  53         std::cerr << "Error while initializing SDL:  " << err.what() << std::endl;
  54     }
  55     
  56     return 1;
  57 }

See Also

SDL_InitSubSystem, SDL_WasInit, SDL_Quit


CategorySDL13API CategorySDL13APIGeneral

SDL-1.3/SDL_Init (last edited 2009-02-13 06:54:06 by KenBull)