summaryrefslogtreecommitdiff
path: root/ui.c
blob: 28f6f5cb7b6625eb2e7d618e0de5ea3879147b02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <ncurses.h>
#include "pieces.h"

/*
 * ncurses ui has an x scale factor of 2
 */

void printrect(int y, int x, int h, int w) {
    // print rectangle whose left corner is at (y, x) and internal area is h*w
    // top & bottom
    for (int c = 0; c < 2 * w; c++) {
        mvaddch(y,          x + c + 1,  ACS_HLINE);
        mvaddch(y + h + 1,  x + c + 1,  ACS_HLINE);
    }
    // left & right
    for (int r = 0; r < h; r++) {
        mvaddch(y + r + 1,  x,              ACS_VLINE);
        mvaddch(y + r + 1,  x + 2 * w + 1,  ACS_VLINE);
    }
    // corners
    mvaddch(y,          x,              ACS_ULCORNER);
    mvaddch(y,          x + 2 * w + 1,  ACS_URCORNER);
    mvaddch(y + h + 1,  x,              ACS_LLCORNER);
    mvaddch(y + h + 1,  x + 2 * w + 1,  ACS_LRCORNER);
    refresh();
}

void printmap(char** map, int y, int x, int mapH, int mapW) {
    for (int r = 0; r < mapH; r++) {
        for (int c = 0; c < mapW; c++) {
            if (map[r][c] == '+') {
                mvaddch(y + r, x + 2 * c, (' ' | A_REVERSE));
                addch(' ' | A_REVERSE);
            } else {
                mvaddch(y + r, x + 2 * c, (' '));
                addch(' ');
            }
        }
    }
    refresh();
}

void printpiece(struct piece* pc, int y, int x, int color_pair) {
    attron(COLOR_PAIR(color_pair));
    for (int r = 0; r < pc->h; r++) {
        for (int c = 0; c < pc->w; c++) {
            if (pc->blocks[r * pc->w + c] == '+') {
                mvaddch(y + r, x + 2 * c, (' ' | A_REVERSE));
                addch(' ' | A_REVERSE);
            }
        }
    }
    attroff(COLOR_PAIR(color_pair));
    refresh();
}

void printpieces(struct piece** hand, int y, int x, int nhand, int highlight) {
    for (int i = 0; i < nhand; i++) {
        if (hand[i] == NULL)
            continue;

        struct piece* pc = hand[i];
        move(y, x);

        printw("%d", i);
        printrect(y + 1, x, pc->h + 2, pc->w + 2);
        printpiece(pc, y + 3, x + 3, (i == highlight ? 1 : 0));

        x += 2 * pc->w + 8;
    }
    refresh();
}