From e37769bfcf7564ebc5e658c7f473230efb9a4bcd Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Thu, 7 Apr 2022 07:24:16 -0700 Subject: add ncurses interface; not interactive yet --- dartbot.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/dartbot.c b/dartbot.c index 7c89a52..bb4f16f 100644 --- a/dartbot.c +++ b/dartbot.c @@ -6,6 +6,7 @@ #include #include #include +#include #define HORIZONTAL_STDEV 24 #define VERTICAL_STDEV 24 @@ -308,23 +309,96 @@ void test_match(int start_points) free(n); } printf("\n"); + + free(v.darts); } } -int main() +void draw(WINDOW *w, struct visit *visits, int n_visits) +{ + werase(w); + box(w, 0, 0); + wmove(w, 0, 2); + waddstr(w, "dartbot"); + + int rem = 501; + char buf[100]; + int buflen = 0; + + int offset = 2; + int start_visit = (LINES - 3 > n_visits) ? 0 : (n_visits - (LINES - 3)); + if (start_visit == 0 && LINES - 3 > n_visits) { + wmove(w, offset-start_visit, 2); + sprintf(buf, "(%2d) %3d", 0, rem); + waddstr(w, buf); + offset = 3; + } + + for (int i = 0; i < n_visits; ++i) { + struct visit v = visits[i]; + rem -= v.points; + + if (i < start_visit) + continue; + + wmove(w, offset+i-start_visit, 2); + + buflen += sprintf(buf, "(%2d) %3d", i+1, rem); + + 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); + } + + buflen = 0; + waddstr(w, buf); + } + + wrefresh(w); +} + +void test_curses_match() { - /* initscr(); - addstr("test"); - refresh(); + curs_set(0); + noecho(); + + WINDOW *w = newwin(LINES, COLS, 0, 0); + + int rem = 501; + printf("(%2d) %3d\n", 0, rem); + struct visit *visits = calloc(1000, sizeof(*visits)); // FIXME + int n_visits = 0; - getch(); + draw(w, visits, n_visits); + wrefresh(w); + + while(rem > 0) { + struct visit v = test_visit(rem); + visits[n_visits] = v; + ++n_visits; + rem -= v.points; + + usleep(250000); + draw(w, visits, n_visits); + } + + for (int i = 0; i < n_visits; ++i) + free(visits[i].darts); + free(visits); + wgetch(w); + delwin(w); endwin(); - */ +} + +int main() +{ srand(time(NULL)); init_sectors(); init_centre_dists(); + test_curses_match(); test_match(501); return 0; -- cgit v1.2.3-70-g09d2