天行健 君子当自强而不息

创建游戏内核(21)【OO改良版】

 

本篇是创建游戏内核(20)【OO改良版】的续篇,关于该内核的细节说明请参阅创建游戏内核(21)


接口:

//======================================================================================
// This class encapsulates for downloadable sound.
//======================================================================================
typedef class DLS
{
public:
    DLS();
    ~DLS();

    IDirectMusicCollection8* get_dm_colletion();

    BOOL attach(SOUND_PTR sound);
    BOOL load(
const char* filename);
    BOOL free();

    
long get_num_patches();
    
long get_patch(long index);
    BOOL exists(
long patch);

private:
    SOUND_PTR m_sound;
    IDirectMusicCollection* m_dm_collection;
} *DLS_PTR;

实现:

//------------------------------------------------------------------------------
// Constructor, zero member data.
//------------------------------------------------------------------------------
DLS::DLS()
{
    memset(
this, 0, sizeof(*this));
}

//------------------------------------------------------------------------------
// Destructor, release DirectMusic resource.
//------------------------------------------------------------------------------
DLS::~DLS()
{
    free();
}

//------------------------------------------------------------------------------
// Release DirectMusic collection object.
//------------------------------------------------------------------------------
BOOL DLS::free()
{
    
if(m_sound == NULL || m_sound->get_dm_loader() == NULL)
        
return FALSE;

    
if(m_dm_collection)
    {
        
if(FAILED(m_sound->get_dm_loader()->ReleaseObjectByUnknown(m_dm_collection)))
            
return FALSE;
    }

    release_com(m_dm_collection);
    
return TRUE;
}

//------------------------------------------------------------------------------
// Create DLS object.
//------------------------------------------------------------------------------
BOOL DLS::attach(SOUND_PTR sound)
{
    free();

    m_sound = sound;

    
if(m_sound == NULL || m_sound->get_dm_loader() == NULL)
        
return FALSE;

    
return TRUE;
}

//------------------------------------------------------------------------------
// Load DirectMusic collection object from specified filename.
//------------------------------------------------------------------------------
BOOL DLS::load(const char* filename)
{
    free();

    
if(m_sound == NULL || m_sound->get_dm_loader() == NULL)
        
return FALSE;

    DMUS_OBJECTDESC _object_desc;

    ZeroMemory(&_object_desc, 
sizeof(DMUS_OBJECTDESC));

    _object_desc.dwSize    = 
sizeof(DMUS_OBJECTDESC);
    _object_desc.guidClass = CLSID_DirectMusicCollection;

    
if(filename == NULL)
    {
        
// get the default collection
        _object_desc.guidObject  = GUID_DefaultGMCollection;
        _object_desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_OBJECT;
    }
    
else
    {
        
// get the colletion object
        _object_desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH;
        mbstowcs(_object_desc.wszName, filename, MAX_PATH);
    }

    HRESULT _rv = m_sound->get_dm_loader()->GetObject(&_object_desc, 
                                IID_IDirectMusicCollection8, (LPVOID*)&m_dm_collection);    

    
if(_rv != S_OK)
    {
        
if(_rv == DMUS_E_LOADER_FAILEDCREATE)
            err_msg_box("The object could not be found or created.");
        
else if(_rv == DMUS_E_LOADER_FAILEDOPEN)
            err_msg_box("File open failed because the file does not exist or is locked.");
        
else if(_rv == DMUS_E_LOADER_FORMATNOTSUPPORTED)            
            err_msg_box("The object cannot be loaded because the data format is not supported.");
        
else if(_rv == DMUS_E_LOADER_NOCLASSID)
            err_msg_box("No class identifier was supplied in the object description.");
        
else if(_rv == E_FAIL)
            err_msg_box("The method did not succeed.");
        
else if(_rv == E_INVALIDARG)
            err_msg_box("Invalid argument. Often, this error results from failing to initialize the dwSize member"
                        " of a structure before passing it to the method.");
        
else if(_rv == E_OUTOFMEMORY)
            err_msg_box("Insufficient memory to complete the task.");
        
else if(_rv == E_POINTER)
            err_msg_box("An invalid pointer, usually NULL, was passed as a parameter.");
        
else if(_rv == REGDB_E_CLASSNOTREG)
            err_msg_box("The object class is not registered.");

        
return FALSE;
    }

    
return TRUE;
}

//------------------------------------------------------------------------------
// Return DirectMusic collection object.
//------------------------------------------------------------------------------
IDirectMusicCollection8* DLS::get_dm_colletion()
{
    
return m_dm_collection;
}

//------------------------------------------------------------------------------
// Return number of patches in DirectMusic collection.
//------------------------------------------------------------------------------
long DLS::get_num_patches()
{
    DWORD _patch;
    
long  _index = 0;

    
// retrieves the patch number and name of an instrument by its index in the collection
    while(m_dm_collection->EnumInstrument(_index, &_patch, NULL, 0))
        ++_index;

    
return _index;
}

//------------------------------------------------------------------------------
// Return patch number by index of the instrument.
//------------------------------------------------------------------------------
long DLS::get_patch(long index)
{
    DWORD _patch;

    
if(m_dm_collection == NULL)
        
return -1;

    
if(FAILED(m_dm_collection->EnumInstrument(index, &_patch, NULL, 0)))
        
return -1;

    
return (long) _patch;
}

//------------------------------------------------------------------------------
// Judge whether specified patch exists.
//------------------------------------------------------------------------------
BOOL DLS::exists(long patch)
{
    IDirectMusicInstrument8* _dm_instrument;

    
if(m_dm_collection == NULL)
        
return FALSE;

    
// retrieve an instrument by its patch number
    if(FAILED(m_dm_collection->GetInstrument(patch, &_dm_instrument)))
        
return FALSE;

    _dm_instrument->Release();

    
return TRUE;
}

posted on 2007-10-10 21:20 lovedday 阅读(175) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论