Exit game from another class (Input)

Started by
50 comments, last by rip-off 12 years, 8 months ago
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:

<BR>...<BR>PostQuitMessage(0); return 0; break;<BR>...<BR>


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

[source lang="cpp"]
#include <dinput.h>
#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 :)
Advertisement
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.

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.
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?

Is it normal that the Main.h file behaves strange compared with the other .h files?


No, `Main.h' is just a name. In what way is it behaving strange?
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.
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?


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?
[source lang="cpp"]
#include <Windows.h>
#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]

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.

http://www.mildspring.com - developing android games

This topic is closed to new replies.

Advertisement