I'm working on console based FPS/Clock display for debugging an Allegro game I'm working on, and I've been trying to figure out how to get the console output to change without having to stick an arbitrary system("cls") at the top of my game loop.
Right now I have...
while(game.running())
{
std::cout
<< "\r" << "FPS: " << getFrameRate()
<< "\n"
<< "\r" << "TIME: " << getTime();
// Other game loop things...
}
What I'm going for is something like this, where the line would be rewritten each time.
FPS: ___
TIME: ___
(where ___ is whatever the current value is)
But when I run I get...
FPS: ___
FPS: ___
FPS: ___
TIME: ___
↑ goes on and on... fills the screen with FPS:__ and a single TIME:__ at the very bottom without overwriting.
'\r' has always confounded me, and I haven't found anything useful on Google. Any help is appreciated.
答案 0 :(得分:1)
When you emit '\r', it puts the cursor on the beginning of the current line. However, if you move the current line downward by emitting a '\n', then the next '\r' is going to return to the beginning of this new line.
You're going to have to emit the terminal characters to return the caret to the top-left of the screen.
答案 1 :(得分:1)
How about using putchar(int) ? Like putchar(0x08) backspace or so.
I'm not sure, but the problem is CR(0x0D Carriage Return) and LF(0x0A Line Feed) doesn't work as you expected. Maybe it is because std::cout class will flush to standard out ( in your case the terminal ) only after several getFrameRate() call.