Reputation: 1
I'm writing simple Tetris game and this is function that prints out field with figures in one game tick. I call this proc in while-loop until the game ends and i want my program to "update" game field(not to type a new one every tick).
void printTetGame(TetGame *tetg)
{
TetField *tf = tetg->field;
TetFigure *t = tetg->figure;
for (int i = 0; i < tf->height; i++)
{
for (int j = 0; j < tf->width; j++)
{
int sym = 0;
if (tf->blocks[i * tf->width + j].b != 0)
sym = 1;
else // if one of figure's blocks is here(on this coordinates)
{
int x = j - t->x;
int y = i - t->y;
if (x >= 0 && x < t->size && y >= 0 && y < t->size)
if (t->blocks[y * t->size + x].b != 0)
sym = 1;
}
printf("%d", sym);
}
}
fflush(stdout); // buffer clean -> field is expectaed to update(not to print one more time)
};
So, i use fflush to clean stdout buffer after field is printed. BUT it continues to print the whole field with figures every game tick. Idk what i'm doing wrong. HELP please.
Upvotes: -1
Views: 120
Reputation: 505
It looks like you are expecting fflush()
to clear the screen, or at least reposition to the top of the screen so that the next set of prints will overwrite (overprint) the new game field on top of the previous tick's output.
That's not what fflush()
does. I'll explain fflush a bit farther down.
To solve your problem, you'll have to do "cursor addressing". You can either do it yourself by learning the "escape sequences" that your terminal uses to position the cursor, or preferably you'll use a library, like ncurses. This library understands how your terminal works and sends the necessary escape codes.
Regarding fflush()
, you probably don't need to use it at all, but it's still something you should know about, so I'll give a mini tutorial.
Very often you will be interested in maximizing the efficiency of your programs. Printing your program's output can be very inefficient. Take your program, for example. You call printf()
for each character, which is a lot of calls to update the screen once.
The printf()
function (and functions that it calls) are written to save up your output from multiple calls. That output is saved in an internal buffer. At various times, it will decide to send all that buffered output to the final destination (which might be your terminal screen, or might be a disk file, or other possible destinations as well). By not sending every call to printf to the final destination and instead buffering multiple calls worth of output and writing it to the destination in a single go, you can greatly increase the efficiency of the whole program.
The fflush()
function allows you to control this buffering process more directly. The call to fflush()
basically tells the I/O software to take any output that's sitting in the buffer waiting to be sent to go ahead and send it to the destination immediately.
When a program is writing to standard output associated with a terminal, it is "line-buffered". I.e. it saves up the output until you print a newline ('\n'). This means that for normal lines ending with a newline, you don't need fflush()
. But since your program does not print any newlines, the fflush()
is helpful to get your printed text sent to your screen in a timely way.
There are other forms of buffering, by the way, beyond the scope of this answer.
One final note. You may have seen programs that do a printf without a newline as a prompt, followed by a read of stdin (perhaps using scanf). The prompt gets flushed even though the prompt string did not have a newline. That seems counter to the above description.
As it happens, interactive input and output historically have a special relationship. Doing a read on a terminal session will perform an implicit flush on terminal output. But note that this behavior is "implementation dependent" and is not guaranteed for all time, so to ensure the desired behavior you should include fflush()
calls when you need the output flushed.
Upvotes: 2