SDL and GC

Tom S h3r3tic at -remove-.mat.uni.torun.pl
Mon Apr 10 08:32:20 PDT 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 +/



More information about the Digitalmars-d-learn mailing list