-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.cpp
345 lines (298 loc) · 7.51 KB
/
gui.cpp
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <iostream>
#include "SDL2/SDL.h"
#include <SDL2/SDL_ttf.h>
#include "gui.h"
#include "solver.h"
#define BACKGROUND_R 0xED
#define BACKGROUND_G 0x92
#define BACKGROUND_B 0x0
#define SELECTED_R 0xDF
#define SELECTED_G 0xDF
#define SELECTED_B 0x27
#define WRONG_R 0xFF
#define WRONG_G 0x0
#define WRONG_B 0x0
#define CORRECT_R 0x0
#define CORRECT_G 0xFF
#define CORRECT_B 0x0
#define DEFAULT 0
#define SELECTED 1
#define WRONG 2
#define CORRECT 3
void sdl_error(const char *message)
{
SDL_Log("\n%s : %s\n", message, SDL_GetError());
}
void update_box(Game *game)
{
int w;
int h;
SDL_GetRendererOutputSize(game->renderer, &w, &h);
if (w<h)
{
game->box.size = w;
game->box.x = 0;
game->box.y = 0;
}
else
{
game->box.size = h;
game->box.x = w/2-game->box.size/2;
game->box.y = 0;
}
}
void update_cases_rectangle(Game *game, int smallLine, int bigLine, int caseSize)
{
int i = game->board.x + bigLine;
int j = game->board.y + bigLine;
int counter_x{0};
int counter_y{0};
int position{0};
while(counter_y < 9)
{
while (counter_x < 9)
{
game->cases[position].rectangle.x = i;
game->cases[position].rectangle.y = j;
game->cases[position].rectangle.h = caseSize;
game->cases[position].rectangle.w = caseSize;
position++;
counter_x++;
i+=caseSize;
if (counter_x%3 == 0)
i+=bigLine;
else
i+=smallLine;
}
counter_x = 0;
i = game->board.x+bigLine;
counter_y++;
j+=caseSize;
if (counter_y%3 == 0)
j+=bigLine;
else
j+=smallLine;
}
}
void update_case_state(Game *game, int position, int state)
{
game->cases[position].state = state;
if (state == SELECTED)
game->selectedCase = position;
if (state == DEFAULT)
game->selectedCase = -1;
switch(game->cases[position].state)
{
case DEFAULT:
game->cases[position].r = 255;
game->cases[position].g = 255;
game->cases[position].b = 255;
break;
case SELECTED:
game->cases[position].r = SELECTED_R;
game->cases[position].g = SELECTED_G;
game->cases[position].b = SELECTED_B;
break;
case WRONG:
game->cases[position].r = WRONG_R;
game->cases[position].g = WRONG_G;
game->cases[position].b = WRONG_B;
break;
case CORRECT:
game->cases[position].r = CORRECT_R;
game->cases[position].g = CORRECT_G;
game->cases[position].b = CORRECT_B;
break;
}
update_case(game, position, 1);
}
void number(Game *game, int position)
{
if (game->cases[position].value != 0)
{
TTF_Init();
TTF_Font *font = TTF_OpenFont("myfont.ttf", game->cases[0].rectangle.w*5);
if (font == NULL)
{
printf("TTF_Font: %s\n", TTF_GetError());
exit(2);
}
SDL_Color color = {0, 0, 0, 255};
char number[1+sizeof(char)];
std::sprintf(number, "%d", game->cases[position].value);
SDL_Surface *surface = TTF_RenderText_Blended(font, number, color);
TTF_CloseFont(font);
SDL_Texture *texture = SDL_CreateTextureFromSurface(game->renderer, surface);
SDL_FreeSurface(surface);
SDL_RenderCopy(game->renderer, texture, NULL, &game->cases[position].rectangle);
//SDL_RenderPresent(game->renderer);
SDL_DestroyTexture(texture);
TTF_Quit();
}
else
{
game->cases[position].value = 0;
SDL_SetRenderDrawColor(game->renderer,
game->cases[position].r,
game->cases[position].g,
game->cases[position].b,
255);
SDL_RenderFillRect(game->renderer, &game->cases[position].rectangle);
//SDL_RenderPresent(game->renderer);
}
}
void update_case(Game *game, int position, int draw)
{
SDL_SetRenderDrawColor(game->renderer,
game->cases[position].r,
game->cases[position].g,
game->cases[position].b,
255);
SDL_RenderFillRect(game->renderer, &game->cases[position].rectangle);
number(game, position);
if (draw)
SDL_RenderPresent(game->renderer);
}
void update_board(Game *game)
{
int smallLine = 1+game->box.size/1000;
int bigLine = smallLine*2;
int caseSize = (game->box.size*80/100 - 6*smallLine - 4*bigLine)/9;
int size = 9*caseSize + 6*smallLine + 4*bigLine;
game->board.x = game->box.x + game->box.size/2 - size/2;
game->board.y = game->box.size/100;
game->board.w = size;
game->board.h = size;
SDL_SetRenderDrawColor(game->renderer, 0, 0, 0, 255);
SDL_RenderFillRect(game->renderer, &(game->board));
update_cases_rectangle(game, smallLine, bigLine, caseSize);
for (int i = 0; i<81; i++)
{
update_case(game, i, 0);
}
}
void draw_main(Game *game)
{
// Background
SDL_SetRenderDrawColor(game->renderer,
BACKGROUND_R,
BACKGROUND_G,
BACKGROUND_B,
0);
SDL_RenderClear(game->renderer);
update_box(game);
// TEST BOX (REMOVE)
/*
SDL_Rect rect;
rect.x = game->box.x;
rect.y = game->box.y;
rect.w = game->box.size;
rect.h = game->box.size;
SDL_SetRenderDrawColor(game->renderer, 255, 0, 0, 255);
SDL_RenderFillRect(game->renderer, &rect);
*/
// END TEST BOX
update_board(game);
// Draw the renderer
SDL_RenderPresent(game->renderer);
}
int get_case_clicked(Game *game, int x, int y)
{
for (int i = 0; i<81; i++)
{
if (x > game->cases[i].rectangle.x &&
x < game->cases[i].rectangle.x + game->cases[i].rectangle.w &&
y > game->cases[i].rectangle.y &&
y < game->cases[i].rectangle.y + game->cases[i].rectangle.h)
{
return i;
}
}
return -1;
}
void on_click_left_event(Game *game)
{
if (game->selectedCase != -1)
update_case_state(game, game->selectedCase, 0);
int x;
int y;
SDL_GetMouseState(&x, &y);
int position = get_case_clicked(game, x, y);
if (position != -1)
{
update_case_state(game, position, SELECTED);
}
}
void on_key_pressed_event(Game *game, char key)
{
if (key == 's')
solve(game);
if (key == 'c')
checkGrid(game);
if (key == 'a')
auto_correct(game);
if (key >= '0' && key <= '9' && game->selectedCase != -1)
{
game->cases[game->selectedCase].value = key - '0';
update_case_state(game, game->selectedCase, DEFAULT);
if(game->state == 0)
{
game->state = 1;
for(int i = 0; i<81; i++)
update_case_state(game, i, DEFAULT);
}
}
}
void gui_main()
{
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
if (SDL_Init(SDL_INIT_VIDEO) != 0)
sdl_error("SDL_Init failed\n");
if (SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_RESIZABLE, &window, &renderer) != 0)
sdl_error("SDL_CreateWindowAndRenderer failed\n");
SDL_SetWindowMinimumSize(window, 700, 500);
// Initialise game
Game game =
{
.window = window,
.renderer = renderer,
.selectedCase{-1},
.state{1},
.board{},
.box{},
.cases{}
};
draw_main(&game);
SDL_Event event;
int running = 1;
while(running)
{
SDL_Delay(70);
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED)
draw_main(&game);
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
on_click_left_event(&game);
break;
case SDL_KEYDOWN:
on_key_pressed_event(&game, event.key.keysym.sym);
break;
case SDL_QUIT:
running = 0;
break;
default:
break;
}
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}