MyPong.exe!WndProc() + 0x2a bytes C++
user32.dll!7e418734()
Here is my code thus far: Pong.h
#ifndef PONG_H
#define PONG_H
#include <cstdlib>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "Resource.h"
//Default CALLBACK for Windows Events
LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);
//Engine event callback type
class EventHandler
{
public:
virtual BOOL HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam) = 0;
};
//Game System Callbacks
class PongApplication
{
public:
virtual BOOL GameInitialize(HINSTANCE hInstance);
virtual void GameStart(HWND hWindow);
virtual void GameEnd();
virtual void GameActivate(HWND hWindow);
virtual void GameDeactivate(HWND hWindow);
virtual void GamePaint(HDC hDC);
virtual void GameCycle();
void setDefault(BOOL default) { m_Default = default; }
BOOL isDefault() { return m_Default; }
protected:
BOOL m_Default;
};
class Engine
{
public:
//Engine Constructor
//Input:
//Application Instance, WindowClassName, Title, Icons, Width and Height of Window
//
Engine(HINSTANCE hInstance, LPSTR szWindowClass, LPSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth,
int iHeight, int iFrameDelay = 50);
//Engine Destructor
//
virtual ~Engine();
//GetEngine
//Input: None
//Returns the singleton Engine object.
static Engine* GetEngine() { return m_pGameEngine; }
//Initialize
//Input: Show Window
//Returns: True if Engine created, false otherwise
BOOL Initialize(int iCmdShow);
//HandleEvent
//Event Handler:
//This will call Event Handler callbacks set by
//the applicaiton.
//Input: standard MFC message information
//Returns: LRESULT handled event?
LRESULT HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);
//AccessorMethods
HINSTANCE GetInstance() const {return m_hInstance; }
HWND GetWindow() const { return m_hWindow; }
void SetWindow(HWND hWnd) { m_hWindow = hWnd; }
WORD GetIcon() const { return m_wIcon; }
WORD GetSmallIcon() const { return m_wSmallIcon; }
int GetWidth() const { return m_iWidth; }
int GetHeight() const { return m_iHeight; }
void GetDimensions(int &width, int &height) { width = m_iWidth; height = m_iHeight; }
int GetFrameDelay() const { return m_iFrameDelay; }
void SetFrameRate(int iFrameRate) { m_iFrameDelay = iFrameRate; }
BOOL GetSleep() const { return m_bSleep; }
void SetSleep(BOOL bSleep) { m_bSleep = bSleep; }
void SetEventHandler(EventHandler* pHandler);
void SetApplication(PongApplication* pGame);
protected:
//Variables
static Engine* 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;
EventHandler* m_pEventHandler;
PongApplication* m_pGame;
};
#endif
#include "Pong.h"
Engine* Engine::m_pGameEngine = NULL;
Engine::Engine(HINSTANCE hInstance, LPSTR szWindowClass, LPSTR szTitle, WORD wIcon,
WORD wSmallIcon, int iWidth, int iHeight, int iFrameDelay)
{
m_pGameEngine = this;
m_hInstance = hInstance;
m_hWindow = NULL;
if ((lstrlen(szWindowClass) > 0) && (lstrlen(szWindowClass) <=32 ))
{
lstrcpy(m_szWindowClass, szWindowClass);
}
else
lstrcpy(m_szWindowClass, "DefaultClass");
if ((lstrlen(szTitle) > 0) && (lstrlen(szTitle) <= 32))
{
lstrcpy(m_szTitle, szTitle);
}
else
lstrcpy(m_szTitle, "DefaultTitle");
m_wIcon = wIcon;
m_wSmallIcon = wSmallIcon;
m_iWidth = iWidth;
m_iHeight = iHeight;
m_iFrameDelay = iFrameDelay;
m_bSleep = TRUE;
m_pGame = NULL;
m_pEventHandler = NULL;
}
Engine::~Engine()
{
if(m_pGame != NULL)
{
if(m_pGame->isDefault())
{
delete m_pGame;
m_pGame = NULL;
}
else
{
m_pGame = NULL;
}
}
if (m_pGameEngine = this)
m_pGameEngine = NULL;
}
BOOL Engine::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;
//Calc the size and pos.
int iWindowWidth = m_iWidth + GetSystemMetrics(SM_CYFIXEDFRAME) *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 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);
//Check application instance
//If no instance exists create default
if(m_pGame == NULL)
{
m_pGame = new PongApplication();
m_pGame->setDefault(TRUE);
}
return TRUE;
}
LRESULT Engine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
// Set the game window and start the game
SetWindow(hWindow);
m_pGame->GameStart(hWindow);
return 0;
case WM_ACTIVATE:
//Activate/deactivate the game and update the sleep status
if (wParam != WA_INACTIVE)
{
m_pGame->GameActivate(hWindow);
SetSleep(FALSE);
}
else
{
m_pGame->GameDeactivate(hWindow);
SetSleep(TRUE);
}
return 0;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hWindow, &ps);
m_pGame->GamePaint(hDC);
EndPaint(hWindow, &ps);
return 0;
case WM_DESTROY:
m_pGame->GameEnd();
PostQuitMessage(0);
return 0;
}
if(m_pEventHandler != NULL)
{
if(!m_pEventHandler->HandleEvent(hWindow, msg, wParam, lParam))
return DefWindowProc(hWindow, msg, wParam, lParam);
}
return DefWindowProc(hWindow, msg, wParam, lParam);
}
void Engine::SetEventHandler(EventHandler *pHandler)
{
if(m_pEventHandler == NULL)
m_pEventHandler = pHandler;
}
void Engine::SetApplication(PongApplication *pGame)
{
if(m_pGame == NULL)
m_pGame = pGame;
}
BOOL PongApplication::GameInitialize(HINSTANCE hInstance)
{
Engine::GetEngine()->SetFrameRate(15);
return TRUE;
}
void PongApplication::GameStart(HWND hWindow)
{
srand(GetTickCount());
}
void PongApplication::GameEnd()
{
Engine* pEngine = Engine::GetEngine();
delete pEngine;
}
void PongApplication::GameActivate(HWND hWindow)
{
HDC hDC;
RECT rect;
GetClientRect(hWindow, &rect);
hDC = GetDC(hWindow);
DrawText(hDC, TEXT("Engine Works!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
ReleaseDC(hWindow, hDC);
}
void PongApplication::GameDeactivate(HWND hWindow)
{
HDC hDC;
RECT rect;
GetClientRect(hWindow, &rect);
hDC = GetDC(hWindow);
DrawText(hDC, TEXT("Engine Works!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
ReleaseDC(hWindow, hDC);
}
void PongApplication::GamePaint(HDC hDC)
{
}
void PongApplication::GameCycle()
{
}
LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
Engine *pEngine = Engine::GetEngine();
if (pEngine != NULL)
return pEngine->HandleEvent(hWindow, msg, wParam, lParam);
return DefWindowProc(hWindow, msg, wParam, lParam);
}
#include "Pong.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;
static int iTickTrigger = 0;
int iTickCount;
Engine *pEngine = new Engine(hInstance, TEXT("MyPong"),
TEXT("MyPong"), IDI_MYPONG, IDI_MYPONG_SM, 800, 600, 50);
if(pEngine == NULL)
return FALSE;
if(!pEngine->Initialize(nShowCmd))
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 (!pEngine->GetSleep())
{
//Check the tick count to see if a gam cycle has elapsed
iTickCount = GetTickCount();
if (iTickCount > iTickTrigger)
{
iTickTrigger = iTickCount +
pEngine->GetFrameDelay();
}
}
}
}
return(int)msg.wParam;
}