Repository navigation
Expand file tree
/
Copy pathui.c
More file actions
278 lines (240 loc) · 7.97 KB
/
Copy pathui.c
File metadata and controls
278 lines (240 loc) · 7.97 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "ui.h"
#include "coord.h"
#include <SDL3/SDL.h>
#include <stdio.h>
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
/*SDL_Color light_tile = {255, 220, 150, 255};
SDL_Color dark_tile = {181-60, 136-60, 99-60, 255};
SDL_Color light_piece = {255, 255, 230, 200};
SDL_Color dark_piece = {0, 0, 0, 150};*/
/*SDL_Color light_piece = {248,112,96, 255};
SDL_Color dark_piece = {179, 163, 148, 255};
SDL_Color light_tile = {205, 215, 214, 255};
SDL_Color dark_tile = {16,37,66, 255};*/ // > red/gray
/*SDL_Color light_piece = {244,211,94, 255};
SDL_Color dark_piece = {218, 65, 103, 255};
SDL_Color light_tile = {235, 235, 211, 255};
SDL_Color dark_tile = {8,61,119, 255};*/
// minimal
SDL_Color light_piece = {0xff, 0xff, 0xff, 0xff};
SDL_Color dark_piece = {0,0,0, 255};
SDL_Color light_tile = {0xdf, 0xdf, 0xdf, 0xff};
SDL_Color dark_tile = {0x7b,0x7b,0x7b, 255};
/*SDL_Color light_tile = {171,129,205,255};
SDL_Color dark_tile = {43,42,104,255};
SDL_Color light_piece = {87,74,226,255};
SDL_Color dark_piece = {226,173,242,255};*/
/*SDL_Color light_tile = {235,235,211,255};
SDL_Color dark_tile = {8,61,119,255};
SDL_Color light_piece = {238,150,75,255};
SDL_Color dark_piece = {249,87,56,255};*/
/*SDL_Color light_tile = {188,196,219,255};
SDL_Color dark_tile = {105,135,201,255};
SDL_Color light_piece = {107,186,236,255};
SDL_Color dark_piece = {8,61-20,119-20,255};*/
//SDL_Color dark_piece = {192,169,176,255};
bool ui__SDL3_init(void)
{
printdb("starting SDL3 initialization...\n");
printdb("initializing video...");
if (!SDL_Init(SDL_INIT_VIDEO))
{
fprintf(stderr, "[SDL3 error] SDL_Init(): \'%s\'\n", SDL_GetError());
return false;
}
printdb("done\n");
printdb("initializing window...");
window = SDL_CreateWindow("dama v0.1", WINDOW_WIDTH, WINDOW_HEIGHT, 0);
if (!window)
{
fprintf(stderr, "[SDL3 error] SDL_CreateWindow(): %s\n", SDL_GetError());
return false;
}
printdb("created window <%p> of size %ux%u\n", window, WINDOW_WIDTH, WINDOW_HEIGHT);
printdb("initializing renderer...");
renderer = SDL_CreateRenderer(window, NULL);
if (!renderer)
{
fprintf(stderr, "[SDL3 error] SDL_CreateRenderer(): %s\n", SDL_GetError());
return false;
}
printdb("created renderer <%p>\n", renderer);
printdb("SDL3 initialization completed\n");
return true;
}
void ui__draw_board(void)
{
for (u8 row = 0; row < BOARD_SIZE; row++)
{
for (u8 col = 0; col < BOARD_SIZE; col++)
{
SDL_FRect rect =
{
.x = (float)(col * CELL_SIZE),
.y = (float)(row * CELL_SIZE),
.w = (float)CELL_SIZE,
.h = (float)CELL_SIZE,
};
if ((row + col) % 2 == 0)
SDL_SetRenderDrawColor(renderer, dark_tile.r, dark_tile.g, dark_tile.b, dark_tile.a); // dark
else
SDL_SetRenderDrawColor(renderer, light_tile.r, light_tile.g, light_tile.b, light_tile.a); // light
SDL_RenderFillRect(renderer, &rect);
}
}
return;
}
void render_circle(SDL_Color color, u16 cx, u16 cy, i32 radius)
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
for (u16 w = 0; w < radius * 2; w++)
{
for (u16 h = 0; h < radius * 2; h++)
{
int dx = radius - w;
int dy = radius - h;
if (dx * dx + dy * dy <= radius * radius)
SDL_RenderPoint(renderer, cx + dx, cy + dy);
}
}
return;
}
void draw_rect_at_xy(coord_t coord, SDL_Color color)
{
SDL_FRect rect =
{
.x = coord.x * CELL_SIZE,
.y = coord.y * CELL_SIZE,
.w = CELL_SIZE,
.h = CELL_SIZE
};
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
SDL_RenderFillRect(renderer, &rect);
return;
}
void draw_circle_at_xy(coord_t coord, u16 radius, SDL_Color color)
{
u16 col = coord.x;
u16 row = coord.y;
u16 cx = col * CELL_SIZE + CELL_SIZE / 2;
u16 cy = row * CELL_SIZE + CELL_SIZE / 2;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
render_circle(color, cx, cy, radius);
return;
}
void ui__draw_hints(coord_t coord, board_t* board, bool capture_available)
{
if (!coord__is_null(coord) && !capture_available)
{
piece_t* piece = piece__get_from_n(coord.n, board);
coord_vec_t* vec = piece__possible_moves(piece, board->indexes);
for (u8 i = 0; i < vec->len; ++i)
{
u16 radius = CELL_SIZE / 2 - 6;
//SDL_Color color = (SDL_Color){100, 200, 200, 200};
SDL_Color color = (SDL_Color){200, 200, 100, 100};
draw_circle_at_xy(coord__from_xy(vec->table[i]->x, vec->table[i]->y), radius, color);
}
piece__free_coord_vec(vec);
}
return;
}
void draw_capture_hint_circle(loc_node_t** chain, u8 count)
{
if (chain)
{
for (u8 i = 0; i < count; ++i)
{
if (chain[i]->next)
draw_capture_hint_circle(chain[i]->next, chain[i]->count);
u16 radius = CELL_SIZE / 2 - 2;
SDL_Color color = (SDL_Color){200, 200, 100, 100};
draw_rect_at_xy(chain[i]->capt, color);
radius = CELL_SIZE / 2 - 6;
color = (SDL_Color){220, 100, 50, 255};
draw_circle_at_xy(chain[i]->capt, radius, color);
radius = CELL_SIZE / 2 - 6;
color = (SDL_Color){200, 200, 100, 100};
draw_circle_at_xy(chain[i]->dest, radius, color);
}
}
return;
}
bool ui__draw_capture_hints(coord_t coord, board_t* board)
{
if (!coord__is_null(coord))
{
piece_t* piece = piece__get_from_n(coord.n, board);
u8 count = 0;
loc_node_t** chain = piece__possible_captures(piece, board, NULL, &count, 4);
if (chain)
{
draw_capture_hint_circle(chain, count);
piece__free_capture_chain(chain, count);
return true;
}
}
return false;
}
void ui__draw_pieces(board_t* board)
{
for (u8 i = 0; i < 32; ++i)
{
if (board->indexes[i] > 0)
{
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
u16 col = coord__from_n(i+1).x;
u16 row = coord__from_n(i+1).y;
u16 cx = col * CELL_SIZE + CELL_SIZE / 2;
u16 cy = row * CELL_SIZE + CELL_SIZE / 2;
u16 radius = CELL_SIZE / 2 - 6;
SDL_Color color;
if (piece__get_from_n(i+1, board)->valid)
{
if (!piece__get_from_n(i+1, board)->king)
{
if (piece__get_from_n(i+1, board)->player)
render_circle(light_piece, cx, cy, radius);
else
render_circle(dark_piece, cx, cy, radius);
}
else
{
if (piece__get_from_n(i+1, board)->player)
{
render_circle(light_piece, cx, cy, radius);
//render_circle(color, cx, cy, radius-4);
//color = (SDL_Color){255, 255, 230, 150};
render_circle(dark_tile, cx, cy, radius-10);
render_circle(light_piece, cx, cy, radius-20);
}
else
{
render_circle(dark_piece, cx, cy, radius);
render_circle(dark_tile, cx, cy, radius-10);
render_circle(dark_piece, cx, cy, radius-20);
}
}
}
}
}
return;
}
void ui__quit_SDL3(void)
{
printdb("freeing SDL3 resources...");
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
printdb("done\n");
return;
}
void* ui__get_renderer(void)
{
return renderer;
}
void* ui__get_window(void)
{
return window;
}