Listing 2.2 contains the source code for the game engine's WinMain() function.
Listing 2.2 The WinMain() Function in the Game Engine Makes Calls to Game Engine Functions and Methods and Provides a Neat Way of Separating Standard Windows Program Code from Game Code
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;
}
Although this WinMain() function is similar to those found in every Windows application, there is an important difference. The difference has to do with the fact that this WinMain() function establishes a game loop that takes care of generating game cycle events at a specified interval. The smallest unit of time measurement in a Windows program is called a tick, which is equivalent to one millisecond and is useful in performing accurate timing tasks. In this case, WinMain() counts ticks in order to determine when it should notify the game that a new cycle is in order. The iTickTrigger and iTickCount variables are used to establish the game cycle timing in WinMain().
The first function called in WinMain() is GameInitialize(), which gives the game a chance to be initialized. Remember that GameInitialize() is a game event function provided as part of the game-specific code for the game; therefore, it isn't a direct part of the game engine. A method that is part of the game engine is Initialize(), which is called to get the game engine itself initialized. From there, WinMain() enters the main message loop for the game program. The else part of the main message loop is where things get interesting. This part of the loop first checks to make sure that the game isn't sleeping and then uses the frame delay for the game engine to count ticks and determine when to call the GameCycle() function to trigger a game cycle event. WinMain() finishes up by calling GameEnd() to give the game program a chance to wrap up the game and clean up after itself.
The other standard Windows function included in the game engine is WndProc(), which is very simple because the HandleEvent() method of the GameEngine class is responsible for processing Windows messages:
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);
}
All WndProc() really does is pass along all messages to HandleEvent(), which might at first seem like a waste of time. However, the idea is to allow a method of the GameEngine class to handle the messages so that they can be processed in a manner that is consistent with the game engine.
Speaking of the GameEngine class, now that you have a feel for the support functions in the game engine, we can move right along and examine specific code in the GameEngine class. Listing 2.3 contains the source code for the GameEngine() constructor and destructor.
Initializing Variables
Listing 2.3 The GameEngine::GameEngine() Constructor Takes Care of Initializing Game Engine Member Variables, Whereas the Destructor is Left Empty for Possible Future Use
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()
{
}
The GameEngine() constructor is relatively straightforward in that it sets all the member variables for the game engine. The only member variable whose setting might seem a little strange at first is m_iFrameDelay, which is set to a default frame delay of 50 milliseconds. You can determine the number of frames (cycles) per second for the game by dividing 1,000 by the frame delay, which in this case results in 20 frames per second. This is a reasonable default for most games, although specific testing might reveal that it needs to be tweaked up or down. Keep in mind that you should always shoot for the highest frame rate (lowest frame delay) possible that allows your game to run smoothly; you don't want to see a game slowing down because it can't keep up with a high frame rate.
The Initialize() method in the GameEngine class is used to initialize the game engine. More specifically, the Initialize() method now performs a great deal of the messy Windows setup tasks, such as creating a window class for the main game window and then creating a window from the class. Listing 2.4 shows the code for the Initialize() method.
Listing 2.4 The GameEngine::Initialize() Method Handles Some of the Dirty Work that Usually Takes Place in 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 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;
}
This code is similar to the Skeleton program example found in Appendix C, and it should be familiar to you if you've done any Windows programming using the Win32 API. An important thing to note in this code is how it determines the game application window size, which is calculated based on the size of the game client area. The GetSystemMetrics() Win32 function is called to get various standard window sizes, such as the width and height of the window frame, as well as the menu height. The position of the game application window is then calculated so that the game is centered on the screen.
The window styles used to describe the main game window are WS_POPUPWINDOW, WS_CAPTION, and WS_MINIMIZEBOX, which result in a window that is not resizable and can't be maximized; however, it does have a menu and can be minimized.
The Initialize() method is a perfect example of isolating generic Windows program code and moving it into the game engine. Another example of this approach is the HandleEvent() method, which is shown in Listing 2.5.
-
game design team
We have emerged as the leading online community for game development of all levels. Our expertise encases all facets for writing PC , Mobile and Online of 2D and 3D Gaming programming and applications development, for instance by using the latest 3D engines, scripting languages and animation techniques, our experienced and qualified team deals with all kind of requirements, whether it be a beginner's choice or an expert gaming action. Most importantly, we endeavor to offer compelling solution and eminent support to our growing community of prospective players and customers.
Categories:
Subscribe to:
Post Comments (Atom)
0 Response for the "Source Code for the WinMain Function"
Post a Comment