框内的 Ncurses 文本在刷新时闪烁
Ncurses text inside box flickering on refresh
我正在尝试在一些将要更新的文本周围画一个框。在占位符中,文本是静态的,但仍然可以观察到闪烁。我的代码如下
/*
* @file main.cc
* @author Tom Eaton
* @date 09 Jan 2018
* @brief Command line interface to show Hawkcell PCB data.
*/
#include <iostream>
#include <string>
#include <cstring>
#include <ncurses.h>
using namespace std;
/*
* Prints string in the horizontal center of row specified.
* @param row - Row that text should be printed on
* @param mesg - Pointer to char array containing message
*/
void printHorizCenter(int row, char* mesg) {
mvprintw(row, ((COLS - strlen(mesg))/2)-1, mesg);
}
/*
* Reads version of PCB and updates char array with version
* @param outVersion - Pointer to char array where version should be stored.
* @param port - Reference to serial port
*/
void printHorizBlock(int row, int startCol, int endCol) {
for(int i = startCol; i<=endCol; i++) {
mvaddch(row, i, ACS_BLOCK);
}
}
int main(int argc, char** argv) {
//Initialise curses
initscr();
cbreak();
echo();
keypad(stdscr, 1);
//Make character capture non blocking
nodelay(stdscr, 1);
//Define char arrays for each cell
char cellOne[2] = {'1', '\n'};
char cellTwo[2] = {'2', '\n'};
char cellThree[2] = {'3', '\n'};
char cellFour[2] = {'4', '\n'};
//Setup curses colour schemes
start_color();
use_default_colors();
curs_set(0);
//Setup curses colour pairs
init_pair(1, COLOR_RED, -1);
init_pair(2, COLOR_WHITE, COLOR_RED);
WINDOW *win = newwin(10, 20, 1, 0);
//Main loop
while (1) {
if(getch() == 12) {
endwin();
cerr << "You pressed left and exited" << endl;
exit(1);
}
//Print top title bar
attron(COLOR_PAIR(1));
printHorizBlock(0, 0, (COLS/2) - (strlen("HawkCell")/2));
attroff(COLOR_PAIR(1));
attron(COLOR_PAIR(2));
printHorizCenter(0, (char *)"HawkCell");
attroff(COLOR_PAIR(2));
attron(COLOR_PAIR(1));
printHorizBlock(0, (COLS/2) + (strlen("HawkCell")/2) - 1, COLS);
attroff(COLOR_PAIR(1));
//Print voltage data
mvprintw(3, 1, "Cell 1: %.3fV\n", 4.0f);
mvprintw(4, 1, "Cell 2: %.3fV\n", 4.0f);
mvprintw(5, 1, "Cell 3: %.3fV\n", 4.0f);
mvprintw(6, 1, "Cell 4: %.3fV\n", 4.0f);
mvprintw(7, 1, "Total: %.3fV\n", 4.0f);
//Print bottom info bar
attron(COLOR_PAIR(1));
printHorizBlock(LINES-1, 0, COLS);
attroff(COLOR_PAIR(1));
attron(COLOR_PAIR(2));
printHorizCenter(LINES-1, (char *)"ctrl+c: Exit, ctrl+l: Toggle logging ");
attroff(COLOR_PAIR(2));
//Call curses refresh function to update screen
box(win, 0, 0);
//touchwin(win);
wrefresh(win);
wrefresh(stdscr);
}
getch();
endwin();
return 0;
}
问题是屏幕重绘时出现闪烁,如下图gif所示(感光警告)Gif showing problem。如您所见,它只发生在包含文本的行上,这让我觉得在编写文本时正在绘制整条线,而不仅仅是特定字符。
我试过使用 wrefresh()
、touchwin()
、erase()
和 refresh()
的各种组合,但我找不到任何不产生闪烁的组合。
如何停止这种闪烁?
问题是您对 stdscr
和 win
的更新交替更新 当前屏幕 (curscr
),并且由于它们写在同一个区域,他们正在重画很多。
您可以通过创建 win
的子window 并使用 that window 来编写文本来改进这一点。
类似
WINDOW *txt = derwin(win, 8, 18, 1, 1);
... 使用 stdscr
:
修改所有调用
mvwprintw(txt, 7, 1, "Total: %.3fV\n", 4.0f);
//Print bottom info bar
wattron(txt, COLOR_PAIR(1));
并使用 wgetch(txt)
而不是 getch()
来获取字符。该框只需要刷新一次(在循环之前),并且 wgetch()
无论如何都会刷新 window:移除最后一个 wrefresh
并在创建 win
之后移动另一个并调用 box
:
WINDOW *win = newwin(10, 20, 1, 0);
box(win, 0, 0);
wrefresh(win);
我正在尝试在一些将要更新的文本周围画一个框。在占位符中,文本是静态的,但仍然可以观察到闪烁。我的代码如下
/*
* @file main.cc
* @author Tom Eaton
* @date 09 Jan 2018
* @brief Command line interface to show Hawkcell PCB data.
*/
#include <iostream>
#include <string>
#include <cstring>
#include <ncurses.h>
using namespace std;
/*
* Prints string in the horizontal center of row specified.
* @param row - Row that text should be printed on
* @param mesg - Pointer to char array containing message
*/
void printHorizCenter(int row, char* mesg) {
mvprintw(row, ((COLS - strlen(mesg))/2)-1, mesg);
}
/*
* Reads version of PCB and updates char array with version
* @param outVersion - Pointer to char array where version should be stored.
* @param port - Reference to serial port
*/
void printHorizBlock(int row, int startCol, int endCol) {
for(int i = startCol; i<=endCol; i++) {
mvaddch(row, i, ACS_BLOCK);
}
}
int main(int argc, char** argv) {
//Initialise curses
initscr();
cbreak();
echo();
keypad(stdscr, 1);
//Make character capture non blocking
nodelay(stdscr, 1);
//Define char arrays for each cell
char cellOne[2] = {'1', '\n'};
char cellTwo[2] = {'2', '\n'};
char cellThree[2] = {'3', '\n'};
char cellFour[2] = {'4', '\n'};
//Setup curses colour schemes
start_color();
use_default_colors();
curs_set(0);
//Setup curses colour pairs
init_pair(1, COLOR_RED, -1);
init_pair(2, COLOR_WHITE, COLOR_RED);
WINDOW *win = newwin(10, 20, 1, 0);
//Main loop
while (1) {
if(getch() == 12) {
endwin();
cerr << "You pressed left and exited" << endl;
exit(1);
}
//Print top title bar
attron(COLOR_PAIR(1));
printHorizBlock(0, 0, (COLS/2) - (strlen("HawkCell")/2));
attroff(COLOR_PAIR(1));
attron(COLOR_PAIR(2));
printHorizCenter(0, (char *)"HawkCell");
attroff(COLOR_PAIR(2));
attron(COLOR_PAIR(1));
printHorizBlock(0, (COLS/2) + (strlen("HawkCell")/2) - 1, COLS);
attroff(COLOR_PAIR(1));
//Print voltage data
mvprintw(3, 1, "Cell 1: %.3fV\n", 4.0f);
mvprintw(4, 1, "Cell 2: %.3fV\n", 4.0f);
mvprintw(5, 1, "Cell 3: %.3fV\n", 4.0f);
mvprintw(6, 1, "Cell 4: %.3fV\n", 4.0f);
mvprintw(7, 1, "Total: %.3fV\n", 4.0f);
//Print bottom info bar
attron(COLOR_PAIR(1));
printHorizBlock(LINES-1, 0, COLS);
attroff(COLOR_PAIR(1));
attron(COLOR_PAIR(2));
printHorizCenter(LINES-1, (char *)"ctrl+c: Exit, ctrl+l: Toggle logging ");
attroff(COLOR_PAIR(2));
//Call curses refresh function to update screen
box(win, 0, 0);
//touchwin(win);
wrefresh(win);
wrefresh(stdscr);
}
getch();
endwin();
return 0;
}
问题是屏幕重绘时出现闪烁,如下图gif所示(感光警告)Gif showing problem。如您所见,它只发生在包含文本的行上,这让我觉得在编写文本时正在绘制整条线,而不仅仅是特定字符。
我试过使用 wrefresh()
、touchwin()
、erase()
和 refresh()
的各种组合,但我找不到任何不产生闪烁的组合。
如何停止这种闪烁?
问题是您对 stdscr
和 win
的更新交替更新 当前屏幕 (curscr
),并且由于它们写在同一个区域,他们正在重画很多。
您可以通过创建 win
的子window 并使用 that window 来编写文本来改进这一点。
类似
WINDOW *txt = derwin(win, 8, 18, 1, 1);
... 使用 stdscr
:
mvwprintw(txt, 7, 1, "Total: %.3fV\n", 4.0f);
//Print bottom info bar
wattron(txt, COLOR_PAIR(1));
并使用 wgetch(txt)
而不是 getch()
来获取字符。该框只需要刷新一次(在循环之前),并且 wgetch()
无论如何都会刷新 window:移除最后一个 wrefresh
并在创建 win
之后移动另一个并调用 box
:
WINDOW *win = newwin(10, 20, 1, 0);
box(win, 0, 0);
wrefresh(win);