Jump to content
  • Advertisement
Sign in to follow this  
falcon93

Exit game from another class (Input)

This topic is 2601 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Hidden

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!