Skip to main content
GameDev.net gamedev.net
🔒 Locked

Exit game from another class (Input)

Started by falcon93 Jul 17, 2011 at 3:21 PM 50 replies 9.3k views
Original Post
falcon93
falcon93
Hi everyone!

How can I exit my game from another class when I'm not in the game loop?
If I want to quit the game from the gameloop itself, I just write:


...
PostQuitMessage(0); return 0; break;
...



However, I'm handeling all my keyboard input in my InputEngine.cpp:

[source lang="cpp"]
#include
#include "InputEngine.h"


#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")


InputEngine::InputEngine(HINSTANCE hInstance, HWND hWnd)
{
DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&din, NULL);
din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, NULL);

dinkeyboard->SetDataFormat(&c_dfDIKeyboard);
dinkeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
}


InputEngine::~InputEngine(void)
{
}


void InputEngine::Update(void)
{
dinkeyboard->Acquire();
dinkeyboard->GetDeviceState(256, (LPVOID)keystate);

HandleInput();
}


void InputEngine::HandleInput(void)
{
if (keystate[DIK_ESCAPE] & 0x80)
{
PostQuitMessage(0);
}
}
[/source]


How can I exit the game from my InputEngine.cpp? Or is it a better way to handle keyboard input?

Please help
alvaro
alvaro
It's probably cleaner to set a flag (a boolean variable) indicating that we should exit, and the main loop can check it. There are a few other alternatives (exit', throw an exception, longjmp'), but I wouldn't recommend any of them: The part of the code that reads the keyboard should be concerned only with reading the keyboard, not with things like how to exit the game.
alvaro
alvaro

If I want to quit the game from the gameloop itself, I just write:


...
PostQuitMessage(0); return 0; break;
...



You don't need to break' after you return'. This probably means that you are not sure what break' does; you may think it's part of the syntax for switch', but it's not.
falcon93
falcon93
Ok, I'll try around with a bool, I'm replying as soon as I've tried it out


Nah, I've programmed in C# before, so I know what a break does. Shortly said, it jumps out of a loop, but only one "level". So if you have one loop inside the other, it will just jump out from the loop it was declared in. As the WinMain is an int function, I guess it just neccessery that I use this?:


...
PostQuitMessage(0); return 0;
...


Is it normal that the Main.h file behaves strange compared with the other .h files?
DaveMS
DaveMS
return will return the value and exit the function, so any code after a return statement is redundant - whether that be in main(), or any other function.
falcon93
falcon93
Well, I've played around with some defferent ways now, but none seems to work. What I thought was that I could use a bool as statement in my game loop, and by changing the value of the bool, quitting the game:


while (!quit)
{
// quit = true will quit the game
}



This, however, didn't work. And what struggles is my Main.h file. This is my whole code in my Main Header file (Main.h):


#include "InputEngine.h"
#include "GraphicsEngine.h"


#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 1024


#pragma once


class Main
{

public:
InputEngine* input_engine;
GraphicsEngine* graphics_engine;

public:
bool quit;

};


As you see, there's a bool called quit, and it is public.


Here I try to use the bool in my Main.cpp file:


while(!quit)
{
}


But I get one error:


IntelliSence: identifier "quit" is undefined


What am I doing wrong?

DaveMS
DaveMS

Well, I've played around with some defferent ways now, but none seems to work. What I thought was that I could use a bool as statement in my game loop, and by changing the value of the bool, quitting the game:


while (!quit)
{
// quit = true will quit the game
}



This, however, didn't work. And what struggles is my Main.h file. This is my whole code in my Main Header file (Main.h):


#include "InputEngine.h"
#include "GraphicsEngine.h"


#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 1024


#pragma once


class Main
{

public:
InputEngine* input_engine;
GraphicsEngine* graphics_engine;

public:
bool quit;

};


As you see, there's a bool called quit, and it is public.


Here I try to use the bool in my Main.cpp file:


while(!quit)
{
}


But I get one error:


IntelliSence: identifier "quit" is undefined


What am I doing wrong?




Can you post the whole of your main.cpp?
falcon93
falcon93
[source lang="cpp"]
#include
#include "Main.h"
#include "InputEngine.h"
#include "GraphicsEngine.h"


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY: PostQuitMessage(0); return 0; break;
}

return DefWindowProc(hWnd, message, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
quit = false;

WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = L"AvoidBallClass";
wc.style = CS_VREDRAW | CS_HREDRAW;

RegisterClassEx(&wc);

HWND hWnd;
hWnd = CreateWindowEx(NULL, L"AvoidBallClass", L"AvoidBall", WS_OVERLAPPED, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);

MSG msg;

InputEngine* input_engine = new InputEngine(hInstance, hWnd);

GraphicsEngine* graphics_engine = new GraphicsEngine(hWnd);
graphics_engine->InitGraphics();

while (true)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

input_engine->Update();

graphics_engine->Update();
graphics_engine->Draw();
}

// TODO: Clean

return msg.wParam;
}
[/source]

abeylin
abeylin
This is a problem:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ quit = false;

