Initialize `scores` array correctly
[oggquiz.git] / ui.c
blobb619ff60bd1ee9e513be84c00aa68d285953d742
1 /*-
2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
4 * you can do whatever you want with this stuff. If we meet some day, and you
5 * think this stuff is worth it, you can buy me a beer in return.
6 * Tobias Rehbein
7 */
9 #include <assert.h>
10 #include <curses.h>
11 #include <err.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <unistd.h>
16 #include "common.h"
17 #include "oggfile.h"
18 #include "oggquiz.h"
19 #include "ui.h"
21 #define RESETGUI() do { \
22 y = 0; \
23 clear(); \
24 } while (0)
26 /* Prototypes */
27 static void print_oggfile(struct oggfile *oggfile);
28 static void print_header(char const *step, struct ui_model *model);
30 /* Global variables */
31 static int tty;
32 static FILE *ftty;
33 static SCREEN *term;
34 static int y;
36 void
37 ui_setup()
39 if ((tty = open("/dev/tty", O_RDONLY)) == -1)
40 err(1, "could not open /dev/tty");
41 if ((ftty = fopen("/dev/tty", "r")) == NULL)
42 err(1, "could not open /dev/tty");
43 if ((term = newterm(NULL, stdout, ftty)) == NULL)
44 errx(1, "could not create /dev/tty terminal");
45 if (initscr() == NULL)
46 errx(1, "could not initialize screen");
47 if (set_term(term) == NULL)
48 errx(1, "could not set /dev/tty terminal");
49 cbreak();
50 noecho();
51 start_color();
52 init_pair(1, COLOR_WHITE, COLOR_BLACK);
53 init_pair(2, COLOR_GREEN, COLOR_BLACK);
54 init_pair(3, COLOR_RED, COLOR_BLACK);
55 intrflush(stdscr, FALSE);
58 void
59 ui_display_quiz(struct ui_model *model, struct options *opts)
61 int i;
63 assert(model != NULL);
64 assert(opts != NULL);
66 RESETGUI();
67 print_header("new turn", model);
69 for (i = 0; i < opts->choices; i++) {
70 if (i % 2 == 1) {
71 attron(A_BOLD);
72 } else
73 attroff(A_BOLD);
74 mvprintw(y++, 1, "%d. %s\n", i + 1, model->oggfiles[i].artist);
75 mvprintw(y++, 1, " %s\n", model->oggfiles[i].album);
76 mvprintw(y++, 1, " %s\n\n", model->oggfiles[i].title);
77 y++;
79 attroff(A_BOLD);
81 attron(A_REVERSE);
82 mvprintw(y, 0, " What are you listening to? (1-%d, 'q' to quit) ", opts->choices);
83 attroff(A_REVERSE);
85 refresh();
88 void
89 ui_display_result(struct ui_model *model, struct options *opts)
91 int i;
93 assert(model != NULL);
94 assert(opts != NULL);
96 RESETGUI();
97 print_header("result", model);
99 if (model->correct == model->guess) {
100 attron(COLOR_PAIR(2));
101 attron(A_BOLD);
102 mvprintw(y++, 1, "You are right!");
103 attroff(A_BOLD);
104 } else {
105 attron(COLOR_PAIR(3));
106 attron(A_BOLD);
107 mvprintw(y++, 1, "Sorry, you are wrong!");
108 attroff(A_BOLD);
109 y++;
110 mvprintw(y++, 1, "Your guess:");
111 y++;
112 print_oggfile(model->guess);
114 attron(COLOR_PAIR(1));
116 y++;
117 mvprintw(y++, 1, "You are listening to:");
118 y++;
119 print_oggfile(model->correct);
120 y++;
121 attron(A_BOLD);
122 mvprintw(y++, 1, "Scoreboard:");
123 attroff(A_BOLD);
124 y++;
125 for (i = 0; i < opts->players; i++) {
126 if (i == model->current_player)
127 attron(A_BOLD);
128 else
129 attroff(A_BOLD);
130 mvprintw(y++, 3, "Player %d: %5d", i + 1, model->scores[i]);
132 attrset(A_NORMAL);
134 attron(A_REVERSE);
135 y++;
136 mvaddstr(y++, 0, " press any key to continue... ");
137 attroff(A_REVERSE);
139 refresh();
142 char
143 ui_get_answer()
145 int in;
147 in = getch();
149 return ((char)in);
152 void
153 ui_pause()
155 getch();
158 void
159 ui_teardown()
161 if (close(tty) == -1)
162 err(1, "could not close fd for /dev/tty");
163 if (fclose(ftty) == EOF)
164 err(1, "could not close /dev/tty");
165 delscreen(term);
168 static void
169 print_oggfile(struct oggfile *oggfile)
171 assert(oggfile !=NULL);
173 mvprintw(y++, 3, "%s", oggfile->artist);
174 mvprintw(y++, 3, "%s", oggfile->album);
175 mvprintw(y++, 3, "%s", oggfile->title);
178 static void
179 print_header(char const *step, struct ui_model *model)
181 assert(step != NULL);
182 assert(model != NULL);
184 attron(A_REVERSE);
185 mvprintw(y, 0, " %s | current turn: %03d | current player: %d ",
186 step, model->turn, model->current_player + 1);
187 y += 2;
188 attroff(A_REVERSE);