4

I'm trying to write a simple terminal-based dungeon-style game in C, but I want to give the user real-time feedback on various processes that they affect.

I remember using kbhit() years ago, but I also remember the cpu going to 100% during the life of the program. I'd like to avoid this entirely. And I'm working in Linux now.

Let's say that you're in a dungeon in a text-based adventure game. The dungeon is sealed off. There's only so much air in the room. The longer you remain there, the lower the oxygen gets. I want to report the oxygen level in the room in real-time, while at the same time accepting commands from the user.

Is there a way to do this?

EDIT: The one idea I had was to use two terminal windows. Commands are taken in one terminal, the results of those commands could be written to a text file. The other terminal will have a program running that runs real-time stuff, and checks periodically on the same text file for updates on rates, etc. Ideally, I'd like to do this in one window.

Korgan Rivera
  • 495
  • 3
  • 9
  • There are a lot of ways to do what you're trying; are you asking for a library recommendation? – Carl Norum Jun 07 '14 at 20:12
  • Try a loop that sets a timer and then waits for a specific time to elapse *or* [a key is pressed](http://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input). – Jongware Jun 07 '14 at 20:21
  • @Jongware I was hoping someone would suggest a method more efficient than polling. – Korgan Rivera Jun 08 '14 at 05:06
  • Just to be sure: [did you take a look at this related?](http://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input?lq=1) – Natan Streppel Jun 08 '14 at 15:20

1 Answers1

3

Sure there is a way to do that. Have a look at ncurses. I think that is the way to go.

Andreas
  • 300
  • 5
  • 15