/*
	Wheels Of Steel
	Copyright (c) 2006, 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.
*/

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Name:         Main
// Purpose:      Program entry point and manipulation of the GDI/Win32 window
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Leak checking

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#ifdef DEBUG

	// Include full information of the source of the leak:

	#ifndef _CRTDBG_MAP_ALLOC
		
		#define _CRTDBG_MAP_ALLOC
		
	#endif
	
	// Needed for this to work
	
	#include <cstdlib>
	#include <crtdbg.h>

	/// In debug mode this is the starting heap state
	
	_CrtMemState crt_beg_mem_state = {0}; 
	
	/// In debug mode this is the end heap state
	
	_CrtMemState crt_end_mem_state = {0};
	
	/// In debug mode this is the difference between the start and end heap states
	
	_CrtMemState crt_dif_mem_state = {0};

#endif

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Includes

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#include "Game.h"
#include "Menu_Main.h"
#include "Window.h"
#include <cassert>

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     main
Purpose:  program entry point
Params:   Standard 
Returns:  exit code

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

// No unreferenced param warning

#pragma warning(disable:4100)

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
	// Uncomment to break at a particular memory allocation number
	
	//_CrtSetBreakAlloc(12524);
	
	// Memory leak checking:
	
	#ifdef DEBUG
		
		// For CRT leak checking
		
		_CrtMemCheckpoint(&crt_beg_mem_state);	
	
	#endif

    // Create the game window

    Window window
    (
        "Wheels of steel (Version " GAME_VERSION ")",
        Game::RESOLUTION_X,
        Game::RESOLUTION_Y
    );
    
    window.center();
    
    // Ask the user does s/he want to run in fullscreen mode

    bool runFullscreen = window.showQuestionBox
    (
        "Do you want to run the game in fullscreen mode? \n"
        "Peformance may be degraded on some machines in fullscreen mode, on others it may increase.",
        "Wheels of steel (Version " GAME_VERSION ") : Run game in fullscreen mode."
    );

    // Set the game into fullscreen mode if answer was yes

    if ( runFullscreen )
    {
        // Launch into fullscren mode

        window.setFullscreen( true, Game::RESOLUTION_X, Game::RESOLUTION_Y );

        // Get rid of the cursor in fullscreen

        window.setCursor(Window::CURSOR_NONE);
    }

    // restore and center the window if not in fullscreen. set position to 0,0 if in fullscreen

    if ( ! runFullscreen )
    {
        window.restore();
        window.center();

        // Need to reset client size after restore: why ???

        window.setClientSize
        (
            Game::RESOLUTION_X,
            Game::RESOLUTION_Y
        );
    }
    else
    {
        window.restore();
        window.setPosition(0,0);
    }

    // Dont allow resizes

    window.setResizeable(false);

    // Start the main menu

    Menu_Main::display(&window);

    // Close the window if it still exists

    if ( window.exists() ) window.close();    
    
	// Debug leak checking
	
	#ifdef DEBUG
		
		// Check for CRT leaks: Get a memory snapshot
			
		_CrtMemCheckpoint(&crt_end_mem_state);			
			
		// Look for leaks:
		
		int leaks = _CrtMemDifference
		(
			& crt_dif_mem_state	,
			& crt_beg_mem_state	,
			& crt_end_mem_state	
		);
			
		// See if there are leaks:
			
		if ( leaks != 0 )
		{
			// Dump all leaks to the console
			
			_CrtMemDumpAllObjectsSince(&crt_beg_mem_state);
			
			// Trigger a breakpoint
			
			DebugBreak();
			
			// Assertion failed
			
			assert(leaks!=0);
		}
	
	#endif	    

    // Return 0 for successful execution

    return 0;
}

// Restore this warning to normal

#pragma warning(default:4100)
