Code:
////////////////////////////////////////////////////////////////////////
// Game Engine For the buliding of games
// this is classed as a header file and can be used in game production
// It is split into six areas, all seperateed with comments
///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// GAME ENGINE //
////////////////////////////////////////////////////////////////////////
#include <windows.h>
////////////////////////////////////////////////////////////////////////
// PART ONE:: DECLARE THE MAIN FUNCTIONS OF THE GAME ENGINE
//
/////////////////////////////////////////////////////////////////////////
BOOL GameInitialize(HINSTANCE hInstance);
void GameStart(HWND hWindow);
void GameEnd();
void GameActivate(HWND hWindow);
void GameDeactivate(HWND hWindow);
void GamePaint(HDC hDC);
void GameCycle();
//////////////////////////////////////////////////////////////////////////
// PART TWO:: CREATE THE GAME ENGINE CLASS
//
//////////////////////////////////////////////////////////////////////////
class GameEngine
{
protected:
// member varibles
static GameEngine* m_pGameEngine;
HINSTANCE m_hInstance;
HWND m_hWindow;
TCHAR m_szWindowClass[32];
TCHAR m_szTitle[32];
WORD m_wIcon, m_wSmallIcon;
int m_iWidth, m_iHeight;
int m_iFrameDelay;
BOOL m_bSleep;
public:
// constructor(s)/deconstructor
GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass,
LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth = 640,
int iHeight = 480);
virtual ~GameEngine();
// general methods
static GameEngine* GetEngine() { return m_pGameEngine; };
BOOL Initialize(int iCmdShow);
LRESULT HandleEvent(HWND hWindow, UINT msg, WPARAM wParam,
LPARAM lParam);
// accessor methods
HINSTANCE GetInstance() { return m_hInstance; };
HWND GetWindow() { return m_hWindow; };
void SetWindow(HWND hWindow) { m_hWindow = hWindow; };
LPTSTR GetTitle() { return m_szTitle; };
WORD GetIcon() { return m_wIcon; };
WORD GetSmallIcon() { return m_wSmallIcon; };
int GetWidth() { return m_iWidth; };
int GetHeight() { return m_iHeight; };
int GetFrameDelay() { return m_iFrameDelay; };
void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 /
iFrameRate; };
BOOL GetSleep() { return m_bSleep; };
void SetSleep(BOOL bSleep) { m_bSleep = bSleep; };
};
/////////////////////////////////////////////////////////////////////////////////
// PART THREE:: WinMain() functon == Makes calls to game engine functions and
// methods
/////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance,
PSTR szCmdLine, int iCmdShow)
{
MSG msg;
static int iTickTrigger = 0;
int iTickCount;
if (GameInitialize(hInstance))
{
// initialize the game engine
if (!GameEngine::GetEngine()->Initialize(iCmdShow))
return FALSE;
// enter the main message loop
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// process the message
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// make sure game engine is not sleeping
if (!GameEngine::GetEngine()->GetSleep())
{
// check the tick count to see if a game cycle has elapsed
iTickCount = GetTickCount();
if (iTickCount > iTickTrigger)
{
iTickTrigger = iTickCount +
GameEngine::GetEngine()->GetFrameDelay();
GameCycle();
}
}
}
}
return (int)msg.wParam;
}
// End the game
GameEnd();
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
// route all window messages to the game engine
return GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam);
}
//////////////////////////////////////////////////////////////////////////////////
// PART FOUR:: Game Engine constructor takes care of initializing Game Engine
// Member variables, deconstructot is left empty for future usage
//////////////////////////////////////////////////////////////////////////////////
GameEngine::GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass,
LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth, int iHeight)
{
// set the member variables for the game engine
m_pGameEngine = this;
m_hInstance = hInstance;
m_hWindow = NULL;
if (lstrlen(szWindowClass) > 0)
lstrcpy(m_szWindowClass, szWindowClass);
if (lstrlen(szTitle) > 0)
lstrcpy(m_szTitle, szTitle);
m_wIcon = wIcon;
m_wSmallIcon = wSmallIcon;
m_iWidth = iWidth;
m_iHeight = iHeight;
m_iFrameDelay = 50; // 20 FPS default
m_bSleep = TRUE;
}
GameEngine::~GameEngine()
{
}
/////////////////////////////////////////////////////////////////////////////
// PART FIVE:: Initialze method takes care of some of the work of
// WinMAIN()
/////////////////////////////////////////////////////////////////////////////
BOOL GameEngine::Initialize(int iCmdShow)
{
WNDCLASSEX wndclass;
// create the window class for the main window
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = m_hInstance;
wndclass.hIcon = LoadIcon(m_hInstance,
MAKEINTRESOURCE(GetIcon()));
wndclass.hIconSm = LoadIcon(m_hInstance,
MAKEINTRESOURCE(GetSmallIcon()));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = m_szWindowClass;
// register the window class
if (!RegisterClassEx(&wndclass))
return FALSE;
// calculate window size and position based on game size
int iWindowWidth = m_iWidth + GetSystemMetrics(SM_CXFIXEDFRAME) * 2,
iWindowHeight = m_iHeight + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 +
GetSystemMetrics(SM_CYCAPTION);
if (wndclass.lpszMenuName != NULL)
iWindowHeight += GetSystemMetrics(SM_CYMENU);
int iXWindowPos = (GetSystemMetrics(SM_CXSCREEN) - iWindowWidth) / 2,
iYWindowPos = (GetSystemMetrics(SM_CYSCREEN) - iWindowHeight) / 2;
// create the window
m_hWindow = CreateWindow(m_szWindowClass, m_szTitle, WS_POPUPWINDOW |
WS_CAPTION | WS_MINIMIZEBOX, iXWindowPos, iYWindowPos, iWindowWidth,
iWindowHeight, NULL, NULL, m_hInstance, NULL);
if (!m_hWindow)
return FALSE;
// show and update window
ShowWindow(m_hWindow, iCmdShow);
UpdateWindow(m_hWindow);
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// PART SIX:: HandleEvent() method handles and receives messages that
// are normally handled by WinProc()
/////////////////////////////////////////////////////////////////////////////
LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam,
LPARAM lParam)
{
// route window messages to game engine member functions
switch (msg)
{
case WM_CREATE:
// set the gamw window and start the game
SetWindow(hWindow);
GameStart(hWindow);
return 0;
case WM_ACTIVATE:
// activate deactivate the game and update the Sleep status
if (wParam != WA_INACTIVE)
{
GameActivate(hWindow);
SetSleep(FALSE);
}
else
{
GameDeactivate(hWindow);
SetSleep(TRUE);
}
return 0;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hWindow, &ps);
// paint the game
GamePaint(hDC);
EndPaint(hWindow, &ps);
return 0;
case WM_DESTROY:
// end the game and exit the application
GameEnd();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWindow, msg, wParam, lParam);
}