views:

503

answers:

4

Hey all,

Long time reader, first time poster :D

I'm having issues trying to read input whilst outputting at the same time. I need a server console for my game which can receive input whilst outputting and not mess up the buffer.

For example, I'm typing "Hello world" and in the process, player deaths, kills, etc. are being outputted into the console, which would result in something like:

Hello Player killed Player2world

Thanks in advance

+2  A: 

Instead of writing output directly to the console, why not spawn a GUI window?

Then, just have one area where output is directed, and a separate input area at the bottom where you can type commands. Kinda like how an irc client would look.

If it has to be console only, I would suggest using something like ncurses (or PDCurses) to create a text based interface.

Fredrick Pennachi
+1  A: 

Without thinking too hard about this, it seems to me you either need a non-blocking input routine for stdin, something like getch() but just returns if there is nothing to read, which you call in a loop while also processing output, or you need two thraeds: one to read, one to write.

jeffamaphone
The input routine is non-blocking, hence the output appearing where you're inputting when you type. See my example, italics is what is outputted whilst I'm typing "Hello world"
Saul Rennison
You may use some kind of synchronization and block/buffer one while the other is happening, or just use two different areas for input and output. I highly suggest you take @AsLanFromNarnia's suggestion and opt for the latter.
aib
A: 

You're developing the console yourself, so this should be no big problem.

The console is basically a set of text lines that are being rendered. User input is echoed so the user sees wha he's typing. This means that the last line of the console is special, it is the "editable input buffer". All other lines are output. When the user hits Enter, you execute the edit buffer. Step 1 in executing is making a private copy of the edit buffer, step 2 is clearing the edit buffer, and step 3 is copying the private copy to output.

Hence, at any moment there's only one partial line and it only changes by user input. All other lines are complete, and change on a line-by-line basis. Your program logging happens between two user inputs and therefore gets itws own line. In a multi-threaded program, this means the "Console::AddLine" function will need an internal mutex (CriticalSection for Win32).

MSalters
A: 

It sounds like you need a layer between the code and your console. Create a wrapper object that does all the console I/O. When someone calls its WriteLine method, the wrapper should erase the currently displayed input (if any), write the line, and then write the input again beneath it.

Peter Ruderman