The 'quit' variable is not declared in function, and doesn't seem to be a global.

I realize you created it in your 'Main.h', but you created it within a class, and WinMain is not part of that class.
Therefore WinMain cannot access 'quit' variable.
falcon93
falcon93
Now I'm really confused

WinMain, isn't that the programs entry method ? Why isn't then WinMain a part of my class Main?



Anyways; I've tried decalring the bool inside the WinMain method, and now atleast it doesn't say that it's undefined. It still doesn't work though. I've pasted both Main.cpp and InputEngine.cpp below. Could anyone please take a look and please help me with what I've done wrong?

Main.cpp
[source lang="cpp"]
#include
#include "Main.h"
#include "InputEngine.h"
#include "GraphicsEngine.h"


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY: PostQuitMessage(0); return 0; break;
}

return DefWindowProc(hWnd, message, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
bool quit = false;

WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = L"AvoidBallClass";
wc.style = CS_VREDRAW | CS_HREDRAW;

RegisterClassEx(&wc);

HWND hWnd;
hWnd = CreateWindowEx(NULL, L"AvoidBallClass", L"AvoidBall", WS_OVERLAPPED, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);

MSG msg;

InputEngine* input_engine = new InputEngine(hInstance, hWnd);

GraphicsEngine* graphics_engine = new GraphicsEngine(hWnd);
graphics_engine->InitGraphics();

while (!quit)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

input_engine->Update();

if (input_engine->escape == true)
{
quit = true;
}

graphics_engine->Update();
graphics_engine->Draw();
}

// TODO: Clean

return msg.wParam;
}
[/source]


InputEngine.cpp
[source lang="cpp"]
#include
#include "InputEngine.h"
#include "Main.h"


#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")


InputEngine::InputEngine(HINSTANCE hInstance, HWND hWnd)
{
DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&din, NULL);
din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, NULL);

dinkeyboard->SetDataFormat(&c_dfDIKeyboard);
dinkeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
}


InputEngine::~InputEngine(void)
{
}


void InputEngine::Update(void)
{
dinkeyboard->Acquire();
dinkeyboard->GetDeviceState(256, (LPVOID)keystate);

HandleInput();
}


void InputEngine::HandleInput(void)
{
if (keystate[DIK_LEFT] & 0x80)
{
escape = true;
}
}
[/source]
falcon93
falcon93
Sorry for beeing unclear

Well, I've looked deeper into the problem, and I know where the problem is, I don't know how to solve it thoguh. The problem is that my code register button presses for all buttons except the escape button. I tried to change IDK_ESCAPE to IDK_SPACE and it worked perfect. So the question is, what's wrong with the DIK_ESCAPE?



if (keystate[DIK_ESCAPE] & 0x80)
{
escape = true;
}



[source lang="cpp"]
#include
#include "InputEngine.h"
#include "Main.h"


#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")


InputEngine::InputEngine(HINSTANCE hInstance, HWND hWnd)
{
escape = false;

DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&din, NULL);
din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, NULL);

dinkeyboard->SetDataFormat(&c_dfDIKeyboard);
dinkeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
}


InputEngine::~InputEngine(void)
{
}


void InputEngine::Update(void)
{
dinkeyboard->Acquire();
dinkeyboard->GetDeviceState(256, (LPVOID)keystate);

HandleInput();
}


void InputEngine::HandleInput(void)
{
if (keystate[DIK_ESCAPE] & 0x80)
{
escape = true;
}
}
[/source]
ApochPiQ
ApochPiQ
I have a feeling is a signed-vs-unsigned char issue (masking off the high bit does weird things on signed chars) but the real solution is that you should not use DirectInput in the first place.
falcon93
falcon93
What should I use instead? You know, I'm so motivated about getting better at programming, so'll try to learn almost anything. Please share your idea with me?
DaveMS
DaveMS
no - winMain is not part of the Main class.

When your program begins it will search for the entry point. It is going to search for the WinMain function with the correct return type and parameters. The fact that you have defined this in the .cpp of the Main class is irrelevant - You could put the WinMain function in any .cpp file you like - it will still work.

I tend to stick the entry point in its own .cpp file.
ApochPiQ
ApochPiQ

What should I use instead? You know, I'm so motivated about getting better at programming, so'll try to learn almost anything. Please share your idea with me?


Since you're apparently on Windows, look into "raw input."
NightCreature83
NightCreature83

Sorry for beeing unclear

Well, I've looked deeper into the problem, and I know where the problem is, I don't know how to solve it thoguh. The problem is that my code register button presses for all buttons except the escape button. I tried to change IDK_ESCAPE to IDK_SPACE and it worked perfect. So the question is, what's wrong with the DIK_ESCAPE?



if (keystate[DIK_ESCAPE] & 0x80)
{
escape = true;
}



[source lang="cpp"]
#include
#include "InputEngine.h"
#include "Main.h"


#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")


InputEngine::InputEngine(HINSTANCE hInstance, HWND hWnd)
{
escape = false;

DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&din, NULL);
din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, NULL);

dinkeyboard->SetDataFormat(&c_dfDIKeyboard);
dinkeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
}


