Now has problems with FreeSound!
Displaying 1-7 of 7 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
the_shoeless_shoe

Hey there.
It seems that if I do this:

mySound = LoadSound("mysound.wav");
playsound(mySound, 128);
FreeSound(mySound);

mySound will never be heard. But take FreeSound out, and everything's fine. So my guess is that the sound is freed before it has finished playing (well, almost at the same time that it's start). I am right? Should I wait for the sound to be finished playing before freeing it?
Or do I use one global sound variable for everything and free it at the end of the script?
Thanks!

Posted on 2005-12-26 01:48:51

mcgrue

You should free the sound when it's done playing.

Using a single sound variable with multiple LoadSound()'s without freeing those sounds would be bad, as it'd cause gradual loss of sound slots.

Frankly, I just never free my sound effects. They're relatively small, as far as things go. you should definitly free sounds you're not re-using when they're done (like spoken dialogue), but there's no need to free small sound effects that are used a whole ton (menu beeps, buzzes, repeating voice tracks of a sexy lady saying "grue, you're so awesome...")

Posted on 2005-12-26 03:34:25

CrazyAznGamer

Quote:
Originally posted by mcgrue

sexy lady saying "grue, you're so awesome...")

That was my girlfriend, you fiend!

Posted on 2005-12-26 10:37:41

Overkill

If this is your second resource problem, I recommend you do something like this, have all your resources declared as global variables:

int sfx_mysound;
int img_myimage;
int msc_mysong;


Have a file called resource.dat, that stores all images/sounds/etc that need to be loaded at the beginning of the game:

sfx_mysound mysound.ogg
img_myimage myimage.png
msc_mysong mysong.it


Then have a resource loader that's called at the beginning of the game!

string FileToString(string filename)
{
string s;
int f = FileOpen(filename, FILE_READ);
if (!f) Exit("Couldn't open "+filename+" for reading.");
while (!FileEOF(f))
{
s = s + FileReadLn(f) + "|";
}
return s;
}

void LoadResources()
{
string toload = FileToString("resources.dat");
string chunk, var, rsc, type;
int idx, idx_max = TokenCount(toload, "|");

Log(">> Loading resources...");

for(idx = 0; idx < idx_max; idx++)
{
chunk = GetToken(toload, "|", idx);
var = GetToken(chunk, " ", 0);
rsc = GetToken(chunk, " ", 1);
type = GetToken(var, "_", 0);
ResourceLoadDisplay((idx * 100) / idx_max, rsc);
if(strcmp(rsc, ""))
{
if(!strcmp(type,"img"))
{
Log("Allocating image "+ rsc + " to variable "+ var);
SetInt(var, LoadImage(rsc));

}
else if(!strcmp(type,"msc"))
{
Log("Allocating song "+ rsc + " to variable "+ var);
SetInt(var, LoadSong(rsc));
}
else if(!strcmp(type,"sfx"))
{
Log("Allocating sound "+ rsc + " to variable "+ var);
SetInt(var, LoadSound(rsc));
}
}
}
ResourceLoadDisplay((idx * 100) / idx_max, "Done!");
Log(">> Resources loaded successfully!");
}

void ResourceLoadDisplay(int progress, string text)
{
RectFill(0, 0, ImageWidth(screen), ImageHeight(screen), RGB($AA, $CC, $FF), screen);
PrintCenter(ImageWidth(screen) / 2, ImageHeight(screen) / 2, screen, defaultfont, "Loading Resources...");
PrintCenter(ImageWidth(screen) / 2, ImageHeight(screen) / 2 + (FontHeight(defaultfont) * 2), screen, defaultfont, text);
RectFill(ImageWidth(screen) / 2 - 50, ImageHeight(screen) / 2 - 15, ImageWidth(screen) / 2 - 50 + progress, ImageHeight(screen) / 2 - 10, RGB($33, $CC, $66), screen);
Rect(ImageWidth(screen) / 2 - 50, ImageHeight(screen) / 2 - 15, ImageWidth(screen) / 2 + 50, ImageHeight(screen) / 2 - 10, RGB(0, 0, 0), screen);
ShowPage();
}

void FreeResources()
{
string toload = FileToString("resources.dat");
string chunk, var, rsc, type;
int idx, idx_max = TokenCount(toload, "|");

Log(">> Freeing resources...");

for(idx = 0; idx < idx_max; idx++)
{
chunk = GetToken(toload, "|", idx);
var = GetToken(chunk, " ", 0);
rsc = GetToken(chunk, " ", 1);
type = GetToken(var, "_", 0);
if(strcmp(rsc, ""))
{
if(!strcmp(type,"img"))
{
Log("Freeing image "+ rsc +" from variable "+ var);
if (ImageValid(GetInt(var)))
{
FreeImage(GetInt(var));
}
}
else if(!strcmp(type,"msc"))
{
Log("Freeing song "+ rsc +" from variable "+ var);
FreeSong(GetInt(var));
}
else if(!strcmp(type,"sfx"))
{
Log("Freeing sound "+ rsc +" from variable "+ var);
FreeSound(GetInt(var));
}
}
}
Log(">> Resources freed okay.");
}

Posted on 2005-12-26 10:43:45 (last edited on 2005-12-26 10:49:11)

the_shoeless_shoe

Hey thanks, that's pretty handy!

Posted on 2005-12-26 13:49:34

mcgrue

Aren't all resources unloaded at the close of the application, regardless of being freed?

Doesn't FreeImage/FreeSound just free the verge handles at the moment?

Doesn't this make this solution largely busywork with no real benefit?

Posted on 2005-12-26 15:49:26

the_shoeless_shoe

Well the LoadResources() part is kinda cool and could certainly be useful

Posted on 2005-12-26 16:22:15


Displaying 1-7 of 7 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.