/***************************************************************************/
/*                                                                         */
/* File: main.cpp                                                          */
/* This is the entry point for our program, an all where going to do in    */
/* here is set up the windows stuff!  Its going to more or less stay the   */
/* same.  So this file will usually be one of those copy and past jobs...  */
/* .. all we do is initilize a window and then set up the message          */
/* processing function, and when "no" messages are benig processed we call */
/* our mainloop() function which is in game.cpp.  This is so that we can   */
/* just keep changing that one file (game.cpp) and leave the windows code  */
/* alone.  One other thing, I put the directX intilisation and de-init     */
/* code in another seperate file... init.cpp I called it... the directX    */
/* init code is called at the start of our program, and the de-init code   */
/* is called at the end.. just before our window is destroyed.!  So this   */
/* in init.cpp is the same over and over again!  So we can just learn it   */
/* once and copy and past it in future.
/*                                                                         */
/***************************************************************************/
//direct3D.cpp
//First Step Tutorial to 3D programming with DirectX 8

#include <windows.h>
#include <stdio.h>

// These few lines are compiler directives that include the DirectX library's
// during linking.  You could instead inside visual studio goto Project->
// settings menu and under the "Link" tabe add the necessary librarys instead.
#pragma comment(lib, "D3d8.lib") //directX 8
#pragma comment(lib, "D3dx8.lib")
#include <d3dx8.h>


// Our directX once init/de_init code will be put in the "init.cpp" file.
#include "init.cpp"
// Any functions will be put in "game.cpp"
#include "game.cpp"

/***************************************************************************/
/*                                                                         */
/* Handle all messages for the main window here                            */
/*                                                                         */
/***************************************************************************/
long _stdcall MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    if(uMsg == WM_DESTROY)
    {
		// inside init.cpp we destroy any directx memory we allocated etc, 
		// tidy up before leaving.
		// TIDY UP DIRECTX HERE BEFORE EXITING HERE!!!!!!!!!!!!!!!!!!!!!!!!!
		de_init();
        PostQuitMessage(0);
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

/***************************************************************************/
/*                                                                         */
/* Program entry point.                                                    */
/*                                                                         */
/***************************************************************************/
int _stdcall WinMain(HINSTANCE i, HINSTANCE, char* k, int) 
{
    MSG msg;
	char szname[] = "DirectX3D";
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, 
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      szname, NULL };
    RegisterClassEx( &wc );
    HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW,
					szname, "Basic Bones Sample", 
					WS_OVERLAPPEDWINDOW,//for fullscreen make into WS_POPUP
					50, 50, 500,500, //for full screen GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
					GetDesktopWindow(), NULL, wc.hInstance, NULL);
    
    // Initilise or directX code here!
	// INIT OUR DIRECTX ONCE HERE AT THE START HERE!!!!!!!!!!!!!!!!!!!!!!!!!
    init(hWnd);

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);	


    // Message loop. Note that this has been modified to allow
    // us to execute if no messages are being processed.
    while(1)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
        {
            if (!GetMessage(&msg, NULL, 0, 0))
                break;
            DispatchMessage(&msg);
        }
        
        // Idle-time processing - call our loop function in game.cpp
		// CALLING OUR DIRECTX CODE IN GAME.CPP FROM HERE!!!!!!!!!!!!!!!!!
        mainloop();
    }
    return 0;
}

