Refresh Behaviour (nCurses)

2019-07-29 04:13发布

I recently was studying ncurses and a doubt just hit me: What EXACTLY does the refresh function does?

I searched a little about it, read some tutorials and even a documentation and my conclusion was that it "refreshes" the actual screen with the format done on the "buffer screen" (it just updates the output on the screen).

Doing some tests I clearly realized I was wrong since the output appeared with and without the refresh function! Below there's a simple program I did just to test it and I can't realize the actual functionality of this function.

#include <ncurses.h>
#include <string.h>

int main() {
  char mesg[] = "Just a String";
  int row, col;

  initscr();
  getmaxyx(stdscr, row, col);

  while(true) {
    refresh();
    mvprintw(row/2, (col - strlen(mesg))/2, "%s", mesg);

    mvprintw(row-2, 0, "This screen has %d rows and %d columns\n", row, col);

    char c = getch();
    if (c == 'e') { row++; }
    else if (c == 'q') { row--; }
    else if (c == 'a') { col--; }
    else if (c == 'd') { col++; }
  }

  getch();
  endwin();

  return 0;
}

I moved the refresh all over the program, I removed it and nothing seems to change. What exactly it does??

标签: c ncurses
1条回答
淡お忘
2楼-- · 2019-07-29 04:27

The getch function calls refresh, which is probably confusing you as you move the explicit call for refresh to different places.

Curses functions write to a virtual screen (i.e., not real) and refresh updates the physical screen (the real one) by comparing the two and making small changes (if possible).

查看更多
登录 后发表回答