/*  SSNES - A frontend for libretro.
 *  Copyright (C) 2010-2012 - Hans-Kristian Arntzen
 *  Copyright (C) 2011-2012 - Daniel De Matteis
 * 
 *  SSNES is free software: you can redistribute it and/or modify it under the terms
 *  of the GNU General Public License as published by the Free Software Found-
 *  ation, either version 3 of the License, or (at your option) any later version.
 *
 *  SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE.  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along with SSNES.
 *  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#ifndef SSNES360_RESOURCES_H
#define SSNES360_RESOURCES_H

//--------------------------------------------------------------------------------------
// Name tag for resources. An app may initialize this structure, and pass
// it to the resource's Create() function. From then on, the app may call
// GetResource() to retrieve a resource using an ascii name.
//--------------------------------------------------------------------------------------
struct RESOURCE
{
   unsigned long dwType;
   unsigned long dwOffset;
   unsigned long dwSize;
   char * strName;
};


// Resource types
enum
{
   RESOURCETYPE_USERDATA       = ( ( 'U' << 24 ) | ( 'S' << 16 ) | ( 'E' << 8 ) | ( 'R' ) ),
   RESOURCETYPE_TEXTURE        = ( ( 'T' << 24 ) | ( 'X' << 16 ) | ( '2' << 8 ) | ( 'D' ) ),
   RESOURCETYPE_CUBEMAP        = ( ( 'T' << 24 ) | ( 'X' << 16 ) | ( 'C' << 8 ) | ( 'M' ) ),
   RESOURCETYPE_VOLUMETEXTURE  = ( ( 'T' << 24 ) | ( 'X' << 16 ) | ( '3' << 8 ) | ( 'D' ) ),
   RESOURCETYPE_VERTEXBUFFER   = ( ( 'V' << 24 ) | ( 'B' << 16 ) | ( 'U' << 8 ) | ( 'F' ) ),
   RESOURCETYPE_INDEXBUFFER    = ( ( 'I' << 24 ) | ( 'B' << 16 ) | ( 'U' << 8 ) | ( 'F' ) ),
   RESOURCETYPE_EOF            = 0xffffffff
};


//--------------------------------------------------------------------------------------
// Name: PackedResource
//--------------------------------------------------------------------------------------
class PackedResource
{
protected:
   unsigned char * m_pSysMemData;        // Alloc'ed memory for resource headers etc.
   unsigned long m_dwSysMemDataSize;

   unsigned char * m_pVidMemData;        // Alloc'ed memory for resource data, etc.
   unsigned long m_dwVidMemDataSize;

   RESOURCE* m_pResourceTags;            // Tags to associate names with the resources
   unsigned long m_dwNumResourceTags;    // Number of resource tags
public:
   int m_bInitialized;                   // Resource is fully initialized
    HRESULT Create( const char * strFilename );
    void    Destroy();
    D3DResource* RegisterResource( D3DResource* pResource ) const
    {
        return pResource;
    }

    void * GetData( unsigned long dwOffset ) const
    {
        return &m_pSysMemData[dwOffset];
    }

    D3DResource* GetResource( unsigned long dwOffset ) const
    {
        return (( D3DResource* )GetData( dwOffset ) );
    }

    D3DTexture* GetTexture( unsigned long dwOffset ) const
    {
        return ( D3DTexture* )GetResource( dwOffset );
    }

    D3DArrayTexture* GetArrayTexture( unsigned long dwOffset ) const
    {
        return ( D3DArrayTexture* )GetResource( dwOffset );
    }

    D3DVertexBuffer* GetVertexBuffer( unsigned long dwOffset ) const
    {
        return ( D3DVertexBuffer* )GetResource( dwOffset );
    }

    void * GetData( const char * strName ) const;

    D3DResource* GetResource( const char * strName ) const
    {
        return ( ( D3DResource* )GetData( strName ) );
    }

    D3DTexture* GetTexture( const char * strName ) const
    {
        return ( D3DTexture* )GetResource( strName );
    }

    D3DArrayTexture* GetArrayTexture( const char * strName ) const
    {
        return ( D3DArrayTexture* )GetResource( strName );
    }

    D3DVertexBuffer* GetVertexBuffer( const char * strName ) const
    {
        return ( D3DVertexBuffer* )GetResource( strName );
    }

    PackedResource();
    ~PackedResource();
};

#endif
