Wiki Page Content

Differences between revisions 11 and 52 (spanning 41 versions)
Revision 11 as of 2009-11-11 20:18:19
Size: 3517
Editor: SheenaSmith
Comment: content added
Revision 52 as of 2017-04-08 21:22:04
Size: 2484
Comment: Fixed description, see Remarks.
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
#pragma disable-camelcase #pragma camelcase off
Line 5: Line 5:
Initializes the SDL library. This must be called before using any other SDL function. Use this function to initialize the SDL library. This must be called before using most other SDL functions.
Line 11: Line 11:
int SDL_Init (Uint32 flags) int SDL_Init(Uint32 flags)
Line 15: Line 15:
These are the flags which may be passed to SDL_Init() -- you should
specify the subsystems which you will be using in your application. They should be OR'd together.

||'''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)||
||'''flags'''||subsystem initialization flags; see [[#Remarks|Remarks]] for details||
Line 29: Line 18:
Returns 0 if successful, -1 otherwise.

If this function fails you can usually get an error message through [[SDL_GetError]], but not always.
Returns 0 on success or a negative error code on failure; call [[SDL_GetError]]() for more information.
Line 34: Line 21:
=== C ===
Line 38: Line 24:
int main(int, char**)  {
    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
        fprintf(stderr,
                
"\nUnable to initialize SDL:  %s\n",                  SDL_GetError()
               
);
int main(int argc, char* argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {
        SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
Line 46: Line 30:
    atexit(SDL_Quit);
  
Line 49: Line 32:
  
    SDL_Quit();
Line 54: Line 39:
=== C++ ===
{{{#!highlight cpp
#include <exception>
#include <string>
#include "SDL.h"
== Remarks ==
[[SDL_Init]]() simply forwards to calling [[SDL_InitSubSystem]](). Therefore, the two may be used interchangeably. Though for readability of your code [[SDL_InitSubSystem]]() might be preferred.
Line 60: Line 42:
class InitError: public std::exception {
    public:
        InitError();
        InitError(const std::string&);
        virtual ~InitError() throw();
        virtual const char* what() const throw();
    private:
        std::string msg;
};
The [[CategoryIO|file I/O]] and [[CategoryThread|threading]] subsystems are initialized by default. You must specifically initialize other subsystems if you use them in your application.
Line 70: Line 44:
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();
}
[[CategoryLog|Logging]] works without initialization, too.
Line 79: Line 46:
class SDL {
    public:
        SDL(Uint32 flags = 0) throw(InitError);
        virtual ~SDL();
};
'''flags''' may be any of the following OR'd together:
Line 85: Line 48:
SDL::SDL(Uint32 flags) throw(InitError) {
    if (SDL_Init(flags) != 0)
        throw InitError();
}
##The following content is included on `SDL_InitSubSystem`. Any changes between these comments will be reflected there as well. Please use caution when editing. Start Include here.
Line 90: Line 50:
SDL::~SDL() {
    SDL_Quit();
}
||'''SDL_INIT_TIMER'''||timer subsystem||
||'''SDL_INIT_AUDIO'''||audio subsystem||
||'''SDL_INIT_VIDEO'''||video subsystem; automatically initializes the events subsystem||
||'''SDL_INIT_JOYSTICK'''||joystick subsystem; automatically initializes the events subsystem||
||'''SDL_INIT_HAPTIC'''||haptic (force feedback) subsystem||
||'''SDL_INIT_GAMECONTROLLER'''||controller subsystem; automatically initializes the joystick subsystem||
||'''SDL_INIT_EVENTS'''||events subsystem||
||'''SDL_INIT_EVERYTHING'''||all of the above subsystems||
||'''SDL_INIT_NOPARACHUTE'''||compatibility; this flag is ignored||
Line 94: Line 60:
/* ... */ ##End Include here.
Line 96: Line 62:
#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.
If you want to initialize subsystems separately you would call [[SDL_Init]](0) followed by [[SDL_InitSubSystem]]() with the desired subsystem flag.
Line 128: Line 65:
 .[[SDL_InitSubSystem]]
 .[[SDL_WasInit]]   .[[SDL_Quit]]
 .[[SDL_InitSubSystem]]
.[[SDL_Quit]]
 .[[SDL_SetMainReady]]

 .[[SDL_WasInit]]
Line 133: Line 71:
 .[[CategoryAPI]]
 .
[[CategoryAPIInitialization]] 
[[CategoryAPI]], [[CategoryInit]]

SDL_Init

Use this function to initialize the SDL library. This must be called before using most other SDL functions.

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

#include "SDL.h"

int main(int argc, char* argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {
        SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
        return 1;
    }

    /* ... */

    SDL_Quit();

    return 0;
}

Remarks

SDL_Init() simply forwards to calling SDL_InitSubSystem(). Therefore, the two may be used interchangeably. Though for readability of your code SDL_InitSubSystem() might be preferred.

The file I/O and threading subsystems are initialized by default. You must specifically initialize other subsystems if you use them in your application.

Logging works without initialization, too.

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; automatically initializes the events subsystem

SDL_INIT_JOYSTICK

joystick subsystem; automatically initializes the events subsystem

SDL_INIT_HAPTIC

haptic (force feedback) subsystem

SDL_INIT_GAMECONTROLLER

controller subsystem; automatically initializes the joystick subsystem

SDL_INIT_EVENTS

events subsystem

SDL_INIT_EVERYTHING

all of the above subsystems

SDL_INIT_NOPARACHUTE

compatibility; this flag is ignored

If you want to initialize subsystems separately you would call SDL_Init(0) followed by SDL_InitSubSystem() with the desired subsystem flag.


CategoryAPI, CategoryInit

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