/** 
 * \file Main.cpp
 * \brief Turtle Shepherd main file.
 * 
 * This file contains two obligatory windows application functions WinMain() and MsgProc().
 *
 * \author Petar Bajic, MPE (C) All Rights Reserved, Homepage: www.mystic-peanut.com
 * \date January, 16 2007.
 */

#include <Windows.h>

#include "Game.h"

CGame* game = NULL;
bool g_GameInitialized = false;
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	// Current mouse position
	float iMouseX = (float)LOWORD(lParam);
	float iMouseY = (float)HIWORD(lParam);
	
	//If game handled event - return.
	if(g_GameInitialized && (game != NULL) && game->OnEvent(hWnd,msg,wParam,lParam))
		return 0;

	switch( msg )
    {
        case WM_DESTROY:
			game->~CGame();
            PostQuitMessage( 0 );
            return 0;
		case WM_KILLFOCUS:
			{
				if(game != NULL)
				{
					game->m_GamePaused = true;
					return 0;
				}
			}
		case WM_SETFOCUS:
			{
				if(game != NULL)
				{
					game->m_GamePaused = false;
					return 0;
				}
			}
		case WM_KEYDOWN:
			if (wParam == VK_ESCAPE)
			{
				game->WriteSettingsToFile();
				game->~CGame();
	            PostQuitMessage( 0 );
		        return 0;
			}
	}
	return DefWindowProc( hWnd, msg, wParam, lParam );
}


//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      L"Turtle Shepherd", NULL };
	WCHAR szExePath[MAX_PATH];
	GetModuleFileName( NULL, szExePath, MAX_PATH );
	HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
	HICON hIcon = ExtractIcon( hInstance, szExePath, 0 ); 

	wc.style = CS_DBLCLKS;
	wc.hIcon = hIcon;
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName = NULL;

	RegisterClassEx( &wc );

	int screenWidth = 700;
	int screenHeight = 550;

    // Create the application's window
    HWND hWnd = CreateWindow( L"Turtle Shepherd", L"Turtle Shepherd",
                              WS_OVERLAPPEDWINDOW, 100, 100, screenWidth, screenHeight,
                              NULL, NULL, wc.hInstance, NULL );

	RECT rcCurrentClient;
	GetClientRect(hWnd, &rcCurrentClient);
	
	// Show the window
	ShowWindow( hWnd, SW_SHOWDEFAULT );
	
	//Display Loading... picture (Ah! To use directX functions we have to draw it after 3D initialization)

	// Initialize Direct3D and whole Game. Load all game objects.
	game = new CGame();
	if(game->Init(hWnd, rcCurrentClient.right, rcCurrentClient.bottom))
    {
		UpdateWindow( hWnd );

		g_GameInitialized = true;
		// Enter the message loop
		MSG msg;
		ZeroMemory( &msg, sizeof(msg) );
		while( msg.message!=WM_QUIT )
		{
			if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
			else
			{
				game->OnFrameMove();
				game->OnRender();
			}
		}
    }
	else
	{
		//popup error message somewhere inside the Game code, and here just clean exit.
		game->~CGame();
	}

    UnregisterClass( L"Turtle Shepherd", wc.hInstance );
    return 0;
}



