Ncurses Notes from the series on the Casual Coder youtube channel.

Tutorial 0: init screen

#include <ncurses.h>
using namespace std;

main() {
  initscr();
  printw("Hello World!");
  refresh();
  int c = getch();
  printw("%d", c);
  getch();
  endwin();
  return 0;
}

Tutorial 1: move

  initscr();
  
  int x, y;
  x = y = 10;
  move(y,x); // note y first, x second

  int c = getch();
  move(0,0);
  printw("%d",c);

  mvprintw(2,3,"%d",c);
  refresh();

  getch();
  clear();

Tutorial 2: windows basics

  WINDOW *win;

  int height=10, width=40, start_y=5, start_x=5;
  win = newwin(height, width, start_y, start_x);
  refresh();
  
  box(win,0,0);
  mvwprintw(win,1,1,"this is my box");
  wrefresh(win);

Tutorial 3: Borders etc

  cbreak(); // ctrl+c exits
  raw();
  noecho();

  // opposites
  nocbreak();
  echo();
  noraw();

  // borders
  box(win,(int)'g',(int)'h');
  int left, right, top, bottom;
  int topleftcorner, toprightcorner, botleftcorner, botrightcorner;
  left = right = (int)'g';
  top = bottom = (int)'h';
  topleftcorner = toprightcorner = botleftcorner = botrightcorner = (int)'+'
  wborder(win,
    left,
    right,
    top,
    bottom,
    topleftcorner,
    toprightcorner,
    botleftcorner,
    botrightcorner);

Tutorial 4: Colours and attributes

  attron();
  printw("...");
  attroff();

  attron(A_STANDOUT);
  wprint("...")
  attroff(A_STANDOUT);

  A_NORMAL, STANDOUT, REVERSE, BLINK, DIM, BOLD, PROTECT, INVIS, ALTCHARSET, CHARTEXT

  COLOR_PAIR(n) COLOR_BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE

  if(!has_colors()) printw("Terminal does not have colours");

  start_color();
  init_pair(1, COLOR_CYAN, COLOR_WHITE);
  attron(COLOR_PAIR(1));

  if( can_change_color() ) {
    init_color(COLOR_CYAN, 0-999, 0-999, 0-999);
  }

Tutorial 5: Getting info about Terminal

  // these are macros, so don't use &'s
  getyx(stdscr, y, x);
  getbegyx(stdscr, yBeg, xBeg);
  getmaxyx(stdsrc, yMax, xMax);

  WINDOW *win = newwin(10,20,10,10);
  getbegyx(win,yBeg,xBeg);
  getmaxyx(win,yMax,xMax);

Tutorial 6: user input

  WINDOW* inputwin = newwin(3,xMax-12,yMax-5,5);
  box(inputwin,0,0);
  refresh();
  wrefresh(inputwin);
  int c = wgetch(inputwin);
  if( c == 'j')
  {
    mvwprintw(inputwin, 1, 1, "You pressed j");
    wrefresh(inputwin);
  }
  if( c == KEY_UP )
  {
    ...
  }

Tutorial 7: Basic menu system

  WINDOW *menuwin = newwin(6,xMax-12,yMax-8,5);
  box(menuwin, 0, 0);
  refresh();
  wrefresh(menuwin);
  keypad(menuwin, true);
  string choices[3] = {"Walk", "Jog", "Run"}; // This is C++
  int choice;
  int highlight = 0;
  while(1)
  {
    for(int i=0; i<3; i++) 
    {
        if( i == highlight )
        {
          wattron(menuwin, A_REVERSE);
        }
        mvwprintw(menuwin,i+1,1,choices[i].c_str());
        wattroff(menuwin, A_REVERSE);
    }
    choice = wgetch(menuwin);

    switch(choice)
    {
      case KEY_UP:
        highlight--;
        if( highlight < 0) highlight == 0;
        break;
      case KEY_DOWN:
        highlight++;
        if( highlight > 2) highlight == 2;
        break;
      default:
        break;
    }
    if( choice == 10) break;
  }
  printw("Your choice was: %s", choices[highlight].c_str());