Adding MIDI Music Support to the Game Engine


Now that you're an MCI programming expert, you're no doubt ready to find out how to incorporate MIDI music capabilities into the game engine. You might not realize it, but you've already seen the majority of the MIDI code required to add music support to the game engine. It's mainly just a matter of creating a clean user interface for opening and closing the MIDI sequencer device, as well as playing MIDI songs. Keep in mind that the entire source code for the game engine and all example programs is included on the accompanying CD-ROM.

The first step required for adding MIDI support to the game engine is to keep track of the MIDI sequencer device ID. This is easily accomplished with a new member variable, which looks like this:

 UINT m_uiMIDIPlayerID; 

The m_uiMIDIPlayerID member variable contains the device ID for the MIDI sequencer. Any time the ID is not , you will know that you have the device open and ready for playing music. If this member variable is set to , the device is closed. This means that you need to initialize the m_uiMIDIPlayerID member variable to in the GameEngine() constructor, like this:

 m_uiMIDIPlayerID = 0; 

This is the only change required in the GameEngine() constructor to support MIDI music, and it simply involves setting the m_uiMIDIPlayerID member variable to .

I mentioned earlier that the most important requirement for the game engine is to establish an interface for carrying out MIDI music tasks. Following are three new methods in the game engine that carry out important MIDI music playback tasks :

 void PlayMIDISong(LPTSTR szMIDIFileName = TEXT(""), BOOL bRestart = TRUE); void PauseMIDISong(); void CloseMIDIPlayer(); 

The roles of these methods hopefully are somewhat self-explanatory in that they are used to play a MIDI song, pause a MIDI song, and close the MIDI player (sequencer). You might be wondering why there isn't a method for opening the MIDI player. The opening of the player is actually handled within the PlayMIDISong() method. In fact, you'll notice that the PlayMIDISong() method has a string parameter that is the filename of the MIDI file to be played . This filename is used as the basis for opening the MIDI player. It might seem strange that the MIDI filename has a default value, meaning that you don't have to provide it if you don't want to. Calling the PlayMIDISong() method with no filename only works if you've already begun playing a song and it is now paused .

The purpose for allowing you to pass an empty filename to the PlayMIDISong() method is to allow you to restart or resume the playback of a song that has already started playing. In this case, the second parameter to the method, bRestart , is used to determine how the song is played. Resuming playback would be useful if you had simply paused the music in a game, whereas restarting the playback would be useful if you were starting a new game and wanted to start the music over.

The PlayMIDISong() method is responsible for opening the MIDI player if it isn't already open. The method first checks to see if the player isn't yet open, and if it isn't, an open command is issued to open the MIDI sequencer device. The device ID for the MIDI sequencer is stored in the m_uiMIDIPlayerID variable. The PlayMIDISong() method continues by restarting the song if the bRestart argument is TRUE . The MIDI song is then played by issuing a play command via MCI. If the play command fails, the MIDI sequencer device is closed.

It's likely that you might at some point want to pause a MIDI song once it has started playing. This is accomplished with the PauseMIDISong() method, which simply issues a pause command to the MIDI player, providing the device ID isn't set to .

The CloseMIDIPlayer() method is used to close the MIDI device and clear the device ID. This method first checks to make sure that the device is indeed open by checking to see if the device ID is not equal to . If the device is open, the CloseMIDIPlayer() method proceeds to close the device by issuing a close command, and then clearing the device ID member variable.



Sams Teach Yourself Game Programming in 24 Hours
Sams Teach Yourself Game Programming in 24 Hours
ISBN: 067232461X
EAN: 2147483647
Year: 2002
Pages: 271

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net