• Advertisement
Sign in to follow this  

DirectInput and Speed

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

Ok, I'm new to DirectX, mostly because my school taugh Java and Java only. Now, I'm converting a game I made in Java as a project into a full fledged 3D game with DirectX. Right now my big problem is slowing the program down. I'm starting on the menu system, but 1 keypress acts like 8 or 9. I could just add a big slow for loop, but that's not proper programming practices. Is there a way to have the input reacted upon on frame and not multiple frames? I can't seem to find anything about this type of problem, but maybe I've missed it. So sorry in advance if I'm reposting about it. Replies for be most welcomed. Thanks. Raikan

Share this post


Link to post
Share on other sites
Advertisement
This seems like an odd problem did you check the directx sdk? If you follow there example of how to setup direct input for keyboard device, it will work with virtually no lag; assuming its setup correctly.

Share this post


Link to post
Share on other sites
/*The solution is simple: take a double buffer and check the state of the previous frame tick.*/

char Buffer[256], PrevBuffer[256];

/*Every frame*/
memcpy(PrevBuffer, Buffer, sizeof(Buffer));
YourDirectInputDevice->GetDeviceState(Buffer, sizeof(Buffer));

/*checking the state of a key work in this way*/
#define CHECK_KEY_STATE(key) ((Buffer[key] & 0x80)&&(!(PrevBuffer[key] & 0x80)))

/*with this algorythm if you press a key at time T the system will catch only the T time key press all others will be ignored because of the check on PrevBuffer wich hold the key state of the previous frame*/

Share this post


Link to post
Share on other sites
DevLiquidKnight, my DirectInput setup is pretty well a direct copy of the SDK's example. Still too fast.

Thanks for the idea bjogio, just tried it. It's a little slower, but still is /way/ too fast. I just put in a framerate calculator. I'm thinking that 300fps is a little fast. Every frame get keyboard input. That's what I need to prevent. I need one frame processed every second... anyone know how I could do such a thing?

Share this post


Link to post
Share on other sites
It might not be a good idea to check the keyboard/mouse state every frame, but more like 1/60 or 1/30 of a second base.

Your could do the movement control on a 60 checks/sec. base and the normal things like keyboard input on 12 checks/sec. base.

I use timers to do get a specific interval.

Share this post


Link to post
Share on other sites
No, you really do want to be checking state as often as possible - you'll be more responsive to user input that way. You might want to look up the docs for the 'buffered' input mode for DirectInput; using buffered input will give you KEYDOWN/KEYUP type events, so you can just react to KEYUPs.

The alternative is to figure out why bjogio's solution isn't working, because it should eliminiate all duplicate signals. Check yer implementation.

You definitely don't want a framerate of 1 frame per second. That's what most people spend their lives trying to avoid.

Share this post


Link to post
Share on other sites
I'm not looking for 1 frame per second rendering, I said to process input at around 1 frame per second. Though, the buffered input with KeyUp and KeyDown should work... hrmm... have to look that one up.

Share this post


Link to post
Share on other sites
You could use windows messages to get keypresses where you dont need extreme speed. Look for the message WM_KEYDOWN.

Graham.

Share this post


Link to post
Share on other sites
Quote:
Original post by Raikan
I'm not looking for 1 frame per second rendering, I said to process input at around 1 frame per second.


Where did I say "rendering?" The term 'framerate' doesn't just apply to rendering, you know. It just means the rate at which you capture / emit sampling frames.

A framerate of 1 FPS for your input system will cause it to miss any keypress which starts just after a sample frame and ends just before the next one.

Share this post


Link to post
Share on other sites
In these dark days of programming I've realized that for a fast action game catching inputs 1/60 sec is enough. Missing a key press is rather impossible (You have to do > 3600 key press at time and I know only a person capable of that speed). For rts/rpg 1/30 is enough. Just segmentate your code and make different update for physic/render/input/net. Personally I made something like this:
physic 1/60 sec
render everytime (or vsynch driven)
input 1/60 sec
net 1/24 sec
for making the rule you can just take the D (delta time passed from last update)
D += DeltaTimePassedInTheLastFrame;
if(D >= rule) Update();
easy.

Share this post


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

  • Advertisement