summaryrefslogtreecommitdiff
path: root/curses.c
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-15 01:18:36 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-15 01:18:36 -0700
commiteda5d1668dbc8529453b7128c4ec2d51aeb1e783 (patch)
tree2ee5072865a761c540be454abcb6871358be65ea /curses.c
parent3ffb56f167a1855731b50eebfc484b7cc5f21036 (diff)
downloaddartboat-eda5d1668dbc8529453b7128c4ec2d51aeb1e783.tar.gz
dartboat-eda5d1668dbc8529453b7128c4ec2d51aeb1e783.tar.xz
split code into multiple files
Diffstat (limited to 'curses.c')
-rw-r--r--curses.c144
1 files changed, 144 insertions, 0 deletions
diff --git a/curses.c b/curses.c
new file mode 100644
index 0000000..4169906
--- /dev/null
+++ b/curses.c
@@ -0,0 +1,144 @@
+#include "curses.h"
+#include "match.h"
+
+#include <ncurses.h>
+#include <stdlib.h>
+
+WINDOW *w, *titlew, *statw, *promptw;
+
+void init_curses()
+{
+ initscr();
+ refresh();
+ start_color();
+ init_colours();
+ curs_set(0);
+ noecho();
+
+ w = newwin(LINES-3, COLS, 1, 0);
+ titlew = newwin(1, COLS, 0, 0);
+ statw = newwin(1, COLS, LINES-2, 0);
+ promptw = newwin(1, COLS, LINES-1, 0);
+ wbkgd(titlew, COLOR_PAIR(C_STATUS));
+ wbkgd(statw, COLOR_PAIR(C_STATUS));
+ waddstr(titlew, " dartbot");
+ wrefresh(titlew);
+}
+
+void free_curses()
+{
+ delwin(promptw);
+ delwin(statw);
+ delwin(titlew);
+ delwin(w);
+ endwin();
+}
+
+void init_colours()
+{
+ FOREACH_COLOUR(GEN_COLOUR_INIT_PAIR)
+}
+
+void curses_status(char *status)
+{
+ wmove(statw, 0, 0);
+ wclrtoeol(statw);
+ waddstr(statw, status);
+ wrefresh(statw);
+}
+
+void curses_prompt(char *prompt)
+{
+ wmove(promptw, 0, 0);
+ wclrtoeol(promptw);
+ waddstr(promptw, prompt);
+ wrefresh(promptw);
+}
+
+void flushbuf(char *buf, int *buflen, int col)
+{
+ if (!(*buflen)) return;
+
+ if (col) wattron(w, col);
+ waddstr(w, buf);
+ if (col) wattroff(w, col);
+
+ *buflen = 0;
+}
+
+int points_colour(int points)
+{
+ if (points >= 180)
+ return COLOR_PAIR(C_140) | A_BOLD;
+ else if (points >= 140)
+ return COLOR_PAIR(C_140);
+ else if (points >= 100)
+ return COLOR_PAIR(C_100);
+ else if (points >= 60)
+ return COLOR_PAIR(C_60);
+ else if (points >= 40)
+ return COLOR_PAIR(C_40);
+ else if (points >= 20)
+ return COLOR_PAIR(C_20);
+ else if (points > 0)
+ return COLOR_PAIR(C_0);
+ else
+ return COLOR_PAIR(C_0) | A_BOLD;
+}
+
+void curses_draw(struct leg *l1, struct leg *l2)
+{
+ werase(w);
+
+ char buf[100];
+ int buflen = 0;
+
+ int wlines = LINES - 3;
+ int n_visits = l1->n_visits > l2->n_visits ? l1->n_visits : l2->n_visits;
+ int start_visit = (wlines > n_visits) ? 0 : (n_visits - wlines);
+ int offset = 0;
+
+ if (start_visit == 0 && wlines > n_visits) {
+ wmove(w, (offset++)-start_visit, 1);
+ buflen = sprintf(buf, "(%2d)", 0);
+ flushbuf(buf, &buflen, COLOR_PAIR(C_VISIT));
+ buflen = sprintf(buf, " %3d %3d", l1->start, l2->start);
+ flushbuf(buf, &buflen, 0);
+ }
+
+ for (int i = start_visit; i < n_visits; ++i) {
+ wmove(w, offset+i-start_visit, 1);
+ buflen += sprintf(buf + buflen, "(%2d)", i+1);
+ flushbuf(buf, &buflen, COLOR_PAIR(C_VISIT));
+ buflen += sprintf(buf + buflen, " ");
+ flushbuf(buf, &buflen, 0);
+
+ struct visit *v = l1->visits + i;
+
+ flushbuf(buf, &buflen, 0);
+ buflen += sprintf(buf + buflen, "%3d", v->points);
+ flushbuf(buf, &buflen, points_colour(v->points));
+
+ buflen += sprintf(buf + buflen, " %3d", v->rem);
+
+ if (i < l2->n_visits) {
+ v = l2->visits + i;
+
+ buflen += sprintf(buf + buflen, " %3d ", v->rem);
+
+ flushbuf(buf, &buflen, 0);
+ buflen += sprintf(buf + buflen, "%3d", v->points);
+ flushbuf(buf, &buflen, points_colour(v->points));
+
+ for (int j = 0; j < v->n_darts; ++j) {
+ char *n = segment_name(v->darts[j]);
+ buflen += sprintf(buf + buflen, j == 0 ? " %4s" :" %4s", n);
+ free(n);
+ }
+ flushbuf(buf, &buflen, COLOR_PAIR(C_DARTS));
+ }
+
+ flushbuf(buf, &buflen, 0);
+ }
+ wrefresh(w);
+}