Sign in to follow this  
  • entries
    122
  • comments
    121
  • views
    68500

User Input Hassles

Sign in to follow this  

303 views

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
Sign in to follow this  


5 Comments


Recommended Comments

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.

Share this comment


Link to comment
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.

Share this comment


Link to comment
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.

Share this comment


Link to comment
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...

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now