Jump to content

  • Log In with Google      Sign In   
  • Create Account


RawInput causes the window to lag severely.


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
10 replies to this topic

#1 mrheisenberg   Members   -  Reputation: 356

Like
1Likes
Like

Posted 02 September 2012 - 09:47 AM

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

Sponsor:

#2 krippy2k8   Members   -  Reputation: 646

Like
1Likes
Like

Posted 02 September 2012 - 02:45 PM

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.

#3 Adam_42   Crossbones+   -  Reputation: 2457

Like
3Likes
Like

Posted 02 September 2012 - 04:51 PM

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


#4 mrheisenberg   Members   -  Reputation: 356

Like
0Likes
Like

Posted 04 September 2012 - 05:32 PM

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? Posted Image

#5 krippy2k8   Members   -  Reputation: 646

Like
0Likes
Like

Posted 04 September 2012 - 06:14 PM

What is it that doesn't work?

#6 mrheisenberg   Members   -  Reputation: 356

Like
0Likes
Like

Posted 04 September 2012 - 06:46 PM

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?

#7 krippy2k8   Members   -  Reputation: 646

Like
0Likes
Like

Posted 05 September 2012 - 12:15 AM

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

#8 mrheisenberg   Members   -  Reputation: 356

Like
0Likes
Like

Posted 05 September 2012 - 07:03 AM

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.

#9 krippy2k8   Members   -  Reputation: 646

Like
0Likes
Like

Posted 05 September 2012 - 02:07 PM

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



#10 mrheisenberg   Members   -  Reputation: 356

Like
0Likes
Like

Posted 06 September 2012 - 02:18 PM

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.

#11 krippy2k8   Members   -  Reputation: 646

Like
0Likes
Like

Posted 06 September 2012 - 05:46 PM

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:

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



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, 06 September 2012 - 05:51 PM.





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