"Beginning Game Programming" - game engine
For christmas i received the book "beginning game programming" by michael morrison. Obviously this book is based upon the fact that you are a windows user, however the only peices of windows specific code are in the game engine shell that you use and build upon through out the book, the rest is just C++.
I was wondering whether it would be possible to translate the game engine into something that i could set up in xcode there are two files: GameEngine.cpp and GameEngine.h .
GameEngine.cpp
GameEngine.h
Since i have no knowledge of windows code if someone could very kindly make this code mac friendly i would be very greatful.
regards, skrew
I was wondering whether it would be possible to translate the game engine into something that i could set up in xcode there are two files: GameEngine.cpp and GameEngine.h .
GameEngine.cpp
Code:
//-----------------------------------------------------------------
// Game Engine Object
// C++ Source - GameEngine.cpp
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "GameEngine.h"
//-----------------------------------------------------------------
// Static Variable Initialization
//-----------------------------------------------------------------
GameEngine *GameEngine::m_pGameEngine = NULL;
//-----------------------------------------------------------------
// Windows Functions
//-----------------------------------------------------------------
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 the game engine isn't 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 Windows messages to the game engine
return GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam);
}
//-----------------------------------------------------------------
// GameEngine Constructor(s)/Destructor
//-----------------------------------------------------------------
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()
{
}
//-----------------------------------------------------------------
// Game Engine General Methods
//-----------------------------------------------------------------
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 the window size and position based upon the 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 the window
ShowWindow(m_hWindow, iCmdShow);
UpdateWindow(m_hWindow);
return TRUE;
}
LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Route Windows messages to game engine member functions
switch (msg)
{
case WM_CREATE:
// Set the game window and start the game
SetWindow(hWindow);
GameStart(hWindow);
return 0;
case WM_SETFOCUS:
// Activate the game and update the Sleep status
GameActivate(hWindow);
SetSleep(FALSE);
return 0;
case WM_KILLFOCUS:
// Deactivate the game and update the Sleep status
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);
}
GameEngine.h
Code:
//-----------------------------------------------------------------
// Game Engine Object
// C++ Header - GameEngine.h
//-----------------------------------------------------------------
#ifndef GAMEENGINE_H
#define GAMEENGINE_H
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include <windows.h>
//-----------------------------------------------------------------
// Windows Function Declarations
//-----------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
//-----------------------------------------------------------------
// Game Engine Function Declarations
//-----------------------------------------------------------------
BOOL GameInitialize(HINSTANCE hInstance);
void GameStart(HWND hWindow);
void GameEnd();
void GameActivate(HWND hWindow);
void GameDeactivate(HWND hWindow);
void GamePaint(HDC hDC);
void GameCycle();
//-----------------------------------------------------------------
// GameEngine Class
//-----------------------------------------------------------------
class GameEngine
{
protected:
// Member Variables
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)/Destructor
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; };
};
#endif
Since i have no knowledge of windows code if someone could very kindly make this code mac friendly i would be very greatful.
regards, skrew
It looks fairly windows-specific in its setting-up of the window, view, but I'd imagine that with a bit of work and knowledge you could easily port that to use some cross-platform windowing API like GLUT or SDL. Someone else here could give you more details.
hmmm, well I've been way too busy with RL stuff do any coding lately, but it appears that i have quite a bit more time on my hands in teh coming months and i was wondering if anyone could send me in the right direction to getting all that code into something mac compatible
Not really related:
Don't you just HATE that microsoft code crap? It's so... ugly. It's like they have no idea how to turn off the caps-lock key. Jeez.
Don't you just HATE that microsoft code crap? It's so... ugly. It's like they have no idea how to turn off the caps-lock key. Jeez.
I am unfamiliar with that book, but GameEngine.cpp is simple enough to port. Unfortunately though, it appears that GameEngine.cpp doesn't provide everything a game shell would need, so porting just that file would be unlikely to provide what you need for the rest of the book. For instance, it doesn't show how input and sound are handled and it doesn't show the rasterization method. I would just take a wild guess that it would be a standard blit to buffer scenario, but it could be heavily GDI based for all I know. If it's blit to buffer, then that can be ported by using OpenGL and rendering to texture. Again, unfortunately, this is likely a chicken or the egg situation. You are trying to learn the basics of game programming, but you need to know the basics of game programming on the Mac first to really have the skills to port from Windows to Mac. This is one of the main reasons that learning game programming on the Mac from the ground up is such a challenge!
AnotherJake is right. This seems like a fairly painful way to learn to program games on your Mac. The book may have much to offer, but for the hands-on learning you should probably look elsewhere.
Hopefully the iDevGames source code will make a triumphant return in the near future. If you want to start learning OpenGL graphics, try NeHe Productions website. Etc.
Hopefully the iDevGames source code will make a triumphant return in the near future. If you want to start learning OpenGL graphics, try NeHe Productions website. Etc.
Measure twice, cut once, curse three or four times.
Possibly Related Threads...
Thread: | Author | Replies: | Views: | Last Post | |
Beginning Game Programming - Choices | Evan Stallings | 8 | 11,819 |
Mar 28, 2013 03:07 PM Last Post: Evan Stallings |
|
Cocoa Game Programming Workshop | jeonghyunhan | 5 | 9,108 |
Mar 20, 2009 08:40 PM Last Post: SethWillits |
|
Starting a 2D game programming. How? | andrern2000 | 4 | 7,226 |
Dec 19, 2007 05:34 PM Last Post: BinarySpike |
|
Game programming resources for Java | JeroMiya | 3 | 6,241 |
Sep 28, 2007 04:06 PM Last Post: JavaGuru |
|
Game Engine | Zander | 34 | 19,768 |
Aug 5, 2005 09:55 AM Last Post: Zander |