Skip to content

Commit c72a8f1

Browse files
committed
Working menu.
Todo: * Change game and theme * Current game and theme appears twice * Save settings
1 parent 015582a commit c72a8f1

File tree

11 files changed

+551
-362
lines changed

11 files changed

+551
-362
lines changed

dos/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ clean
1717
.c.obj: .autodepend
1818
$(CC) $(CFLAGS) $<
1919

20-
csol.exe: card.obj game.obj main.obj rc.obj theme.obj ui.obj util.obj scores.obj csv.obj
20+
csol.exe: card.obj game.obj main.obj rc.obj theme.obj ui.obj util.obj scores.obj csv.obj menu.obj color.obj
2121
$(LINK) $(LDFLAGS) n $@ f *.obj l $(LIBCURSES)

dos/csolrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ scores 1
66
stats 1
77
smart_cursor 1
88
alt_cursor 1
9+
show_menu 1

src/color.c

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/* csol
2+
* Copyright (c) 2017 Niels Sonnich Poulsen (http://nielssp.dk)
3+
* Licensed under the MIT license.
4+
* See the LICENSE file or http://opensource.org/licenses/MIT for more information.
5+
*/
6+
7+
#include "color.h"
8+
9+
#include <string.h>
10+
#ifdef USE_PDCURSES
11+
#include <curses.h>
12+
#else
13+
#include <ncurses.h>
14+
#endif
15+
16+
struct color_name {
17+
char *name;
18+
int index;
19+
};
20+
21+
struct color_name default_colors[] = {
22+
{"default", -1},
23+
{"black", COLOR_BLACK},
24+
{"red", COLOR_RED},
25+
{"green", COLOR_GREEN},
26+
{"yellow", COLOR_YELLOW},
27+
{"blue", COLOR_BLUE},
28+
{"magenta", COLOR_MAGENTA},
29+
{"cyan", COLOR_CYAN},
30+
{"white", COLOR_WHITE},
31+
{"bright_black", COLOR_BLACK | 8},
32+
{"bright_red", COLOR_RED | 8},
33+
{"bright_green", COLOR_GREEN | 8},
34+
{"bright_yellow", COLOR_YELLOW | 8},
35+
{"bright_blue", COLOR_BLUE | 8},
36+
{"bright_magenta", COLOR_MAGENTA | 8},
37+
{"bright_cyan", COLOR_CYAN | 8},
38+
{"bright_white", COLOR_WHITE | 8},
39+
{NULL, -1}
40+
};
41+
42+
static short find_color(Theme *theme, short index, char *name) {
43+
if (name) {
44+
struct color_name *default_color;
45+
Color *color;
46+
for (color = theme->colors; color; color = color->next) {
47+
if (strcmp(color->name, name) == 0) {
48+
return color->index;
49+
}
50+
}
51+
for (default_color = default_colors; default_color->name; default_color++) {
52+
if (strcmp(default_color->name, name) == 0) {
53+
return default_color->index;
54+
}
55+
}
56+
}
57+
return index;
58+
}
59+
60+
static void find_and_init_color_pair(Theme *theme, short index, ColorPair color_pair) {
61+
short fg = find_color(theme, color_pair.fg, color_pair.fg_name);
62+
short bg = find_color(theme, color_pair.bg, color_pair.bg_name);
63+
if (fg >= COLORS) {
64+
printf("Unsupported color index: %d\n", fg);
65+
}
66+
if (bg >= COLORS) {
67+
printf("Unsupported color index: %d\n", bg);
68+
}
69+
init_pair(index, fg, bg);
70+
}
71+
72+
void init_theme_colors(Theme *theme) {
73+
Color *color;
74+
short index = 15;
75+
start_color();
76+
for (color = theme->colors; color; color = color->next) {
77+
short color_index = color->name ? ++index : color->index;
78+
color_content(color_index, &color->old_red, &color->old_green, &color->old_blue);
79+
if (init_color(color_index, color->red, color->green, color->blue) == 0) {
80+
color->index = color_index;
81+
} else {
82+
printf("Unable to initialize color: %s\n", color->name);
83+
}
84+
}
85+
find_and_init_color_pair(theme, COLOR_PAIR_BACKGROUND, theme->background);
86+
find_and_init_color_pair(theme, COLOR_PAIR_EMPTY, theme->empty_layout.color);
87+
find_and_init_color_pair(theme, COLOR_PAIR_BACK, theme->back_layout.color);
88+
find_and_init_color_pair(theme, COLOR_PAIR_RED, theme->red_layout.color);
89+
find_and_init_color_pair(theme, COLOR_PAIR_BLACK, theme->black_layout.color);
90+
}
91+
92+
void ui_list_colors() {
93+
int i, pair = 0;
94+
initscr();
95+
curs_set(0);
96+
start_color();
97+
printw("Colors: %d Color pairs: %d\n", COLORS, COLOR_PAIRS);
98+
printw("Standard colors\n");
99+
for (i = 0; i < 8; i++) {
100+
switch (i) {
101+
case COLOR_BLACK:
102+
printw("%-8s", "black");
103+
break;
104+
case COLOR_RED:
105+
printw("%-8s", "red");
106+
break;
107+
case COLOR_GREEN:
108+
printw("%-8s", "green");
109+
break;
110+
case COLOR_YELLOW:
111+
printw("%-8s", "yellow");
112+
break;
113+
case COLOR_BLUE:
114+
printw("%-8s", "blue");
115+
break;
116+
case COLOR_MAGENTA:
117+
printw("%-8s", "magenta");
118+
break;
119+
case COLOR_CYAN:
120+
printw("%-8s", "cyan");
121+
break;
122+
case COLOR_WHITE:
123+
printw("%-8s", "white");
124+
break;
125+
default:
126+
printw("%-8s", "");
127+
}
128+
}
129+
printw("\n");
130+
for (i = 0; i < 8; i++) {
131+
init_pair(++pair, i, 0);
132+
attron(COLOR_PAIR(pair));
133+
printw(" %2d ", i);
134+
init_pair(++pair, 0, i);
135+
attron(COLOR_PAIR(pair));
136+
printw(" %2d ", i);
137+
attroff(COLOR_PAIR(pair));
138+
}
139+
if (COLORS > 8) {
140+
printw("\nHigh-intensity colors\n");
141+
for (i = 8; i < 16; i++) {
142+
init_pair(++pair, i, 0);
143+
attron(COLOR_PAIR(pair));
144+
printw(" %2d ", i);
145+
init_pair(++pair, 0, i);
146+
attron(COLOR_PAIR(pair));
147+
printw(" %2d ", i);
148+
attroff(COLOR_PAIR(pair));
149+
}
150+
if (COLORS > 16) {
151+
printw("\n216 colors\n");
152+
for (i = 0; i < 6; i++) {
153+
int j;
154+
for (j = 0; j < 18; j++) {
155+
int bg = 16 + i * 36 + j;
156+
init_pair(++pair, 7, bg);
157+
attron(COLOR_PAIR(pair));
158+
printw(" %3d", bg);
159+
attroff(COLOR_PAIR(pair));
160+
}
161+
printw("\n");
162+
}
163+
for (i = 0; i < 6; i++) {
164+
int j;
165+
for (j = 18; j < 36; j++) {
166+
int bg = 16 + i * 36 + j;
167+
init_pair(++pair, 0, bg);
168+
attron(COLOR_PAIR(pair));
169+
printw(" %3d", bg);
170+
attroff(COLOR_PAIR(pair));
171+
}
172+
printw("\n");
173+
}
174+
printw("Grayscale colors\n");
175+
for (i = 0; i < 24; i++) {
176+
int bg = 232 + i;
177+
int fg = i >= 12 ? 0 : 7;
178+
init_pair(++pair, fg, bg);
179+
attron(COLOR_PAIR(pair));
180+
printw("%3d", bg);
181+
attroff(COLOR_PAIR(pair));
182+
}
183+
}
184+
}
185+
getch();
186+
endwin();
187+
use_default_colors();
188+
}

src/color.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* csol
2+
* Copyright (c) 2017 Niels Sonnich Poulsen (http://nielssp.dk)
3+
* Licensed under the MIT license.
4+
* See the LICENSE file or http://opensource.org/licenses/MIT for more information.
5+
*/
6+
7+
#ifndef COLOR_H
8+
#define COLOR_H
9+
10+
#include "theme.h"
11+
12+
#define COLOR_PAIR_BACKGROUND 1
13+
#define COLOR_PAIR_EMPTY 2
14+
#define COLOR_PAIR_BACK 3
15+
#define COLOR_PAIR_RED 4
16+
#define COLOR_PAIR_BLACK 5
17+
18+
void init_theme_colors(Theme *theme);
19+
void ui_list_colors();
20+
21+
#endif
22+

src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ui.h"
1414
#include "util.h"
1515
#include "scores.h"
16+
#include "color.h"
1617

1718
#include <stdio.h>
1819
#include <stdlib.h>

0 commit comments

Comments
 (0)