-4

So, I am making a game, and I need input function to have deadline, and I want that time to be displayed in one of the console corners. I have timer running on separate thread. I want to show user how much time he has left, lets say to write varibale TimeLeft on end of the first line,but keep all other lines intact. So is there any function that allows me to print/display/cout variable on specific place inside console window. Working in C++, Windows platform, console app

zdf
  • 4,382
  • 3
  • 18
  • 29

1 Answers1

1

On Windows you can use SetConsoleCursorPosition() to set the position in the console, where your output from print/cout will be written. You need to pass the console handle and a COORD structure to the function. You can get the handle with GetStdHandle().

https://msdn.microsoft.com/en-us/library/windows/desktop/ms686025%28v=vs.85%29.aspx https://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx

Here's a simple example:

HANDLE hndl=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos={0,0};
SetConsoleCursorPosition(hndl, pos);

But why are you running your timer in a separate thread? There shouldn't be any need to do that. Just measure the time elapsed from the start of the game (see examples here: How to use clock() in C++), and then subtract the elapsed time from the total time limit.

Community
  • 1
  • 1
Steadybox
  • 165
  • 2
  • 7
  • I *think* that SetConsoleCursorPosition is global, so using it in a thread could interfere with the output from the main thread. WriteConsoleOutput might be a better choice. – Harry Johnston Mar 19 '16 at 22:23