Wiki Page Content

Differences between revisions 1 and 10 (spanning 9 versions)
Revision 1 as of 2009-10-17 18:31:14
Size: 3485
Editor: KenBull
Comment: Documented SDL_Init
Revision 10 as of 2009-11-11 18:39:41
Size: 3484
Editor: SheenaSmith
Comment: minor corrections
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#pragma section-numbers 2 #pragma section-numbers off
Line 5: Line 5:
`SDL_Init` initializes the SDL library. This must be called before using any other SDL function. Initializes the SDL library. This must be called before using any other SDL function.
Line 7: Line 7:
<<TableOfContents(3)>> <<TableOfContents()>>
Line 11: Line 11:
#include "SDL.h"

int SDL_Init (Uint32 flags);
int SDL_Init (Uint32 flags)
Line 20: Line 18:
||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_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)||
Line 31: Line 29:
Returns `0` if successful, `-1` otherwise. Returns 0 if successful, -1 otherwise.
Line 33: Line 31:
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. If this function fails you can usually get an error message through [[SDL_GetError]], but not always.
Line 125: Line 123:
You can call `SDL_Init(0)` or `SDL_Init(SDL_INIT_NOPARACHUTE)` to initialize SDL without initializing any subsystems. You can call SDL_Init(0) or SDL_Init(SDL_INIT_NOPARACHUTE) to initialize SDL without initializing any subsystems.
Line 130: Line 128:
[[SDL_InitSubSystem]], [[SDL_WasInit]], [[SDL_Quit]]  .[[SDL_InitSubSystem]]
 .
[[SDL_WasInit]]
 .
[[SDL_Quit]] 
Line 133: Line 133:
[[CategoryAPI]]  .[[CategoryAPI]]
 .[[CategoryAPIInitialization]]

SDL_Init

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

Syntax

int SDL_Init (Uint32 flags)

Function Parameters

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)

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.

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

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).

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.


None: SDL_Init (last edited 2017-04-08 21:22:04 by PhilippWiesemann)

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit