User Input Hassles

Published November 28, 2008
Advertisement
I'm not actually sure how to proceed here. User input is easy enough on its own, but I need to continually Draw() at a certain framerate as well as watch for user input (and act on it wen it comes in). I created a Keyboard class, but I'm really not sure how to do this.

I want to draw, at the moment, once every 0.25 seconds. I also want to watch for user input constantly, and move the "camera" every time a key is pressed (but only allow them to move once every so often). And I don't want to continually loop, because I think that hogs CPU; I just want to do something if there's something to do, or it's time to draw.

The only thing I've managed to do, that works in some respect, checks the console input buffer every time through the loop and sets the appropriate element in an array of bools depending on what key event comes through. Then it draws, and Draw() checks the time elapsed between the last time we drew and now, and if we're good to go, it checks the arrow key booleans and moves accordingly. The problem here is that it only works if we held the key down when Draw() actually draws. If we hit an arrow key after 50ms since the last Draw(), and let go at 100ms since the last draw, it's like we never hit the key at all - Draw() doesn't recognize that we tried to move. And it keeps looping whether we have anything to do or not.

Obviously I'm new to handling keyboard input like this. I think WaitForMultipleObjects() could help (because it can wait on an input buffer handle), but I still have no idea how to proceed. A poke in the right direction would be hugely appreciated! =\
~Jonathan
Previous Entry Wrapping world display
Next Entry Keyboard input
0 likes 5 comments

Comments

matt_j
For my text-mode engine I just set a flag that says that the screen (or part of it, at least) should be refreshed. It is cleared after the refresh.

Also, my CHAR_INFO array that represents the screen is global - never allocated on draw. So, if I make a small change to the screen I don't have to re-fill the whole thing. Dunno if that's helpful or not.
November 28, 2008 08:58 PM
matt_j
Also, you can use PeekConsoleInput to check if any keys are read. I don't think it should hog the CPU that much. WriteConsoleOutput is probably pretty fast, I bet.
November 28, 2008 09:01 PM
Twisol
November 28, 2008 11:52 PM
Twisol
Quote:Original post by matt_j
For my text-mode engine I just set a flag that says that the screen (or part of it, at least) should be refreshed. It is cleared after the refresh.

Also, my CHAR_INFO array that represents the screen is global - never allocated on draw. So, if I make a small change to the screen I don't have to re-fill the whole thing. Dunno if that's helpful or not.


On the first one, that's a good idea. I'll look into it when I have other moving entities on the map, but right now I only -really- need to draw when I get user input.

On the second one, I have a World class which keeps an internal world layout map, and the Draw() funtion just snips out a given rect and puts it to the screen. I'll worry about figuring out which parts of the screen need updating later; right now I just want this to work first.
November 28, 2008 11:52 PM
Twisol
For some freakish reason, my response came up before your second comment Pretend your two came first. *scratches head*

EDIT: And so did this one, at least on my screen...
November 28, 2008 11:53 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement