943,808 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2897
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Aug 3rd, 2009
0

Re: Reading char from stdin without waiting for input

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?
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Aug 3rd, 2009
0

Re: Reading char from stdin without waiting for input

Click to Expand / Collapse  Quote originally posted by Duoas ...
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".
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Aug 4th, 2009
0

Re: Reading char from stdin without waiting for input

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Aug 4th, 2009
0

Re: Reading char from stdin without waiting for input

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".
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Aug 4th, 2009
0

Re: Reading char from stdin without waiting for input

Click to Expand / Collapse  Quote originally posted by Hiroshe ...
Assuming that the terminal is no smaller that 80x80 and no bigger than 160 high, graphics are simple without using a library.
Quote ...
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.


Quote ...
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).
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 4th, 2009
0

Re: Reading char from stdin without waiting for input

Yup, newlines.
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Aug 6th, 2009
0

Re: Reading char from stdin without waiting for input

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Aug 6th, 2009
0

Re: Reading char from stdin without waiting for input

Click to Expand / Collapse  Quote originally posted by Duoas ...
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.
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Aug 6th, 2009
1

Re: Reading char from stdin without waiting for input

Sorry I was so grouchy.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Return 0;
Next Thread in C Forum Timeline: urgent





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC