我正在尝试在一些将要更新的文本周围绘制一个框。在占位符中,文本是静态的,但仍然会观察到闪烁。我的代码在
之下 /*
* @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()
的各种组合,但我无法找到任何不会产生闪烁的组合。
如何阻止这种闪烁?
答案 0 :(得分:0)
问题是,您对win
和curscr
的更新会更新当前屏幕(win
),因为他们在同一区域内写信,他们正在做很多重新粉刷。
您可以通过创建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()
来获取字符。该框只需要刷新一次(在循环之前),wrefresh
仍然会刷新窗口:删除最后一个win
并在创建box
并调用{{}后移动另一个1}}:
WINDOW *win = newwin(10, 20, 1, 0);
box(win, 0, 0);
wrefresh(win);