/*
	Trig Wars.
	Copyright (c) 2008, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//=====================================================================================================================
/// @brief		Main entry point for the program. Program startup.
//=====================================================================================================================

#include "Precompiled.h"
#include "Windows\Window.h"
#include "Windows\Dialogs.h"
#include "Graphics\Display.h"
#include "Game\GameCore.h"

//=====================================================================================================================
/// @brief		Program entry point. This is where the program begins
/// @param		this_instance - handle to the program module
/// @param		prev_instance - not used on Win32 or later
/// @param		command_line - pointer to the command line arguments
/// @param		show_command - indication for what way the window should present itself
/// @post		There should be no leaked ReferencedObject derived objects
/// @post		There should be no leaked 
//=====================================================================================================================

// Visual Studio: Don't show this warning:

#pragma warning(disable:4100)

int WINAPI WinMain
( 
  HINSTANCE		this_instance	, 
  HINSTANCE		prev_instance	, 
  LPSTR			command_line	, 
  int			show_command 
)
{
	// Uncomment to break at a particular memory allocation number

	#ifdef USE_DEBUG_CONSOLE
	
		// Initialise the console:
	
		DebugConsole::Create();
		DebugConsole::SetPosition(0,0);
		DebugConsole::SetSize(Game::Window::GetScreenSize().x,200);
												
	#endif
	
	// Initialise for leak checking:
	
	#ifdef DEBUG
	
		Game::ReferencedObject::DebugInit();
	
	#endif
	
	// Initialise the high resolution timer:
	
	Game::HighResolutionTimer::Initialize();
	
	// Ask the user do they want to run fullscreen:
	
	bool fullscreen = Game::Dialogs::ShowQuestionBox
	(
		0,
		"Run the game in fullscreen?\nPeformance may be severely reduced on some systems in windowed mode." , 
		"Fullscreen mode"
	);
	
	// Store window here:
	
	Game::Window * window = 0;
	
	// See if true:
	
	if ( fullscreen == false )
	{
		// Make the application window:
		
		window = new Game::Window
		( 
			Game::GameCore::GAME_TITLE, false , Game::Vec2u(800,600)
		);
	}
	else
	{
		// Make the application window:
		
		window = new Game::Window
		( 
			Game::GameCore::GAME_TITLE, true , Game::Window::GetScreenSize()
		);	
	}
	
	// Set window cursor:
	
	if ( fullscreen == true )
	{	
		Game::Mouse::SetMouseCursor( window , Game::Mouse::CURSOR_NONE );
	}
	
	// Only run the game if the display setup correctly:
	
	if ( window->GetDisplay()->IsValid() )
	{	
		// Make the game:
		
		Game::GameCore * game = new Game::GameCore( window );
		
		// Give the main window focus:
		
		window->GiveFocus();
		
		// Run the window until it is closed:
		
		while ( window->Exists() )
		{
			// Request refresh:
			
			window->RequestRepaint();
			
			// Run it:
			
			window->Run();
			
			// Run the console for a bit:
			
			#ifdef USE_DEBUG_CONSOLE
			
				DebugConsole::RunPendingCommands();
			
			#endif
		}
		
		// Done with the game:
		
		game->RemoveReference();
	}
	else
	{
		// Could not setup a display:
		
		Game::Errors::IssueWarning( "Could not setup a DirectX9 display. Now exiting.");
	}
	
	// Done with the window:
	
	delete window;	
	
	// Shutdown the high resolution timer
	
	Game::HighResolutionTimer::Shutdown();
	
	// Shutdown the console:
	
	#ifdef USE_DEBUG_CONSOLE
	
		DebugConsole::Shutdown();
		
	#endif
	
	// Shutdown reference counted object checks for leaks: this will break if there are leaks
	
	#ifdef DEBUG
	
		Game::ReferencedObject::DebugShutdown();
	
	#endif	
	
	// Check for native c++ leaks:
	
	#ifdef DEBUG
	
		ENSURE( _CrtDumpMemoryLeaks() == 0 , "There must be no memory leaks" )
	
	#endif
	
	// Return zero to indicate successful excution:
	
	return 0;
}

// Visual Studio: Renable this warning:

#pragma warning(default:4100)
