Thread overview
SDL and GC
Mar 27, 2006
Mark Harviston
Apr 10, 2006
lanael
Apr 10, 2006
Tom S
Apr 10, 2006
lanael
March 27, 2006
I'm using the SDL and D to make a simple 2D game.
I'm having trouble with loading Sounds
I'd like to do something like this:

class someobject {
static Mix_Chunk* sound_effect;
static this(){
sound_effect = Mix_LoadWAV("seffect.wav");
}
int draw(){
//..other code..//
if( Mix_PlayChannel( -1, sound_effect, 0 ) == -1 ) { assert(); }
//..more code..//
}
}


but that fails, the sound isn't loaded so I have to do:
class someobject {
static Mix_Chunk* sound_effect;
int draw(){
//..other code..//
if(sound_effect is null) {
sound_effect = Mix_LoadWAV("seffect.wav");
}
if( Mix_PlayChannel( -1, sound_effect, 0 ) == -1 ) { assert(); }
//..more code..//
}
}

do I need to declare the Mix_Chunks as auto?
Is this a GC problem?
Is this and SDL problem?
(for the record I have the same problem with fonts)
I honestly had no clue where to ask this question, I figured this was as a good as any.
--Mark
April 10, 2006
> I'm using the SDL and D to make a simple 2D game.
> I'm having trouble with loading Sounds


> sound_effect = Mix_LoadWAV("seffect.wav");

I'm not sure about your problem, but don't forget that the SDL functions are C functions so they take zero terminated strings. I, personnaly, use that :

char[] aSoundFile="sound.wav";
Mix_Chunk *sample=null;
sample=Mix_LoadWAV_RW(SDL_RWFromFile(toStringz(aSoundFile), "rb"), 1);


April 10, 2006
lanael wrote:
>> I'm using the SDL and D to make a simple 2D game.
>> I'm having trouble with loading Sounds
> 
> 
>> sound_effect = Mix_LoadWAV("seffect.wav");
> 
> I'm not sure about your problem, but don't forget that the SDL functions are C functions so they take zero terminated strings. I, personnaly, use that :
> 
> char[] aSoundFile="sound.wav";
> Mix_Chunk *sample=null;
> sample=Mix_LoadWAV_RW(SDL_RWFromFile(toStringz(aSoundFile), "rb"), 1);

Don't forget that character literals are allocated with an implicit \0 at their ends, so this code:

sample = Mix_LoadWAV_RW(SDL_RWFromFile("sound.wav", "rb"), 1);

will be perfectly fine :)

Moreover, references and pointers are initialized to null in D, so you don't have to write 'Mix_Chunk* sample = null;", simply stating "Mix_Chunk* sample;" will be enough.


As for Mark's original problem:

The D language spec states that "A static constructor is defined as a function that performs initializations before the main() function gets control", thus you're probably trying to load sounds using SDL before it even gets a chance to initialize itself. You might try doing the following:

class SomeObject
{
    static Mix_Chunk* soundEffect;
    this() {
        static bool soundEffectLoaded = false;
        if (!soundEffectLoaded) {
            soundEffect = Mix_LoadWAV("seffect.wav");
            soundEffectLoaded = true;
        }
    }
}

You might also do it this way:

class SomeObject
{
    static Mix_Chunk* soundEffect;
    this() {
        if (soundEffect is null) {
            soundEffect = Mix_LoadWAV("seffect.wav");
        }
    }
}

The difference is that, when you use the second code and SDL fails to load the .wav file, it will try to load it in any subsequent constructor runs for the SomeObject class.

Make sure that you're not creating any SomeObject instances before you initialize SDL. That or create some kind of a postponed loading system, where you only create /loading requests/ which are processed as soon as the subsystem responsible for fulfilling them has been initialized.


-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d-pu s+: a-->----- C+++$>++++ UL P+ L+ E--- W++ N++ o? K? w++ !O !M V? PS- PE- Y PGP t 5 X? R tv-- b DI- D+ G e>+++ h>++ !r !y
------END GEEK CODE BLOCK------

Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
April 10, 2006
> Don't forget that character literals are allocated with an implicit \0 at their ends, so this code:
>
> sample = Mix_LoadWAV_RW(SDL_RWFromFile("sound.wav", "rb"), 1);
>
> will be perfectly fine :)

yep; but I usually don't hardcode filenames ! :)


> Moreover, references and pointers are initialized to null in D, so you don't have to write 'Mix_Chunk* sample = null;", simply stating "Mix_Chunk* sample;" will be enough.

right. I wrote this a long time ago; when I wasn't sure about the D behavior !  You never know ! :)

Anyway, great explanation about the static stuffs. ( which I don't use at all : I don't know how to use them )