🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Exit game from another class (Input)

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

return 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]
Advertisement
What does "it doesn't work" mean?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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

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

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


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, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Ok, I'll give Raw Input a try. I'll be back as soon as I've tried it out, hopefully successfully :wink:
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]

This topic is closed to new replies.

Advertisement