forked from bktomer/ChessProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.c
2206 lines (2058 loc) · 63.6 KB
/
gui.c
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"
SDL_Surface* chess_logo = NULL;
SDL_Surface* background = NULL;
SDL_Surface* screen = NULL;
SDL_Surface* next_player_logo = NULL;
SDL_Surface* game_mode_logo = NULL;
SDL_Surface* set_the_board_button = NULL;
SDL_Surface* pvp_button = NULL;
SDL_Surface* pvc_button = NULL;
SDL_Surface* white_button = NULL;
SDL_Surface* black_button = NULL;
SDL_Surface* white_s_button = NULL;
SDL_Surface* black_s_button = NULL;
SDL_Surface* cancel_button = NULL;
SDL_Surface* new_game_button = NULL;
SDL_Surface* load_game_button = NULL;
SDL_Surface* quit_game_button = NULL;
SDL_Surface* pvc_s_button = NULL;
SDL_Surface* pvp_s_button = NULL;
SDL_Surface* dol_logo = NULL;
SDL_Surface* player_col_logo = NULL;
SDL_Surface* difficulty_logo = NULL;
SDL_Surface* best_depth_button = NULL;
SDL_Surface* best_depth_s_button = NULL;
SDL_Surface* depth_1_button = NULL;
SDL_Surface* depth_1_s_button = NULL;
SDL_Surface* depth_2_button = NULL;
SDL_Surface* depth_2_s_button = NULL;
SDL_Surface* depth_3_button = NULL;
SDL_Surface* depth_3_s_button = NULL;
SDL_Surface* depth_4_button = NULL;
SDL_Surface* depth_4_s_button = NULL;
SDL_Surface* start_the_game_button = NULL;
SDL_Surface* cmd_logo = NULL;
SDL_Surface* thinking_label = NULL;
SDL_Surface* chessboard = NULL;
SDL_Surface* mainmenu_button = NULL;
SDL_Surface* savegame_button = NULL;
SDL_Surface* bestmove_button = NULL;
SDL_Surface* last_move_button = NULL;
SDL_Surface* WK = NULL;
SDL_Surface* WQ = NULL;
SDL_Surface* WR = NULL;
SDL_Surface* WB = NULL;
SDL_Surface* WN = NULL;
SDL_Surface* WP = NULL;
SDL_Surface* BK = NULL;
SDL_Surface* BQ = NULL;
SDL_Surface* BR = NULL;
SDL_Surface* BB = NULL;
SDL_Surface* BN = NULL;
SDL_Surface* BP = NULL;
SDL_Surface* WS = NULL;
SDL_Surface* BS = NULL;
SDL_Surface* WK_S = NULL;
SDL_Surface* WQ_S = NULL;
SDL_Surface* WR_S = NULL;
SDL_Surface* WB_S = NULL;
SDL_Surface* WN_S = NULL;
SDL_Surface* WP_S = NULL;
SDL_Surface* BK_S = NULL;
SDL_Surface* BQ_S = NULL;
SDL_Surface* BR_S = NULL;
SDL_Surface* BB_S = NULL;
SDL_Surface* BN_S = NULL;
SDL_Surface* BP_S = NULL;
SDL_Surface* WS_S = NULL;
SDL_Surface* BS_S = NULL;
SDL_Surface* check_logo = NULL;
SDL_Surface* mate_w_logo = NULL;
SDL_Surface* mate_b_logo = NULL;
SDL_Surface* tie_logo = NULL;
SDL_Surface* illegal_move_logo = NULL;
SDL_Surface* set_and_ret_button = NULL;
SDL_Surface* clear_button = NULL;
SDL_Surface* wb_init_logo = NULL;
SDL_Surface* wrong_piece_logo = NULL;
SDL_Surface* rook_p = NULL;
SDL_Surface* knight_p = NULL;
SDL_Surface* queen_p = NULL;
SDL_Surface* bishop_p = NULL;
SDL_Surface* choose_p = NULL;
SDL_Surface* click_ts_logo = NULL;
SDL_Surface* click_tl_logo = NULL;
SDL_Surface* ns_logo = NULL;
SDL_Surface* slot1_button = NULL;
SDL_Surface* slot2_button = NULL;
SDL_Surface* slot3_button = NULL;
SDL_Surface* slot4_button = NULL;
SDL_Surface* slot5_button = NULL;
SDL_Surface* slot6_button = NULL;
SDL_Surface* slot7_button = NULL;
SDL_Surface* click_tr_button = NULL;
SDL_Surface* game_saved_logo = NULL;
SDL_Rect NG_button = { 550, 150, 204, 54 };
SDL_Rect LG_button = { 550, 250, 204, 54 };
SDL_Rect Q_button = { 550, 350, 204, 54 };
SDL_Rect PVP_button = { 270, 150, 212, 50 };
SDL_Rect PVC_button = { 490, 150, 212, 50 };
SDL_Rect Cancel_button = { 490, 400, 212, 50 };
SDL_Rect STB_button = { 490, 330, 212, 50 };
SDL_Rect W_button = { 270, 220, 212, 50 };
SDL_Rect B_button = { 490, 220, 212, 50 };
SDL_Rect D1_button = { 250, 170, 75, 50 };
SDL_Rect D2_button = { 330, 170, 75, 50 };
SDL_Rect D3_button = { 410, 170, 75, 50 };
SDL_Rect D4_button = { 490, 170, 75, 50 };
SDL_Rect BD_button = { 570, 170, 212, 50 };
SDL_Rect return_AI_button = { 390, 310, 285, 50 };
SDL_Rect D1_M_button = { 140, 220, 75, 50 };
SDL_Rect D2_M_button = { 220, 220, 75, 50 };
SDL_Rect D3_M_button = { 300, 220, 75, 50 };
SDL_Rect D4_M_button = { 380, 220, 75, 50 };
SDL_Rect BD_M_button = { 460, 220, 212, 50 };
SDL_Rect return_M_button = { 390, 300, 285, 50 };
SDL_Rect W_AI_button = { 300, 240, 212, 50 };
SDL_Rect B_AI_button = { 530, 240, 212, 50 };
SDL_Rect SG_button = { 610, 0, 190, 50 };
SDL_Rect MM_button = { 610, 100, 190, 50 };
SDL_Rect Q_GW_button = { 610, 550, 190, 50 };
SDL_Rect CB_logo = { 0, 0, 600, 600 };
SDL_Rect bm_button = { 610, 200, 190, 50 };
SDL_Rect lm_button = {610, 250, 190, 50};
SDL_Rect start_PSW_button = { 270, 330, 212, 50 };
SDL_Rect s1_button = { 150, 200, 212, 50 };
SDL_Rect s2_button = { 150, 270, 212, 50 };
SDL_Rect s3_button = { 150, 340, 212, 50 };
SDL_Rect s4_button = { 450, 200, 212, 50 };
SDL_Rect s5_button = { 450, 270, 212, 50 };
SDL_Rect s6_button = { 450, 340, 212, 50 };
SDL_Rect s7_button = { 450, 410, 212, 50 };
SDL_Rect ctr_button = { 250, 550, 285, 50 };
SDL_Rect WP_box = { 620, 0 , 75 , 75 };
SDL_Rect WN_box = { 620, 80, 75, 75 };
SDL_Rect WR_box = { 620, 160, 75, 75 };
SDL_Rect WB_box = { 620, 240, 75, 75 };
SDL_Rect WQ_box = { 620, 320, 75, 75 };
SDL_Rect WK_box = { 620, 400, 75, 75 };
SDL_Rect BP_box = { 700, 0, 75, 75 };
SDL_Rect BN_box = { 700, 80, 75, 75 };
SDL_Rect BR_box = { 700, 160, 75, 75 };
SDL_Rect BB_box = { 700, 240, 75, 75 };
SDL_Rect BQ_box = { 700, 320, 75, 75 };
SDL_Rect BK_box = { 700, 400, 75, 75 };
SDL_Rect sat_button = { 607, 550, 190, 50 };
SDL_Rect clr_button = { 607, 480, 190, 50 };
//promotions buttons
SDL_Rect KP_button = { 610, 100, 190, 50 };
SDL_Rect BP_button = { 610, 175, 190, 50 };
SDL_Rect RP_button = { 610, 250, 190, 50 };
SDL_Rect QP_button = { 610, 325, 190, 50 };
SDL_Rect thinking_button = { 610, 325, 200, 60 };
SDL_Event event;
/*
* Freeing all available SDL resources
*/
void freeAll(){
//Free the following surfaces
SDL_FreeSurface(chess_logo);
SDL_FreeSurface(background);
SDL_FreeSurface(quit_game_button);
SDL_FreeSurface(new_game_button);
SDL_FreeSurface(load_game_button);
SDL_FreeSurface(game_mode_logo);
SDL_FreeSurface(set_the_board_button);
SDL_FreeSurface(white_button);
SDL_FreeSurface(black_button);
SDL_FreeSurface(white_s_button);
SDL_FreeSurface(black_s_button);
SDL_FreeSurface(cancel_button);
SDL_FreeSurface(pvp_button);
SDL_FreeSurface(pvc_button);
SDL_FreeSurface(next_player_logo);
SDL_FreeSurface(dol_logo);
SDL_FreeSurface(pvc_s_button);
SDL_FreeSurface(pvp_s_button);
SDL_FreeSurface(player_col_logo);
SDL_FreeSurface(difficulty_logo);
SDL_FreeSurface(best_depth_button);
SDL_FreeSurface(depth_1_button);
SDL_FreeSurface(depth_2_button);
SDL_FreeSurface(depth_3_button);
SDL_FreeSurface(depth_4_button);
SDL_FreeSurface(depth_1_s_button);
SDL_FreeSurface(depth_2_s_button);
SDL_FreeSurface(depth_3_s_button);
SDL_FreeSurface(depth_4_s_button);
SDL_FreeSurface(best_depth_s_button);
SDL_FreeSurface(start_the_game_button);
SDL_FreeSurface(cmd_logo);
SDL_FreeSurface(set_and_ret_button);
SDL_FreeSurface(clear_button);
SDL_FreeSurface(wb_init_logo);
SDL_FreeSurface(wrong_piece_logo);
SDL_FreeSurface(chessboard);
SDL_FreeSurface(mainmenu_button);
SDL_FreeSurface(savegame_button);
SDL_FreeSurface(WK);
SDL_FreeSurface(WQ);
SDL_FreeSurface(WR);
SDL_FreeSurface(WB);
SDL_FreeSurface(WN);
SDL_FreeSurface(WP);
SDL_FreeSurface(BK);
SDL_FreeSurface(BQ);
SDL_FreeSurface(BR);
SDL_FreeSurface(BB);
SDL_FreeSurface(BN);
SDL_FreeSurface(BP);
SDL_FreeSurface(WS);
SDL_FreeSurface(BS);
SDL_FreeSurface(WK_S);
SDL_FreeSurface(WQ_S);
SDL_FreeSurface(WR_S);
SDL_FreeSurface(WB_S);
SDL_FreeSurface(WN_S);
SDL_FreeSurface(WP_S);
SDL_FreeSurface(BK_S);
SDL_FreeSurface(BQ_S);
SDL_FreeSurface(BR_S);
SDL_FreeSurface(BB_S);
SDL_FreeSurface(BN_S);
SDL_FreeSurface(BP_S);
SDL_FreeSurface(WS_S);
SDL_FreeSurface(BS_S);
SDL_FreeSurface(check_logo);
SDL_FreeSurface(mate_w_logo);
SDL_FreeSurface(mate_b_logo);
SDL_FreeSurface(tie_logo);
SDL_FreeSurface(illegal_move_logo);
SDL_FreeSurface(bestmove_button);
SDL_FreeSurface(thinking_label);
SDL_FreeSurface(last_move_button);
SDL_FreeSurface(rook_p);
SDL_FreeSurface(choose_p);
SDL_FreeSurface(knight_p);
SDL_FreeSurface(bishop_p);
SDL_FreeSurface(queen_p);
SDL_FreeSurface(click_ts_logo);
SDL_FreeSurface(click_tl_logo);
SDL_FreeSurface(ns_logo);
SDL_FreeSurface(slot1_button);
SDL_FreeSurface(slot2_button);
SDL_FreeSurface(slot3_button);
SDL_FreeSurface(slot4_button);
SDL_FreeSurface(slot5_button);
SDL_FreeSurface(slot6_button);
SDL_FreeSurface(slot7_button);
SDL_FreeSurface(click_tr_button);
SDL_FreeSurface(game_saved_logo);
SDL_FreeSurface(screen);
}
/*
* Setting color key with stability check
*/
void safe_SDL_SetColorKey(SDL_Surface* surface, int R, int G, int B){
if (SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, R, G, B)) != 0) {
printf("ERROR: failed to set color key: %s\n", SDL_GetError());
freeAll();
exit(1);
}
}
/*
* Flipping the buffer with stability check
*/
void safe_SDL_Flip(SDL_Surface* surface){
//Update the surface
if (surface != NULL){
if (SDL_Flip(surface) != 0)
{
printf("ERROR: failed to flip buffer: %s\n", SDL_GetError());
freeAll();
exit(1);
}
}
}
/*
* Applying the surface function
*/
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
if (SDL_BlitSurface(source, NULL, destination, &offset) != 0){
printf("ERROR: failed to blit image: %s\n", SDL_GetError());
freeAll();
exit(1);
}
}
/*
* Detecting whether the given coordinates are in a specified Rect
*/
int xyInRect(int x, int y, SDL_Rect rect){
return ((x > rect.x) && (x < rect.x + rect.w) && (y > rect.y) && (y < rect.y + rect.h));
}
/*
* Loading bitmaps function
*/
SDL_Surface *load_image(char* filename)
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
char* full_path = concat("images/", filename);
//Load the image
loadedImage = SDL_LoadBMP(full_path);
free(full_path);
//If nothing went wrong in loading the image
if (loadedImage != NULL)
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat(loadedImage);
//Free the old image
SDL_FreeSurface(loadedImage);
}
//Return the optimized image
return optimizedImage;
}
/*
* Concatenating two strings
*/
char* concat(char* s1, char* s2)
{
char* result = calloc(strlen(s1) + strlen(s2) + 1,sizeof(char)); //+1 for the zero-terminator
validate(result, "concat");
int i;
for ( i = 0; i < (int)strlen(s1); i++){
result[i] = s1[i];
}
for (int j = 0; j < (int)strlen(s2); j++){
result[i++] = s2[j];
}
return result;
}
/*
* Loading Main window
*/
void loadMainWindow(){
//Load the images
chess_logo = load_image("chesslogo.bmp");
background = load_image("background.bmp");
quit_game_button = load_image("quit.bmp");
new_game_button = load_image("newgame.bmp");
load_game_button = load_image("loadgame.bmp");
//Set the window caption
SDL_WM_SetCaption("Main Window", NULL);
//Check if bitmap loading have succeeded
if (chess_logo == NULL || background == NULL || quit_game_button == NULL || load_game_button == NULL || new_game_button == NULL)
{
printf("ERROR: unable to load bitmap: %s\n", SDL_GetError());
freeAll();
exit(1);
}
//Apply the background to the screen
apply_surface(0, 0, background, screen);
//Apply the logo to the screen
apply_surface(250, 0, chess_logo, screen);
//Apply the quit button to the screen
apply_surface(550, 350, quit_game_button, screen);
//Apply the New Game button to the screen
apply_surface(550, 150, new_game_button, screen);
//Apply the Load Game button to the screen
apply_surface(550, 250, load_game_button, screen);
//The mouse offsets
int x = 0, y = 0;
int running = 1;
while (running){
//While there's an event to handle
while (SDL_PollEvent(&event))
{
if (event.type == SDL_MOUSEBUTTONDOWN)
{
//Get the mouse offsets
x = event.button.x;
y = event.button.y;
//If the left mouse button was pressed
if (event.button.button == SDL_BUTTON_LEFT)
{
//If New Game button was clicked
if (xyInRect(x, y, NG_button))
{
//Show the selection window for a player to choose his options
loadPlayersSelectionWindow(0);
}
//If Load Game button was clicked
if (xyInRect(x, y, LG_button))
{
// Loading the saved board and showing the selection window to the user -> Loading saved board must be implemented here
loadSaveGameOrLoadGameWindow(0);
}
//If Quit Game button was clicked
if (xyInRect(x, y, Q_button))
{
running = 0;
}
}
}
//If the user has Xed out the window
if (event.type == SDL_QUIT)
{
//Quit the program
running = 0;
}
safe_SDL_Flip(screen);
}
}
freeAll();
//Quit SDL
SDL_Quit();
exit(0);
}
/*
* Loading Players Selection window
*/
void loadPlayersSelectionWindow(){
apply_surface(0, 0, background, screen);
apply_surface(250, 0, chess_logo, screen);
//Load the relevant images
set_the_board_button = load_image("set_the_board.bmp");
next_player_logo = load_image("nextplayer.bmp");
game_mode_logo = load_image("gamemode.bmp");
pvc_button = load_image("pvc.bmp");
pvp_button = load_image("pvp.bmp");
pvc_s_button = load_image("pvc_s.bmp");
pvp_s_button = load_image("pvp_s.bmp");
dol_logo = load_image("dol_logo.bmp");
cancel_button = load_image("cancel.bmp");
black_button = load_image("black.bmp");
white_button = load_image("white.bmp");
white_s_button = load_image("white_s.bmp");
black_s_button = load_image("black_s.bmp");
start_the_game_button = load_image("start_the_game.bmp");
//Set the window caption
SDL_WM_SetCaption("Player Selection Window", NULL);
//Check if bitmap loading have succeeded
if (set_the_board_button == NULL || next_player_logo == NULL || game_mode_logo == NULL || pvc_button == NULL || pvp_button == NULL || cancel_button == NULL
|| black_button == NULL || white_button == NULL || white_s_button == NULL || black_s_button == NULL || start_the_game_button == NULL)
{
printf("ERROR: unable to load bitmap: %s\n", SDL_GetError());
freeAll();
exit(1);
}
//Apply the game mode logo to the screen
apply_surface(50, 150, game_mode_logo, screen);
//Apply the set the board button to the screen
apply_surface(490, 330, set_the_board_button, screen);
//Apply the Start The Game button
apply_surface(270, 330, start_the_game_button, screen);
//Apply the next player logo to the screen
apply_surface(50, 220, next_player_logo, screen);
if (game_mode == 1){
//Apply the PVP button to the screen
apply_surface(490, 150, pvc_button, screen);
apply_surface(270, 150, pvp_s_button, screen);
}
else{
//Apply the PVC button to the screen
apply_surface(490, 150, pvc_s_button, screen);
apply_surface(270, 150, pvp_button, screen);
}
//Apply the White,Black buttons to the screen according to the settings
if (next_player == 'W'){
apply_surface(270, 220, white_s_button, screen);
apply_surface(490, 220, black_button, screen);
}
else{
apply_surface(270, 220, white_button, screen);
apply_surface(490, 220, black_s_button, screen);
}
//Apply the Cancel button to the screen
apply_surface(490, 400, cancel_button, screen);
//Apply the "default or loaded settings are highlighted" logo
apply_surface(200, 565, dol_logo, screen);
//The mouse offsets
int x = 0, y = 0;
int running = 1;
while (running){
//While there's an event to handle
while (SDL_PollEvent(&event))
{
if (event.type == SDL_MOUSEBUTTONDOWN)
{
//Get the mouse offsets
x = event.button.x;
y = event.button.y;
//If the left mouse button was pressed
if (event.button.button == SDL_BUTTON_LEFT)
{
if (xyInRect(x, y, W_button))
{
//Setting next player color to white
apply_surface(270, 220, white_s_button, screen);
apply_surface(490, 220, black_button, screen);
next_player = 'W';
}
if (xyInRect(x, y, B_button))
{
//Setting next player color to black
apply_surface(270, 220, white_button, screen);
apply_surface(490, 220, black_s_button, screen);
next_player = 'B';
}
if (xyInRect(x, y, PVP_button))
{
//Set game mode to player vs. player and load window to choose minimax depth for the best move option
game_mode = 1;
loadMinimaxDepthWindow();
}
if (xyInRect(x, y, PVC_button))
{
//Set game mode to player vs. computer and load AI Settings Window
game_mode = 2;
loadAISettingsWindow();
}
if (xyInRect(x, y, STB_button))
{
//Setting the game board
loadBoardSettingWindow();
}
if (xyInRect(x, y, start_PSW_button))
{
//After all settings were set , start the game
loadGameWindow();
}
if (xyInRect(x, y, Cancel_button))
{
//Return to main window
minimax_depth = 1;
game_mode = 1;
user_color = 'W';
next_player = 'W';
init_board();
loadMainWindow();
}
}
}
//If the user has Xed out the window
if (event.type == SDL_QUIT)
{
//Quit the program
running = 0;
}
safe_SDL_Flip(screen);
}
}
freeAll();
//Quit SDL
SDL_Quit();
exit(0);
}
/*
* Loading a window to choose a minimax depth for the Best Move (for a user who chose to play Player vs. Player game mode)
*/
void loadMinimaxDepthWindow(){
apply_surface(0, 0, background, screen);
apply_surface(250, 0, chess_logo, screen);
//Load the relevant images
depth_1_button = load_image("1.bmp");
depth_1_s_button = load_image("1_s.bmp");
depth_2_button = load_image("2.bmp");
depth_2_s_button = load_image("2_s.bmp");
depth_3_button = load_image("3.bmp");
depth_3_s_button = load_image("3_s.bmp");
depth_4_button = load_image("4.bmp");
depth_4_s_button = load_image("4_s.bmp");
best_depth_button = load_image("best.bmp");
best_depth_s_button = load_image("best_s.bmp");
cmd_logo = load_image("cmd_logo.bmp");
click_tr_button = load_image("click_tr.bmp");
//Set the window caption
SDL_WM_SetCaption("Minimax Depth Settings Window", NULL);
//Check if bitmap loading have succeeded
if (depth_1_button == NULL || depth_2_button == NULL || depth_3_button == NULL || depth_4_button == NULL
|| best_depth_button == NULL || depth_1_s_button == NULL || depth_2_s_button == NULL
|| depth_3_s_button == NULL || depth_4_s_button == NULL || click_tr_button == NULL || best_depth_s_button == NULL || cmd_logo == NULL)
{
printf("ERROR: unable to load bitmap: %s\n", SDL_GetError());
freeAll();
exit(1);
}
//Apply the Choose Minimax Depth logo
apply_surface(110, 150, cmd_logo, screen);
//Apply start the game button to the screen
apply_surface(390, 300, click_tr_button, screen);
//Apply the minimax depth buttons according to the minimax_depth
if (minimax_depth == 1){
apply_surface(140, 220, depth_1_s_button, screen);
apply_surface(220, 220, depth_2_button, screen);
apply_surface(300, 220, depth_3_button, screen);
apply_surface(380, 220, depth_4_button, screen);
apply_surface(460, 220, best_depth_button, screen);
}
else if (minimax_depth == 2){
apply_surface(140, 220, depth_1_button, screen);
apply_surface(220, 220, depth_2_s_button, screen);
apply_surface(300, 220, depth_3_button, screen);
apply_surface(380, 220, depth_4_button, screen);
apply_surface(460, 220, best_depth_button, screen);
}
else if (minimax_depth == 3){
apply_surface(140, 220, depth_1_button, screen);
apply_surface(220, 220, depth_2_button, screen);
apply_surface(300, 220, depth_3_s_button, screen);
apply_surface(380, 220, depth_4_button, screen);
apply_surface(460, 220, best_depth_button, screen);
}
else if (minimax_depth == 4){
apply_surface(140, 220, depth_1_button, screen);
apply_surface(220, 220, depth_2_button, screen);
apply_surface(300, 220, depth_3_button, screen);
apply_surface(380, 220, depth_4_s_button, screen);
apply_surface(460, 220, best_depth_button, screen);
}
else{
apply_surface(140, 220, depth_1_button, screen);
apply_surface(220, 220, depth_2_button, screen);
apply_surface(300, 220, depth_3_button, screen);
apply_surface(380, 220, depth_4_button, screen);
apply_surface(460, 220, best_depth_s_button, screen);
}
//Apply the "default or loaded settings are highlighted" logo
apply_surface(200, 565, dol_logo, screen);
int x = 0, y = 0;
int running = 1;
while (running){
//While there's an event to handle
while (SDL_PollEvent(&event))
{
if (event.type == SDL_MOUSEBUTTONDOWN)
{
//Get the mouse offsets
x = event.button.x;
y = event.button.y;
//If the left mouse button was pressed
if (event.button.button == SDL_BUTTON_LEFT)
{
if (xyInRect(x, y, D1_M_button))
{
//Setting minimax depth to 1
apply_surface(140, 220, depth_1_s_button, screen);
apply_surface(220, 220, depth_2_button, screen);
apply_surface(300, 220, depth_3_button, screen);
apply_surface(380, 220, depth_4_button, screen);
apply_surface(460, 220, best_depth_button, screen);
minimax_depth = 1;
}
if (xyInRect(x, y, D2_M_button))
{
//Setting minimax depth to 2
apply_surface(140, 220, depth_1_button, screen);
apply_surface(220, 220, depth_2_s_button, screen);
apply_surface(300, 220, depth_3_button, screen);
apply_surface(380, 220, depth_4_button, screen);
apply_surface(460, 220, best_depth_button, screen);
minimax_depth = 2;
}
if (xyInRect(x, y, D3_M_button))
{
//Setting minimax depth to 3
apply_surface(140, 220, depth_1_button, screen);
apply_surface(220, 220, depth_2_button, screen);
apply_surface(300, 220, depth_3_s_button, screen);
apply_surface(380, 220, depth_4_button, screen);
apply_surface(460, 220, best_depth_button, screen);
minimax_depth = 3;
}
if (xyInRect(x, y, D4_M_button))
{
//Setting minimax depth to 4
apply_surface(140, 220, depth_1_button, screen);
apply_surface(220, 220, depth_2_button, screen);
apply_surface(300, 220, depth_3_button, screen);
apply_surface(380, 220, depth_4_s_button, screen);
apply_surface(460, 220, best_depth_button, screen);
minimax_depth = 4;
}
if (xyInRect(x, y, BD_M_button))
{
//Setting minimax depth to best
apply_surface(140, 220, depth_1_button, screen);
apply_surface(220, 220, depth_2_button, screen);
apply_surface(300, 220, depth_3_button, screen);
apply_surface(380, 220, depth_4_button, screen);
apply_surface(460, 220, best_depth_s_button, screen);
minimax_depth = 0;
}
if (xyInRect(x, y, return_M_button))
{
//Return to Players Selection Window
loadPlayersSelectionWindow();
}
}
}
//If the user has Xed out the window
if (event.type == SDL_QUIT)
{
//Quit the program
running = 0;
}
safe_SDL_Flip(screen);
}
}
freeAll();
//Quit SDL
SDL_Quit();
exit(0);
}
/*
* Loading AI Settings window
*/
void loadAISettingsWindow(){
apply_surface(0, 0, background, screen);
apply_surface(250, 0, chess_logo, screen);
//Load the relevant images
difficulty_logo = load_image("difficulty.bmp");
player_col_logo = load_image("player_color.bmp");
depth_1_button = load_image("1.bmp");
depth_1_s_button = load_image("1_s.bmp");
depth_2_button = load_image("2.bmp");
depth_2_s_button = load_image("2_s.bmp");
depth_3_button = load_image("3.bmp");
depth_3_s_button = load_image("3_s.bmp");
depth_4_button = load_image("4.bmp");
depth_4_s_button = load_image("4_s.bmp");
black_button = load_image("black.bmp");
white_button = load_image("white.bmp");
white_s_button = load_image("white_s.bmp");
black_s_button = load_image("black_s.bmp");
best_depth_button = load_image("best.bmp");
best_depth_s_button = load_image("best_s.bmp");
click_tr_button = load_image("click_tr.bmp");
//Set the window caption
SDL_WM_SetCaption("AI Settings Window", NULL);
//Check if bitmap loading have succeeded
if (difficulty_logo == NULL || player_col_logo == NULL || depth_1_button == NULL || depth_2_button == NULL || depth_3_button == NULL || depth_4_button == NULL
|| black_button == NULL || white_button == NULL || best_depth_button == NULL || depth_1_s_button == NULL || depth_2_s_button == NULL
|| depth_3_s_button == NULL || depth_4_s_button == NULL || black_s_button == NULL || white_s_button == NULL || click_tr_button == NULL || best_depth_s_button == NULL)
{
printf("ERROR: unable to load bitmap: %s\n", SDL_GetError());
freeAll();
exit(1);
}
//Apply the difficulty logo to the screen
apply_surface(0, 170, difficulty_logo, screen);
//Apply the player color logo to the screen
apply_surface(12, 240, player_col_logo, screen);
if (minimax_depth == 1){
apply_surface(250, 170, depth_1_s_button, screen);
apply_surface(330, 170, depth_2_button, screen);
apply_surface(410, 170, depth_3_button, screen);
apply_surface(490, 170, depth_4_button, screen);
apply_surface(570, 170, best_depth_button, screen);
}
else if (minimax_depth == 2){
apply_surface(250, 170, depth_1_button, screen);
apply_surface(330, 170, depth_2_s_button, screen);
apply_surface(410, 170, depth_3_button, screen);
apply_surface(490, 170, depth_4_button, screen);
apply_surface(570, 170, best_depth_button, screen);
}
else if (minimax_depth == 3){
apply_surface(250, 170, depth_1_button, screen);
apply_surface(330, 170, depth_2_button, screen);
apply_surface(410, 170, depth_3_s_button, screen);
apply_surface(490, 170, depth_4_button, screen);
apply_surface(570, 170, best_depth_button, screen);
}
else if (minimax_depth == 4){
apply_surface(250, 170, depth_1_button, screen);
apply_surface(330, 170, depth_2_button, screen);
apply_surface(410, 170, depth_3_button, screen);
apply_surface(490, 170, depth_4_s_button, screen);
apply_surface(570, 170, best_depth_button, screen);
}
else{
apply_surface(250, 170, depth_1_button, screen);
apply_surface(330, 170, depth_2_button, screen);
apply_surface(410, 170, depth_3_button, screen);
apply_surface(490, 170, depth_4_button, screen);
apply_surface(570, 170, best_depth_s_button, screen);
}
if (user_color == 'W'){
//Apply the White button to the screen highlighted
apply_surface(300, 240, white_s_button, screen);
//Apply the Black button to the screen
apply_surface(530, 240, black_button, screen);
}
else{
//Apply the White button to the screen
apply_surface(300, 240, white_button, screen);
//Apply the Black button to the screen highlighted
apply_surface(530, 240, black_s_button, screen);
}
//Apply the start the game button to the screen
apply_surface(390, 310, click_tr_button, screen);
//Apply the "default or loaded settings are highlighted" logo
apply_surface(200, 565, dol_logo, screen);
//The mouse offsets
int x = 0, y = 0;
int running = 1;
while (running){
//While there's an event to handle
while (SDL_PollEvent(&event))
{
if (event.type == SDL_MOUSEBUTTONDOWN)
{
//Get the mouse offsets
x = event.button.x;
y = event.button.y;
//If the left mouse button was pressed
if (event.button.button == SDL_BUTTON_LEFT)
{
if (xyInRect(x, y, W_AI_button))
{
//Setting user color to white
apply_surface(300, 240, white_s_button, screen);
apply_surface(530, 240, black_button, screen);
user_color = 'W';
}
if (xyInRect(x, y, B_AI_button))
{
//Setting user color to black
apply_surface(300, 240, white_button, screen);
apply_surface(530, 240, black_s_button, screen);
user_color = 'B';
}
if (xyInRect(x, y, D1_button))
{
//Setting minimax depth to 1
apply_surface(250, 170, depth_1_s_button, screen);
apply_surface(330, 170, depth_2_button, screen);
apply_surface(410, 170, depth_3_button, screen);
apply_surface(490, 170, depth_4_button, screen);
apply_surface(570, 170, best_depth_button, screen);
minimax_depth = 1;
}
if (xyInRect(x, y, D2_button))
{
//Setting minimax depth to 2
apply_surface(250, 170, depth_1_button, screen);
apply_surface(330, 170, depth_2_s_button, screen);
apply_surface(410, 170, depth_3_button, screen);
apply_surface(490, 170, depth_4_button, screen);
apply_surface(570, 170, best_depth_button, screen);
minimax_depth = 2;
}
if (xyInRect(x, y, D3_button))
{
//Setting minimax depth to 3
apply_surface(250, 170, depth_1_button, screen);
apply_surface(330, 170, depth_2_button, screen);
apply_surface(410, 170, depth_3_s_button, screen);
apply_surface(490, 170, depth_4_button, screen);
apply_surface(570, 170, best_depth_button, screen);
minimax_depth = 3;
}
if (xyInRect(x, y, D4_button))
{
//Setting minimax depth to 4
apply_surface(250, 170, depth_1_button, screen);
apply_surface(330, 170, depth_2_button, screen);
apply_surface(410, 170, depth_3_button, screen);
apply_surface(490, 170, depth_4_s_button, screen);
apply_surface(570, 170, best_depth_button, screen);
minimax_depth = 4;
}
if (xyInRect(x, y, BD_button))
{
//Setting minimax depth to best
apply_surface(250, 170, depth_1_button, screen);
apply_surface(330, 170, depth_2_button, screen);
apply_surface(410, 170, depth_3_button, screen);
apply_surface(490, 170, depth_4_button, screen);
apply_surface(570, 170, best_depth_s_button, screen);
minimax_depth = 0;
}
if (xyInRect(x, y, return_AI_button))
{
//Return to Player Selection Window
loadPlayersSelectionWindow();
}
}
}
//If the user has Xed out the window
if (event.type == SDL_QUIT)
{
//Quit the program
running = 0;
}
safe_SDL_Flip(screen);
}
}
freeAll();
//Quit SDL
SDL_Quit();
exit(0);
}
/*
* Loading Board Setting window
*/
void loadBoardSettingWindow(){
if (SDL_FillRect(screen, 0, SDL_MapRGB(screen->format,255,255,255)) != 0) {
printf("ERROR: failed to draw rect: %s\n", SDL_GetError());
freeAll();
exit(1);
}
//Load the relevant images
chessboard = load_image("chessboard.bmp");
WK = load_image("WK.bmp");
WN = load_image("WN.bmp");
WB = load_image("WB.bmp");
WQ = load_image("WQ.bmp");
WP = load_image("WP.bmp");
WR = load_image("WR.bmp");
BK = load_image("BK.bmp");
BN = load_image("BN.bmp");
BB = load_image("BB.bmp");
BQ = load_image("BQ.bmp");
BP = load_image("BP.bmp");
BR = load_image("BR.bmp");
WS = load_image("WS.bmp");
BS = load_image("BS.bmp");
wb_init_logo = load_image("wb_init.bmp");
wrong_piece_logo = load_image("wrong_piece.bmp");
set_and_ret_button = load_image("setandreturn.bmp");
clear_button = load_image("clear.bmp");
safe_SDL_SetColorKey(WP, 255, 255, 255);
safe_SDL_SetColorKey(WN, 255, 255, 255);
safe_SDL_SetColorKey(WB, 255, 255, 255);
safe_SDL_SetColorKey(WQ, 255, 255, 255);
safe_SDL_SetColorKey(WR, 255, 255, 255);
safe_SDL_SetColorKey(WK, 255, 255, 255);
safe_SDL_SetColorKey(BP, 255, 255, 255);
safe_SDL_SetColorKey(BN, 255, 255, 255);