RawInput causes the window to lag severely.

Started by
9 comments, last by krippy2k8 11 years, 7 months ago
This is pretty much my input handler:

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



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


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


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:

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();
}
}

What am I doing wrong?Did I initialize with the wrong flags?I also tried it like this:


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;




Same thing happens..
Advertisement
You need to put all of your message processing inside your PeekMessage block. i.e.


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

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




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.
In addition to what krippy2k8 said, you need read all messages every frame:


while (!done)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_INPUT)
{
...
}
}
gGraphics->Render();
}
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:


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




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? smile.png
What is it that doesn't work?

What is it that doesn't work?


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

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();
}
}



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:

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



If I put a message box under if (raw->header.dwType== RIM_TYPEMOUSE), 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:

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


like right after gGraphics->Render() 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?
Yeah, you want to test usButtonFlags, not ulButtons. See MSDN

Yeah, you want to test usButtonFlags, not ulButtons. See MSDN


yeah I tried that too but it only works in the while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) 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 while with an if,it lags again extremely.
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:


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


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:


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




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


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

This topic is closed to new replies.

Advertisement