예제 #1
0
//------------------------------------------------------------------------------------------------
// Name:  obtainD3DTexture
// Desc:  Loads the image denoted by this class using the specified device
//------------------------------------------------------------------------------------------------
bool PackImage::obtainD3DTexture(LPDIRECT3DDEVICE9 pd3dDevice, DWORD options, LPDIRECT3DTEXTURE9* ppd3dTexture) const
{
    // Fail without a device or if something is wrong with the output pointer
    if (!pd3dDevice || !ppd3dTexture) return false;

    // Get the pack from which to load this image
    dc::dcTable<ResourcePack>::Element* packElement = ((PackImage*)this)->myResourcePack.getReferencedResource();
    if (!packElement) return false;
    ResourcePack* pack = packElement->getImplementation();
    if (!pack) return false;

    // Obtain the image in the pack
    dcBuffer buffer;
    if (APP_ERROR(!pack->openResource(myResourceIndex.getValue(), &buffer))
        ("Failed to open image %i in pack file", myResourceIndex))
        return false;

    // Obtain options flags
    bool managed = (options & D3DTEX_MANAGED) != 0;
    bool mipmap = (options & D3DTEX_MIPMAP) != 0;
    bool systemmem = (options & D3DTEX_SYSTEMMEM) != 0;
    D3DPOOL pool = systemmem ? D3DPOOL_SYSTEMMEM : (managed ? D3DPOOL_MANAGED : D3DPOOL_DEFAULT);

    // Holds return codes
    HRESULT hr;

    // Save a NULL first (just in case something goes wrong)
    *ppd3dTexture = NULL;

    // The destination for the texture interface
    LPDIRECT3DTEXTURE9 pd3dTexture;

    // Load the texture
    hr = D3DXCreateTextureFromFileInMemoryEx(pd3dDevice, buffer.getDataPointer(),
                   buffer.getDataSize(), D3DX_DEFAULT, D3DX_DEFAULT,
                   mipmap ? D3DX_DEFAULT : 1, 0, D3DFMT_UNKNOWN, pool,
                   D3DX_DEFAULT, D3DX_FILTER_POINT, 0, NULL, NULL, &pd3dTexture);

    // TODO:
    // buffer.destroy();

    // If there is a problem, there's not much more we can do
    if (APP_WARNING(FAILED(hr))("Couldn't load PackImage %i", myResourceIndex))
        return false;

    // Assign the output texture
    *ppd3dTexture = pd3dTexture;

    // Success
    return true;
}
예제 #2
0
//------------------------------------------------------------------------------------------------
// Name:  obtainSourceGeometry
// Desc:  Loads the geometry held by this class into the destination set
//------------------------------------------------------------------------------------------------
bool PackMesh::obtainSourceGeometry(LPDIRECT3DDEVICE9 pd3dDevice, SubsetGeometry* subsetGeometry) const
{
    // Fail without a device or if something is wrong with the output pointer
    if (APP_ERROR(!pd3dDevice || !subsetGeometry)("Invalid parameter to obtainSourceGeometry"))
        return false;

    // Get the pack from which to load this mesh
    dc::dcTable<ResourcePack>::Element* packElement = ((PackMesh*)this)->myResourcePack.getReferencedResource();
    if (!packElement) return false;
    ResourcePack* pack = packElement->getImplementation();
    if (!pack) return false;

    // Obtain the mesh in the pack
    dcBuffer buffer;
    if (APP_ERROR(!pack->openResource(myResourceIndex.getValue(), &buffer))
        ("Failed to open image %i in pack file", myResourceIndex))
        return false;

    // Set up a buffer reader to scan data from the returned information
    dcBuffer::Reader bufferReader(&buffer);

    // Get the number of subsets in this mesh
    size_t subsets;
    if (!bufferReader.read(&subsets, sizeof(subsets))) return false;

    // Whether or not loading failed
    bool failed = false;

    // Load each of the subsets
    for (size_t s = 0; s < subsets; ++s)
    {
        // Stores subset information while it is being loaded
        Geometry* geometry = NULL;
        SubsetIndex subset;
        DWORD vertices, indices;

        // Read the subset index information and the geometry data from the file
        if (APP_ERROR(!bufferReader.read(&subset, sizeof(subset)) ||
                       !bufferReader.read(&vertices, sizeof(vertices)) ||
                       !bufferReader.read(&indices, sizeof(indices)))
           ("Error while reading mesh data"))
        {
            // We were unable to load this mesh
            failed = true;

            // Exit the subset loop
            break;
        }

        // Create geometry for this mesh subset's data
       if (APP_FATAL(FAILED(AllocateGeometry(vertices, indices, &geometry)))
            ("Out of memory while allocating subset %i of %s (%i vertices/%i indices)",
              subset, getPathString().c_str(), vertices, indices))
       {
           // Couldn't load the mesh
           failed = true;
           
           // Exit this loop
           break;
       }

        // Load the geometry buffers
       if (APP_ERROR(!bufferReader.read(geometry->pVertices, sizeof(GeometryVertex) * vertices) ||
                      !bufferReader.read(geometry->pIndices, sizeof(GeometryIndex) * indices))
          ("Couldn't load geometry vertex/index data for subset %i of %s",
            subset, getPathString().c_str()))
       {
           // Unable to load
           failed = true;

           // Quit the loading process
           break;
       }

       // Add the data we just loaded as a mesh subset
       subsetGeometry->insert(SubsetGeometry::value_type(subset, geometry));
    }

    // If we failed, get rid of anything that managed to be loaded
    if (failed)
        DeallocateGeometry(subsetGeometry);

    // Return whether or not this operation succeeded
    return !failed;
}