Hello, I decided to create a simple console game (something like tennis) but i have a problem. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void obj_move(x,y){
// do something
// maybe move a ball?
}
int getinput(){
getch(); //!!!!
}
int main(){
while(true){
obj_move(x,y);
getinput();
}
return 0;
}
i need refresh ball position every 10 miliseconds and get user input but if user doesn't entered anything then ball won't move!
I need to check if user entered something then do something with that input but if there's no input continue the program. Any ideas?
Thanks.
There are two choices: a) get input in a separate thread, or b) use a non-blocking version of getch() (http://en.wikipedia.org/wiki/Blocking_(computing)).
For a), you can use either system calls or Boost. Boost makes threading very easy.
I don't know if conio has a non-blocking version of getch(), but I know that curses* does. If you're going to use curses, I think it's a good idea to stop using conio.
* curses is a traditional UNIX library used for console I/O. There currently exist two clones of it: ncurses, which is LGPL'd, and PDcurses, which is in the public domain.