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.");
}