How do you...

Started by
15 comments, last by Fractal 22 years, 6 months ago
To avoid that problem of not being able to reuse the s key for typing, you can set up a state variable that can be toggled by some other key (perhaps the one right above tab), which determines whether you''re in "typing" mode, or if you''re in the regular game mode. It will allow you to have multiple functionality for any key, determined by the state you''re in.

To give a very hacked (but understandable) example, and borrowing from Oluseyi''s code:

  // some state variable declaredbool typing = false;// main game loopwhile(not_done){  // other stuff...  //*** input section ***/  if(kbhit())  {    if (typing)      FillString();  // typing handler    else    {      // find out which key was hit      switch(getch())      {      case ''`'':        typing = true;        break;      case ''s'': // upper or lower case; use fall-through      case ''S'':        save_game();        break;      // other case labels...      }    }  }  // other stuff...}  

The typing handler would systematically get keyboard inputs and begin filling an input buffer, and would fire off some "string entered" event once the user hit enter (or whatever you find appropriate). You could also allow the user to cancel his typing request by hitting some other key, which would erase the buffer and not send a string out. Once either occurs, typing would be set to false once again.

"Don''t be afraid to dream, for out of such fragile things come miracles."
Advertisement
Hey guys,

I think he''s looking for a solution that isn''t blocking on IO. That is what the select solution did. It provided a non-blocking solution to the problem. The cin solution is a blocking solution. Is kbhit() blocking? If not, it''d work great for what he wants.

RandomTask
kbhit() return true if the keyboard has been hit. It doesn''t wait for input and it returns immediately. It''s perfect for elementary response (it''s useless if there may be simultaneous keypresses).
Now I know I may sound like a complete newb saying this (and I am ), but what''s the "while(not_done)" for? By this I mean what''s the "not_done" for? I realise that you expect me to put something else here, but what?

Could I be any more dumber?
(What do you mean, "No"?)
Could I be any dumber?(What do you mean, "No"?)
bool not_done = true;

When you''re ready to exit the program, set not_done to false. Writing
while(!done) 

has the same effect as writing
while(not_done) 

if you declare them as opposites.
by chance are you a DigiPen Student? Check out MSDN search for ReadConsoleInput or PeekConsoleInput. These will help a lot, if you use these your input is stored in a buffer you can check in some sort of game loop. This is also great for console graphics for graphics search for GetConsoleScreenBufferInfo using these you can create back buffers for awesome ASCII animations!!!! If you are infact a DigiPen student stop by Euclid if you have any questions.

PEACE
Also GetAsyncKeyState()works well, it gets the state of a specified key (up or down), it returns immediatly and doesn''t require a messagepump, message handler or any other win32 specific stuff.

This topic is closed to new replies.

Advertisement