// Beginning Game Programming, Second Edition
// winmain.cpp - Windows framework source code file
#include <time.h>
#include <stdio.h>
#include "game.h"
// Cross platform compatible
#include "CPTimer.h"
// Mouse state information.
bool LMBDown = false;
int mouseX = 0, mouseY = 0;
//timer
CPTimer timer;
double times;
unsigned long startTick;
// flag to show the FPS on title bar
bool show_FPS = true;
// flag to enable/disable the FRAME RATE cap
bool cap = true;
//function prototypes
bool InitializeObjects();
//window event callback function
LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
//call the "front-end" shutdown function
Game_End(hWnd);
//tell Windows to kill this program
PostQuitMessage(0);
return 0;
break;
case WM_LBUTTONDOWN:
LMBDown = true;
break;
case WM_LBUTTONUP:
LMBDown = false;
break;
case WM_MOUSEMOVE:
mouseX = LOWORD (lParam);
mouseY = HIWORD (lParam);
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//create the window class structure
WNDCLASSEX wc;
wc
.cbSize
= sizeof(WNDCLASSEX
);
//fill the struct with info
//wc.style = CS_HREDRAW | CS_VREDRAW;
wc.style = CS_CLASSDC;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0L;
wc.cbWndExtra = 0L;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = APPTITLE;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//set up the window with the class info
return RegisterClassEx(&wc);
}
//
static int win_screen_x = 0;
static int win_screen_y = 0;
static int win_fullscreen = 0; // false
void setVar(string token, string value)
{
int iValue = atoi(value.c_str());
if (token == "SCREEN_X")
win_screen_x = iValue;
if (token == "SCREEN_Y")
win_screen_y = iValue;
if (token == "SCREEN_FULL")
win_fullscreen = iValue;
}
bool loadVars(string filename)
{
// open the file
ifstream input_file(filename.c_str());
if (! input_file.is_open())
{
cout << "Unable to open file: " << filename << endl;
return false;
}
// cout << endl << "Loading variables from config file ..." << endl;
// setup variables
string inputText;
string tToken = "" , tValue = "";
string lastToken;
//
const int MAX_TOKENS = 99;
string tokens[MAX_TOKENS];
string values[MAX_TOKENS]; // string (int) value of each token
int currChar = 0;
int tokensFound = 0;
int tLoc = 0, tVLoc = 0;
char tChar;
int lineCnt = 0;
bool blankLine = true;
bool declaringVar = false;
// 2-D array for variables
// read from file
while (! input_file.eof() )
{
// get it one line at a time
getline (input_file, inputText);
lineCnt++;
currChar = 0;
tLoc = 0;
blankLine = true;
// get tokens
//token value
// while not at end of the string
{
tChar = inputText[currChar];
// parse string
// is it an expression ? then check for prior declaration of variable name
if ( tChar == '/' )
currChar
= strLen; // exit loop
// if it is a letter or _ character.
if (isLetter(tChar) || (tChar == '_'))
{
// its a letter, so we assume its a token string
tToken += tChar;
tLoc++;
blankLine = false;
}
else if (isNumber(tChar))
{
// its a number, so we assume its a value
tValue += tChar;
tVLoc++;
blankLine = false;
}
else
{
// white space or special tokens
// handle special characters
if ( tLoc > 0 )
{
// close out the token
tokens[tokensFound] = tToken;
cout << tToken;
lastToken = tToken;
tToken = ""; // reset for next token
tLoc = 0;
blankLine = false;
}
if ( tVLoc > 0 )
{
// close out the value token
values[tokensFound] = tValue;
cout << " = " << tValue << endl;
setVar(tokens[tokensFound],values[tokensFound]); // override default
tokensFound++; // increment for next token
tValue = ""; // reset for next token
tVLoc = 0;
blankLine = false;
}
}
// next character
currChar++;
} // while ... parsing each line
} // while ... not end of file
input_file.close();
return true;
}
//
//entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HWND hWnd;
// Load the Config data from file
bool read_from_config = loadVars("setup.cfg");
// int screen_x = getToken("SCREEN_WIDTH"); // this should be the value from the config file
// int screen_y = getToken("SCREEN_HEIGHT");
// register the class
MyRegisterClass(hInstance);
//set up the screen in windowed or fullscreen mode?
DWORD style;
if (FULLSCREEN)
style = WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP;
else
style = WS_OVERLAPPED;
/*
// debug, set app tile to current value of SCREEN_HEIGHT/WIDTH variables
// int to string
ostringstream buffer;
// buffer << SCREEN_WIDTH << "x" << SCREEN_HEIGHT << "," << FULLSCREEN << " " << read_from_config << ":" << screen_x << "x" << screen_y;
buffer << win_screen_x << "x" << win_screen_y << "," << win_fullscreen << " " << read_from_config << ":" << screen_x << "x" << screen_y;
string screen_res = buffer.str();
LPCSTR AppTitle = screen_res.c_str();
*/
//create a new window
hWnd = CreateWindow(
APPTITLE, //window class
APPTITLE, // AppTitle, //APPTITLE, //title bar
style, //window style
//CW_USEDEFAULT, //x position of window
//CW_USEDEFAULT, //y position of window
0,
0,
win_screen_x, //SCREEN_WIDTH, //width of the window
win_screen_y, //SCREEN_HEIGHT, //height of the window
GetDesktopWindow(), //parent window
NULL, //menu
hInstance, //application instance
NULL); //window parameters
//was there an error creating the window?
if (!hWnd)
return FALSE;
//display the window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
//initialize the game
if (!Game_Init(hWnd))
{
MessageBox(hWnd, "Error initializing the game", "Error", MB_OK);
return 0;
}
if(!InitializeObjects()) return false;
//SetCursor(LoadCursor(NULL, IDC_ARROW));
ShowCursor(TRUE);
CPTimer fps,update;
// keep track of the current frame
int frame = 0,c_frame = 0;
//start the frame timer
fps.start();
double start = fps.get_ticks();
update.start();
// main message loop
int done = 0;
while (!done)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//look for quit message
if (msg.message == WM_QUIT)
done = 1;
//decode and pass messages on to WndProc
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// if we want to cap the frame rate
if ( cap == true )
{
if ((fps.get_ticks() - start) >= (1000 / FRAMES_PER_SECOND))
{
start = fps.get_ticks();
// increment the frame counter
frame++;
//process game loop (else prevents running after window is closed)
Game_Run(hWnd);
}
}
else
{
// increment the frame counter
frame++;
//process game loop (else prevents running after window is closed)
Game_Run(hWnd);
}
}
if (show_FPS)
if ( update.get_ticks() > 1000 )
{
//The frame rate as a string
std::stringstream caption;
//Calculate the frames per second and create the string
caption << "BugHunt 2942AD (DirectX) ~Avg FPS: " << frame / ( fps.get_ticks() / 1000.f );
//Reset the caption
SetWindowText(hWnd,caption.str().c_str());
// reset the counter for updates
update.start();
}
}
return msg.wParam;
}
bool InitializeObjects()
{
timer.start();
return true;
}