-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChess2D.h
957 lines (813 loc) · 28.3 KB
/
Chess2D.h
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
#ifndef _CHESS2D_H
#define _CHESS2D_H
#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/glfw3.h>
#include <cstdio>
#include <bits/stdc++.h>
#include <windows.h>
#include "image_loader.h"
#include "engine.h"
#include "shader_source.h"
#include "gl.h"
using namespace std;
#define EVENT_PROCESS_WAIT_TIME 0.03
#define AI_EASY 1
#define AI_MEDIUM 2
#define AI_HARD 3
#define AI_OPENING 10
#define AI_MIDDLE_GAME 11
#define AI_END_GAME 12
#define START_WINDOW 1
#define START_WINDOW_ONE_PLAYER_EASY 1
#define START_WINDOW_ONE_PLAYER_MEDIUM 2
#define START_WINDOW_ONE_PLAYER_HARD 3
#define START_WINDOW_TWO_PLAYER 4
#define START_WINDOW_EXIT_GAME 5
#define PLAY_WINDOW 2
#define RESUME_WINDOW 3
#define RESUME_WINDOW_RESUME 1
#define RESUME_WINDOW_UNDO 2
#define RESUME_WINDOW_BACK 3
#define RESUME_WINDOW_EXIT 4
#define CHECKMATE1_WINDOW 4
#define CHECKMATE1_WINDOW_BACK 1
#define CHECKMATE1_WINDOW_EXIT 2
#define CHECKMATE2_WINDOW 5
#define CHECKMATE2_WINDOW_BACK 1
#define CHECKMATE2_WINDOW_EXIT 2
#define STALEMATE_WINDOW 6
#define STALEMATE_WINDOW_BACK 1
#define STALEMATE_WINDOW_EXIT 2
#define PAWN1_WINDOW 7
#define PAWN1_KNIGHT 3
#define PAWN1_ROOK 4
#define PAWN1_QUEEN 1
#define PAWN1_BISHOP 2
#define PAWN2_WINDOW 8
#define PAWN2_KNIGHT 3
#define PAWN2_ROOK 4
#define PAWN2_QUEEN 1
#define PAWN2_BISHOP 2
struct RGBA
{
float r;
float g;
float b;
float a;
RGBA() {};
RGBA(float R, float G, float B) { r = R; b = B; g = G; }
};
float textures[][8] = { {0, 0, 0, 0, 0, 0, 0,0 },
{5.0/6.0,0.5, 1.0, 0.5, 1.0, 0, 5.0/6.0,0 },
{3.0/6.0,0.5, 4.0/6.0,0.5, 4.0/6.0,0, 3.0/6.0,0 },
{4.0/6.0,0.5, 5.0/6.0,0.5, 5.0/6.0,0, 4.0/6.0,0 },
{2.0/6.0,0.5, 3.0/6.0,0.5, 3.0/6.0,0, 2.0/6.0,0 },
{1.0/6.0,0.5, 2.0/6.0,0.5, 2.0/6.0,0, 1.0/6.0,0 },
{0.0/6.0,0.5, 1.0/6.0,0.5, 1.0/6.0,0, 0,0 },
{5.0/6.0,1.0, 1.0,1.0, 1.0,0.5, 5.0/6.0,0.5 },
{3.0/6.0,1.0, 4.0/6.0,1.0, 4.0/6.0,0.5,3.0/6.0,0.5 },
{4.0/6.0,1.0, 5.0/6.0,1.0, 5.0/6.0,0.5,4.0/6.0,0.5 },
{2.0/6.0,1.0, 3.0/6.0,1.0, 3.0/6.0,0.5,2.0/6.0,0.5 },
{1.0/6.0,1.0, 2.0/6.0,1.0, 2.0/6.0,0.5,1.0/6.0,0.5 },
{0.0/6.0,1.0, 1.0/6.0,1.0, 1.0/6.0,0.5,0,0.5 } };
RGBA white(0.8,0.8,0.8);
RGBA black(0.4, 0.4, 0.4);
RGBA selected_color(0.1, 0.8, 0.1);
RGBA highlighted_color(0.3, 1.0, 0.3);
class Chess2D
{
private:
static float board_left;
static float board_top;
static float board_width;
static float board_height;
static int window_width;
static int window_height;
GLuint vao;
GLuint vbo;
GLuint eao;
Shader *board_shader;
GLFWwindow *window;
//static GLfloat *vertices;
vector<float> vertices;
unsigned int num_vertices;
vector<int> elements;
unsigned int num_elements;
static Engine *engine;
void LoadBoardColor();
void LoadPieceTextures();
void DrawBoard();
static void ProcessMouseInput(GLFWwindow*,int,int,int);
static void ProcessKeyInput(GLFWwindow*,int,int,int,int);
static bool selected;
static int prev_x;
static int prev_y;
static moves pawn_promo_move;
static int current_player;
static bool single_player_mode;
static int display_status;
string debug_file;
static vector<ImageMenu*> menu;
int MainLoop(GLFWwindow*);
public:
Chess2D(int width = 800 ,int height = 600,int boardwidth = 600,int boardheight = 600);
int StartGame(string fdebug = "");
~Chess2D();
};
int Chess2D::window_width = 0;
int Chess2D::window_height = 0;
float Chess2D::board_width = 0;
float Chess2D::board_height = 0;
float Chess2D::board_left = 0;
float Chess2D::board_top = 0;
Engine* Chess2D::engine = NULL;
bool Chess2D::selected = false;
int Chess2D::prev_x = 0;
int Chess2D::prev_y = 0;
int Chess2D::current_player = 1;
bool Chess2D::single_player_mode = false;
int Chess2D::display_status = 0;
vector<ImageMenu*> Chess2D::menu;
moves Chess2D::pawn_promo_move = moves(-1,-1,-1,-1);
//Load vertex shader, Fragment shader, set glfw window hints, initialize data
Chess2D::Chess2D(int width, int height, int boardwidth, int boardheight)
{
window_width = width;
window_height = height;
board_width = (float)boardwidth*2/window_width;
board_height = (float)boardheight*2/window_height;
board_left = -board_width/2;
board_top = board_height/2;
//Initialize and load data. Each square has 4 vertices
//Position: 2 floats
//Colour: 3 floats
//Texture: 2 floats
vertices.resize(4*64*7);
for(int i=0;i<4*64*7;i++)
vertices[i] = 0.0f;
int index = 0;
for(int j=7;j>=0;j--)
for(int i=0;i<8;i++)
{
float x = i, y=j;
vertices[index] = board_left + board_width*x/8;
vertices[index+1 ] = board_top - board_height*y/8;
x++; index+=7;
vertices[index] = board_left + board_width*x/8;
vertices[index+1 ] = board_top - board_height*y/8;
y++;index+=7;
vertices[index] = board_left + board_width*x/8;
vertices[index+1 ] = board_top - board_height*y/8;
x--; index+=7;
vertices[index] = board_left + board_width*x/8;
vertices[index+1 ] = board_top - board_height*y/8;
index+=7;
}
num_vertices = 4*8*8*7;
elements.resize(6*64);
index = 0;
GLuint index2 = 0;
for(int i=0;i<64;i++)
{
elements[index2] = index;
elements[index2+1] = index+1;
elements[index2+2] = index+2;
elements[index2+3] = index;
elements[index2+4] = index+2;
elements[index2+5] = index+3;
index+=4;
index2+=6;
}
num_elements = 8*8*6;
//Load engine for chess
engine = new Engine(true);
LoadBoardColor();
LoadPieceTextures();
if(!glfwInit())
{
fputs("Unable to load GLFW\n", stderr);
return;
}
//Set appropriate window hints to
//load OpenGL
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
const GLFWvidmode *mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
//Create window
//And set appropriate window settings
window = glfwCreateWindow(window_width, window_height, "Chess 2D", NULL, NULL);
if(!window)
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE);
glfwWindowHint(GLFW_VISIBLE, false);
window = glfwCreateWindow(window_width, window_height, "Chess 2D", NULL, NULL);
if(!window)
{
fputs("Unable to create window\n", stderr);
return;
}
}
//Set callback functions for
//Mouse and keyboard events
glfwSetMouseButtonCallback(window, ProcessMouseInput);
glfwSetKeyCallback(window, ProcessKeyInput);
glfwMakeContextCurrent(window);
//Initialize glew to
//Link to OpenGL functions
glewExperimental = GL_TRUE;
if(glewInit()!=GLEW_OK)
{
fputs("Unable to load GLEW\n", stderr);
return;
}
//Load Vertex Array Object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//Load Vertex Buffer Objects
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, num_vertices*sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
//Load Vertex Element Objects
glGenBuffers(1, &eao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eao);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_elements*sizeof(GLuint), &elements[0], GL_STATIC_DRAW);
//Load Shaders
board_shader = new Shader(vertex_shader_source, fragment_shader_source);
board_shader->Load();
//Load Textures
GLuint tex;
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
//Load paramter for the texture.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Load the texture file
int tex_width, tex_height;
unsigned char* pixels = loadBMP("images/chess.bmp", &tex_width, &tex_height);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
unload_BMP(pixels);
//Set position attribute
GLint posAttrib = glGetAttribLocation(board_shader->id, "position");
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, (7*sizeof(GLfloat)), 0); //Now stores in the VAO vao
glEnableVertexAttribArray(posAttrib);
//Set color attributes for the board
GLint colAttrib = glGetAttribLocation(board_shader->id, "color");
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
glEnableVertexAttribArray(colAttrib);
glfwSwapInterval(1);
//Set texture attributes for the board
GLuint texAttrib = glGetAttribLocation(board_shader->id, "texcoord");
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 7*sizeof(GLfloat), (void*)(5*sizeof(GLfloat)));
glEnableVertexAttribArray(texAttrib);
//Enable alpha ( transparency )
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//Deallocate memory used up by the image file
unload_BMP(pixels);
//Load start menu
{
Rect<int> r{0, 0, 800, 600};
Rect<int> op1{160, 190, 530, 47};
Rect<int> op2{87, 264, 633, 49};
Rect<int> op3{136, 347, 556, 50};
Rect<int> op4{236, 432, 326, 52};
Rect<int> op5{330, 520, 109, 53};
vector<Rect<int> > options;
options.push_back(op1);
options.push_back(op2);
options.push_back(op3);
options.push_back(op4);
options.push_back(op5);
menu.push_back(new ImageMenu("images/start_menu.bmp", 800, 600, r, options));
}
//Load resume menu
{
Rect<int> r{0, 0, 800, 600};
Rect<int> op1{166, 84, 524, 61};
Rect<int> op2{300, 180, 198, 62};
Rect<int> op3{57, 280, 708, 65};
Rect<int> op4{330, 374, 146, 68};
vector<Rect<int> > options;
options.push_back(op1);
options.push_back(op2);
options.push_back(op3);
options.push_back(op4);
menu.push_back(new ImageMenu("images/resume_menu.bmp", 800, 600, r, options));
}
//Load checkmate menu where player 1 wins
{
Rect<int> r{0, 0, 800, 600};
Rect<int> op1{101, 296, 632, 55};
Rect<int> op2{328, 389, 124, 57};
vector<Rect<int> > options;
options.push_back(op1);
options.push_back(op2);
menu.push_back(new ImageMenu("images/checkmate1_menu.bmp", 800, 600, r, options));
}
//Load checkmate menu where player 2 wins
{
Rect<int> r{0, 0, 800, 600};
Rect<int> op1{101, 296, 632, 55};
Rect<int> op2{328, 389, 124, 57};
vector<Rect<int> > options;
options.push_back(op1);
options.push_back(op2);
menu.push_back(new ImageMenu("images/checkmate2_menu.bmp", 800, 600, r, options));
}
//Load stalemate menu
{
Rect<int> r{0, 0, 800, 600};
Rect<int> op1{110, 296, 632, 55};
Rect<int> op2{347, 389, 124, 57};
vector<Rect<int> > options;
options.push_back(op1);
options.push_back(op2);
menu.push_back(new ImageMenu("images/stalemate_menu.bmp", 800, 600, r, options));
}
//Load pawn promotion 1 menu
{
Rect<int> r{0, 0, 800, 600};
Rect<int> op1{32, 296, 145, 141};
Rect<int> op2{261,299, 79, 141};
Rect<int> op3{424, 285, 161,164};
Rect<int> op4{619,287,157,167};
vector<Rect<int> > options;
options.push_back(op1);
options.push_back(op2);
options.push_back(op3);
options.push_back(op4);
menu.push_back(new ImageMenu("images/pawn1_menu.bmp", 800, 600, r, options));
}
//Load pawn promotion 2 menu
{
Rect<int> r{0, 0, 800, 600};
Rect<int> op1{32, 296, 145, 141};
Rect<int> op2{261,299, 79, 141};
Rect<int> op3{424, 285, 161,164};
Rect<int> op4{619,287,157,167};
vector<Rect<int> > options;
options.push_back(op1);
options.push_back(op2);
options.push_back(op3);
options.push_back(op4);
menu.push_back(new ImageMenu("images/pawn2_menu.bmp", 800, 600, r, options));
}
}
int Chess2D::StartGame(string fdebug)
{
if(fdebug.size()==0)
{
display_status = START_WINDOW;
}
else
{
delete engine;
engine = new Engine(fdebug, true, AI_EASY);
display_status = PLAY_WINDOW;
}
//Show window and start game
glfwShowWindow(window);
current_player = engine->GetCurrentPlayer();
return MainLoop(window);
}
int Chess2D::MainLoop(GLFWwindow *window)
{
//prev_event_time with glfwGetTime()
//is used to create delay between
//consecutive page refreshes
double prev_event_time = glfwGetTime();
//Text object which writes text on window
Text text;
while(!glfwWindowShouldClose(window))
{
current_player = engine->GetCurrentPlayer();
//Clear color buffer bit i.e. previous drawings
glClear(GL_COLOR_BUFFER_BIT);
//Draw the correct window
switch(display_status)
{
case PLAY_WINDOW:
{
DrawBoard();
int game_status = engine->GetGameStatus();
//Get status of the game
//Game is stopped if there's either a checkmate or stalemate
switch(game_status)
{
case GAME_CHECK:
text.Draw(-0.2,0.5, 0.25, "Check!");
break;
case GAME_CHECKMATE:
if(engine->GetCurrentPlayer()==1)
{
display_status = CHECKMATE2_WINDOW;
}
else
{
display_status = CHECKMATE1_WINDOW;
}
break;
case GAME_STALEMATE:
display_status = STALEMATE_WINDOW;
break;
}
}
break;
//Draw the appropriate menu
case START_WINDOW:
menu[0]->DrawMenu();
break;
case RESUME_WINDOW:
DrawBoard();
menu[1]->DrawMenu(true);
break;
case CHECKMATE1_WINDOW:
DrawBoard();
menu[2]->DrawMenu(true);
break;
case CHECKMATE2_WINDOW:
DrawBoard();
menu[3]->DrawMenu(true);
break;
case STALEMATE_WINDOW:
DrawBoard();
menu[4]->DrawMenu(true);
break;
case PAWN1_WINDOW:
DrawBoard();
menu[5]->DrawMenu(true);
break;
case PAWN2_WINDOW:
DrawBoard();
menu[6]->DrawMenu(true);
break;
}
//Swap buffers to display final window
glfwSwapBuffers(window);
//Create delay between consecutive refreshes
while(glfwGetTime()-prev_event_time<EVENT_PROCESS_WAIT_TIME)
Sleep(1);
prev_event_time = glfwGetTime();
glfwPollEvents();
}
return 0;
}
void Chess2D::DrawBoard()
{
//Load texture for board
//Basically the different pieces on the board
LoadPieceTextures();
//Load color for the board
//This includes colors forselected pieces
LoadBoardColor();
//Load data to OpenGL Buffers
glBufferSubData(GL_ARRAY_BUFFER, 0, num_vertices * sizeof(GLfloat), &vertices[0]);
//Finally draw the board
glDrawElements(GL_TRIANGLES, 8*8*6, GL_UNSIGNED_INT, 0);
}
Chess2D::~Chess2D()
{
//Call destructor for shader
delete board_shader;
//Delete buffers allocated for board
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &eao);
glDeleteVertexArrays(1, &vao);
}
void Chess2D::LoadBoardColor()
{
//default prev_ai_move is -2,-2,-2,-2.
//Anything that is not legal
static moves prev_ai_move = moves(-2,-2,-2,-2);
moves ai_move;
int index = 0;
//Load selections for ai player's move
//if single_player_mode is loaded
if(single_player_mode)
ai_move = engine->GetAIMove();
//Load board colors
for(int j=0;j<8;j++)
for(int i = 0;i<8;i++)
{
RGBA board_color;
if(selected)
{
bool possible_move = engine->IsValidMove(prev_x, prev_y, i, j);
if(i==prev_x && j==prev_y)
board_color = selected_color;
else if(possible_move)
board_color = highlighted_color;
else
if((i+j)%2 == 0)
board_color = white;
else
board_color = black;
//Discard AI move highlight
prev_ai_move = ai_move;
}
else
{
if((i+j)%2 == 0)
board_color = white;
else
board_color = black;
//Also highlight moves made by computer in single player mode
if(ai_move.x0 != prev_ai_move.x0 || ai_move.y0!= prev_ai_move.y0 || ai_move.x1 != prev_ai_move.x1 || ai_move.y1 !=prev_ai_move.y1)
if(single_player_mode)
{
if(i==ai_move.x0 && j==ai_move.y0)
board_color = highlighted_color;
else if(i==ai_move.x1 && j==ai_move.y1)
board_color = selected_color;
}
}
for(int k=0;k<4;k++)
{
vertices[index+2] = board_color.r;
vertices[index+3] = board_color.g;
vertices[index+4] = board_color.b;
index+=7;
}
}
}
void Chess2D::LoadPieceTextures()
{
//Load textures for board
//These textures represent
//the different pieces
int index = 0;
for(int j=0;j<8;j++)
for(int i=0;i<8;i++)
{
int piece = engine->GetPiece(i,j);
//Temporary
if(piece<0)
piece = 6 - piece;
for(int k=0;k<4;k++)
{
vertices[index+5]=textures[piece][2*k];
vertices[index+6]=textures[piece][2*k+1];
index+=7;
}
}
}
void Chess2D::ProcessMouseInput(GLFWwindow *window, int button,int action,int mods)
{
//Not take inputs when computer is thinking
if(action ==GLFW_PRESS && engine->GetGameStatus()!=GAME_THINKING)
{
double x,y;
//Get cursor position
glfwGetCursorPos(window, &x, &y);
//Process the input
//correctly
//based on the window being displayed
switch(display_status)
{
case PLAY_WINDOW:
x/=window_width;
y/=window_height;
x = 2*x - 1;
y = 1-2*y;
int square_x, square_y;
square_x = (x-board_left)*8/board_width;
square_y = (board_top - y)*8/board_height;
//Convert to coordinates used by engine
square_y = 7 - square_y;
if((x>=board_left && x<=board_left+board_width && y<=board_top && y>=board_top - board_height))
{
if(!selected)
{
if((current_player==1 && engine->GetPiece(square_x,square_y)>0) || (current_player==2 && engine->GetPiece(square_x, square_y)<0))
{
selected = true;
prev_x = square_x;
prev_y = square_y;
}
}
else
{
if(prev_x == square_x && prev_y == square_y)
selected = false;
else if((current_player==1 && engine->GetPiece(square_x,square_y)>0) || (current_player==2 && engine->GetPiece(square_x, square_y)<0))
{
selected = true;
prev_x = square_x;
prev_y = square_y;
}
else if(engine->IsValidMove(prev_x, prev_y, square_x, square_y))
{
selected = false;
if(engine->IsPawnPromotion(prev_x, prev_y, square_x, square_y))
{
pawn_promo_move = moves(prev_x, prev_y, square_x, square_y);
if(current_player == 1)
display_status = PAWN1_WINDOW;
else if(current_player == 2)
display_status = PAWN2_WINDOW;
break;
}
engine->ProcessInput(prev_x, prev_y, square_x, square_y);
}
}
}
break;
case START_WINDOW:
{
int result = menu[0]->ProcessInput(2*x/window_width-1.0f, 1.0f-2*y/window_height);
if(result==START_WINDOW_ONE_PLAYER_EASY)
{
single_player_mode = true;
if(engine)
delete engine;
engine = new Engine(true, AI_EASY);
display_status = PLAY_WINDOW;
}
else if(result==START_WINDOW_ONE_PLAYER_MEDIUM)
{
single_player_mode = true;
if(engine)
delete engine;
engine = new Engine(true, AI_MEDIUM);
display_status = PLAY_WINDOW;
}
else if(result==START_WINDOW_ONE_PLAYER_HARD)
{
single_player_mode = true;
if(engine)
delete engine;
engine = new Engine(true, AI_HARD);
display_status = PLAY_WINDOW;
}
else if(result == START_WINDOW_TWO_PLAYER)
{
single_player_mode = false;
if(engine)
delete engine;
engine = new Engine(false);
display_status = PLAY_WINDOW;
}
else if(result == START_WINDOW_EXIT_GAME)
{
glfwSetWindowShouldClose(window, true);
}
break;
}
case RESUME_WINDOW:
{
int result = menu[1]->ProcessInput(2*x/window_width-1.0f, 1.0f-2*y/window_height);
if(result == RESUME_WINDOW_RESUME)
{
display_status = PLAY_WINDOW;
}
else if(result == RESUME_WINDOW_UNDO)
{
bool modified = engine->UndoGame();
if(modified)
selected=false;
//display_status = PLAY_WINDOW;
}
else if(result == RESUME_WINDOW_BACK)
{
display_status = START_WINDOW;
selected = false;
}
else if(result == RESUME_WINDOW_EXIT)
{
glfwSetWindowShouldClose(window, true);
}
break;
}
case CHECKMATE1_WINDOW:
{
int result = menu[2]->ProcessInput(2*x/window_width-1.0f, 1.0f-2*y/window_height);
if(result==CHECKMATE1_WINDOW_BACK)
{
display_status = START_WINDOW;
selected = false;
}
else if(result==CHECKMATE1_WINDOW_EXIT)
{
glfwSetWindowShouldClose(window, true);
}
break;
}
case CHECKMATE2_WINDOW:
{
int result = menu[3]->ProcessInput(2*x/window_width-1.0f, 1.0f-2*y/window_height);
if(result==CHECKMATE2_WINDOW_BACK)
{
display_status = START_WINDOW;
selected = false;
}
else if(result==CHECKMATE2_WINDOW_EXIT)
{
glfwSetWindowShouldClose(window, true);
}
break;
}
case STALEMATE_WINDOW:
{
int result = menu[4]->ProcessInput(2*x/window_width-1.0f, 1.0f-2*y/window_height);
if(result==STALEMATE_WINDOW_BACK)
{
display_status = START_WINDOW;
selected = false;
}
else if(result==STALEMATE_WINDOW_EXIT)
{
glfwSetWindowShouldClose(window, true);
}
break;
}
case PAWN1_WINDOW:
{
int pawn_promo_code = 0;
int result = menu[5]->ProcessInput(2*x/window_width-1.0f, 1.0f-2*y/window_height);
switch(result)
{
case PAWN1_QUEEN:
pawn_promo_code = 0;
break;
case PAWN1_BISHOP:
pawn_promo_code = 1;
break;
case PAWN1_KNIGHT:
pawn_promo_code = 2;
break;
case PAWN1_ROOK:
pawn_promo_code = 3;
break;
}
if(result!=0)
{
engine->ProcessInput(pawn_promo_move.x0, pawn_promo_move.y0, pawn_promo_move.x1, pawn_promo_move.y1, pawn_promo_code);
display_status = PLAY_WINDOW;
}
break;
}
case PAWN2_WINDOW:
{
int pawn_promo_code = 0;
int result = menu[6]->ProcessInput(2*x/window_width-1.0f, 1.0f-2*y/window_height);
switch(result)
{
case PAWN2_QUEEN:
pawn_promo_code = 0;
break;
case PAWN2_BISHOP:
pawn_promo_code = 1;
break;
case PAWN2_KNIGHT:
pawn_promo_code = 2;
break;
case PAWN2_ROOK:
pawn_promo_code = 3;
break;
}
if(result!=0)
{
engine->ProcessInput(pawn_promo_move.x0, pawn_promo_move.y0, pawn_promo_move.x1, pawn_promo_move.y1, pawn_promo_code);
display_status = PLAY_WINDOW;
}
break;
}
}
}
}
void Chess2D::ProcessKeyInput(GLFWwindow* window, int key, int scancode, int action, int mods)
{
//Ctrl-z is keyboard shortcut for undo
if(key==GLFW_KEY_Z && mods==GLFW_MOD_CONTROL && action==GLFW_PRESS)
{
if(display_status == PLAY_WINDOW || display_status == RESUME_WINDOW)
{
bool modified = engine->UndoGame();
if(modified)
selected=false;
}
}
else if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
if(display_status == PLAY_WINDOW)
{
display_status = RESUME_WINDOW;
}
else if(display_status == RESUME_WINDOW)
{
display_status = PLAY_WINDOW;
}
}
}
#endif // _CHESS2D_H