'ncurses blank lines (newline) not being displayed on terminal/console window
I'm following a ncurses tutorial from here: https://www.viget.com/articles/game-programming-in-c-with-the-ncurses-library/
It shows how to make a simple bouncing character that goes left to right on the terminal window.
Here is the code, I modified it slightly:
#include <ncurses.h>
#include <unistd.h>
#include <string.h>
#define DELAY 30000
int main(int argc, char *argv[])
{
int x = 0, y = 0;
int max_y = 0, max_x = 0;
int next_x = 0;
int direction = 1;
initscr(); // Initialize the window
noecho(); // Don't echo any keypresses
curs_set(TRUE); // Don't display a cursor
getmaxyx(stdscr, max_y, max_x); // Global var `stdscr` is created by the call to `initscr()`, get screen dimensions.
while(1)
{
clear();
mvprintw(y, x, "o"); // Moving object
// Some basic info being displayed to know how the object is moving.
mvprintw(1,1, "direction: %d", direction);
mvprintw(2,1, "next_x: %d", next_x);
mvprintw(3,1, "x: %d", x);
mvprintw(4,1, "max_y: %d max_x: %d ", max_y, max_x);
mvprintw(5,1, "\n\n\n"); // <---- Here is the newline, not displayed on terminal.
refresh();
usleep(DELAY);
next_x = x + direction;
if (next_x >= max_x || next_x < 0)
{
direction*= -1;
}
else
{
x+= direction;
}
}
endwin();
}
Based on my code, I should get three newlines (blank lines), but terminal does not display it after I stop the program and scroll up.
These are the results displayed when I scroll up to the terminal after stopping the program:
o
direction: 1
next_x: 32
x: 32
max_y: 45 max_x: 162
o
direction: 1
next_x: 33
x: 33
max_y: 45 max_x: 162
o
direction: 1
next_x: 34
x: 34
max_y: 45 max_x: 162
o
direction: 1
next_x: 35
x: 35
max_y: 45 max_x: 162
o
direction: 1
next_x: 36
x: 36
max_y: 45 max_x: 162
o
direction: 1
next_x: 37
x: 37
max_y: 45 max_x: 162
o
direction: 1
next_x: 38
x: 38
max_y: 45 max_x: 162
o
direction: 1
next_x: 39
x: 39
max_y: 45 max_x: 162
o
direction: 1
next_x: 40
x: 40
max_y: 45 max_x: 162
As you can see, absolutely no newline is being displayed, between each iteration. I have tried null characters and it does not work either. For some reason, terminal discards newline or null characters.
How can I make terminal show the three blank new lines when I scroll upwards after stopping the program?
Using FreeBSD 13.1, clang compiler, headless, no desktop environment.
Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|