DirectInput -> Framerate Drops

Started by
6 comments, last by BlueLagoon 20 years, 10 months ago
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!
Advertisement
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]
Ethereal
It''s not that much... The actual time it takes to finish the frame only goes up by 3 ms.
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
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]
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
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.
sorry double post

[edited by - RhoneRanger on June 3, 2003 11:29:11 PM]
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno

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.



TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
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]

This topic is closed to new replies.

Advertisement