#include <MFCPlatform\MFCPlatform.hpp>




//
// Process any messages before GameOS sees them
//
LRESULT CALLBACK WinProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	return GameOSWinProc( hWnd, uMsg, wParam, lParam );
}






int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow )
{
	HWND		Window;
	WNDCLASS	wc;

//
// Create a window
//
	nCmdShow=nCmdShow;
	hPrevInst=hPrevInst;

	wc.style = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
	wc.lpfnWndProc = (WNDPROC) WinProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = NULL;
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
	wc.lpszMenuName = "Test MFC";
	wc.lpszClassName = "Test MFC";

	RegisterClass(&wc);

	RECT WindowRect;

	WindowRect.left=0;
	WindowRect.top=0;
	WindowRect.right=640;
	WindowRect.bottom=480;
	AdjustWindowRect( &WindowRect,WS_OVERLAPPED,0 );

	Window = CreateWindow( "Test MFC", "Test MFC",
							WS_OVERLAPPEDWINDOW,
							CW_USEDEFAULT, CW_USEDEFAULT,
							WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top,
							NULL, NULL, hInstance, NULL);

	ShowWindow( Window, SW_SHOWDEFAULT );


//
// Init GameOS with window created
//
	InitGameOS( hInstance, Window, lpCmdLine );


//
// Now deal with windows messages (MFC would normally do this)
//
	MSG	msg;
	DWORD Terminate=0;

	__try
	{
		while( !Terminate )
		{
			while( PeekMessage(&msg,NULL,0,0,PM_REMOVE) )
			{
				if( msg.message==WM_QUIT ) 
					break;

				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
		
			Terminate=RunGameOSLogic();
		}
	}
	__except( ProcessException(GetExceptionInformation()) )
	{
	}

//
// Application can exit any game engine code here
//
	if( Environment.TerminateGameEngine )
		Environment.TerminateGameEngine();

//
// Exit app
//
	ExitGameOS();

	return 0;
}






DWORD	Seed;

//
//
//
void __stdcall UpdateRenderers()
{
//
// Always use the same random numbers
//
	gos_srand( Seed );
//
// Make viewport the whole screen (0,0 top left), clear back buffer and z buffer
//
	gos_SetupViewport( 1,1.0,1,0x00304000, 0.0, 0.0, 1.0, 1.0 );
//
// Draw 1000 triangles in the middle of the screen
//
	gos_VERTEX v[4];
	memset( v,0,sizeof(v) );
	v[0].rhw	= 1.0;
	v[0].argb	= 0xffff0000;
	v[1].rhw	= 1.0;
	v[1].argb	= 0xff00ff00;
	v[2].rhw	= 1.0;
	v[2].argb	= 0xff0000ff;

	for( int t0=0; t0<1000; t0++ )
	{
		float xo=(float)(192+(gos_rand()&255));
		float yo=(float)(112+(gos_rand()&255));
		
		v[0].x		= xo;
		v[0].y		= yo;

		v[1].x		= xo+20.0f;
		v[1].y		= yo;

		v[2].x		= xo+10.0f;
		v[2].y		= yo+25.0f;

		gos_DrawTriangles( &v[0], 3 );
	}
}


void __stdcall DoGameLogic()
{
//
// Make sure all random numbers on UpdateRenders are the same
//
	Seed=gos_rand();
}



//
// Setup the GameOS structure
//
void __stdcall GetGameOSEnvironment( char* CommandLine )
{
	CommandLine=CommandLine;
	Environment.applicationName			= "Test MFC";
	Environment.screenWidth				= 640;
	Environment.screenHeight			= 480;
	Environment.bitDepth				= 16;

	Environment.DoGameLogic				= DoGameLogic;
	Environment.UpdateRenderers			= UpdateRenderers;
}

