That's exactly what I was looking for
I'm wondering, are there any side effects to using this method though? Like...your computer locking up into a turtle slow speed when you try to click the X button?
I seem to still be having troubles with receiving messages...
Main.cpp
Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "Game.h"
int APIENTRY WinMain(HINSTANCE MyInstance, HINSTANCE PrevInstance, LPSTR kposzArgs, int nWinMode)
{
Game MyGame(MyInstance); //Can I use the Instance like this?
while (MyGame.bRunning)
{
MyGame.MessagePump();
MyGame.RenderScene();
}
return 0;
}
Game.h
Code:
#include <D3DX8.h>
#pragma comment(lib,"D3D8.lib")
#pragma comment(lib,"D3DX8.lib")
#pragma once
class Game
{
public:
Game(HINSTANCE hInstance);
~Game(void);
void MessagePump(void);
void RenderScene(void);
bool bRunning;
static LRESULT CALLBACK SWinFunc(HWND hWnd, unsigned int Msg, WPARAM wParam, LPARAM lParam);
protected: //What does "Protected" mean?
LRESULT CALLBACK WinFunc(HWND hWnd, unsigned int Msg, WPARAM wParam, LPARAM lParam);
private:
D3DFORMAT Get16BitMode(void);
bool GetScreenMode(void);
bool InitWindow(void);
bool InitD3D(void);
bool InitScene(void);
void KillScene(void);
void KillD3D(void);
void KillWindow(void);
unsigned long Width, Height;
bool Fullscreen;
HWND MainWindow;
HINSTANCE MainInstance;
LPDIRECT3D8 D3D;
IDirect3DDevice8 *D3DDevice;
};
And Finally, the Game.cpp
Code:
Game::Game(HINSTANCE hInstance)
{
MainInstance = hInstance;
Fullscreen = GetScreenMode();
bRunning = InitWindow();
if (bRunning) bRunning = InitD3D();
if (bRunning) bRunning = InitScene();
}
Game::~Game(void)
{
KillScene();
KillD3D();
KillWindow();
}
bool Game::InitWindow(void)
{
WNDCLASS WCL;
DWORD Style;
WCL.cbClsExtra = 0;
WCL.cbWndExtra = 0;
WCL.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
WCL.hCursor = LoadCursor(NULL, IDC_ARROW);
WCL.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WCL.hInstance = MainInstance; //The only important thing about this function is that I'm using the Private Member that
//had the original Instance passed into it from the Main.cpp file. Is that okay to do?
WCL.lpfnWndProc = SWinFunc;
WCL.lpszClassName = "MyClass";
WCL.lpszMenuName = NULL;
WCL.style = CS_OWNDC;
if (!RegisterClass(&WCL)) return false;
if (Fullscreen)
{
Width = GetSystemMetrics(SM_CXSCREEN);
Height = GetSystemMetrics(SM_CYSCREEN);
Style = WS_POPUP;
}
else
{
Width = GetSystemMetrics(SM_CXSCREEN);
Height = GetSystemMetrics(SM_CYSCREEN);
Style = WS_OVERLAPPED|WS_SYSMENU;
}
MainWindow = CreateWindow("MyClass", "RPG", Style, 0, 0, Width, Height, NULL, NULL, MainInstance, this);
if (!MainWindow) return false;
ShowWindow(MainWindow, SW_SHOW);
UpdateWindow(MainWindow);
SetFocus(MainWindow);
return true;
}
//My Two Message Handling Functions
LRESULT CALLBACK Game::SWinFunc(HWND hWnd, unsigned int Msg, WPARAM wParam, LPARAM lParam)
{
if (Msg == WM_NCCREATE)
{
LPCREATESTRUCT LPC = (LPCREATESTRUCT)lParam;
SetWindowLong(hWnd, GWL_USERDATA, (long)LPC->lpCreateParams);
SetWindowLong(hWnd, 0, 1);
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
Game *pMyGame;
if (GetWindowLong(hWnd, 0))
{
pMyGame = (Game *)GetWindowLong(hWnd, GWL_USERDATA);
return pMyGame->WinFunc(hWnd, Msg, wParam, lParam);
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
LRESULT CALLBACK Game::WinFunc(HWND hWnd, unsigned int Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_KEYDOWN:
bRunning = false;
return 0;
break;
case WM_CLOSE:
bRunning = false;
return 0;
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}
return (DefWindowProc(hWnd,Msg,wParam,lParam));
}
It seems that my second Message Handler (the non-static one) never fires because if I press a key once the app is running, nothing happens. I think the reason behind my lock-ups is that bRunning is always true and the loop in Main.cpp starts going ridiculously quickly once the window is closed, and hence the lockup.
I'm wondering if anyone sees something that's causing my Message Handler #2 not to be fired?