Sign in to follow this  

RawInput causes the window to lag severely.

This topic is 1958 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

This is pretty much my input handler:

[CODE]Input* gInput = 0;
Input::Input()
{
raw = 0;
}
Input::~Input()
{
}
void Input::Release()
{

}
void Input::Initialize()
{
Rid[0].usUsagePage = 1;
Rid[0].usUsage = 6;
Rid[0].dwFlags = 0;
Rid[0].hwndTarget=NULL;
Rid[1].usUsagePage = 1;
Rid[1].usUsage = 2;
Rid[1].dwFlags = 0;
Rid[1].hwndTarget=NULL;
RegisterRawInputDevices(Rid, 2, sizeof(Rid[0]));
}
void Input::Update()
{
if (raw->header.dwType== RIM_TYPEMOUSE)
{
lastMouseX = raw->data.mouse.lLastX;
lastMouseY = raw->data.mouse.lLastY;
mouseFlags = raw->data.mouse.usFlags;
mouseButtonFlags = raw->data.mouse.usButtonFlags;
leftButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN;
rightButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_RIGHT_BUTTON_DOWN;
middleButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_MIDDLE_BUTTON_DOWN;
if(raw->data.mouse.ulButtons & RI_MOUSE_WHEEL)
{
wheelZ = raw->data.mouse.usButtonData;
}
}
if (raw->header.dwType== RIM_TYPEKEYBOARD)
{
keyCode=raw->data.keyboard.VKey;
bool keyPressed=raw->data.keyboard.Flags & RI_KEY_BREAK;
}
}
[/CODE]


[CODE]class Input
{
#pragma region REGION:INPUT_METHODS
public:
Input();
~Input();

void Release();
void Initialize();
void Update();
#pragma endregion
#pragma region REGION:INPUT_MEMBERS
public:
RAWINPUTDEVICE Rid[2];
RAWINPUT *raw;
long lastMouseX;
long lastMouseY;
USHORT mouseFlags;
USHORT mouseButtonFlags;
USHORT wheelZ;
bool leftButtonDown;
bool rightButtonDown;
bool middleButtonDown;
USHORT keyCode;
#pragma endregion
};
extern Input* gInput;


#endif[/CODE]

When I initialize it in the main function like this:[CODE]gInput = new Input();
gInput->Initialize();[/CODE]

The window starts lagging and all 4 cores of my CPU show increased load,as much as 20% - 40% !!
I tried to read input with Enter and LeftMouseButton,but it doesn't respond:

[CODE]while(!done)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT)
{
done = true;
}

if(msg.message == WM_INPUT)
{
UINT bufferSize;
GetRawInputData((HRAWINPUT)msg.lParam, RID_INPUT, NULL, &bufferSize, sizeof (RAWINPUTHEADER));
BYTE *buffer=new BYTE[bufferSize];
GetRawInputData((HRAWINPUT)msg.lParam, RID_INPUT, (LPVOID)buffer, &bufferSize, sizeof (RAWINPUTHEADER));
SafeDelete(gInput->raw);
gInput->raw = (RAWINPUT*)buffer;
gInput->Update();
if(gInput->keyCode = VK_RETURN)
{
done = true;
}
if(gInput->leftButtonDown)
{
done = true;
}
}
else
{
gGraphics->Render();
}
}
[/CODE]
What am I doing wrong?Did I initialize with the wrong flags?I also tried it like this:

[CODE]
Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x02;
Rid[0].dwFlags = RIDEV_NOLEGACY;
Rid[0].hwndTarget = 0;


Rid[1].usUsagePage = 0x01;
Rid[1].usUsage = 0x06;
Rid[1].dwFlags = RIDEV_NOLEGACY;
Rid[1].hwndTarget = 0;

[/CODE]


Same thing happens..

Share this post


Link to post
Share on other sites
You need to put all of your message processing inside your PeekMessage block. i.e.

[CODE]
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);

if (msg.message == WM_INPUT)
{
...
}
}

[/CODE]


PeekMessage doesn't clear the MSG structure if no message is read, so processing WM_INPUT outside of that block means that any time you receive a WM_INPUT message, you're going to continue to process that same message over and over and over until another message is received. Which means your Render is not going to get called very often.

Share this post


Link to post
Share on other sites
In addition to what krippy2k8 said, you need read all messages every frame:

[CODE]
while (!done)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_INPUT)
{
...
}
}
gGraphics->Render();
}
[/CODE]

Share this post


Link to post
Share on other sites
thank you for your advices,it doesn't cause a heavy cpu load now,but it still doesn't work for some reason,here is how I check the input:

[CODE]
if (raw->header.dwType== RIM_TYPEMOUSE)
{
lastMouseX = raw->data.mouse.lLastX;
lastMouseY = raw->data.mouse.lLastY;
mouseFlags = raw->data.mouse.usFlags;
mouseButtonFlags = raw->data.mouse.usButtonFlags;
leftButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN;
rightButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_RIGHT_BUTTON_DOWN;
middleButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_MIDDLE_BUTTON_DOWN;
if(raw->data.mouse.ulButtons & RI_MOUSE_WHEEL)
{
wheelZ = raw->data.mouse.usButtonData;
}
}
if (raw->header.dwType== RIM_TYPEKEYBOARD)
{
keyCode=raw->data.keyboard.VKey;
bool keyPressed=raw->data.keyboard.Flags & RI_KEY_BREAK;
}


[/CODE]

