//
// Asura_PSP_TextureManagement.h
//
// Created by KJFL - Tuesday, February 10, 2004
//
// Copyright (C) 2004 Rebellion, All Rights Reserved.
//

#ifdef ASURA_USE_PRAGMA_ONCE
#pragma once
#endif

#ifndef __ASURA_PSP_TEXTUREMANAGEMENT_H__
#define __ASURA_PSP_TEXTUREMANAGEMENT_H__

#include "Animation/Asura_Animation.h"
#include "Core/Memory/Asura_ResourceAllocator.h"
#include "Debugging/Asura_Assert.h"

#include "Rendering/Asura_PSP_TextureHeader.h"
#include "Rendering/Asura_PSP_VRAMManagement.h"


class Asura_PSP_Texture : public Asura_PSP_TextureHeader
{
	public:

		Asura_PSP_Texture()
		{
			m_pTextureBase = 0;
			m_pClutBase = 0;
			m_iVRAMHandler = Asura_PSP_VRAMManagement::ASURA_PSP_VRAM_INVALID; 
			m_uTextureTotalSize = 0; 
		}

		~Asura_PSP_Texture()
		{
			Asura_ResourcePushType(ASURA_RESOURCE_TEXTURE);
			{
				Asura_ResourceDeallocateMemoryBlock( m_pTextureBase );
				Asura_ResourceDeallocateMemoryBlock( m_pClutBase );
			}
			Asura_ResourcePopType();
		}

		void CalculateTotalTextureSize()
		{
			u_int uPixelSizeNibbles = 2;
			u_int uWidth = m_usWidth;
			u_int uHeight = m_usHeight;

			switch (m_ucTextureFormatID)
			{
				default:
				{
					Asura_Assertf( false, ("Unknown texture format id <%d>", m_ucTextureFormatID) );
					return;
				}
				case ASURA_PSP_TEXTUREFORMAT_8888:
				{
					uPixelSizeNibbles = 8;
					break;
				}
				case ASURA_PSP_TEXTUREFORMAT_IDX8:
				{
					uPixelSizeNibbles = 2;
					break;
				}
				case ASURA_PSP_TEXTUREFORMAT_IDX4:
				{
					uPixelSizeNibbles = 1;
					break;
				}
			}

			// Calculate total mipmap size
			m_uTextureTotalSize = 0; 

			for (u_char u=0; u<=m_ucNumberOfMipmaps; u++)
			{
				m_uTextureTotalSize += (uWidth * uHeight * uPixelSizeNibbles) >> 1; 
				uWidth >>= 1; 
				uHeight >>= 1; 
			}
		}

		int		FindPaletteIndex(const unsigned int uColor);
		void	TintPaletteEntriesWithColor(const unsigned int uAlphaKey, const unsigned int uColor);

		void*				m_pTextureBase;
		void*				m_pClutBase;
		int					m_iVRAMHandler;
		unsigned int		m_uTextureTotalSize; 

	private:


};

class Asura_PSP_TextureManagement
{
	public:

		static void RequestTextureIndex( const int iTextureIndex ); 
		
		static void RequestTextureData( const int iTextureIndex ); 
		static void RequestPaletteDataForTexture( const int iTextureIndex, const void* pPaletteData ); 

		static void RequestPaletteData( const unsigned int uNumberOfPaletteEntries, const void* pPaletteData ); 

		static void InvalidateTexturesInVRAM(); // (SB - 26/02/07) Remove cached textures from VRAM

		const static Asura_PSP_Texture* GetTexture( const int iTextureIndex ); 

	protected:

		static void SetTextureIndex( const int iTextureIndex ); 
		static void SetTextureData( const int iTextureIndex ); 
		static void SetPaletteData( const unsigned int uNumberOfPaletteEntries, const void* pPaletteData ); 
		
		static void UploadTexture( int iTextureIndex, bool bUploadPaletteDataFromTexture = true );

		static int		s_iLastTextureIndex; 
		static void*	s_pLastPaletteData; 

	friend class Asura_Render_States;
};


#endif//__ASURA_PSP_TEXTUREMANAGEMENT_H__
