Archived

This topic is now archived and is closed to further replies.

DirectInput -> Framerate Drops

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

I''ve got a working 3D Engine. Now the next thing to add was DirectInput functionality. In the beginning only for the keyboard. I''ve added the following code (which is important for the speed I think).
// Macros
#define    KeyDown(data, n)    ((data[n] & 0x80) ? true : false)
#define    KeyUp(data, n)    ((data[n] & 0x80) ? false : true)

// Globals
LPDIRECTINPUT lpdi;
LPDIRECTINPUTDEVICE m_keyboard;
unsigned char keystate[256];
 
This for the "initialisation part". Then I''ve ONLY added the following in the Render loop:
if (FAILED(m_keyboard->GetDeviceState(sizeof(unsigned char[256]), (LPVOID)keystate))) //{ return; }
if (KeyDown(keystate, DIK_LCONTROL)) { return; }
 
This 2 rules in the render loop, makes my framerate drop from 86fps to 71fps, a difference of 15fps! And as you can see, I''m not doing very much yet with the keyboard input. Is this cause of DirectInput, or can I just not program? :D And what can I do about it Thnx!

Share this post


Link to post
Share on other sites
it was a long time since i used unbuffered input but shouldn't
if (FAILED(m_keyboard->GetDeviceState(sizeof(unsigned char[256]), (LPVOID)keystate))) //{ return; } 

change to
if (FAILED(m_keyboard->GetDeviceState(sizeof(unsigned char[256]), (LPVOID)&keystate))) //{ return; }  


[edited by - Metus on June 3, 2003 7:22:54 PM]

Share this post


Link to post
Share on other sites
soo?

If you want to use dinput, it will cause a little performance hit at first glance.

BUT consider that you want to move, turn, strafe, open a menu, etc.

If you tried to do all that in the windows message pump, it would be much much more of a drop in frame time than if you did it in DInput.

EDIT:

I also found a few things you could optimize in your code.

In your globals, after keydown etc do this:

char buffer[256];

then in the actual loop

Keyboard->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);

then you can use your keydown/keyup macros

if(keydown(buffer,DIK_ESCAPE)
//do something;

I hope this helped!

[edited by - RhoneRanger on June 3, 2003 10:37:49 PM]

Share this post


Link to post
Share on other sites
Dont''t query the keyboard state every frame - that''s overkill!
Setup a seperate thread instead that queries the keyboard state
every 20ms or so.

Share this post


Link to post
Share on other sites

darookie, I disagree totally. You should query the device every frame.

Besides if you did query every 20 ms, that is 50 fps!! it is easier to query each frame (which MIGHT cause a 10 ms diff) then to check to see if 20 ms has passed.

Besides, most current games run at 40 fps or so, so checking would be useless.



Share this post


Link to post
Share on other sites
I don't mean to be argumentative, but what darookie said was extremely close to what I was thinking. It seems useless to query the input so many times per second. You don't have to limit yourself to multi-threading; there should be other options available, even a small counter so it checks every 5th or 10th loop. But I would give it some more thought before abandoning the idea. It could really give your frame rate a boost. Just make sure your frame rate stays relatively constant throughout.





[JESUS SAVES|Planet Half-Life]




[edited by - gilligancoder on June 8, 2003 12:56:28 AM]

Share this post


Link to post
Share on other sites