Search code examples
cncurses

Keeping aspect ratio in C (ncurses)


I need to make a game using ncurses in C. The game needs to be 80(char)x24(char). I need an status bar on the bottom (5 char) so I hardcoded it.

I made this loop to keep things centralized, but I can't figure out how to keep the aspect ratio when the terminal screen is resized.

Is there any way to do it using ncurses?

I need the screen to always stay at least 80x24, perhaps using fullscreen mode always, I don't really need screen resizing.

Here's the loop to keep things in their places (status bar poorly hardcoded, the aspect ratio is a mess)

/* LOOP TO CENTRALIZE FOR ANY RESIZING */
  while (1){
    getmaxyx(stdscr, yMax, xMax);
    clear();
    mvprintw(yMax/24, xMax/80, "BEG");
    mvprintw(yMax/2, xMax/2, "CENTER %d %d", yMax, xMax);

    attron(COLOR_PAIR(1)); /* bottom status bar (5 lines) */
    int i, j;
    for ( j=(yMax-5) ; j <= yMax ; j++){

      for ( i=0 ; i <= xMax ; i++ ){
        mvprintw(j, i, " ");
      }

    }
    attroff(COLOR_PAIR(1));
    refresh();
  }

Solution

  • Unless your program reads input, e.g., calls getch, ncurses will continue using the original screen-size (and look confused). When you call getch after ncurses receives a SIGWINCH, it returns KEY_RESIZE, and at that point ncurses updates its screen-size.