maybe I'm getting my info from a bad source?I read about it here: http://www.toymaker.info/Games/html/raw_input.html
Can anyone suggest another basic tutorial for RawInput? [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
[quote name='krippy2k8' timestamp='1346804044' post='4976623']
What is it that doesn't work?
[/quote]

well basically in the loop where it checks for WM_INPUT(which I fixed now thanks to you guys) I do this:

[CODE]void Application::Run()
{
MSG msg;
bool done, result;
ZeroMemory(&msg, sizeof(MSG));

done = false;
while(!done)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message == WM_QUIT)
{
done = true;
}
if(msg.message == WM_INPUT)
{
UINT bufferSize;
GetRawInputData((HRAWINPUT)msg.lParam, RID_INPUT, NULL, &bufferSize, sizeof (RAWINPUTHEADER));
BYTE *buffer=new BYTE[bufferSize];
GetRawInputData((HRAWINPUT)msg.lParam, RID_INPUT, (LPVOID)buffer, &bufferSize, sizeof (RAWINPUTHEADER));
SafeDelete(gInput->raw);
gInput->raw = (RAWINPUT*)buffer;
gInput->Update();
}
}
gGraphics->Render();
}
}
[/CODE]


so I'm basically setting the raw from the RAWINPUT* raw that is a member of the Input class to the info I get from the message loop and then I set a bunch of bools depending on it:

[CODE]void Input::Update()
{
if (raw->header.dwType== RIM_TYPEMOUSE)
{
lastMouseX = raw->data.mouse.lLastX;
lastMouseY = raw->data.mouse.lLastY;
mouseFlags = raw->data.mouse.usFlags;
mouseButtonFlags = raw->data.mouse.usButtonFlags;
leftButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN;
rightButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_RIGHT_BUTTON_DOWN;
middleButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_MIDDLE_BUTTON_DOWN;
if(raw->data.mouse.ulButtons & RI_MOUSE_WHEEL)
{
wheelZ = raw->data.mouse.usButtonData;
}
}
if (raw->header.dwType== RIM_TYPEKEYBOARD)
{
keyCode=raw->data.keyboard.VKey;
bool keyPressed=raw->data.keyboard.Flags & RI_KEY_BREAK;
}
}[/CODE]


If I put a message box under [b]if (raw->header.dwType== RIM_TYPEMOUSE)[/b], it pops up every time i click or move the mouse,which means it works perfectly.Same when I test for the keyboard.The problem is that when I do this somewhere else:

[CODE]if(gInput->leftButtonDown)
{
MessageBox(NULL,L"lolzorz",L"lololol",MB_OK);
}[/CODE]

like right after [b]gGraphics->Render()[/b] and it should make a pop up box when I click the mouse button,right?Well it doesn't.I tried with keys and with everything,it doesn't respond to any input.I'm thinking the way I set the bools is wrong?

Share this post


Link to post
Share on other sites
Yeah, you want to test usButtonFlags, not ulButtons. See [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ms645578%28v=vs.85%29.aspx"]MSDN[/url]

Share this post


Link to post
Share on other sites
[quote name='krippy2k8' timestamp='1346825749' post='4976716']
Yeah, you want to test usButtonFlags, not ulButtons. See [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ms645578%28v=vs.85%29.aspx"]MSDN[/url]
[/quote]

yeah I tried that too but it only works in the [b]while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) [/b]block,I have the feeling that the program stays inside of this block of code forever,since nothing works outisde of it,but if I replace the [b]while [/b]with an [b]if[/b],it lags again extremely.

Share this post


Link to post
Share on other sites
Okay I see. The messages indicate transition states, not the complete state of the buttons. What's probably happening is you're getting mouse movement messages after your button down messages, so when you test for mouse button down in the mouse movement message it is coming up false and clearing your button down flag.

So you'll want to do something like this in your Update:

[CODE]
if (raw->header.dwType== RIM_TYPEMOUSE)
{
if (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_DOWN)
leftButtonDown = true;
else if (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_UP)
leftButtonDown = false;
}

[/CODE]

Share this post


Link to post
Share on other sites
[quote name='krippy2k8' timestamp='1346875670' post='4976963']
Okay I see. The messages indicate transition states, not the complete state of the buttons. What's probably happening is you're getting mouse movement messages after your button down messages, so when you test for mouse button down in the mouse movement message it is coming up false and clearing your button down flag.

So you'll want to do something like this in your Update:

[CODE]
if (raw->header.dwType== RIM_TYPEMOUSE)
{
if (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_DOWN)
leftButtonDown = true;
else if (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_UP)
leftButtonDown = false;
}

[/CODE]
[/quote]

Yes,but I am alredy getting good input,the problem is that the way it is now:
[CODE]while(!done)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//Dostuff;
}
}
//Dostuff2;[/CODE]

Dostuff2 will never be executed,because the program stays in [b]while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))[/b] forever.If I change it to [b][u]if[/u](PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))[/b],it starts to lag again.I'm confused,since in MSDN they also say you should use the [b]while(PeekMessage(...))[/b] way,how will it ever get out of the loop?My [b]PeekMessage[/b] seems to return [b]true [/b]always.

Share this post


Link to post
Share on other sites
Your //Dostuff2 is outside of the main loop, that will never be reached until your message loop completely ends (i.e. the application is closing.)

Do this:

[CODE]
while(!done)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//Dostuff;
}
//Dostuff2;
}

[/CODE]


If that is what you actually have and Dostuff2 is never reached, then you must be doing something within the inner loop to generate new messages.

You may want to post your actual code again Edited by krippy2k8

Share this post


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