0

I have a Mac, so I don't have access to any windows.h functions, or any other windows libraries in C++. I am creating a game in C++. The controls of the game are the characters w,a,s,d. Currently I have the input method as std:cin. However, the problem is after the user types each character they must hit enter every time. Furthermore, the fact the std::cin does not timeout means that the user can essentially 'pause' the game to think about what move to make next (which ruins much of the fun of this game).

I need a function like std::cin but with a timeout of about .25 seconds. A function that will return as soon as the user types the character (without the need of the user hitting enter) would also work; but a function like std::cin with a timeout would be preferred. Please don't suggest window's library functions as I am again a Mac user using terminal.

Are there any standard c++ functions within the standard mac Libraries which will function equivalently to std::cin with a timeout of T that will run correctly in a Mac terminal?

Zac Wimer
  • 89
  • 5
  • 3
    You'll have to brew this your own. With linux/unix based systems (isn't Mac OS similarly such?) you can use a `poll()` loop listening to `fd = 1`. – πάντα ῥεῖ Sep 05 '14 at 18:05
  • 4
    [`std::cin`](http://en.cppreference.com/w/cpp/io/cin) is for reading formatted input. You need something completely different. For example: https://www.libsdl.org/ – moooeeeep Sep 05 '14 at 18:07
  • 1
    A similar question: http://stackoverflow.com/questions/421860/c-c-capture-characters-from-standard-input-without-waiting-for-enter-to-be-pr – Amadeus Sep 05 '14 at 18:41

2 Answers2

1

Any reason you can't use int istream::get() ?

It's a blocking call though. I'm sure there is a better way to do it, but the quickest hack I can think of is to spawn a thread (just before calling get()) that waits for 0.25 secs and then writes some 'timeout char' say 't' to teh stream.

Kashyap
  • 15,354
  • 13
  • 64
  • 103
  • I cannot use cin.get() because that function as well requires the user to hit return before the program receives the character as an input. – Zac Wimer Sep 05 '14 at 18:18
  • @Zac: Not true. Apparently you have line buffering on in your terminal. – Lightness Races in Orbit Sep 05 '14 at 18:35
  • @ZacWimer `get()` and `operator >>()` work differently. That's kinda the point. `operator >>()` returns when you give it a line feed (like enter); `get()` will return on first keystroke. – Kashyap Sep 06 '14 at 01:25
  • I think I do, but as well in the IDE I have been using. I will try to switch it off. Thanks. – Zac Wimer Sep 06 '14 at 02:42
1

You can use the ncurses library, putting the terminal in cbreak() (non-blocking) mode, and disabling input delay with nodelay():

WINDOW *window = initscr();
cbreak();
nodelay(window, TRUE);

Then the getch() function will not block, returning ERR if no character is ready, so you can poll it in a loop and break when your timeout has been reached.

However, this may also require that you use ncurses for screen output, which may be a dealbreaker.

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166