SDL_Init() vs SDL_InitSubSystem()

Someone on IRC is asking when it’s appropriate to use SDL_InitSubSystem().
I actually don’t know. In the past, I’ve been known to do the following,
and it works:

if (SDL_Init(SDL_VIDEO) < 0)
… abort

if (sound_enabled)
if (SDL_Init(SDL_AUDIO) < 0)
{
fprintf(stderr, “What? I can’t hear you!\n!”);
sound_enabled = FALSE;
}
else
… load sounds

And in creating libcrtxy, I’m having it initialize SDL_VIDEO and create
a display surface. The programmer using libcrtxy should then use
SDL directly for everything else (joystick, sound, events).

In that case, should THEY be using SDL_InitSubSystem() or is SDL_Init() safe?

And when/if things fail inside libcrtxy (i.e., can’t open video display),
should I just have it do an SDL_Quit(), or is it more friendly for me to
have it do an SDL_QuitSubSystem(SDL_VIDEO) (the only thing it init’s)…?

The man pages don’t really help. I’ve done the multiple-SDL_Init() method
for years, with no problem, but I always do them consecutively
(no other SDL function calls between them).

Thx!–
-bill!
“Tux Paint” - free children’s drawing software for Windows / Mac OS X / Linux!
Download it today! http://www.tuxpaint.org/

Hello !

Someone on IRC is asking when it’s appropriate to use SDL_InitSubSystem().
I actually don’t know. In the past, I’ve been known to do the following,
and it works:

if (SDL_Init(SDL_VIDEO) < 0)
… abort

if (sound_enabled)
if (SDL_Init(SDL_AUDIO) < 0)
{
fprintf(stderr, “What? I can’t hear you!\n!”);
sound_enabled = FALSE;
}
else
… load sounds

As to my knowledge in the second case you have to
use SDL_InitSubSystem. When SDL_Init (SDL_INIT_VIDEO);
SDL_Init (SDL_INIT_AUDIO); works for you then this is more
luck then wanted behaviour.

CU

Ok, good to know.

For libcrtxy, should I simply require that NOTHING about SDL get initialized
before my XY_init() is called? (That’s where I call SDL_Init()).

Someone has to call SDL_Init(), and I think it’s fine for me to do so,
and only ask for SDL_VIDEO. Then the programmers using libcrtxy would need
to call SDL_InitSubSystem().

In that case, should I do something like:

if (SDL_WasInit(0) != 0)
{
/* Oops, they touched SDL before we did! */
return(XY_FALSE);
}

SDL_Init(SDL_VIDEO);

or should I do something more flexible like this?

if (SDL_WasInit(0) != 0)
{
/* They touched SDL before we did */

if (!SDL_WasInit(SDL_VIDEO))
{
  /* Make sure video is available: */
  SDL_InitSubsystem(SDL_VIDEO);
}

}
else
{
/* They left it to us to init SDL entirely */

SDL_Init(SDL_VIDEO);

}

I like the looks of the latter, but I’m obviously not an expert at
initializing SDL’s subsystems! :^DOn Wed, Aug 13, 2008 at 01:09:52AM +0200, Torsten Giebl wrote:

As to my knowledge in the second case you have to
use SDL_InitSubSystem. When SDL_Init (SDL_INIT_VIDEO);
SDL_Init (SDL_INIT_AUDIO); works for you then this is more
luck then wanted behaviour.


-bill!
“Tux Paint” - free children’s drawing software for Windows / Mac OS X / Linux!
Download it today! http://www.tuxpaint.org/

Hello !

Someone on IRC is asking when it’s appropriate to use SDL_InitSubSystem().
I actually don’t know. In the past, I’ve been known to do the following,
and it works:

if (SDL_Init(SDL_VIDEO) < 0)
… abort

if (sound_enabled)
if (SDL_Init(SDL_AUDIO) < 0)
{
fprintf(stderr, “What? I can’t hear you!\n!”);
sound_enabled = FALSE;
}
else
… load sounds

When you call the second time SDL_Init (xyxy) it might erase
some of SDL state variables about what is initialized and so on.

I would add a call to your library BKLib_Init (SDL_INIT_VIDEO; …);
and then check inside that function if adding SDL_INIT_VIDEO
is needed or if the user already has used it.

Tell the user that he is not allowed to use the SDL_Init together
with this library. He should use BKLib_Init as his SDL_Init.

On Windows DLLs have their own space, so every surface
that is created in your library, also has to be freed there.

CU

THAT’s good to know.

Right now it does keep track of a single surface, and does free it
at XY_quit() if it exists.On Wed, Aug 13, 2008 at 01:24:13AM +0200, Torsten Giebl wrote:

On Windows DLLs have their own space, so every surface
that is created in your library, also has to be freed there.


-bill!
“Tux Paint” - free children’s drawing software for Windows / Mac OS X / Linux!
Download it today! http://www.tuxpaint.org/

Hrm, and I guess if the user does nothing but XY_init() and XY_quit(), and
never init’s any SDL subsystems, perhaps my XY_quit() should test
for whether ONLY video has been initialized, and leave it up to the caller
to de-init any other SDL subsystems.

if (SDL_WasInit(0) == SDL_VIDEO)
{
/* Only video was init, so we can safely quit all of SDL: /
SDL_Quit();
}
else
{
/
More than video was init; just quit the video subsystem, and let
the caller quit any other subsystems they called, specifically, or
just use SDL_Quit() themselves: */
SDL_QuitSubsystem(SDL_VIDEO);
}

I guess the question is… is SDL_Quit() a catch-all? Or does it HAVE to
be called, even if SDL_QuitSubsystem() was called for each and every
subsystem? (The man pages are vague.)

Thx!On Tue, Aug 12, 2008 at 04:23:57PM -0700, Bill Kendrick wrote:

or should I do something more flexible like this?

-bill!
“Tux Paint” - free children’s drawing software for Windows / Mac OS X / Linux!
Download it today! http://www.tuxpaint.org/

Hello !

I guess the question is… is SDL_Quit() a catch-all? Or does it HAVE to
be called, even if SDL_QuitSubsystem() was called for each and every
subsystem? (The man pages are vague.)

SDL_QuitSubSystem (flags) should clearly
remove everything that is needed.

SDL_Quit () = SDL_QuitSubSystem (Everythingthatwasinited)

CU