#include <Stuff\StuffHeaders.hpp>
#include <DLLPlatform\DLLPlatform.hpp>
#include <Compost\CompostHeaders.hpp>
#include "ImageLib\image.h"

void __stdcall InitializeGameEngine()
{
	//
	//---------------------
	// Initialize libraries
	//---------------------
	//
	Stuff::InitializeClasses();

	Compost::TexturePool::Instance = new Compost::TexturePool;
	Register_Pointer(Compost::TexturePool::Instance);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void __stdcall DoGameLogic()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void __stdcall UpdateDisplay()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void __stdcall TerminateGameEngine()
{
	//
	//-------------------
	// Turn off libraries
	//-------------------
	//
	Unregister_Pointer(Compost::TexturePool::Instance);
	delete Compost::TexturePool::Instance;

	Stuff::TerminateClasses();
}

//
// Setup the GameOS structure
//
void __stdcall GetGameOSEnvironment( char* CommandLine )
{
	CommandLine = CommandLine;
	Environment.applicationName			= "CompostTest";
	Environment.directoryPath			=	"";
	Environment.screenWidth				= 640;
	Environment.screenHeight			= 480;
	Environment.fullScreen				= FALSE;
	Environment.bitDepth				= 16;
	Environment.debugLog				= "CompostTest.txt";
	Environment.spew					= "";			//"GameOS*";

	Environment.UpdateRenderers			= UpdateDisplay;
	Environment.DoGameLogic				= DoGameLogic;
	Environment.InitializeGameEngine	= InitializeGameEngine;
	Environment.TerminateGameEngine		= TerminateGameEngine;

	Environment.Renderer				= 0;
	Environment.FullScreenDevice		= 0;
	
	Environment.AntiAlias				= 0;			// true/false - Enable full screen antialiasing
	
	//
	// Texture infomation
	//
	Environment.Texture_S_256			= 0;
	Environment.Texture_S_128			= 0;
	Environment.Texture_S_64			= 0;
	Environment.Texture_S_32			= 0;
	Environment.Texture_S_16			= 0;
	
	Environment.Texture_K_256			= 0;
	Environment.Texture_K_128			= 0;
	Environment.Texture_K_64			= 0;
	Environment.Texture_K_32			= 0;
	Environment.Texture_K_16			= 0;
	
	Environment.Texture_A_256			= 0;
	Environment.Texture_A_128			= 0;
	Environment.Texture_A_64			= 0;
	Environment.Texture_A_32			= 0;
	Environment.Texture_A_16			= 0;
}

int
	__cdecl main(int argc, char *argv[])
{
	InitGameOS(NULL, NULL, "blabla");
	InitializeGameEngine();

/*	for(int i=1;i<argc;i++)
	{
		if(NULL!=strstr(argv[i], "-bsp") || NULL!=strstr(argv[i], "/bsp"))
		{
//			doBSP = true;
		}
	}
*/
	Compost::TexturePool::Instance->SetTexturePath("Assets\\Graphics");

Image tmpimg;
tmpimg.LoadTga("Assets\\Graphics\\Test.tga");




	Compost::FeaturePool *FPool = new Compost::FeaturePool();
	Register_Pointer(FPool);
	
	Check_Object(Stuff::FileStreamManager::Instance);
	Stuff::FileStream *fstr = new Stuff::FileStream;
	Register_Object(fstr);

	fstr->Open("Assets\\Graphics\\Test.fgd", Stuff::FileStream::ReadOnly);
	FPool->LoadIndex(fstr);

	Compost::FeatureGrid *FGrid = new Compost::FeatureGrid(fstr, FPool);
	Register_Pointer(FGrid);

	Unregister_Object(fstr);
	delete fstr;

	BYTE *dest_pic = new BYTE [256*256*4];


	tmpimg.MaskTo(RGBMask(0xff0000,0x00ff00,0x0000ff));
	BYTE *dat=tmpimg.Lock();
	
	int i, j, xp, yp, count=0;
	for(i=0;i<FGrid->GetRows();i++)
	{
		for(j=0;j<FGrid->GetColumns();j++)
		{
			FGrid->ComposeGrid(j, i, dest_pic);

//----------------------------------------------------
// Verify that the produced picture is the correct one
//----------------------------------------------------

			for(yp=0;yp<256;yp++)
			{
				for(xp=0;xp<256;xp++)
				{
					int dpc = (yp*256+xp)*4;
					int dc = ( (j*256+yp)*256*FGrid->GetColumns()+i*256+xp)*3;

					Verify(dest_pic[dpc]==dat[dc]);
					Verify(dest_pic[dpc+1]==dat[dc+1]);
					Verify(dest_pic[dpc+2]==dat[dc+2]);
				}
			}
		}
	}
	tmpimg.UnLock();

	for(int k=0;k<16;k++)
	{
		for(i=0;i<FGrid->GetRows();i++)
		{
			for(j=0;j<FGrid->GetColumns();j++)
			{
				FGrid->ComposeGrid(j, i, dest_pic);
			}
		}
	}


	Unregister_Pointer(FGrid);
	delete FGrid;

	Unregister_Pointer(FPool);
	delete FPool;

	TerminateGameEngine();
	ExitGameOS();
	return 0;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Compost::FetchTextureData(Compost::Feature_Texture *tex, int index, int type)
{
	return;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Compost::FetchTextureData(Compost::Feature_Texture *tex, const char *name, int type)
{
	MString fname(name);
	fname += ".bid";

	HGOSFILE hfile;
	gos_OpenFile(&hfile, fname, READONLY);

	int size =	(tex->width+2)*(tex->height+2)
				+ (tex->width/2+2)*(tex->height/2+2)
				+ (tex->width/4+2)*(tex->height/4+2);

	if(type!=Compost::Feature_Texture::FTT_1)
	{
		size *= 2;
	}
	tex->data = new BYTE [size];

	gos_ReadFile(hfile, tex->data, size);
	gos_CloseFile(hfile);

	return;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Compost::DestroyTextureData(Compost::Feature_Texture *tex)
{
	delete [] tex->data;
}
