gpt4 book ai didi

c - 刷新时框内的 Ncurses 文本闪烁

转载 作者:行者123 更新时间:2023-11-30 16:37:00 26 4
gpt4 key购买 nike

我正在尝试在一些将要更新的文本周围绘制一个框。在占位符中,文本是静态的,但仍然观察到闪烁。我的代码如下

 /*
* @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 showing problem 。正如您所看到的,它只发生在包含文本的行上,这让我认为在编写文本时会绘制整行,而不仅仅是特定字符。

我尝试过使用 wrefresh()touchwin()erase()refresh() 的各种组合 但我找不到任何不产生闪烁的。

如何停止这种闪烁?

最佳答案

问题是您对 stdscr 的更新和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 :

WINDOW *win = newwin(10, 20, 1, 0);
box(win, 0, 0);
wrefresh(win);

关于c - 刷新时框内的 Ncurses 文本闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48171567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com