// include dsound, dmusic#include <dsound.h> #include <dmksctrl.h>#include <dmusici.h>#include <dmusicc.h>#include <dmusicf.h>// direct music globalsIDirectMusicPerformance *dm_perf = NULL;IDirectMusicLoader *dm_loader = NULL;IDirectMusicSegment *dm_segment = NULL;IDirectMusicSegmentState *dm_segstate = NULL;
int Game_Init(void *parms = NULL, int num_parms = 0){// this is called once after the initial window is created and// before the main event loop is entered, do all your initialization// here// set up directmusicif (FAILED(CoInitialize(NULL))) { // Terminate the application. return(0); } // end if// create the performancedm_perf = CreatePerformance();if (dm_perf == NULL) { return(0);// Failure -- performance not created } // end if// initialize the performanceif (FAILED(dm_perf->Init(NULL, NULL, main_window_handle))) { return(0);// Failure -- performance not initialized } // end if // add the port to the performanceif (FAILED(dm_perf->AddPort(NULL))) { return(0);// Failure -- port not initialized } // end if// create the loader to load object(s) such as midi filedm_loader = CreateLoader();if (dm_loader == NULL) { return(0);// Failure -- loader not created } // end if// release the old segmentif (dm_segment) { dm_segment->Release(); dm_segment = NULL; } // end if// load the segmentif (dm_loader) { dm_segment = LoadMIDISegment(dm_loader,L"BATTLE.MID"); } // end if// start the songif (dm_segment) { dm_perf->PlaySegment(dm_segment, 0, 0, &dm_segstate); } // end if// return success or failure or your own return code herereturn(1);} // end Game_Init/////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////IDirectMusicPerformance* CreatePerformance(void){// this function creates the performanceIDirectMusicPerformance* pPerf; if (FAILED(CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, IID_IDirectMusicPerformance, (void**)&pPerf))) { // return null pPerf = NULL; } // end if return pPerf;} // end CreatePerformance////////////////////////////////////////////////////////////IDirectMusicLoader* CreateLoader(void){// this function creates the loader IDirectMusicLoader* pLoader; if (FAILED(CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, IID_IDirectMusicLoader, (void**)&pLoader))) { pLoader = NULL; } // end if return pLoader;} // end CreateLoader///////////////////////////////////////////////////////////IDirectMusicSegment* LoadMIDISegment(IDirectMusicLoader* pLoader, WCHAR *wszMidiFileName ){// this function loads a midi segment off diskDMUS_OBJECTDESC ObjDesc; HRESULT hr;IDirectMusicSegment* pSegment = NULL; // get current working directorychar szDir[_MAX_PATH];WCHAR wszDir[_MAX_PATH]; if(_getcwd( szDir, _MAX_PATH ) == NULL) { return NULL; } // end if// convert to wide charactersMULTI_TO_WIDE(wszDir, szDir);// set the search directoryhr = pLoader->SetSearchDirectory(GUID_DirectMusicAllTypes,wszDir, FALSE);if (FAILED(hr)) { return NULL; } // end if // setup object descriptionZeroMemory(&ObjDesc, sizeof(DMUS_OBJECTDESC));ObjDesc.dwSize = sizeof(DMUS_OBJECTDESC);ObjDesc.guidClass = CLSID_DirectMusicSegment;wcscpy(ObjDesc.wszFileName, wszMidiFileName );ObjDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME; // load the object and query it for the IDirectMusicSegment interface// This is done in a single call to IDirectMusicLoader::GetObject// note that loading the object also initializes the tracks and does // everything else necessary to get the MIDI data ready for playback.hr = pLoader->GetObject(&ObjDesc,IID_IDirectMusicSegment, (void**) &pSegment);if (FAILED(hr)) return(0); // ensure that the segment plays as a standard MIDI file// you now need to set a parameter on the band track// Use the IDirectMusicSegment::SetParam method and let // DirectMusic find the trackby passing -1 (or 0xFFFFFFFF) in the dwGroupBits method parameter.hr = pSegment->SetParam(GUID_StandardMIDIFile,-1, 0, 0, (void*)dm_perf);if (FAILED(hr)) return(0); // This step is necessary because DirectMusic handles program changes and // bank selects differently for standard MIDI files than it does for MIDI // content authored specifically for DirectMusic. // The GUID_StandardMIDIFile parameter must be set before the instruments are downloaded. // The next step is to download the instruments. // This is necessary even for playing a simple MIDI file // because the default software synthesizer needs the DLS data // for the General MIDI instrument set// If you skip this step, the MIDI file will play silently.// Again, you call SetParam on the segment, this time specifying the GUID_Download parameter:hr = pSegment->SetParam(GUID_Download, -1, 0, 0, (void*)dm_perf);if (FAILED(hr)) return(0);// return the pointerreturn pSegment; } // end LoadSegment