#include "CompostHeaders.hpp"

DWORD Compost::Number_Of_Worked_On_Textures;
DEFINE_TIMER(Compost, Composting_Time);
DWORD Compost::Number_Of_Used_128_Textures = 0;
DWORD Compost::Number_Of_Used_256_Textures = 0;

HGOSHEAP
	Compost::Heap = NULL;


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
static bool __stdcall Check_0() {return TerrainTextureLogistic::GetResolution() == 0;}
static bool __stdcall Check_1() {return TerrainTextureLogistic::GetResolution() == 1;}
static bool __stdcall Check_2() {return TerrainTextureLogistic::GetResolution() == 2;}
static bool __stdcall Check_3() {return TerrainTextureLogistic::GetResolution() == 3;}

static void __stdcall Activate_0() {TerrainTextureLogistic::SetResolution(0);}
static void __stdcall Activate_1() {TerrainTextureLogistic::SetResolution(1);}
static void __stdcall Activate_2() {TerrainTextureLogistic::SetResolution(2);}
static void __stdcall Activate_3() {TerrainTextureLogistic::SetResolution(3);}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//

MaterialEntry Compost::MaterialEntries[128];
int Compost::MaterialEntriesCount = 0;
int Compost::DarkFactor = 128;

int
	Feature_Texture::RecalculateSize()
{
	int dsize = 0;

	switch(flags & Compost::Feature_Texture::TextureTypeMask)
	{
		case Compost::Feature_Texture::TextureMode::FTT_555:
		case Compost::Feature_Texture::TextureMode::FTT_4444:
		case Compost::Feature_Texture::TextureMode::FTT_1_M:
		{
			dsize = depth*(width+2)*(height+2);
			dsize += depth*((width>>1)+2)*((height>>1)+2);
			dsize += depth*((width>>2)+2)*((height>>2)+2);
		
			dataSize = dsize;
		}
		break;
		case Compost::Feature_Texture::TextureMode::FTT_1:
		{
			dsize = depth*(width+2)*(height+2);
			dataSize = dsize;
		}	
		break;
		case Compost::Feature_Texture::TextureMode::FTT_1_1:
		{
			dsize = depth*(2+ (width>>3))*(2+height);
			dataSize = dsize;
		}
		break;
		default:
			STOP(("Unknown Compost Texture Format !!"));
		break;
	}

	return dsize;
}

const char*
	Feature::GetBlendModeName(Blend_Mode bm)
{
	static char name[1024];

	switch(bm)
	{
		case Blend_Mode::Paste:
			strcpy(name, "Paste");
		break;
		case Blend_Mode::Added:
			strcpy(name, "Added");
		break;
		case Blend_Mode::Multiply:
			strcpy(name, "Multiply");
		break;
		case Blend_Mode::Blend555_1:
			strcpy(name, "Blend555_1");
		break;
		case Blend_Mode::Blend555_2:
			strcpy(name, "Blend555_2");
		break;
		case Blend_Mode::Blend4444:
			strcpy(name, "Blend4444");
		break;
		case Blend_Mode::StretchMultiply:
			strcpy(name, "StretchMultiply");
		break;
		case Blend_Mode::Blend555_1_1:
			strcpy(name, "Blend555_1_1");
		break;
		case Blend_Mode::StretchPaste:
			strcpy(name, "StretchPaste");
		break;
		case Blend_Mode::Blend555_1_S:
			strcpy(name, "Blend555_1_S");
		break;
		default:
			strcpy(name, "Unknown");
	};

	return name;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Compost::InitializeClasses(NotationFile *startup_ini)
{
	AddDebuggerMenuItem("Libraries\\Graphics Options\\Terrain Texture Level 0", Check_0, Activate_0, NULL );
	AddDebuggerMenuItem("Libraries\\Graphics Options\\Terrain Texture Level 1", Check_1, Activate_1, NULL );
	AddDebuggerMenuItem("Libraries\\Graphics Options\\Terrain Texture Level 2", Check_2, Activate_2, NULL );
	AddDebuggerMenuItem("Libraries\\Graphics Options\\Terrain Texture Level 3", Check_3, Activate_3, NULL );

	Verify(!Heap);
	Heap = gos_CreateMemoryHeap("Compositing");
	Check_Pointer(Heap);
	gos_PushCurrentHeap(Heap);

	//
	//---------------------
	// Setup the statistics
	//---------------------
	//
#if !(defined(NO_STATS) && defined(NO_TIMERS))
	StatisticFormat( "" );
	StatisticFormat( "Compositing" );
	StatisticFormat( "===========" );
	StatisticFormat( "" );
#endif

#ifdef LAB_ONLY
	Number_Of_Used_128_Textures = Number_Of_Reserved_Textures;
	Number_Of_Used_256_Textures = Number_Of_Reserved_Textures/2;
#endif

#if !defined(NO_STATS)
	AddStatistic( "Textures in work",	"#",		gos_DWORD, &Number_Of_Worked_On_Textures, Stat_AutoReset );
	AddStatistic( "128 Textures used",	"#",		gos_DWORD, &Number_Of_Used_128_Textures, Stat_AutoReset + Stat_GraphMax);
	AddStatistic( "256 Textures used",	"#",		gos_DWORD, &Number_Of_Used_256_Textures, Stat_AutoReset + Stat_GraphMax);
#endif

	Initialize_Timer(Composting_Time, "Compost time");

	if (startup_ini)
	{
		Check_Object(startup_ini);
		Page *page = startup_ini->FindPage("Graphics Options");
		if (page)
		{
			Check_Object(page);
			int compositing;
			if (page->GetEntry("Compositing", &compositing))
				TerrainTextureLogistic::SetResolution(compositing);
		}
	}

	gos_PopCurrentHeap();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Compost::TerminateClasses(NotationFile *startup_ini)
{
	if (startup_ini)
	{
		Check_Object(startup_ini);
		Page *page = startup_ini->SetPage("Graphics Options");
		Check_Object(page);
		int compositing = TerrainTextureLogistic::GetResolution();
		page->SetEntry("Compositing", compositing);
	}

	Check_Pointer(Heap);
	gos_DestroyMemoryHeap(Heap);
	Heap = NULL;
}
