#include <stdlib.h>
#include <ncurses.h>
#include "io.h"

int sx = 1,
    sy = 2;
WINDOW *scr;

void
pmove(int x, int y) {
  move(y, x);
}

void
pputch(int x, int y, char c, int a) {
  int cc;
  
  if (! a)
    a = C_LIGHT_GRAY;
  
  cc = COLOR_PAIR(a);
  
  if (a > 7)
    cc |= A_BOLD;
  
  mvaddch(y, x, c | cc);
}

void
pputstr(int x, int y, char *s, int a) {
  int n, c;
  
  c = COLOR_PAIR(a);
  
  if (a > 7)
    c |= A_BOLD;
  
  move(y, x);
  
  for (n = 0; n < strlen(s); n++)
    addch(s[n] | c);
}

void
pclear(int y, int r) {
  for (; r > 0; r--) {
    move(y + r - 1, 0);
    clrtoeol();
  }
}

static int ct[MAX_COLOR] =
{
  COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN, COLOR_RED, COLOR_MAGENTA,
  COLOR_YELLOW, COLOR_WHITE, COLOR_BLACK, COLOR_BLUE, COLOR_GREEN,
  COLOR_CYAN, COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE
};

void
pinit() {
  int f;
  
  scr = initscr();
  
  cbreak(); noecho(); nonl();
  
  intrflush(scr, FALSE);
  keypad(scr, TRUE);
  
  start_color();
  
  for (f = 0; f < MAX_COLOR; f++) {
    init_pair(f, ct[f], COLOR_BLACK); // that's _evil_ but... ;-)
  }
  
  refresh();
}

void
pdone() {
  endwin();
  printf("\n");
}
