// main.cpp : Defines the entry point for the application.
//
// include the Direct3D Library file
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
//#pragma comment(lib, "dsound.lib")
//#pragma comment (lib, "dinput8.lib")
//#pragma comment (lib, "dxguid.lib")
//#pragma comment(lib, "ddraw.lib")
//#pragma comment(lib, "dxguid.lib")
#define _CRT_SECURE_NO_WARNINGS
#define STRICT
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
//#define VC_EXTRALEAN
// Windows Header Files:
//#include <ddraw.h>
#include <windows.h>
#include <windowsx.h>
//#include <MMSystem.h>
#include <D3dx9.h>
#include <d3d9.h>
#include "Direct3D9.h"
#include "Vector2D.h"
#include "main.h"
#include "Timer.h"
#include "Game.h"
//#include "DirectDraw.h"
//#include "Singleton.h"
////////////////////////////////////////////////////////////////
//Global Functions Decarations
////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWindow,UINT msg, WPARAM wParam, LPARAM lParam);
bool m_fullScreen = false;
bool m_running;
Game* m_game;
Timer m_timer;
unsigned __int64 m_lastTime;
unsigned __int64 m_timerFrequency;
float m_timeCounter;
float m_frameTime;
float secPerCent;
////////////////////////////////////////////////////////////////
//Global Functions
////////////////////////////////////////////////////////////////
void CleanUp()
{
delete m_game;
Direct3D9::Destroy();
//DirectDraw::Destroy();
}
//////////////////////////////////////////////////////
//The render method for the program,
//everything that need to be rendered will be placed here.
//////////////////////////////////////////////////////
void
Render(HWND hWindow)
{
//cleans the screen
Direct3D9::GetInstance()->ClearSurface();
m_game->Render(Direct3D9::GetInstance()->GetD3dDevice());
Direct3D9::GetInstance()->GetD3dDevice()->Present(NULL,NULL,NULL,NULL);
//D3dx9::GetD3dDevice()->Present(0,0,0,0);
//
//DirectDraw::GetInstance()->ClearSurface(hWindow);
//m_game->Render(DirectDraw::GetInstance()->GetBackBuffer());
//DirectDraw::GetInstance()->PresentBackBuffer(hWindow);
}
///////////////////////////////////////////////////////////
//Update method that controls all things checked per
//program cycle
///////////////////////////////////////////////////////////
void
Update()
{
float timeNow = (float)m_timer.GetTime();
float timeDiff = timeNow -(float)m_lastTime;
m_lastTime = (unsigned __int64)timeNow;
float timeDiffFloat = timeDiff * secPerCent;
//DirectDraw::GetInstance()->RestoreSurfaces();
m_game->Update(timeDiffFloat);
}
////////////////////////////////////////////////////////////////
//This is the WinMain Function, Like the Main Function of a console application
//The WinMain Function is the entry point of a windows program,
//It is mainly used to set up the window then within a while loop,
//process the windows messages recieved by the system,
////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
//m_enviromentPosition = Vector2D(900,0);
//This is the name of the application
static TCHAR szAppName[] = TEXT("Direct3D9 Behaviour Systems");
//WNDCLASSEX structure contains window class information
WNDCLASSEX wndclass;
//HWND is the window handle
HWND hWindow;
//Is contains the latest windows message
MSG msg;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = NULL;
//This Registers the windows class with the system, if any parameter is invalid
//the program will terminate here
if(!RegisterClassEx(&wndclass))
return 0;
RECT rc;
// Calculate size of window based on desired client window size
rc.left = 0;
rc.top = 0;
rc.right = 800;
rc.bottom = 600;
if (m_fullScreen)
{
hWindow = CreateWindowEx(0, szAppName, szAppName, WS_POPUP,
0, 0,rc.right-rc.left, rc.bottom-rc.top,NULL, NULL, hInstance, NULL );
}
else
{
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
hWindow = CreateWindowEx(0, szAppName, szAppName, WS_OVERLAPPEDWINDOW,
100, 100,rc.right-rc.left, rc.bottom-rc.top,NULL, NULL, hInstance, NULL );
}
//Now show and update the window for the first time
ShowWindow(hWindow,iCmdShow);
UpdateWindow(hWindow);
m_running = true;
//DirectDraw::Create();
Direct3D9::Create();
//if (DirectDraw::GetInstance()->CreateDevice( hWindow ,800 , 600 ,m_fullScreen) != 1)
//{
// MessageBox( hWindow, "Failed to create surfaces", "Error", MB_OK );
// return 0;
//}
if (Direct3D9::GetInstance()->InitialiseDirect3D(hWindow) != 1)
{
MessageBox( hWindow, "Failed to create surfaces", "Error", MB_OK );
return 0;
}
InvalidateRect( hWindow, NULL, TRUE );
//we create a timer to force the game to update every 1/60th of a second
//otherwise there could be preformance issues as the buffers are being swapped
//too quickly
SetTimer(hWindow,Refresh,1000/200,NULL);
m_game = new Game(Direct3D9::GetInstance()->GetD3dDevice());
m_timerFrequency = m_timer.GetFrequency();
secPerCent = 1.0f / (float)m_timerFrequency;
m_lastTime = m_timer.GetTime();
//this is the main look of the windows application , while the window is open
//its processes and translates windows messages into something useable,
//then sends the message to the relivent location.
//when the program closes, the program exits the loop and the messages will end, resulting on the exit of this loop.
while(m_running)
{
while(PeekMessage(&msg, NULL, 0,0, PM_NOREMOVE))
{
BOOL bGetResult = GetMessage(&msg, NULL, 0, 0);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Update();
}
CleanUp();
return (int)msg.wParam;
}
//////////////////////////////////////////////////////////////////////////
//The WndProc method processes the messages sent through windows,
//each message contains WM_ and then the message, for example, if the user clicks the left mouse button
//the message WM_LBUTTONDOWN is sent.
//////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWindow,UINT msg,WPARAM wParam,LPARAM lParam)
{
//this switch statement processes the message sent, here you can program the code
//for each type of message's reaction
switch(msg)
{
//WM_PAINT is a render command
case WM_PAINT:
ValidateRect( hWindow, NULL );
return 0;
break;
//WM_LBUTTONDOWN is a message send every time the left mouse button is clicked over the Client area
case WM_TIMER:
switch(wParam)
{
case Refresh:
Render(hWindow);
break;
}
return 0;
break;
case WM_LBUTTONDOWN:
//m_BackGround->SetScrollVelocity(Vector2D(-m_BackGround->GetScrollVelocity().x,0));
return 0;
break;
//the WM_DESTROY message is the message sent when the program is going to terminate
case WM_DESTROY:
DestroyWindow( hWindow );
m_running = false;
PostQuitMessage(0);
return 0;
break;
}
//it returns default processing for any window message that an switch statement does not process
return DefWindowProc(hWindow,msg,wParam,lParam);
}