-
Notifications
You must be signed in to change notification settings - Fork 0
/
boardGfx.c
253 lines (230 loc) · 6.32 KB
/
boardGfx.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
/*
* boardGfx.c
*/
#include "boardGfx.h"
mouseStruct mousePos = {0,0};
BUTTON resetButtonInt = {25,748,100,40, 0, "Reset", resetBoard};
BUTTON computerButton = {150,748,125,40, 0, "Computer", initiateComputer};
/* respondToWin(): Displays who won after someone won.
*
*
* Revision History:
* 6/22/2015 (JV): Initial version
*/
void respondToWin(){
rgbStruct rgbColor;
if(winner == 2){
getColorRgb(COLOR_WHITE,&rgbColor);
glColor3f(rgbColor.r, rgbColor.g, rgbColor.b);
Font(GLUT_BITMAP_TIMES_ROMAN_24,"BLACK WINS!",400,30);
}
if(winner == 1){
getColorRgb(COLOR_WHITE,&rgbColor);
glColor3f(rgbColor.r, rgbColor.g, rgbColor.b);
Font(GLUT_BITMAP_TIMES_ROMAN_24,"RED WINS!",400,30);
}
}
/* clickedOnButton: checks to weather the click in in range
* of button.
*
* Revision History:
* 6/16/2015 (JV): Initial Version.
*
*/
int clickedOnButton(BUTTON *button, int x, int y){
if(button)
{
if( x > button->x && x < button->x+button->w){
if(y > button->y && y < button->y+button->h) {
return 1;
}
}
}
return 0;
}
/* pressedButton: checks to weather the button is pressed down.
*
* Revision History:
* 6/16/2015 (JV): Initial Version.
*
*/
void pressedButton(BUTTON *button, int x, int y ){
if (button) {
if (clickedOnButton(button,x,y)) {
button->state = 1;
}
}
}
/* releaseButton: checks to weather the button has been
* released.
*
* Revision History:
* 6/16/2015 (JV): Initial Version.
*
*/
void releasedButton(BUTTON *button, int x, int y ){
if (button) {
if (clickedOnButton(button,mousePos.xCordPress, mousePos.yCordPress) && clickedOnButton(button,x,y)) {
if (button->callBackFunction) {
button->state = 0;
player = 1;
totalMoves = 0;
button->callBackFunction();
}
}
}
}
/*
* yellowBoard:
*
* Revision History:
* 6/11/2015 (JV): Initial Version
* 6/12/2015 (JB): moved the body into the drawRectangle function in gfxLib.c
*/
void yellowBoard(){
drawRectangle(BOARD_VERTEX1_X,BOARD_VERTEX1_Y,BOARD_VERTEX2_X,BOARD_VERTEX2_Y,COLOR_YELLOW);
drawRectangleLine(BOARD_VERTEX1_X,BOARD_VERTEX1_Y,BOARD_VERTEX2_X,BOARD_VERTEX2_Y,COLOR_BLACK);
}
/*
* placeCircle:
* This will take the cordinated of the board. (0,0) being
* the Bottom most, left most position and (6,5) being the
* top most, right most position.
*
* Revision History:
* 6/11/2015 (JV): Initial Version
* 6/12/2015 (JB): removed magic numbers
*/
void placeCircle(int x, int y, colorEnum curColor){
int xPosInt, yPosInt;
rgbStruct rgbColor;
if (x >= NUM_COLS || x < 0 || y >= NUM_COLS || y < 0) {
fprintf(stderr,"Invalid position of chip (%d,%d)\n",x,y);
exit(-1);
}
xPosInt = X_START + x*X_SPACE;
yPosInt = Y_START - y*Y_SPACE;
drawCircle(xPosInt,yPosInt,40,curColor);
if (player == 2 && !winner) {
getColorRgb(COLOR_WHITE,&rgbColor);
glColor3f(rgbColor.r, rgbColor.g, rgbColor.b);
Font(GLUT_BITMAP_TIMES_ROMAN_24,"Red's Turn",400,30);
}
if (player == 1 && !winner) {
getColorRgb(COLOR_WHITE,&rgbColor);
glColor3f(rgbColor.r, rgbColor.g, rgbColor.b);
Font(GLUT_BITMAP_TIMES_ROMAN_24,"Black's Turn",400,30);
}
drawRing(xPosInt,yPosInt,CIRCLE_RADIUS,COLOR_BLACK);
}
/*
* mouseButton: determines the location and when button was clicked o
* the mouse
*
* Revision History:
* 6/13/2015 (JV): Initial version
*/
void mouseButton(int button,int state,int x, int y){
if (state == GLUT_DOWN)
{
if (button == GLUT_LEFT_BUTTON) {
pressedButton(&resetButtonInt,x,y);
pressedButton(&computerButton,x,y);
mousePos.xCordPress = x;
mousePos.yCordPress = y;
//printf("button pressed at (%d,%d)\n",x,y);
}
}
else
{
if (button == GLUT_LEFT_BUTTON) {
releasedButton(&resetButtonInt,x,y);
releasedButton(&computerButton,x,y);
mouseResponder(x, y, winner);
mousePos.xCordPress = 0;
mousePos.yCordPress = 0;
//printf("button released at (%d,%d)\n",x,y);
}
}
glutPostRedisplay();
}
/*
* drawCircles:
* draws all circles based on current board state.
*
* Revision History:
* 6/13/2015 (JB): Initial Version
*/
void drawCircles(){
int x,y;
colorEnum curColor;
chipColorEnum chipColor;
for(y=NUM_ROWS-1;y>=0;y--){
for(x=0;x<NUM_COLS;x++){
chipColor = getChipAt(&boardState,x,y);
switch(chipColor){
case RED_CHIP:
curColor = COLOR_RED;
break;
case BLACK_CHIP:
curColor = COLOR_BLACK;
break;
case BLANK_CHIP:
curColor = COLOR_WHITE;
break;
}
placeCircle(x,y,curColor);
}
}
}
/*
* display:
*
* Revision History:
* 6/11/2015 (JV): Initial version
* 6/12/2015 (JV): added an example to add chips.
*/
void display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,WINDOW_WIDTH,WINDOW_HEIGHT,0,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
yellowBoard();
//Adds all the circles to the board.
drawCircles();
drawButton(&resetButtonInt, COLOR_WHITE);
drawButton(&computerButton, COLOR_WHITE);
respondToWin();
glutSwapBuffers();
}
/*
* startGfx:
*
* Revision History:
* 6/13/2015 (JB): Inital Version - moved from connectFour.c
*/
void startGfx(void){
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition (100, 25);
glutCreateWindow ("Connect Four");
//Changes the window background to blue.
glClearColor(BLUE_R,BLUE_G,BLUE_B,WINDOW_ALPHA);
//Keeps the Display open and running.
glutDisplayFunc(display);
glutMouseFunc(mouseButton);
glutMainLoop();
destroyBoard();
}
/*
* computerPlayer:If the user decided to play agains a
* computer returns 1.
*
* Revision Hisotry:
* 6/13/2015 (JB): Initial Version
*/
int computerPlayer(){
return 1;
}