InputEngine::~InputEngine(void)
{
}


void InputEngine::Update(void)
{
dinkeyboard->Acquire();
dinkeyboard->GetDeviceState(256, (LPVOID)keystate);

HandleInput();
}


void InputEngine::HandleInput(void)
{
if (keystate[DIK_ESCAPE] & 0x80)
{
escape = true;
}
}
[/source]


Your real problem is that you are trying to use DirectInput for keyboard input MS itself has said you shouldn't do this. Read this for more info and the link to the MS stuff


falcon93
falcon93
Ok, I'll give Raw Input a try. I'll be back as soon as I've tried it out, hopefully successfully
falcon93
falcon93
Ok, I've been trying the Raw input stuff, but now I'm stuck again


Firstly, my Input class has a CheckInput method that requires a LPARAM variable from the games message handler. How am I supposed to be able to access it? Here's the code:

[source lang="cpp"]
#include
#include "Main.h"
#include "Graphics.h"
#include "Input.h"
#include "Player.h"


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY: PostQuitMessage(0); return 0;
}

return DefWindowProc(hWnd, message, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = L"AvoidBallClass";
wc.style = CS_VREDRAW | CS_HREDRAW;

RegisterClassEx(&wc);

HWND hWnd;
hWnd = CreateWindowEx(NULL, L"AvoidBallClass", L"AvoidBall", WS_OVERLAPPED, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);

MSG msg;

Graphics* graphics = new Graphics(hWnd);
Input* input = new Input();
Player* player = new Player(*graphics, 100, 100);

while (true)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

input->CheckInput(lParam);

graphics->Begin();
graphics->Draw(*player->sprite, player->x, player->y);
graphics->End();
}

// TODO: Clean up resources.

return msg.wParam;
}
[/source]

As you can see on row 50, I need to access the lParam parameter from the WindowProc method. How can I do that?



Also, this is my Input class. Does this seem to be correct, or have I missed anything? (havn't managed to test it due to the above problem). Code:

[source cpp="cpp"]
#include
#include "Input.h"
#include "Main.h"


Input::Input()
{
RAWINPUTDEVICE rid[1];

rid[0].usUsagePage = 1;
rid[0].usUsage = 6;
rid[0].dwFlags = 0;
rid[0].hwndTarget = NULL;

RegisterRawInputDevices(rid, 1, sizeof(RAWINPUTDEVICE));
}


Input::~Input(void)
{
}


void Input::CheckInput(LPARAM lParam)
{
UINT bufferSize;
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &bufferSize, sizeof (RAWINPUTHEADER));
BYTE* buffer=new BYTE[bufferSize];
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, (LPVOID)buffer, &bufferSize, sizeof (RAWINPUTHEADER));

RAWINPUT* raw = (RAWINPUT*) buffer;
if (raw->header.dwType == RIM_TYPEKEYBOARD)
{
USHORT keyCode = raw->data.keyboard.VKey;

switch (keyCode)
{
case VK_LEFT: MessageBox(NULL, L"Left", NULL, NULL); break;
case VK_RIGHT: MessageBox(NULL, L"Right", NULL, NULL); break;
case VK_UP: MessageBox(NULL, L"Up", NULL, NULL); break;
case VK_DOWN: MessageBox(NULL, L"Down", NULL, NULL); break;
}
}
}
[/source]
Vortez
Vortez
The problem is that you use a class instead of simply putting your code in the message loop function. Here's how i do it:


LRESULT CALLBACK MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
//Process quit message
case WM_DESTROY:
case WM_CLOSE:
PostQuitMessage(0);
break;


case WM_KEYDOWN:
if(wParam == VK_ESCAPE){
PostQuitMessage(0);
break;
}
pKeyState[wParam] = true;
pVortez3DEngine->OnKeyDown((UINT)wParam);
break;
case WM_KEYUP:
pKeyState[wParam] = false;
pVortez3DEngine->OnKeyUp((UINT)wParam);
break;


default: break;
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

You can put more stuff there like mouse input and such. Also notice the pVortez3DEngine->OnKeyUp((UINT)wParam); wich mean that you can process all you input in another function if you want to. KeyState is simply an array of 256 bool that can be used to tell if one or multiple keys are pressed. As for the game loop, it should be done like this (that's why im using a pointer to my class object above).

void CVortez3DEngine::GameLoop()
{
MSG msg;
bool done = false;

// Application loop.
while(!done){
if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)){
// If a quit message is received then stop rendering and quit the app.
if(msg.message == WM_QUIT){
done = true;
}
TranslateMessage(&msg); // Translate any messages.
DispatchMessage(&msg); // Dispatch any messages.
} else {
EngineTimer.Tick(); // Update engine timer
UpdateScene(EngineTimer.g_ElapsedTime); // Control the game flow
RenderScene(); // Render a frame.
}
}
}

void CVortez3DEngine::OnKeyDown(UINT KeyPressed){}
{
// Your code...
}

void CVortez3DEngine::OnKeyUp(UINT KeyPressed){}
{
// Your code...
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.