⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 midi.cpp

📁 人最近写的一个飞行类的游戏,主要目的是为了锻炼一下自己,提高编程水平,由于本人C++基础不是很好,所以程序中有很多地方设计的不是很合理,希望大家能够多提建议!
💻 CPP
字号:
// Midi.cpp: implementation of the CMidi class.
//
//////////////////////////////////////////////////////////////////////

#include "Midi.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMidi::CMidi()
{
	InitDirectMusic();
}

CMidi::~CMidi()
{
	ReleaseMusic();	
}

BOOL CMidi::InitDirectMusic()
{
	dm_perf = NULL; 
	dm_loader = NULL;  
	g_pSegState = NULL;
	for(int i = 0 ; i < 2 ; i++)
	{
		pSegment[i] = NULL;
	}

	if (FAILED(CoInitialize(NULL)))
	{    
		// Terminate the application.
		return(FALSE);
	}   // end if

	// create the performance
	if (FAILED(CoCreateInstance(CLSID_DirectMusicPerformance,
								NULL,
								CLSCTX_INPROC,
								IID_IDirectMusicPerformance,
								(void**)&dm_perf)))    
	{
		// return null        
		return(FALSE);
	}	// end if

	// initialize the performance, check if directsound is on-line if so, use the
	// directsound object, otherwise create a new one
	if (FAILED(dm_perf->Init(NULL, NULL, NULL)))
	{
		return(FALSE); // Failure -- performance not initialized
	}   // end if 

	// add the port to the performance
	if (FAILED(dm_perf->AddPort(NULL)))
	{    
		return(FALSE);// Failure -- port not initialized
	}   // end if

	// create the loader to load object(s) such as midi file
	if (FAILED(CoCreateInstance(
			  CLSID_DirectMusicLoader,
			  NULL,
			  CLSCTX_INPROC, 
			  IID_IDirectMusicLoader,
			  (void**)&dm_loader)))
	{
		// error
		return(FALSE);
	}	// end if	
	
	if(!LoadMidiResource())
		return FALSE;

	return TRUE;
}

BOOL CMidi::LoadMidiResource()
{
	pSegment[0] = CreateSndSeg("sound//menu.mid");
	pSegment[1] = CreateSndSeg("sound//game.mid");

	for(int i = 0 ; i < 2 ; i++)
	{
		if( NULL == pSegment[i] )
			return FALSE;
	}
	return TRUE;
}

void CMidi::ReleaseMusic()
{
	if (dm_perf)
	   dm_perf->Stop(NULL, NULL, 0, 0 ); 
	
	for(int i = 0 ; i < 2 ; i++)
	{
		if(pSegment[i])
		{
			pSegment[i]->SetParam(GUID_Unload, -1, 0, 0, (void*)dm_perf);

			pSegment[i]->Release();
		}
	}	

	// CloseDown and Release the performance object.    
	if (dm_perf)
	{
		dm_perf->CloseDown();
		dm_perf->Release();     
	} // end if

	// Release the loader object.
	if (dm_loader)
	   dm_loader->Release();     

	// Release COM
	CoUninitialize(); 	
}

void CMidi::Play(int num)
{
	if (pSegment[num])
	{
		pSegment[num]->SetRepeats(-1);
		dm_perf->PlaySegment(pSegment[num], 0, 0, &g_pSegState);
	}
}

void CMidi::Stop()
{
	dm_perf->Stop(NULL, NULL, 0, 0);
}

IDirectMusicSegment* CMidi::CreateSndSeg(char *filename)
{
	IDirectMusicSegment *pSegment; 
	HRESULT hr;
//下面这段在win98下会出问题
/*  char szDir[_MAX_PATH] ;
    WCHAR wszDir[_MAX_PATH];
    if( 0 == GetCurrentDirectory(  _MAX_PATH, szDir ) )
    {
        return NULL;
    }
    MULTI_TO_WIDE(wszDir, szDir);
    hr = dm_loader->SetSearchDirectory(GUID_DirectMusicAllTypes,
        wszDir, FALSE);
    if (FAILED(hr)) 
    {
        return NULL;
    }
*/
	// convert filename to wide string
	WCHAR wfilename[_MAX_PATH]; 
	MULTI_TO_WIDE(wfilename, filename);

	DMUS_OBJECTDESC ObjDesc;
    ObjDesc.guidClass = CLSID_DirectMusicSegment;
    ObjDesc.dwSize = sizeof(DMUS_OBJECTDESC);
    wcscpy( ObjDesc.wszFileName, wfilename );
    ObjDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME;

	dm_loader->GetObject(&ObjDesc,
				IID_IDirectMusicSegment, (void**) &pSegment);

    hr = pSegment->SetParam(GUID_StandardMIDIFile,
            -1, 0, 0, (void*)dm_perf);
	if (FAILED(hr))
		return NULL;

	hr = pSegment->SetParam(GUID_Download, -1, 0, 0, (void*)dm_perf);
	if (FAILED(hr))
	   return NULL;

	return pSegment;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -