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

📄 mload.cpp

📁 游戏编程精华02-含有几十个游戏编程例子
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                            pframeCur->m_pScaleKeys[iKey].vScale = pFileScaleKey->vScale;
                            
                            pFileScaleKey += 1;
                        }
                    }
                    else if (dwKeyType == 2)
                    {
                        if (pframeCur->m_pPositionKeys != NULL)
                        {
                            hr = E_INVALIDARG;
                            goto e_Exit;
                        }
                        
                        pframeCur->m_pPositionKeys = new SPositionKey[cKeys];
                        if (pframeCur->m_pPositionKeys == NULL)
                        {
                            hr = E_OUTOFMEMORY;
                            goto e_Exit;
                        }
                        
                        pframeCur->m_cPositionKeys = cKeys;
                        
                        pFilePosKey =  (SPositionKeyXFile*)(pData + (sizeof(DWORD) * 2));
                        for (iKey = 0;iKey < cKeys; iKey++)
                        {
                            pframeCur->m_pPositionKeys[iKey].dwTime = pFilePosKey->dwTime;
                            pframeCur->m_pPositionKeys[iKey].vPos = pFilePosKey->vPos;
                            
                            pFilePosKey += 1;
                        }
                    }
                    else if (dwKeyType == 4)
                    {
                        if (pframeCur->m_pMatrixKeys != NULL)
                        {
                            hr = E_INVALIDARG;
                            goto e_Exit;
                        }
                        
                        pframeCur->m_pMatrixKeys = new SMatrixKey[cKeys];
                        if (pframeCur->m_pMatrixKeys == NULL)
                        {
                            hr = E_OUTOFMEMORY;
                            goto e_Exit;
                        }
                        
                        pframeCur->m_cMatrixKeys = cKeys;
                        
                        pFileMatrixKey =  (SMatrixKeyXFile*)(pData + (sizeof(DWORD) * 2));
                        for (iKey = 0;iKey < cKeys; iKey++)
                        {
                            pframeCur->m_pMatrixKeys[iKey].dwTime = pFileMatrixKey->dwTime;
                            pframeCur->m_pMatrixKeys[iKey].mat = pFileMatrixKey->mat;
                            
                            pFileMatrixKey += 1;
                        }
                    }
                    else
                    {
                        hr = E_INVALIDARG;
                        goto e_Exit;
                    }
                }
                
                GXRELEASE(pxofobjChild);
            }
        }
        
        GXRELEASE(pxofChild);
    }
    
e_Exit:
    GXRELEASE(pxofobjChild);
    GXRELEASE(pxofChild);
    GXRELEASE(pxofobjChildRef);
    return hr;
}




HRESULT CShaderPaletteSkin::LoadAnimationSet(LPDIRECTXFILEDATA pxofobjCur, SDrawElement *pde,
                                            DWORD options, SFrame *pframeParent)
{
    SFrame *pframeCur;
    const GUID *type;
    HRESULT hr = S_OK;
    LPDIRECTXFILEDATA pxofobjChild = NULL;
    LPDIRECTXFILEOBJECT pxofChild = NULL;
    DWORD cchName;
    
    pframeCur = new SFrame();
    if (pframeCur == NULL)
    {
        hr = E_OUTOFMEMORY;
        goto e_Exit;
    }
    pframeCur->bAnimationFrame = true;
    
    pframeParent->AddFrame(pframeCur);
    
    hr = pxofobjCur->GetName(NULL, &cchName);
    if (FAILED(hr))
        goto e_Exit;
    
    if (cchName > 0)
    {
        pframeCur->szName = new char[cchName];
        if (pframeCur->szName == NULL)
        {
            hr = E_OUTOFMEMORY;
            goto e_Exit;
        }
        
        hr = pxofobjCur->GetName(pframeCur->szName, &cchName);
        if (FAILED(hr))
            goto e_Exit;
    }
    
    
    // Enumerate child objects.
    // Child object can be data, data reference or binary.
    // Use QueryInterface() to find what type of object a child is.
    while (SUCCEEDED(pxofobjCur->GetNextObject(&pxofChild)))
    {
        // Query the child for it's FileData
        hr = pxofChild->QueryInterface(IID_IDirectXFileData,
            (LPVOID *)&pxofobjChild);
        if (SUCCEEDED(hr))
        {
            hr = pxofobjChild->GetType(&type);
            if (FAILED(hr))
                goto e_Exit;
            
            if( TID_D3DRMAnimation == *type )
            {
                hr = LoadAnimation(pxofobjChild, pde, options, pframeCur);
                if (FAILED(hr))
                    goto e_Exit;
            }
            
            GXRELEASE(pxofobjChild);
        }
        
        GXRELEASE(pxofChild);
    }
    
e_Exit:
    GXRELEASE(pxofobjChild);
    GXRELEASE(pxofChild);
    return hr;
}




HRESULT SplitMesh
        (
            LPD3DXMESH  pMesh,              // ASSUMPTION:  *pMesh is attribute sorted & has a valid attribute table
            DWORD       iAttrSplit,         // **ppMeshB gets the mesh comprising of this attribute range onward
            DWORD*      rgiAdjacency, 
            DWORD       optionsA, 
            DWORD       optionsB, 
            LPD3DXMESH* ppMeshA, 
            LPD3DXMESH* ppMeshB
        )
{
    *ppMeshA = NULL;

    *ppMeshB = NULL;

    
    HRESULT hr  = S_OK;

    PBYTE   pbVerticesIn    = NULL;
    PBYTE   pbIndicesIn     = NULL;
    DWORD*  piAttribsIn     = NULL;

    LPD3DXMESH pMeshA   = NULL;
    LPD3DXMESH pMeshB   = NULL;
    
    LPD3DXBUFFER pVertexRemapA  = NULL;
    LPD3DXBUFFER pVertexRemapB  = NULL;
        
    DWORD*  rgiAdjacencyA   = NULL;
    DWORD*  rgiAdjacencyB   = NULL;




    LPDIRECT3DDEVICE8   pDevice     = NULL;


    D3DXATTRIBUTERANGE* rgAttrTable = NULL;

    DWORD               cAttrTable  = 0;



    DWORD   cVerticesA;
    DWORD   cVerticesB;

    DWORD   cFacesA;
    DWORD   cFacesB;

    DWORD   cbVertexSize;

    DWORD   dw32bit;


    dw32bit         = pMesh->GetOptions() & D3DXMESH_32BIT;

    //cbVertexSize    = D3DXGetFVFVertexSize(pMesh->GetFVF());
	cbVertexSize = sizeof(PSVertex);

    hr  = pMesh->GetDevice(&pDevice);

    if (FAILED(hr))
        goto e_Exit;


    hr  = pMesh->GetAttributeTable(NULL, &cAttrTable);

    if (FAILED(hr))
        goto e_Exit;

    
    rgAttrTable = new D3DXATTRIBUTERANGE[cAttrTable];

    if (rgAttrTable == NULL)
    {
        hr  = E_OUTOFMEMORY;

        goto e_Exit;
    }


    hr  = pMesh->GetAttributeTable(rgAttrTable, NULL);

    if (FAILED(hr))
        goto e_Exit;

    if (iAttrSplit == 0)
    {
        cVerticesA  = 0;

        cFacesA     = 0;
    }
    else if (iAttrSplit >= cAttrTable)
    {
        cVerticesA  = pMesh->GetNumVertices();

        cFacesA     = pMesh->GetNumFaces();
    }
    else
    {
        cVerticesA  = rgAttrTable[iAttrSplit].VertexStart;

        cFacesA     = rgAttrTable[iAttrSplit].FaceStart;
    }

    cVerticesB  = pMesh->GetNumVertices() - cVerticesA;

    cFacesB     = pMesh->GetNumFaces() - cFacesA;


    hr  = pMesh->LockVertexBuffer(D3DLOCK_READONLY, &pbVerticesIn);

    if (FAILED(hr))
        goto e_Exit;


    hr  = pMesh->LockIndexBuffer(D3DLOCK_READONLY, &pbIndicesIn);

    if (FAILED(hr))
        goto e_Exit;


    hr  = pMesh->LockAttributeBuffer(D3DLOCK_READONLY, &piAttribsIn);

    if (FAILED(hr))
        goto e_Exit;


    if (cFacesA && cVerticesA)
    {
        PBYTE   pbVerticesOut   = NULL;
        PBYTE   pbIndicesOut    = NULL;
        DWORD*  piAttribsOut    = NULL;
        DWORD i;

        hr  = D3DXCreateMeshFVF(cFacesA, cVerticesA, optionsA | dw32bit, pMesh->GetFVF(), pDevice, &pMeshA);

        if (FAILED(hr))
            goto e_ExitA;


        hr  = pMeshA->LockVertexBuffer(0, &pbVerticesOut);

        if (FAILED(hr))
            goto e_ExitA;


        hr  = pMeshA->LockIndexBuffer(0, (LPBYTE*)&pbIndicesOut);

        if (FAILED(hr))
            goto e_ExitA;


        hr  = pMeshA->LockAttributeBuffer(0, &piAttribsOut);

        if (FAILED(hr))
            goto e_ExitA;


        memcpy(pbVerticesOut, pbVerticesIn, cVerticesA * cbVertexSize * sizeof(BYTE));

        if (dw32bit)
        {
            memcpy(pbIndicesOut, pbIndicesIn, cFacesA * 3 * sizeof(DWORD));
        }
        else
        {
            memcpy(pbIndicesOut, pbIndicesIn, cFacesA * 3 * sizeof(WORD));
        }

      
        memcpy(piAttribsOut, piAttribsIn, cFacesA * sizeof(DWORD));


        rgiAdjacencyA   = new DWORD[cFacesA * 3];

        if (rgiAdjacencyA == NULL)
        {
            hr  = E_OUTOFMEMORY;

            goto e_ExitA;
        }


        for (i = 0; i <  cFacesA * 3; i++)
        {
            rgiAdjacencyA[i]    = (rgiAdjacency[i] < cFacesA) ? rgiAdjacency[i] : 0xFFFFFFFF;
        }


e_ExitA:

        if (pbVerticesOut != NULL)
        {
            pMeshA->UnlockVertexBuffer();
        }


        if (pbIndicesOut != NULL)
        {
            pMeshA->UnlockIndexBuffer();
        }


        if (piAttribsOut != NULL)
        {
            pMeshA->UnlockAttributeBuffer();
        }


        if (FAILED(hr))
            goto e_Exit;
    }


    // calculate Mesh A's attribute table

    if (pMeshA != NULL)
    {
        hr  = pMeshA->OptimizeInplace
                      (
                          D3DXMESHOPT_VERTEXCACHE,
                          rgiAdjacencyA,
                          NULL,
                          NULL,
                          &pVertexRemapA
                      );

        if (FAILED(hr))
            goto e_Exit;
    }


    if (cFacesB && cVerticesB)
    {
        PBYTE   pbVerticesOut   = NULL;
        PBYTE   pbIndicesOut    = NULL;
        DWORD*  piAttribsOut    = NULL;
        DWORD i;

        hr  = D3DXCreateMeshFVF(cFacesB, cVerticesB, optionsB | dw32bit, pMesh->GetFVF(), pDevice, &pMeshB);

        if (FAILED(hr))
            goto e_ExitB;


        hr  = pMeshB->LockVertexBuffer(0, &pbVerticesOut);

        if (FAILED(hr))
            goto e_ExitB;


        hr  = pMeshB->LockIndexBuffer(0, &pbIndicesOut);

        if (FAILED(hr))
            goto e_ExitB;


        hr  = pMeshB->LockAttributeBuffer(0, &piAttribsOut);

        if (FAILED(hr))
            goto e_ExitB;


        memcpy(pbVerticesOut, pbVerticesIn + (cVerticesA * cbVertexSize), cVerticesB * cbVertexSize * sizeof(BYTE));


        // copy & renumber indices

        if (dw32bit)
        {
            for (DWORD i = 0; i < cFacesB * 3; i++)
            {
                ((DWORD*)pbIndicesOut)[i]    = ((DWORD*)pbIndicesIn)[(cFacesA * 3) + i]  - (DWORD)cVerticesA;
            }
        }
        else
        {
            for (DWORD i = 0; i < cFacesB * 3; i++)
            {
                ((WORD*)pbIndicesOut)[i]    = ((WORD*)pbIndicesIn)[(cFacesA * 3) + i]  - (WORD)cVerticesA;
            }
        }

        memcpy(piAttribsOut, piAttribsIn + cFacesA, cFacesB * sizeof(DWORD));


        rgiAdjacencyB   = new DWORD[cFacesB * 3];

        if (rgiAdjacencyB == NULL)
        {
            hr  = E_OUTOFMEMORY;

            goto e_ExitB;
        }


        // copy & renumber adjacency

        for (i = 0; i < cFacesB * 3; i++)
        {
            rgiAdjacencyB[i]    = (rgiAdjacency[(cFacesA * 3) + i] >= cFacesA && rgiAdjacency[(cFacesA * 3) + i] != 0xFFFFFFFF) ? rgiAdjacency[(cFacesA * 3) + i] - cFacesA : 0xFFFFFFFF;
        }


e_ExitB:

        if (pbVerticesOut != NULL)
        {
            pMeshB->UnlockVertexBuffer();
        }


        if (pbIndicesOut != NULL)
        {
            pMeshB->UnlockIndexBuffer();
        }


        if (piAttribsOut != NULL)
        {
            pMeshB->UnlockAttributeBuffer();
        }


        if (FAILED(hr))
            goto e_Exit;
    }


    // calculate Mesh B's attribute table

    if (pMeshB != NULL)
    {
        hr  = pMeshB->OptimizeInplace
                      (
                          D3DXMESHOPT_ATTRSORT,
                          rgiAdjacencyB,
                          NULL,
                          NULL,
                          &pVertexRemapB
                      );

        if (FAILED(hr))
            goto e_Exit;
    }


e_Exit:
    
    if (rgAttrTable != NULL)
    {
        delete[] rgAttrTable;
    }

    if (pbVerticesIn != NULL)
    {
        pMesh->UnlockVertexBuffer();
    }

    if (pbIndicesIn != NULL)
    {
        pMesh->UnlockIndexBuffer();
    }

    if (piAttribsIn != NULL)
    {
        pMesh->UnlockAttributeBuffer();
    }
    
    if (rgiAdjacencyA != NULL)
    {
        delete[] rgiAdjacencyA;
    }

    if (rgiAdjacencyB != NULL)
    {
        delete[] rgiAdjacencyB;

⌨️ 快捷键说明

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