#include "stdafx.h"
#include <d3dx9.h>
#include "Show3DObject.h"

//-----------------------------------------------------------------------------
// Name: Graphics Engine Includes
//-----------------------------------------------------------------------------
#include "Direct3D.h"
#include "GameEngine.h"
#include "GameDemoEngine.h"
//-----------------------------------------------------------------------------
// Name: Global Variables
// Desc: Direct3D and GameEngine Objects
//-----------------------------------------------------------------------------
CDirect3D   *d3d        = NULL;
CGameEngine *GameEngine = NULL;

const char szTitle[] = "3D View";
const char szWindowClass[] = "Simple3D Window";

//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	if (GameEngine && !GameEngine->ProcessMessage(hWnd, msg, wParam, lParam))
	{
	    switch( msg )
	    {
		    case WM_DESTROY:
			    PostQuitMessage( 0 );
				return 0;
			
			case WM_KEYDOWN:
				{
					switch( wParam )
					{
						case VK_ESCAPE:
							PostQuitMessage(0);
							break;
					}
				}
				break;
		}
	}

    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,
                      szWindowClass, NULL };
    RegisterClassEx( &wc );
    // Create the application's window
    HWND hWnd = CreateWindow( szWindowClass, szTitle,
                              WS_OVERLAPPEDWINDOW, 0,0,640, 480,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL );
    // Initialize Direct3D
	CProfile pf;
	pf.GetProfile(".\\game.ini");
	d3d = new CDirect3D(hWnd,pf.fullScreen, pf.screenWidth , pf.screenHeight);

	if( d3d->Initialized )
    {
			GameEngine = (CGameEngine *)new CGameDemoEngine(d3d);	
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );

            // 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
                    GameEngine->RenderScene(d3d);
            }        
    }

	// Freeing Memory
	delete d3d;
	delete GameEngine;
    UnregisterClass( szWindowClass, wc.hInstance );

    return 0;
}
