// Application.cpp: implementation of the CApplication class.
//
//////////////////////////////////////////////////////////////////////
#include "Application.h"
#include "timer.h"
//////////////////////////////////////////////////////////////////////
// Globals
RefCountPtr<CApplication> pApp;
char szProgName[] = "TableHockey3D";
char szClassName[] = "TableHockey";
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CApplication::CApplication()
: bRun(true),
bActive(true),
FramesPerSecond(0)
{
}
CApplication::~CApplication()
{
OutputDebugString("CApplication destructor\n");
}
bool CApplication::Init(HINSTANCE hInstance)
{
// store hInstance
hInst = hInstance;
pData = new CData();
pInput = new CInput();
pView = new CView();
pLog = new CLog();
return true;
}
int CApplication::Run()
{
// Ask The User Which Screen Mode They Prefer
if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
pView->bFullscreen = false;
else
pView->bFullscreen = true;
// initialize CView, must be done before CInput
if (!pView->Init())
return 0;
if (!pInput->Init())
return 0;
pLog->Log("Entering message loop...");
MSG msg;
// counter to decide when the FPS must be updated again
static iFrames = 0;
static iSeconds = 0;
const int FPS_DELAY = 500;
CTimer timer;
timer.init();
while(bRun) // Loop That Runs While done=FALSE
{
// measure time
timer.update();
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
bRun=false; // Stop program
}
else // If Not, Deal With Window Messages
{
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
// check the user input
pInput->Update();
// here you can add your own keyboard handling, useful for
// testing to show a certain scene, e.g.
//if (pInput->IsKeyDown('1') pView->ShowMyScene();
// pView will update the current Scene
// within the Scene all input processing will be done
pView->Update();
// constant framerate of 30fps,
// FramesPerSecond will be updated with a delay, else the
// numbers change too fast
timer.update();
iFrames++;
iSeconds += timer.DTick;
if (iSeconds > FPS_DELAY)
{
FramesPerSecond = (float) (1000.0 * iFrames/ iSeconds);
iSeconds = 0;
iFrames = 0;
}
int iWait = 33 - timer.DTick;
if (iWait > 0)
Sleep(iWait);
}
return msg.wParam;
}
void CApplication::Exit()
{
pView->Exit();
pData->Exit();
}
//////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
UINT uMsg, // Message For This Window
WPARAM wParam, // Additional Message Information
LPARAM lParam) // Additional Message Information
{
switch (uMsg) // Check For Windows Messages
{
case WM_ACTIVATE: // Watch For Window Activate Message
{
if (!HIWORD(wParam)) // Check Minimization State
{
pApp->bActive = TRUE; // Program Is Active
}
else
{
pApp->bActive = FALSE; // Program Is No Longer Active
}
return 0;
}
case WM_SYSCOMMAND: // Intercept System Commands
{
switch (wParam) // Check System Calls
{
case SC_SCREENSAVE: // Screensaver Trying To Start?
case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
return 0;
}
break; // Exit
}
case WM_CLOSE: // Did We Receive A Close Message?
{
PostQuitMessage(0); // Send A Quit Message
return 0;
}
case WM_KEYDOWN: // Is A Key Being Held Down?
{
pApp->pInput->SetKey(wParam); // mark key as true
return 0;
}
case WM_KEYUP: // Has A Key Been Released?
{
pApp->pInput->SetKey(wParam, false); // mark key as false (up)
return 0;
}
case WM_SIZE: // Resize The OpenGL Window
{
// LoWord=Width, HiWord=Height
pApp->pData->SetDimension(LOWORD(lParam), HIWORD(lParam));
// resize view
pApp->pView->Resize();
return 0;
}
}
// Pass All Unhandled Messages To DefWindowProc
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, // Instance
HINSTANCE hPrevInstance, // Previous Instance
LPSTR lpCmdLine, // Command Line Parameters
int nCmdShow) // Window Show State
{
// create a new application
pApp = new CApplication();
pApp->Init(hInstance);
int iReturn = pApp->Run();
pApp->Exit();
// delete application
//delete pApp;
return (iReturn); // Exit The Program
}