【问题标题】:Ncurses text inside box flickering on refresh刷新时框内的Ncurses文本闪烁
【发布时间】:2018-01-09 15:31:26
【问题描述】:

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

 /*
 * @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() 的各种组合,但我找不到任何不会产生闪烁的组合。

如何停止这种闪烁?

【问题讨论】:

    标签: c command-line-interface ncurses


    【解决方案1】:

    问题是您对stdscrwin 的更新交替更新当前屏幕 (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);
    

    【讨论】:

      猜你喜欢
      • 2014-01-30
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 2020-02-29
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多