Linux terminal animation is the best way to delay frame printing (in C)

I am working on a simple clone clone for a terminal and need a way to delay the print of a β€œframe”.

I have a two dimensional array

screen[ROWS][COLUMNS] 

and the function that prints the screen

 void printScreen() { int i = 0; int j; while(i < ROWS) { j = 0; while(j < COLUMNS) { printf("%c", screen[i][j]); j++; } i++; } } 

It seems like when I do

 printScreen(); usleep(1000000); printScreen(); 

it will be sleep execution during printScreen() .

Any advice for this type of animation on the terminal would be greatly appreciated. Perhaps I am doing this completely wrong. How is this done with ASCII movies like this ?

EDIT I'm going with ncurses. Thank you for the suggestion.

On Ubuntu, sudo aptitude install libncurses5-dev and compile with -lncurses .

+3
source share
3 answers

Ascii movies are run using aalib, which acts as a graphic display driver. Most people developing full-fledged console applications and games use curses frameworks or a version like ncurses. The only real restriction on this route is that you need to get full ptty (you cannot accept it).

+3
source

stdout is buffered. He will not actually send the output to the terminal device until he is prompted to print a new line or explicitly clear it.

To clear the output, simply add:

 fflush(stdout); 

Also, since all you do is print a single character, printf is an overflow. You can replace your printf:

 putchar(screen[i][j]); 
+3
source

If you understand correctly, you need to add fflush(stdout); before returning from printScreen() . But there are much better (simpler) ways to create text animation and control the terminal. Take a look at ncurses , for example.

+2
source

Source: https://habr.com/ru/post/1402056/


All Articles