0
0

I have 2 memsongs in my program (IDR_XM1 and IDR_XM2) and I want to play the first song first, then at a certain point in time the first song shall be stopped and the second song shall start.

I made a class Music with the memsong methods. I create an instance of this class:

[code:3w3kz3vg]Music music;[/code:3w3kz3vg]
Then I initialize the first song:

[code:3w3kz3vg]currentSong = IDR_XM1;
music.stop ();
music.init (currentSong);
music.play ();[/code:3w3kz3vg]
(I use music.stop because this method may not only be called at the start of the program but also later on.)

To switch to the second song, I use very similar code:

[code:3w3kz3vg]inline void Game::EnterVictory (float tickCount)
{
// ... code not related to music ...

if (currentSong != IDR_XM2)
{
    currentSong = IDR_XM2;
    music.stop ();
    music.init (currentSong);
    music.play ();
}

}[/code:3w3kz3vg]
Now when I run this, I get a crash after music.init. Why?

In case you need it, the class Music looks like the following:

[code:3w3kz3vg]FMUSIC_MODULE *mod;

typedef struct
{
int length;
int pos;
unsigned char *data;
} MEMFILE;

unsigned int memopen (char *name)
{
MEMFILE *memfile;

memfile = new MEMFILE;

HRSRC rec;
HGLOBAL handle;

rec = FindResource (NULL, MAKEINTRESOURCE (IDR_XM1), name);
handle = LoadResource (NULL, rec);

memfile->data = (unsigned char *)LockResource (handle);
memfile->length = SizeofResource (NULL, rec);
memfile->pos = 0;

return (unsigned int) memfile;

}

unsigned int memopen2 (char *name)
{
MEMFILE *memfile;

memfile = new MEMFILE;

HRSRC rec;
HGLOBAL handle;

rec = FindResource (NULL, MAKEINTRESOURCE (IDR_XM2), name);
handle = LoadResource (NULL, rec);

memfile->data = (unsigned char *)LockResource (handle);
memfile->length = SizeofResource (NULL, rec);
memfile->pos = 0;

return (unsigned int) memfile;

}

void memclose (unsigned int handle)
{
MEMFILE *memfile = (MEMFILE *)handle;

delete (memfile);

}

int memread (void *buffer, int size, unsigned int handle)
{
MEMFILE *memfile = (MEMFILE *)handle;

if (memfile->pos + size >= memfile->length)
    size = memfile->length - memfile->pos;

memcpy (buffer, (char *)memfile->data+memfile->pos, size);
memfile->pos += size;

return size;

}

void memseek (unsigned int handle, int pos, signed char mode)
{
MEMFILE *memfile = (MEMFILE *)handle;

if (mode == 0)
    memfile->pos = pos;
else if (mode == 1)
    memfile->pos += pos;
else if (mode == 2)
    memfile->pos = memfile->length + pos;

if (memfile->pos > memfile->length)
    memfile->pos = memfile->length;

}

int memtell (unsigned int handle)
{
MEMFILE *memfile = (MEMFILE *)handle;

return memfile->pos;

}

Music::Music ()
{
}

Music::~Music ()
{
}

bool Music::init (int songnum)
{
if (songnum == IDR_XM1)
FSOUND_File_SetCallbacks (memopen, memclose, memread, memseek, memtell);
else
FSOUND_File_SetCallbacks (memopen2, memclose, memread, memseek, memtell);
if (!FSOUND_Init (44100, 0))
return false;
if (!(mod = FMUSIC_LoadSong ("XM", NULL)))
return false;

return true;

}

void Music::play ()
{
FMUSIC_PlaySong (mod);
}

void Music::stop ()
{
FMUSIC_StopSong (mod);
}[/code:3w3kz3vg]

  • You must to post comments
Showing 0 results
Your Answer

Please first to submit.