-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGui.c
More file actions
1498 lines (1371 loc) · 45.7 KB
/
Gui.c
File metadata and controls
1498 lines (1371 loc) · 45.7 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# include "Gui.h"
/** Main function of gui mode;
* This is the main-event loop of the program when run in gui mode. **/
int main_gui(Config c) {
SDL_Event event;
/* Initialize the SDL library (starts the event loop) */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
/* Clean up on exit, exit on window close and interrupt */
atexit(SDL_Quit);
window w;
draw_main_window(&w, &c);
SDL_Event mouseMotion;
while (SDL_WaitEvent(&event) >= 0) {
//computer turn..
if(w.id=='g'&&c.MODE==ONE_PLAYER && c.TURN!=c.USER_COLOR && c.END_GAME==0){
List * PC_moves=init_list();
if (PC_moves==NULL){
free_window(w);
exit(0);
return 0;
}
if (!generate_legal_moves(&c, PC_moves)) { // malloc failure...
free_list(PC_moves);
free_window(w);
exit(0);
return 0;
}
if (!alphabeta(c, PC_moves)) { // malloc error
c.END_GAME = 1;
exit(0);
free_window(w);
return 0;
}
// Computer turn (independent of events)
print_list(PC_moves);
Move comp_move = best_move(PC_moves);
int src_index, dst_index;
src_index = loc_to_index(comp_move.src);
dst_index = loc_to_index(comp_move.dst);
button comp_b = w.children[0].children[src_index];
make_move(&c, PC_moves, comp_move);
draw_move_made(&w, &c, src_index, dst_index, comp_move.src, comp_move.dst, &comp_b, BOARD_RECT);
c.TURN = 1 - c.TURN;
draw_curr_state(&w, &c);
free_list(PC_moves);
}
// Recognize mouse-button-down event and determine number of the pressed button.
switch (event.type) {
case SDL_MOUSEBUTTONDOWN: {
Uint8 *keys;
keys = SDL_GetKeyState(NULL);
if (keys[SDLK_ESCAPE] == SDL_PRESSED) {
free_window(w);
exit(0);
} else {
int pressed_button = find_panel(mouseMotion, w);
onClick(pressed_button, &w, &c);
}
}
break;
// Follow all mouse motion events
case SDL_MOUSEMOTION: {
mouseMotion = event;
break;
}
case SDL_QUIT: {
free_window(w);
exit(0);
}
break;
}
}
printf("SDL_WaitEvent error: %s\n", SDL_GetError());
exit(1);
}
/** GUI widgets creators:
* All functions below generate a widget to be used in the program.
* Each of the creators receives the necessary structure fields **/
SDL_Rect create_rect(int x, int y, int width, int height) {
SDL_Rect rect = { x, y, width, height };
return rect;
}
int create_button(button* b, SDL_Rect parent_rect, int x_pos, int y_pos,
int width, int height, image image) {
b->rect = create_rect(parent_rect.x + x_pos, parent_rect.y + y_pos, width,
height);
b->image = image;
b->button_window = IMG_Load(image.pathToImage);
if (b->button_window == NULL) {
fprintf(stderr, "Couldn't set 800x600x8 video mode: %s\n",
SDL_GetError());
return 0;
}
return 1;
}
/** Expects: a new pathToImage.
* Changes the image of button b (used when background needs to be changed etc.);
* Returns: 0 in case of error, 1 otherwise **/
int edit_button(button* b, char* pathToImage) {
b->image = create_image(pathToImage);
SDL_FreeSurface(b->button_window);
b->button_window = IMG_Load(b->image.pathToImage);
if (b->button_window == NULL) {
fprintf(stderr, "Couldn't set 800x600x8 video mode: %s\n",
SDL_GetError());
return 0;
}
return 1;
}
image create_image(char pathToImage[100]) {
image image;
strcpy(image.pathToImage, pathToImage);
return image;
}
int create_panel(SDL_Rect parent_rect, panel *p, int children_len, int x_pos,
int y_pos, int width, int height, image image,
button children[MAX_BUTTONS]) {
p->rect = create_rect(parent_rect.x + x_pos, parent_rect.y + y_pos, width,
height);
int i;
for (i = 0; i < MAX_BUTTONS; i++) {
p->children[i] = children[i];
}
p->children_len = children_len;
p->image = image;
if (strncmp(image.pathToImage, "empty", 5) != 0) {
p->panel_window = IMG_Load(image.pathToImage);
if (p->panel_window == NULL) {
fprintf(stderr, "Couldn't set 800x600x8 video mode: %s\n",
SDL_GetError());
return 0;
}
}
return 1;
}
int create_window(window* w, int children_len, char header[20],
panel children[10], image bgImage, char id) {
w->children_len = children_len;
strcpy(w->header, header);
int i;
for (i = 0; i < 10; i++) {
w->children[i] = children[i];
}
w->bgImage = bgImage;
w->id = id;
w->main_window = SDL_SetVideoMode(800, 600, 32, 0);
if (w->main_window == NULL) {
fprintf(stderr, "Couldn't set 800x600x8 video mode: %s\n",
SDL_GetError());
return 0;
}
return 1;
}
/** GUI widgets drawers:
* All functions below draw a widget on the screen.
* Each of the functions receives the widget, and its parent if needed **/
int draw_window(window w) {
SDL_WM_SetCaption(w.header, 0); // NULL can be some icon in the future
// SDL_WM_SetIcon(SDL_LoadBMP("mainIcon.bmp"), NULL); // Doesn't work
// Set Background
SDL_Surface* bg_temp = SDL_LoadBMP(w.bgImage.pathToImage);
SDL_BlitSurface(bg_temp, NULL, w.main_window, NULL);
SDL_Flip(w.main_window);
SDL_FreeSurface(bg_temp);
// Draw panels
panel child;
int i = 0;
for (child = w.children[i]; i < w.children_len; child = w.children[++i]) {
draw_panel(w.main_window, child);
}
return 1;
}
int draw_panel(SDL_Surface* mainWindow, panel p) {
if (strncmp(p.image.pathToImage, "empty", 5 != 0)) {
SDL_BlitSurface(p.panel_window, NULL, mainWindow, &(p.rect));
SDL_Flip(mainWindow);
}
button child;
int i = 0;
for (child = p.children[i]; i < p.children_len; child = p.children[++i]) {
draw_button(mainWindow, child);
}
return 1;
}
int draw_button(SDL_Surface* mainWindow, button b) {
SDL_BlitSurface(b.button_window, NULL, mainWindow, &(b.rect));
SDL_Flip(mainWindow);
return 1;
}
/** Window-drawing functions:
* All functions below draw some window of the program (main, settings, player selection etc.).
* Each of the functions initializes all widgets (images, buttons, panels) and sets all children lists as required. **/
int draw_main_window(window * w, PtrConfig c) {
//initalize c
c->DEPTH = 1;
c->BEST = 0;
c->MODE = 1;
c->TURN = 1;
c->USER_COLOR = 1;
c->END_GAME = 0;
init_board(c);
//initialize variables
button panel_children[MAX_BUTTONS];
button panel_children_title[MAX_BUTTONS];
button panel_children_quit[MAX_BUTTONS];
panel p1;
panel p2;
panel quit_panel;
panel slots;
button newGame;
button loadGame;
button quit;
//create_rects
SDL_Rect p1_rect = create_rect(0, 0, 80, 200);
//create_images
image newGameImage = create_image("graphics/NewGame.png");
image loadGameImage = create_image("graphics/LoadGame.png");
image quitImage = create_image("graphics/Quit.png");
image window_image = create_image("graphics/bg.bmp");
image logo_image = create_image("graphics/Logo.png");
image panel_image = create_image("graphics/transparent.png");
//create_buttons
create_button(&newGame, p1_rect, 30, 150, BUTTON_W, BUTTON_H, newGameImage);
create_button(&loadGame, p1_rect, 30, 225, BUTTON_W, BUTTON_H,
loadGameImage);
panel_children[0] = newGame;
panel_children[1] = loadGame;
create_button(&quit, create_rect(600, 500, 182, 54), 0, 0, BUTTON_W,
BUTTON_H, quitImage);
panel_children_quit[0] = quit;
panel window_children[10];
draw_empty_panel(&slots, ZERO_RECT, c);
create_panel(ZERO_RECT, &p1, 2, 0, 0, 212, 300, panel_image,
panel_children);
create_panel(ZERO_RECT, &p2, 0, 240, 30, 600, 200, logo_image,
panel_children_title);
create_panel(ZERO_RECT, &quit_panel, 1, 600, 500, 182, 54, panel_image,
panel_children_quit);
window_children[0] = p1;
window_children[1] = p2;
window_children[2] = quit_panel;
window_children[3] = slots;
create_window(w, 4, "Chess", window_children, window_image, 'm');
return draw_window(*w);
}
int draw_player_selection_window(window *w, Config* c) {
free_window(*w);
// initialize variables
button panel_children[MAX_BUTTONS];
panel window_children[10];
panel p;
panel header;
button header_children[MAX_BUTTONS];
button playerVsPlayer;
button playerVsPC;
button white;
button black;
button set_board;
button cancel;
button next;
// create_rects;
SDL_Rect p_rect = create_rect(0, 0, 80, 400);
image panel_image = create_image("graphics/transparent.png");
image bg_image = create_image("graphics/bg.bmp");
image header_image = create_image("graphics/PlayerSelection.png");
image playerVsPlayerImage =
(c->MODE == TWO_PLAYERS) ?
create_image("graphics/PlayerVsPlayerPressed.png") :
create_image("graphics/PlayerVsPlayer.png");
image playerVsPCImage =
(c->MODE == ONE_PLAYER) ?
create_image("graphics/PlayerVsPCPressed.png") :
create_image("graphics/PlayerVsPC.png");
image whiteBeginsImage =
(c->TURN == WHITE) ?
create_image("graphics/WhiteBeginsPressed.png") :
create_image("graphics/WhiteBegins.png");
image blackBeginsImage =
(c->TURN == BLACK) ?
create_image("graphics/BlackBeginsPressed.png") :
create_image("graphics/BlackBegins.png");
image setBoardImage = create_image("graphics/SetTheBoard.png");
image cancelImage = create_image("graphics/Cancel.png");
image nextImage = create_image("graphics/Next.png");
create_button(&playerVsPlayer, p_rect, 30, 150, BUTTON_W, BUTTON_H,
playerVsPlayerImage);
create_button(&playerVsPC, p_rect, 250, 150, BUTTON_W, BUTTON_H,
playerVsPCImage);
create_button(&white, p_rect, 30, 225, BUTTON_W, BUTTON_H,
whiteBeginsImage);
create_button(&black, p_rect, 250, 225, BUTTON_W, BUTTON_H,
blackBeginsImage);
create_button(&set_board, p_rect, 30, 300, BUTTON_W, BUTTON_H,
setBoardImage);
create_button(&cancel, p_rect, 30, 450, BUTTON_W, BUTTON_H, cancelImage);
create_button(&next, p_rect, 585, 450, BUTTON_W, BUTTON_H, nextImage);
panel_children[0] = playerVsPlayer;
panel_children[1] = playerVsPC;
panel_children[2] = white;
panel_children[3] = black;
panel_children[4] = set_board;
panel_children[5] = cancel;
panel_children[6] = next;
create_panel(ZERO_RECT, &header, 0, 164, 30, 472, 97, header_image,
header_children);
create_panel(ZERO_RECT, &p, 7, 0, 0, 800, 600, panel_image, panel_children);
window_children[0] = p;
window_children[1] = header;
create_window(w, 2, "Chess", window_children, bg_image, 'p');
return draw_window(*w);
}
int draw_settings_window(window* w, Config* c) {
free_window(*w);
// initialize variables
panel window_children[10];
panel header;
button header_children[MAX_BUTTONS];
panel difficulty;
button difficulty_children[MAX_BUTTONS];
panel panel;
button panel_children[MAX_BUTTONS];
// initialize buttons
button diff1;
button diff2;
button diff3;
button diff4;
button diffBest;
button IPlayWhite;
button IPlayBlack;
button cancel;
button next;
image panel_image = create_image("graphics/transparent.png");
image bg_image = create_image("graphics/bg.bmp");
image header_image = create_image("graphics/Settings.png");
image difficultyImage = create_image("graphics/Difficulty.png");
image diff1Image =
(c->DEPTH == 1) ?
create_image("graphics/1Pressed.png") :
create_image("graphics/1.png");
image diff2Image =
(c->DEPTH == 2) ?
create_image("graphics/2Pressed.png") :
create_image("graphics/2.png");
image diff3Image =
(c->DEPTH == 3) ?
create_image("graphics/3Pressed.png") :
create_image("graphics/3.png");
image diff4Image =
(c->DEPTH == 4 && c->BEST == 0) ?
create_image("graphics/4Pressed.png") :
create_image("graphics/4.png");
image diffBestImage =
(c->BEST) ?
create_image("graphics/BestPressed.png") :
create_image("graphics/Best.png");
image IPlayWhiteImage =
(c->USER_COLOR == WHITE) ?
create_image("graphics/IPlayWhitePressed.png") :
create_image("graphics/IPlayWhite.png");
image IPlayBlackImage =
(c->USER_COLOR == BLACK) ?
create_image("graphics/IPlayBlackPressed.png") :
create_image("graphics/IPlayBlack.png");
image cancelImage = create_image("graphics/Cancel.png");
image nextImage = create_image("graphics/Next.png");
// create rects;
SDL_Rect p_rect = create_rect(0, 0, 80, 400);
create_button(&diff1, p_rect, 30, 160, 100, BUTTON_H, diff1Image);
create_button(&diff2, p_rect, 140, 160, 100, BUTTON_H, diff2Image);
create_button(&diff3, p_rect, 250, 160, 100, BUTTON_H, diff3Image);
create_button(&diff4, p_rect, 360, 160, 100, BUTTON_H, diff4Image);
create_button(&diffBest, p_rect, 470, 160, 100, BUTTON_H, diffBestImage);
create_button(&IPlayWhite, p_rect, 30, 280, BUTTON_W, BUTTON_H,
IPlayWhiteImage);
create_button(&IPlayBlack, p_rect, 250, 280, BUTTON_W, BUTTON_H,
IPlayBlackImage);
create_button(&cancel, p_rect, 30, 450, BUTTON_W, BUTTON_H, cancelImage);
create_button(&next, p_rect, 585, 450, BUTTON_W, BUTTON_H, nextImage);
panel_children[0] = diff1;
panel_children[1] = diff2;
panel_children[2] = diff3;
panel_children[3] = diff4;
panel_children[4] = diffBest;
panel_children[5] = IPlayWhite;
panel_children[6] = IPlayBlack;
panel_children[7] = cancel;
panel_children[8] = next;
create_panel(ZERO_RECT, &header, 0, 269, 30, 261, 97, header_image,
header_children);
create_panel(ZERO_RECT, &difficulty, 0, 13, 100, 214, 70, difficultyImage,
difficulty_children);
create_panel(ZERO_RECT, &panel, 9, 0, 0, 800, 600, panel_image,
panel_children);
window_children[0] = header;
window_children[1] = difficulty;
window_children[2] = panel;
create_window(w, 3, "Chess", window_children, bg_image, 's');
return draw_window(*w);
}
int draw_set_board_window(window* w, Config* c) {
free_window(*w);
// initialize variables
panel window_children[10];
panel board_panel;
panel menu;
// create images
image bg_image = create_image("graphics/transparent.png");
// create rects;
SDL_Rect board_rect = BOARD_RECT;
SDL_Rect menu_rect = create_rect(600, 0, 200, 600);
create_board_panel(&board_panel, board_rect, c);
create_set_board_menu(&menu, menu_rect, c);
window_children[0] = board_panel;
window_children[1] = menu;
create_window(w, 2, "Chess", window_children, bg_image, 'b');
return draw_window(*w);
}
/** Expects: menu panel, menu rectangle and a configuration pointer c.
* Creates the panel containing all pieces to be placed on the board in the Set-The-Board phase.
* Assigns menu_panel to the created panel, and returns 1. **/
int create_set_board_menu(panel* menu_panel, SDL_Rect menu_rect, Config * c) {
button menu_children[MAX_BUTTONS];
char pieces[6] = { 'm', 'n', 'b', 'r', 'q', 'k' };
char button_path[23];
strcpy(button_path, "graphics/pieces/ u.png");
int i;
for (i = 0; i < 6; i++) {
button_path[16] = pieces[i];
image button_image = create_image(button_path);
button b1;
create_button(&b1, menu_rect, 20, SPACE + (SQUARE_SIZE + SPACE - 4) * i,
SQUARE_SIZE,
SQUARE_SIZE, button_image);
menu_children[2 * i] = b1;
button_path[16] = toupper(pieces[i]);
button_image = create_image(button_path);
button b2;
create_button(&b2, menu_rect, 120,
SPACE + (SQUARE_SIZE + SPACE - 4) * i, SQUARE_SIZE,
SQUARE_SIZE, button_image);
menu_children[2 * i + 1] = b2;
}
image bin_image = create_image("graphics/pieces/tu.png");
image cancel_image = create_image("graphics/SmallCancel.png");
image cont_image =
(c->MODE == ONE_PLAYER) ?
create_image("graphics/SmallNext.png") :
create_image("graphics/Play.png");
button bin, cancel, cont;
create_button(&bin, menu_rect, 72, SPACE + (SQUARE_SIZE + SPACE - 4) * 6,
SQUARE_SIZE, SQUARE_SIZE, bin_image);
create_button(&cancel, menu_rect, 8, SPACE + (SQUARE_SIZE + SPACE - 4) * 7,
92, BUTTON_H, cancel_image);
create_button(&cont, menu_rect, 103, SPACE + (SQUARE_SIZE + SPACE - 4) * 7,
92, BUTTON_H, cont_image);
menu_children[12] = bin;
menu_children[13] = cancel;
menu_children[14] = cont;
image bg_image = create_image("graphics/whitebgPanel.bmp");
create_panel(ZERO_RECT, menu_panel, 15, 600, 0, 200, 600, bg_image,
menu_children);
return 1;
}
/** Expects: board panel, board rectangle and a configuration pointer c.
* Creates the board panel for both Set-The-Board and Game window.
* Assigns board_panel to the created panel, and returns 1. **/
int create_board_panel(panel* board_panel, SDL_Rect board_rect, Config * c) {
button board_children[MAX_BUTTONS];
image bg_image = create_image("graphics/Board.png");
// Fill board_children with 64 buttons
int i, j;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
Location loc = create_location(i, j);
board_children[i * (BOARD_SIZE) + j] = custom_button(loc,
BOARD(i, j), 0, board_rect);
}
}
create_panel(ZERO_RECT, board_panel, MAX_BUTTONS, 0, 0, 600, 600, bg_image,
board_children);
return 1;
}
/** Expects: window w a configuration pointer c.
* Draws the complete game window (including the board and the side-menu).
* Returns the result of draw_curr_state(w, c)**/
int draw_game_window(window* w, Config* c) {
free_window(*w);
// initialize panels
panel window_children[10];
panel board_panel;
panel menu;
panel toBeFilled;
// create images
image bg_image = create_image("graphics/transparent.png");
// create rects;
SDL_Rect board_rect = BOARD_RECT;
SDL_Rect menu_rect = create_rect(600, 0, 200, 600);
create_board_panel(&board_panel, board_rect, c);
create_game_menu(&menu, menu_rect, c);
draw_empty_panel(&toBeFilled, ZERO_RECT, c);
window_children[0] = board_panel;
window_children[1] = menu;
window_children[2] = toBeFilled;
create_window(w, 3, "Chess", window_children, bg_image, 'g');
draw_window(*w);
return draw_curr_state(w, c);
}
/** Expects: menu panel, menu rectangle and a pointer to configuration c.
* Creates the side-menu in game window.
* Assigns menu_panel to the created menu and returns 1 **/
int create_game_menu(panel* menu_panel, SDL_Rect menu_rect, Config* c) {
button menu_children[MAX_BUTTONS];
// initialize buttons
button getMove;
button saveGame;
button mainMenu;
button quit;
// initialize images
image bg_image = create_image("graphics/whitebgPanel.bmp");
image getMove_image = create_image("graphics/GetMove.png");
image saveGame_image = create_image("graphics/SaveGame.png");
image mainMenu_image = create_image("graphics/MainMenu.png");
image quit_image = create_image("graphics/Quit.png");
create_button(&getMove, menu_rect, 8, 13, BUTTON_W, BUTTON_H,
getMove_image);
create_button(&saveGame, menu_rect, 8, 17 + BUTTON_H + SPACE, BUTTON_W,
BUTTON_H, saveGame_image);
create_button(&mainMenu, menu_rect, 8, 470, BUTTON_W, BUTTON_H,
mainMenu_image);
create_button(&quit, menu_rect, 8, 470 + BUTTON_H + SPACE, BUTTON_W,
BUTTON_H, quit_image);
menu_children[0] = getMove;
menu_children[1] = saveGame;
menu_children[2] = mainMenu;
menu_children[3] = quit;
create_panel(ZERO_RECT, menu_panel, 4, 600, 0, 200, 600, bg_image,
menu_children);
return 1;
}
/** Expects: a declared panel toBeFilled, some rectangle and a pointer to configuration c.
* Draws an empty panel for cases in which panels should be re-drawn on an empty surface.
* Returns 1. **/
int draw_empty_panel(panel* toBeFilled, SDL_Rect rect, Config* c) {
button toBeFilled_children[MAX_BUTTONS];
image bg_image = create_image("graphics/transparent.png");
create_panel(ZERO_RECT, toBeFilled, 0, 0, 0, 0, 0, bg_image,
toBeFilled_children);
return 1;
}
/** Expects: slots panel, slots rectangle and a pointer to configuration c.
* Creates the slots panel for both "Load Game" and "Save Game" options.
* Assigns slots_panel the created panel and returns 1. **/
int create_slots_panel(panel* slots_panel, SDL_Rect slots_rect, Config* c) {
button slots_children[MAX_BUTTONS];
// initialize buttons
int endOfLoop = ((SLOTS_NUM % 2) == 0)? SLOTS_NUM : SLOTS_NUM - 1;
int i;
char path[19] = "graphics/slotx.png";
for (i = 0; i < endOfLoop; i++) {
button b;
path[13] = '0' + i;
image button_image = create_image(path);
create_button(&b, slots_rect, 32 + (54 + 3 * SPACE) * (i % 2),
((i - (i % 2)) / 2) * (54 + SPACE), 54, 54,
button_image);
slots_children[i] = b;
}
if ((SLOTS_NUM % 2) == 1) { // odd number of slots
button b;
path[13] = '0' + i;
image button_image = create_image(path);
create_button(&b, slots_rect, 73, ((i - (i % 2)) / 2) * (54 + SPACE), 54, 54,
button_image);
slots_children[i] = b;
}
image SlotsPanelBG = create_image("graphics/transparent.png");
create_panel(ZERO_RECT, slots_panel, SLOTS_NUM, slots_rect.x, slots_rect.y,
slots_rect.w, slots_rect.h, SlotsPanelBG, slots_children);
return 1;
}
/** Expects: depths panel, depths rectangle and a pointer to configuration c.
* Creates the depths panel for "Get Move" option in game window.
* Assigns depths_panel the created panel and returns 1. **/
int draw_depths_panel(panel* depths_panel, SDL_Rect depths_rect, Config* c) {
button depths_children[MAX_BUTTONS];
// initialize buttons
int i;
char path[20] = "graphics/depthx.png";
for (i = 0; i < 4; i++) {
button b;
path[14] = '0' + (i + 1);
image button_image = create_image(path);
create_button(&b, depths_rect, 32 + (54 + 3 * SPACE) * (i % 2),
((i - (i % 2)) / 2) * (54 + SPACE) + 9 * SPACE, 54, 54,
button_image);
depths_children[i] = b;
}
button b;
path[14] = 'b';
image button_image = create_image(path);
create_button(&b, depths_rect, 73, 2 * (54 + SPACE) + 9 * SPACE, 54, 54,
button_image);
depths_children[4] = b;
image depth_image = create_image("graphics/Depth.png");
button depth;
create_button(&depth, depths_rect, 40, 22, 150, 38, depth_image);
depths_children[5] = depth;
image DepthsPanelBG = create_image("graphics/transparent.png");
create_panel(ZERO_RECT, depths_panel, 6, 600, 137, 198, 310, DepthsPanelBG,
depths_children);
return 1;
}
/** Expects: promote panel, some rectangle and a pointer to configuration c.
* Creates the promote panel for when a pawn reaches the last row.
* Assigns promote_panel the created panel and returns 1. **/
int draw_promote_panel(panel* promote_panel, SDL_Rect rect, Config* c) {
button promote_children[MAX_BUTTONS];
// initialize buttons
char pieces[4] = { 'q', 'n', 'b', 'r' };
char path[23] = "graphics/pieces/xu.png";
int i;
for (i = 0; i < 4; i++) {
button b;
path[16] = (c->TURN == BLACK) ? toupper(pieces[i]) : pieces[i];
image button_image = create_image(path);
create_button(&b, rect, 20 + (SQUARE_SIZE + 3 * SPACE) * (i % 2),
((i - (i % 2)) / 2) * (SQUARE_SIZE + SPACE) + 9 * SPACE,
SQUARE_SIZE, SQUARE_SIZE, button_image);
promote_children[i] = b;
}
image DepthsPanelBG = create_image("graphics/transparent.png");
create_panel(ZERO_RECT, promote_panel, 4, 600, 137, 198, 310, DepthsPanelBG,
promote_children);
return 1;
}
/** Expects: window w, configuration pointer c, a game-state and some rectangle.
* Draws "Check", "Mate" or "Tie" on the side menu according to state.
* Returns 1. **/
int draw_state(window* w, Config *c, int state, SDL_Rect rect) {
panel p;
button p_children[MAX_BUTTONS];
image PanelBG;
switch (state) {
case 0: //mate
PanelBG = create_image("graphics/Mate.png");
break;
case 1: //tie
PanelBG = create_image("graphics/Tie.png");
break;
case 2: //check
PanelBG = create_image("graphics/Check.png");
break;
case 3:
PanelBG = create_image("graphics/transparent.png");
}
create_panel(ZERO_RECT, &p, 0, rect.x + 40, rect.y + 300, rect.w, rect.h,
PanelBG, p_children);
free_panel(w->children[2]);
w->children[2] = p;
draw_panel(w->main_window, w->children[1]);
draw_panel(w->main_window, w->children[2]);
return 1;
}
/** Expects: window w, configuration pointer c.
* Identifies the game-state and draws the correct banner on the side menu, using draw_state function from above.
* Returns 1. **/
int draw_curr_state(window* w, Config *c) {
SDL_Rect menu_rect = create_rect(600, 0, 200, 600);
List* opponent_moves = init_list();
if (opponent_moves == NULL) { // malloc failure...
exit(0);
return 0;
}
if (!generate_legal_moves(c, opponent_moves)) { // malloc failure...
free_list(opponent_moves);
exit(0);
return 0;
}
int ch = check(c);
switch (ch) {
case 2: //malloc failure
printf("check function failed!");
c->END_GAME = 1;
free_list(opponent_moves);
exit(0);
return 0;
case 0: //no check
if (opponent_moves->size == 0) { //tie
draw_state(w, c, 1, menu_rect);
c->END_GAME = 1;
} else if (w->children[2].image.pathToImage[9] != 't') { //it was check before
draw_state(w, c, 3, menu_rect);
}
break;
case 1: //check
if (opponent_moves->size == 0) { //no moves
draw_state(w, c, 0, menu_rect);
printf("Mate! %s player wins the game\n",
c->TURN == BLACK ? "White" : "Black");
c->END_GAME = 1;
} else { //display check
draw_state(w, c, 2, menu_rect);
print_message(CHECK);
}
break;
}
free_list(opponent_moves);
return 1;
}
/** OnClick functions **/
/** All the function below deal with OnClick events.
* onClick recognizes the current window and then calls the corresponding OnClick functions, unique for the specific window.
* Each specific OnClick functions switches cases between pressed buttons - and responds according to the button pressed.
* Macros are used as cases (instead of integers) to make code more readable. **/
/** Expects: Location loc, piece, button_selected integer and a rectangle.
* Returns a button with the correct image regarding the argument 'piece', and whether the button is selected or not. */
button custom_button(Location loc, char piece, int button_selected,
SDL_Rect p_rect) {
char button_path[23];
strcpy(button_path, "graphics/pieces/");
button_path[16] = (piece == EMPTY) ? 'e' : piece;
button_path[17] = (button_selected) ? 's' : 'u';
strcpy(&(button_path[18]), ".png");
button b;
image button_image = create_image(button_path);
create_button(&b, p_rect, 28 + loc.col * SQUARE_SIZE,
28 + (7 - loc.row) * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE,
button_image);
return b;
}
int onClick_main_window(int event, window* w, Config* c) {
panel p;
switch (event) {
case NEW_GAME_CASE: {
draw_player_selection_window(w, c);
break;
}
case LOAD_GAME_CASE: {
create_slots_panel(&p, create_rect(20, 287, 200, 310), c);
free_panel(w->children[3]);
w->children[3] = p;
draw_panel(w->main_window, w->children[3]);
break;
}
case QUIT_CASE: {
free_window(*w);
exit(0);
break;
}
}
if (8 <= event && event <= (8 + SLOTS_NUM - 1)) {
char path[18] = "SavedGames/x.xml\n";
path[11] = '0' + event - 8;
load_file(path, c);
draw_player_selection_window(w, c);
}
return 0;
}
int onClick_player_selection_window(int event, window* w, Config* c) {
switch (event) {
case PLAYER_VS_PLAYER_CASE: {
c->MODE = TWO_PLAYERS;
toggle_buttons(w, 0, "graphics/PlayerVsPlayerPressed.png",
"graphics/PlayerVsPC.png", 0);
break;
}
case PLAYER_VS_PC_CASE: {
c->MODE = ONE_PLAYER;
toggle_buttons(w, 0, "graphics/PlayerVsPlayer.png",
"graphics/PlayerVsPCPressed.png", 0);
break;
}
case WHITE_BEGINS_CASE: {
c->TURN = WHITE;
toggle_buttons(w, 0, "graphics/WhiteBeginsPressed.png",
"graphics/BlackBegins.png", 2);
break;
}
case BLACK_BEGINS_CASE: {
c->TURN = BLACK;
toggle_buttons(w, 0, "graphics/WhiteBegins.png",
"graphics/BlackBeginsPressed.png", 2);
break;
}
case SET_BOARD_CASE: {
draw_set_board_window(w, c);
break;
}
case CANCEL_PS_CASE: {
free_window(*w);
draw_main_window(w, c);
break;
}
case NEXT_PS_CASE: {
if (c->MODE == TWO_PLAYERS) {
return draw_game_window(w, c);
} else {
return draw_settings_window(w, c);
}
}
}
return 0;
}
int onClick_settings_window(int event, window* w, Config* c) {
switch (event) {
case DIFF_1: {
c->BEST = 0;
c->DEPTH = 1;
char paths[5][30] = { "graphics/1Pressed.png", "graphics/2.png",
"graphics/3.png", "graphics/4.png", "graphics/Best.png" };
toggle_diffs(w, paths, 0);
break;
}
case DIFF_2: {
c->BEST = 0;
c->DEPTH = 2;
char paths[5][30] = { "graphics/1.png", "graphics/2Pressed.png",
"graphics/3.png", "graphics/4.png", "graphics/Best.png" };
toggle_diffs(w, paths, 0);
break;
}
case DIFF_3: {
c->BEST = 0;
c->DEPTH = 3;
char paths[5][30] = { "graphics/1.png", "graphics/2.png",
"graphics/3Pressed.png", "graphics/4.png", "graphics/Best.png" };
toggle_diffs(w, paths, 0);
break;
}
case DIFF_4: {
c->BEST = 0;
c->DEPTH = 4;
char paths[5][30] = { "graphics/1.png", "graphics/2.png",
"graphics/3.png", "graphics/4Pressed.png", "graphics/Best.png" };
toggle_diffs(w, paths, 0);
break;
}
case DIFF_BEST: {
c->BEST = 1;
c->DEPTH = 4;
char paths[5][30] = { "graphics/1.png", "graphics/2.png",
"graphics/3.png", "graphics/4.png", "graphics/BestPressed.png" };
toggle_diffs(w, paths, 0);
break;
}
case USER_WHITE_CASE: {
c->USER_COLOR = WHITE;
toggle_buttons(w, 2, "graphics/IPlayWhitePressed.png",
"graphics/IPlayBlack.png", 5);
break;
}
case USER_BLACK_CASE: {
c->USER_COLOR = BLACK;
toggle_buttons(w, 2, "graphics/IPlayWhite.png",
"graphics/IPlayBlackPressed.png", 5);
break;
}
case CANCEL_S_CASE: {
free_window(*w);
return draw_main_window(w, c);
}
case PLAY_S_CASE: {
return draw_game_window(w, c);
}
}
return 0;
}
int onClick_set_board_window(int event, window* w, Config* c) {
switch (event) {
case 80: // Cancel
free_window(*w);
return draw_main_window(w, c);
case 81: // Next / Play
if (ok_to_start(c)) {
if (c->MODE == TWO_PLAYERS) {
free_panel(w->children[1]);
panel menu;
SDL_Rect menu_rect = create_rect(600, 0, 200, 600);
create_game_menu(&menu, menu_rect, c);
w->children[1] = menu;
panel toBeFilled;
draw_empty_panel(&toBeFilled, ZERO_RECT, c);
w->children[2] = toBeFilled;
w->children_len = 3;
w->id = 'g';
draw_panel(w->main_window, w->children[1]);
return draw_curr_state(w, c);
} else {
return draw_settings_window(w, c);
}
}
}
if (67 <= event && event <= 79) { // Piece is chosen
int i;
for (i = 67; i <= 79; i++) { //go over pieces, see if anyone is marked
char new_char = 'u';
button b = w->children[1].children[i - 67];
if (i == event) {
new_char = 's';
if (b.image.pathToImage[17] == 's') {
break;
}
}