Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Exit game from another class (Input)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
51 replies to this topic

#1 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 17 July 2011 - 09:21 AM

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

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13671

Like
0Likes
Like

Posted 17 July 2011 - 09:33 AM

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.

#3 Álvaro   Crossbones+   -  Reputation: 13671

Like
0Likes
Like

Posted 17 July 2011 - 09:38 AM

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.

#4 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 17 July 2011 - 09:57 AM

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?

#5 DaveMS   Members   -  Reputation: 185

Like
0Likes
Like

Posted 17 July 2011 - 10:53 AM

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.

#6 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 17 July 2011 - 10:56 AM

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?



#7 DaveMS   Members   -  Reputation: 185

Like
0Likes
Like

Posted 17 July 2011 - 11:07 AM

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?

#8 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 17 July 2011 - 11:12 AM

[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]



#9 abeylin   Members   -  Reputation: 239

Like
0Likes
Like

Posted 17 July 2011 - 11:37 AM

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


#10 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 17 July 2011 - 01:17 PM

Now I'm really confused :blink:

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 <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){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: Cleanreturn msg.wParam;}[/source]


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

#11 ApochPiQ   Moderators   -  Reputation: 16079

Like
0Likes
Like

Posted 17 July 2011 - 01:55 PM

What does "it doesn't work" mean?

#12 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 17 July 2011 - 02:06 PM

Sorry for beeing unclear :wink:

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 <dinput.h>#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]

#13 ApochPiQ   Moderators   -  Reputation: 16079

Like
1Likes
Like

Posted 17 July 2011 - 02:22 PM

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.

#14 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 17 July 2011 - 02:31 PM

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

#15 DaveMS   Members   -  Reputation: 185

Like
2Likes
Like

Posted 17 July 2011 - 02:34 PM

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.

#16 ApochPiQ   Moderators   -  Reputation: 16079

Like
0Likes
Like

Posted 17 July 2011 - 03:07 PM

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

#17 NightCreature83   Crossbones+   -  Reputation: 2936

Like
0Likes
Like

Posted 17 July 2011 - 04:55 PM

Sorry for beeing unclear :wink:

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 <dinput.h>#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



Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#18 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 17 July 2011 - 05:05 PM

Ok, I'll give Raw Input a try. I'll be back as soon as I've tried it out, hopefully successfully :wink:

#19 falcon93   Members   -  Reputation: 121

Like
0Likes
Like

Posted 01 August 2011 - 03:37 PM

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 <Windows.h>#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 <Windows.h>#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]

#20 Vortez   Crossbones+   -  Reputation: 2704

Like
0Likes
Like

Posted 01 August 2011 - 04:15 PM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS