Reading char from stdin without waiting for input

Thread Solved

Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Reading char from stdin without waiting for input

 
0
  #1
Aug 2nd, 2009
I'm seeing if it is possible to write a pong game only using standard C with maximum portability. I've worked out everything except user input. When the game is playing, If the user has no key pressed down, the paddle does not move but the ball should keep moving. I can only think of two way's of doing this. One is to get the user to hold down an extra key at all times. The other is to shut off the buffer on stdin using setvbuf(), which will hopefully make getchar() return EOF or NULL when no key is being pressed. Would these work? Does anyone have any other idea's?
Thanks.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: Reading char from stdin without waiting for input

 
0
  #2
Aug 2nd, 2009
I think you would have to look at the ncurses library .. or the pdcurses library for the Window's equivalent to avoid any platform specific requirements.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Reading char from stdin without waiting for input

 
0
  #3
Aug 2nd, 2009
Originally Posted by yellowSnow View Post
I think you would have to look at the ncurses library .. or the pdcurses library for the Window's equivalent to avoid any platform specific requirements.
That would be cheating. I want to see what can be done with standard C alone.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Reading char from stdin without waiting for input

 
0
  #4
Aug 2nd, 2009
Originally Posted by Hiroshe View Post
That would be cheating. I want to see what can be done with standard C alone.
Prepare to be disappointed.
http://c-faq.com/osdep/cbreak.html
Last edited by Dave Sinkula; Aug 2nd, 2009 at 3:07 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 92
Reputation: deepugtm has a little shameless behaviour in the past 
Solved Threads: 2
deepugtm deepugtm is offline Offline
Junior Poster in Training

Re: Reading char from stdin without waiting for input

 
0
  #5
Aug 2nd, 2009
I also had the same problem in past,
I think, if you are using Turbo C, under <dos.h>, there is a function called kbhit(), checks keyboard hit.
  1. while(1){
  2. // Ball movement codes,
  3. if(kbhit()){
  4. //check input ASCII and move your paddle.
  5.  
  6. }
  7. //exiting loop conditions
  8. }
Last edited by deepugtm; Aug 2nd, 2009 at 3:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Reading char from stdin without waiting for input

 
0
  #6
Aug 2nd, 2009
kbhit() function is in conio.h library and conio.h is not a standard library which is not what Hiroshe want
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Reading char from stdin without waiting for input

 
0
  #7
Aug 2nd, 2009
Is it standard behaviour for a key just pressed to outpower a key being held down? ie. The user holds down the space bar for the whole game, and the "up" key out powers the spacebar? If not, than I guess I'll have a theird key for "stay still".
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Reading char from stdin without waiting for input

 
1
  #8
Aug 2nd, 2009
Originally Posted by Hiroshe View Post
Is it standard behaviour for a key just pressed to outpower a key being held down? ie. The user holds down the space bar for the whole game, and the "up" key out powers the spacebar? If not, than I guess I'll have a theird key for "stay still".
As you saw in the link...
Originally Posted by Dave Sinkula View Post
Prepare to be disappointed.
http://c-faq.com/osdep/cbreak.html
The standard behavior is to do nothing in particular for any keypress, save the return key.
Only when the user is satisfied and presses the RETURN key (or equivalent) is the line made available to the calling program.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Reading char from stdin without waiting for input

 
0
  #9
Aug 2nd, 2009
Hmm... Then I guess the best I can do in standard C is to pause the game and get the user input (up, down, still) befor continuing on.

I'm too stubborn. Maybe instead of the space bar, get the user to hold down return. Just write an input function that ignores '\n'. That way the stream continuously updates. Would this work? I'll try it at home.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Reading char from stdin without waiting for input

 
0
  #10
Aug 2nd, 2009
Nope, It didn't work. When I hold down return and press a character, it wait's for the another return. Thanks for helping.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC