Reading char from stdin without waiting for input

Thread Solved

Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Reading char from stdin without waiting for input

 
0
  #11
Aug 3rd, 2009
Standard C doesn't have anything to do with it.

You need to modify the input stream's mode to "unbuffered". You probably want to turn off "echo" also.

Both of these things are OS-dependent things to do. What OS are you targeting?
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
  #12
Aug 3rd, 2009
Originally Posted by Duoas View Post
Standard C doesn't have anything to do with it.

You need to modify the input stream's mode to "unbuffered". You probably want to turn off "echo" also.

Both of these things are OS-dependent things to do. What OS are you targeting?
I'm not targeting any OS. That's the challenge. Any standard C code should be able to run on any computer that has full support for a C compiler. I'm trying to write the program so it's 100% standard and 100% portable. Why would I do this? I don't have a clue.

I can deal with an extra character printing to the screen in a standard way. I can also handle all the graphics in a standard way. Just need to get the user input without inturrupting the game. I already suggested switching stream mode's in my first post. It look's like the best way to handle it is to have a theird key for "no input".
"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: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Reading char from stdin without waiting for input

 
0
  #13
Aug 4th, 2009
Standard C doesn't have any concept of console or graphics displays. Hence, what you are trying to do is technically impossible.

If you are using a graphics library then you should be able to get unbuffered keyboard input from it.

Otherwise, how are you doing the pong game in console? Whatever library you are using should have a way to get unbuffered keyboard input.

Every display and every computer system is different. Libraries like NCurses are written to help mitigate those differences, but they ultimately are OS/hardware bound. There is no such thing as a standard C display.

Turning off buffered-input and echo is really not that difficult to do, nor is polling the keyboard for any ready input.

All we want to know is: system/OS for each target.
Then we can point you in the right direction for the necessarily OS-dependent methods to do what you want.

Holding down a key to keep the program running is, IMO, not a good design decision.
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
  #14
Aug 4th, 2009
The point is to make it portable, not make a good game. I'm not going to make a game that's been done 1000's of time whithout a twist. The game will compile on any computer that supports C, I am not targeting any platform. Keep the thought of using a 3rd party lib out of this thread!

Assuming that the terminal is no smaller that 80x80 and no bigger than 160 high, graphics are simple without using a library. Use your brain power, you can figure it out. I'll even give you a hint: Figure out a way to erase the screen (can be done in one line of code). Then the rest should be obvious.

>Turning off buffered-input and echo is really not that difficult to do
Read my first post. I already suggested turning off the buffer. Is it portable? (my original question)
Also, turning off echo is not possible in standard C. But it was never a problem. I can deal with it.

I already figured out how to do everything besides get user input. I have already suggested turning the input stream's buffer off. I already asked if it is portable. It seems, even though bad, you can only do it by having a theird input key for "do not move".
"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
  #15
Aug 4th, 2009
Originally Posted by Hiroshe View Post
Assuming that the terminal is no smaller that 80x80 and no bigger than 160 high, graphics are simple without using a library.
Q: How can I clear the screen?
A: Such things depend on the terminal type (or display) you're using. You will have to use a library such as termcap, terminfo, or curses, or some system-specific routines, to perform these operations.


For clearing the screen, a halfway portable solution is to print a form-feed character ('\f'), which will cause some displays to clear. Even more portable (albeit even more gunky) might be to print enough newlines to scroll everything away (although of course this leaves the cursor at the bottom of the screen, not the top).
"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
  #16
Aug 4th, 2009
Yup, newlines.
"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: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Reading char from stdin without waiting for input

 
0
  #17
Aug 6th, 2009
Well, Hiroshe, since your brain power is obviously so much greater than mine, the answer to your input question is simply: you've already figured it out.

Writing portable code has nothing to do with avoiding system dependencies. Rather, it has everything to do with how your code is bound to those dependencies.

But, since you so knowingly disagree, you can continue without me.
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
  #18
Aug 6th, 2009
Originally Posted by Duoas View Post
Well, Hiroshe, since your brain power is obviously so much greater than mine, the answer to your input question is simply: you've already figured it out.

Writing portable code has nothing to do with avoiding system dependencies. Rather, it has everything to do with how your code is bound to those dependencies.

But, since you so knowingly disagree, you can continue without me.
I marked it solved at post 10, so yes I have already figured it out. You can't. Simple as that.

As long as the code can run on a platform with: a keybored, 80x80 ascii terminal and support for C, It would be (for this thread)"portable."

I know what you mean by "how your code is bound to those dependencies," but as I said, that was not the goal. If I was making the game for real, I would follow your advice (good advice which I agree with), however I want to do something different (just for the sake of doing it). I didn't mean to say your advice was not helpful, but it's out of scope.

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  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Reading char from stdin without waiting for input

 
1
  #19
Aug 6th, 2009
Sorry I was so grouchy.
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