-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuiMainAux.c
197 lines (167 loc) · 6.38 KB
/
GuiMainAux.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
/*
* GuiMainAux.c
*
* Created on: Mar 11, 2018
* Author: tomhe
*/
#include "GuiMainAux.h"
//extern ChessGame *game;
//extern Widget piecesPosArr[BOARD_LENGTH][BOARD_LENGTH];
SDL_Window* createWindow(const char* window_name, int width, int height) {
SDL_Window* window = SDL_CreateWindow(
//"Chessprog",
window_name,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
width,
height,
SDL_WINDOW_SHOWN);
//make sure window was created successfully - in the calling function
return window;
}
void changeWindowName(SDL_Window* window, const char* new_window_name) {
SDL_SetWindowTitle(window, new_window_name);
}
SDL_Renderer* createRenderer(SDL_Window* window) {
SDL_Renderer* rend = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if (rend!=NULL) { // ensure renderer supports transparency
SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND);
}
//make sure renderer was created successfully - in the calling function
return rend;
}
int messageBoxBeforeExitingGame() {
const SDL_MessageBoxButtonData buttons[] = {
{ /* .flags, .buttonid, .text */ 0, 0, "no" },
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "yes" },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "cancel" },
};
const SDL_MessageBoxColorScheme colorScheme = {
{ /* .colors (.r, .g, .b) */
/* [SDL_MESSAGEBOX_COLOR_BACKGROUND] */
{120, 91, 58},
/* [SDL_MESSAGEBOX_COLOR_TEXT] */
{255, 255, 255},
/* [SDL_MESSAGEBOX_COLOR_BUTTON_BORDER] */
{255, 255, 255},
/* [SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND] */
{61, 39, 9},
/* [SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED] */
{34, 177, 76}
}
};
const SDL_MessageBoxData messageboxdata = {
SDL_MESSAGEBOX_INFORMATION, /* .flags */
NULL, /* .window */
"Chessprog - Save The Game?", /* .title */
"Do you want to save the current Chess game?", /* .message */
SDL_arraysize(buttons), /* .numbuttons */
buttons, /* .buttons */
&colorScheme /* .colorScheme */
};
int buttonid;
if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0) {
SDL_Log("ERROR: error displaying message box");
return -1;
}
return buttonid;
}
void errorMessageBox(char* error) {
const SDL_MessageBoxButtonData buttons[] = {
{ /* .flags, .buttonid, .text */ 0, 0, "OK" }, };
const SDL_MessageBoxColorScheme colorScheme = {
{ /* .colors (.r, .g, .b) */
/* [SDL_MESSAGEBOX_COLOR_BACKGROUND] */
{ 255, 0, 0 },
/* [SDL_MESSAGEBOX_COLOR_TEXT] */
{ 0, 255, 0 },
/* [SDL_MESSAGEBOX_COLOR_BUTTON_BORDER] */
{ 255, 255, 0 },
/* [SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND] */
{ 0, 0, 255 },
/* [SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED] */
{ 255, 0, 255 }
}
};
const SDL_MessageBoxData messageboxdata = {
SDL_MESSAGEBOX_INFORMATION, /* .flags */
NULL, /* .window */
"Chessprog - Error!", /* .title */
error, /* .message */
SDL_arraysize(buttons), /* .numbuttons */
buttons, /* .buttons */
&colorScheme /* .colorScheme */
};
int buttonid;
if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0) {
SDL_Log("ERROR: error displaying message box");
}
}
int createWidgets(SDL_Renderer* rend){
char * const w_pics[]={"chessPictures/w rook.bmp", "chessPictures/w knight.bmp", "chessPictures/w bishop.bmp",
"chessPictures/w queen.bmp", "chessPictures/w king.bmp", "chessPictures/w bishop.bmp",
"chessPictures/w knight.bmp", "chessPictures/w rook.bmp"};
char * const b_pics[]={"chessPictures/b rook.bmp", "chessPictures/b knight.bmp", "chessPictures/b bishop.bmp",
"chessPictures/b queen.bmp", "chessPictures/b king.bmp", "chessPictures/b bishop.bmp",
"chessPictures/b knight.bmp", "chessPictures/b rook.bmp"};
SDL_Rect rects[4][8];
for(int i=0;i<8;i++){
rects[0][i] = (SDL_Rect){ .x = BOARD_X_UP+ BOARD_SQUARE_WIDTH*i ,
.y = BOARD_Y_UP+BOARD_SQUARE_WIDTH*7, .w = BOARD_SQUARE_WIDTH, .h = BOARD_SQUARE_WIDTH };
pieces[i]=createPieceWidget(rend, w_pics[i], rects[0][i], NULL,
game->gameBoard[0][i],'1',i+'A', true, 148, 227, 254);
if (pieces[i]==NULL) {
return 0; //meaning there is an error in creating the pieces
}
}
for(int i=0;i<8;i++){
rects[3][i] = (SDL_Rect){ .x = BOARD_X_UP+ BOARD_SQUARE_WIDTH*i ,
.y = BOARD_Y_UP+BOARD_SQUARE_WIDTH*0, .w = BOARD_SQUARE_WIDTH, .h = BOARD_SQUARE_WIDTH };
pieces[3*8+i]=createPieceWidget(rend, b_pics[i], rects[3][i], NULL,
game->gameBoard[7][i],'8',i+'A', true, 148, 227, 254);
if (pieces[3*8+i]==NULL) {
return 0; //meaning there is an error in creating the pieces
}
}
for(int i=0;i<8;i++){
rects[1][i] = (SDL_Rect){ .x = BOARD_X_UP+ BOARD_SQUARE_WIDTH*i ,
.y = BOARD_Y_UP+BOARD_SQUARE_WIDTH*6, .w = BOARD_SQUARE_WIDTH, .h = BOARD_SQUARE_WIDTH };
pieces[1*8+i]=createPieceWidget(rend, "chessPictures/w pawn.bmp", rects[1][i], NULL,
game->gameBoard[1][i],'2',i+'A', true, 148, 227, 254);
if (pieces[1*8+i]==NULL) {
return 0; //meaning there is an error in creating the pieces
}
}
for(int i=0;i<8;i++){
rects[2][i] = (SDL_Rect){ .x = BOARD_X_UP+ BOARD_SQUARE_WIDTH*i ,
.y = BOARD_Y_UP+BOARD_SQUARE_WIDTH*1, .w = BOARD_SQUARE_WIDTH, .h = BOARD_SQUARE_WIDTH };
pieces[2*8+i]=createPieceWidget(rend, "chessPictures/b pawn.bmp", rects[2][i], NULL,
game->gameBoard[6][i],'7',i+'A', true, 148, 227, 254);
if (pieces[2*8+i]==NULL) {
return 0; //meaning there is an error in creating the pieces
}
}
return 1; //creation is a success!!
}
void destroyPieces(){
for(int i=0; i<4;i++){
for(int j=0; j<8; j++){
destroyWidget(pieces[i*8+j]); //from Widget C file, checks if a widget pointer is not null
}
}
}
void setDefaultPiecesPositions(){
PieceWidget *curr_piece=NULL;
//printf("ok\n");
for(int i=0; i<BOARD_LENGTH; i++){
for(int j=0; j<BOARD_LENGTH; j++){
piecesPosArr[i][j]=NULL;
}
}
for(int i=0; i<4;i++){
for(int j=0; j<8; j++){
curr_piece=pieces[i*8+j]->data;
piecesPosArr[curr_piece->r_index-'1'][curr_piece->c_index-'A']=pieces[i*8+j];
}
}
}