#include <GameOS\GameOS.hpp>
#include <GameOS\ToolOS.hpp>
#include <GOSScript\GOSScriptHeaders.hpp>

#include <memory.h>
#include <string.h>

int l2r = -100;
int delta = 10;
int set = 0;

HGOSAUDIO hares = 0;
HGOSAUDIO hares2 = 0;
DWORD videoTexture = 0;
DWORD videoTexture2 = 0;
HGOSVIDEO video = 0;
HGOSVIDEO video2 = 0;
												  
int _stdcall ModifyPaneData(void * instance, int nargs, void * args[16]);
int _stdcall RunModal(void * instance, int nargs, void * args[16]);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall InitializeGameEngine()
{
	//
	//---------------------------
	// Start the Script Processor
	//---------------------------
	//
	gosScript_InitializeProcessor();

	gosScript_RegisterCallback("ModifyPaneData",&ModifyPaneData,GOSVAR_INT,0, 0);
	gosScript_RegisterCallback("RunModal",&RunModal, GOSVAR_INT,0, 0);

//	gosScript_ExecuteScript("assets\\scripts\\callbacktest.script",0x1000,0);

	gosVideo_CreateResourceAsTexture( &video, &videoTexture, "assets\\graphics\\skiing.avi" );
	gosVideo_CreateResourceAsTexture( &video2, &videoTexture2, "assets\\graphics\\vidtest2.avi" );
	gosVideo_SetPlayMode( video, gosVideo_Loop );	
	gosVideo_SetPlayMode( video2, gosVideo_Loop );	

	gosAudio_CreateResource(&hares, gosAudio_CachedFile, "Assets\\Sounds\\cuckoo[2].wav" );
	gosAudio_AssignResourceToChannel( 31, hares);
	gosAudio_SetChannelPlayMode(31,gosAudio_Loop);

}

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall UpdateDisplay()
{
	// let gosScript do it's thing, instead...
	gos_SetupViewport( 0,1.0,0,0,0.0,0.0,1.0,1.0 );

	gos_VERTEX verts[4];

	verts[0].x = 0;
	verts[0].y = 0;
	verts[0].z = 0.0;
	verts[0].rhw = 1.0;
	verts[0].argb =	0xffffffff;
	verts[0].frgb = 0;
	verts[0].u = 0.0f;
	verts[0].v = 0.0f;

	verts[1].x = 640;
	verts[1].y = 0;
	verts[1].z = 0.0;
	verts[1].rhw = 1.0;
	verts[1].argb =	0xffffffff;
	verts[1].frgb = 0;
	verts[1].u = 1.0f;
	verts[1].v = 0.0f;

	verts[2].x = 640;
	verts[2].y = 480;
	verts[2].z = 0.0;
	verts[2].rhw = 1.0;
	verts[2].argb =	0xffffffff;
	verts[2].frgb = 0;
	verts[2].u = 1.0f;
	verts[2].v = 1.0f;

	verts[3].x = 0;
	verts[3].y = 480;
	verts[3].z = 0.0;
	verts[3].rhw = 1.0;
	verts[3].argb =	0xffffffff;
	verts[3].frgb = 0;
	verts[3].u = 0.0f;
	verts[3].v = 1.0f;

	gos_SetRenderState(gos_State_Texture, videoTexture);

	gos_DrawQuads( verts, 4);


	verts[0].x = 0;
	verts[0].y = 0;
	verts[0].z = 0.0;
	verts[0].rhw = 1.0;
	verts[0].argb =	0xffffffff;
	verts[0].frgb = 0;
	verts[0].u = 0.0f;
	verts[0].v = 0.0f;

	verts[1].x = 64;
	verts[1].y = 0;
	verts[1].z = 0.0;
	verts[1].rhw = 1.0;
	verts[1].argb =	0xffffffff;
	verts[1].frgb = 0;
	verts[1].u = 1.0f;
	verts[1].v = 0.0f;

	verts[2].x = 64;
	verts[2].y = 48;
	verts[2].z = 0.0;
	verts[2].rhw = 1.0;
	verts[2].argb =	0xffffffff;
	verts[2].frgb = 0;
	verts[2].u = 1.0f;
	verts[2].v = 1.0f;

	verts[3].x = 0;
	verts[3].y = 48;
	verts[3].z = 0.0;
	verts[3].rhw = 1.0;
	verts[3].argb =	0xffffffff;
	verts[3].frgb = 0;
	verts[3].u = 0.0f;
	verts[3].v = 1.0f;

	gos_SetRenderState(gos_State_Texture, videoTexture2);

	gos_DrawQuads( verts, 4);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall TerminateGameEngine()
{
	//
	//--------------------------
	// Stop the Script Processor
	//--------------------------
	//
	gosScript_ShutdownProcessor();

	if (hares)
	{
		gosAudio_DestroyResource(&hares);
	}

}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall GetGameOSEnvironment(char* CommandLine)
{
	CommandLine = CommandLine;
	Environment.applicationName			= "Skunkworks";
	Environment.screenWidth				= 640;
	Environment.screenHeight			= 480;
	Environment.fullScreen				= 0;
	Environment.bitDepth				= 16;
	Environment.debugLog				= "";
	Environment.spew					= "Gameos_MemoryVerbose";
	Environment.memoryTraceLevel		= 5;
	Environment.TimeStampSpew			= 0;
	Environment.UpdateRenderers			= UpdateDisplay;
	Environment.DoGameLogic				= DoGameLogic;
	Environment.InitializeGameEngine	= InitializeGameEngine;
	Environment.TerminateGameEngine		= TerminateGameEngine;

	Environment.FullScreenDevice		= 0;
	Environment.AntiAlias				= 0;			// true/false - Enable full screen antialiasing
	Environment.Renderer				= 0;

	Environment.soundDevice				= 0;
	Environment.soundChannels			= 32;
																																				   
//
// Texture infomation
//

	Environment.Texture_S_256			= 1;
	Environment.Texture_S_128			= 1;
	Environment.Texture_S_64			= 1;
	Environment.Texture_S_32			= 1;
	Environment.Texture_S_16			= 1;

	Environment.Texture_K_256			= 1;
	Environment.Texture_K_128			= 1;
	Environment.Texture_K_64			= 1;
	Environment.Texture_K_32			= 1;
	Environment.Texture_K_16			= 1;

	Environment.Texture_A_256			= 1;
	Environment.Texture_A_128			= 1;
	Environment.Texture_A_64			= 1;
	Environment.Texture_A_32			= 1;
	Environment.Texture_A_16			= 1;

	BYTE Guid[]={ 0xc5,0xcd,0x64,0x12,0x79,0xab,0x4c,0x55,0xad,0xce,0xee,0x13,0x10,0xa4,0x30,0x50 };
	memcpy( Environment.NetworkGUID, Guid, 16 );			// Test Latency, join in progress is default game
	Environment.NetworkGame				= 1;
	Environment.NetworkMaxPlayers		= 8;
}


int _stdcall ModifyPaneData(void * , int , void * args[16])
{
	DWORD * startofData = (DWORD *) args[0];
	int width = (int) args[1];
	int height = (int) args[2];

	char ** arrayOfStrings = (char**) args[3];
	if (arrayOfStrings[2])
	{
		gos_Free(arrayOfStrings[2]);
	}
	arrayOfStrings[2] = (char *) gos_Malloc(128);
	strcpy(arrayOfStrings[2], "bite my ass!");

	if (arrayOfStrings[3])
	{
		gos_Free(arrayOfStrings[3]);
	}
	arrayOfStrings[3] = (char *) gos_Malloc(128);
	strcpy(arrayOfStrings[3], "my hole burns");

	for(int i = 0; i < width; i++)
	{
		for(int j = 0; j < height; j++)
		{
			float frand =  ((float)gos_rand() / 32767.0f) *3;
			int rand = (int) frand;
			switch(rand)
			{
				case 0:
					*startofData = 0xffff0000;
					break;
				case 1:
					*startofData = 0xff00ff00;
					break;
				case 2:
				    *startofData = 0xff0000ff;
					break;
			}
			startofData++;
		}
	}

	return true;
} 

int _stdcall RunModal(void * instance, int nargs, void * args[16])
{
	gos_NetStartGame("fart.script");
	return 0;
}
