If you are using a Linux terminal, you can adjust the termios structure in order to turn off buffered input. I used this back when I was in school for a terminal based game, and for more user-friendly data entry into the terminal. I'll try to dig up the snippit for you and edit this post if and when I find it.
/*This code was retyped manually from the terminal*/
/* BufferToggle.hpp
* @Author
* Luc Lieber
*
* A simple class to toggle buffered input
*/
#include <termios.h>
class BufferToggle
{
private:
struct termios t;
public:
/*
* Disables buffered input
*/
void off(void)
{
tcgetattr(STDIN_FILENO, &t); //get the current terminal I/O structure
t.c_lflag &= ~ICANON; //Manipulate the flag bits to do what you want it to do
tcsetattr(STDIN_FILENO, TSCANOW, &t); //Apply the new settings
}
/*
* Enables buffered input
*/
void on(void)
{
tcgetattr(STDIN_FILENO, &t); //get the current terminal I/O structure
t.c_lflag |= ICANON; //Manipulate the flag bits to do what you want it to do
tcsetattr(STDIN_FILENO, TSCANOW, &t); //Apply the new settings
}
};
_Example_
1 2 3 4 5 6 7 8 9 10
#include <iostream>
int main()
{
BufferToggle bt;
std::getchar(); //waits for you to press enter before proceeding to the next instruction
bt.off();
std::getchar(); //processes next instruction as soon as you type a character (no enter)
bt.on();
std::getchar(); //waits for you to press enter before proceeding to the next instruction
}
Please, feel free to expand upon it in any way you see fit.
And i don't think getch() and unbuffered input would work cause, Im trying animation using asci art, so, i cant wait for input.
_getch() will work fine for the purpose of getting input, its just that _getch() is often used in conjunction with _kbhit(). This notifies you if a key has been pressed.
Of course this is an infinite loop. You must code the following somewhere in your code to end the program under whatever circumstances you deem worthy.
You can use the preprocessor to make the code as portable as you want. In the console based game that I made, there was a user input thread as well as a thread used for the console clearing and redrawing.