-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuiMain.c
More file actions
168 lines (139 loc) · 4.42 KB
/
GuiMain.c
File metadata and controls
168 lines (139 loc) · 4.42 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
/*
* GuiMain.c
*
* Created on: Mar 11, 2018
* Author: tomhe
*/
#include "GuiMain.h"
//global variables from GuiGlobalArgs
ChessGame* game;
Widget *piecesPosArr[BOARD_LENGTH][BOARD_LENGTH];
Widget* pieces[4*BOARD_LENGTH];
bool isGameSaved;
int GuiMain() {
char* error;
//extern Widget* piecesPosArr[BOARD_LENGTH][BOARD_LENGTH];
// initialize SDL2 for video
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("ERROR: unable to init SDL: %s\n", SDL_GetError());
return 1;
}
// create an SDL window
SDL_Window* main_window = createWindow("Chessprog" ,800, 600);
if (main_window==NULL) {
error="Error: unable to create window. Exiting...\n";
errorMessageBox(error);
printf("%s", error);
SDL_Quit();
return 1;
}
// create a renderer for the window
SDL_Renderer* rend = createRenderer(main_window);
if (rend==NULL) {
error="Error: unable to create window. Exiting...\n";
errorMessageBox(error);
printf("%s", error);
SDL_DestroyWindow(main_window);
SDL_Quit();
return 1;
}
//creating the game
game=createChessGame();
if (game==NULL) {
error="Error: unable to create new game. Exiting...\n";
errorMessageBox(error);
printf("%s", error);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(main_window);
SDL_Quit();
return 1;
}
//used for showing messageBox "do you want to save the game...."
isGameSaved=true;
//creating piece widgets
if (!createWidgets(rend)) { //error in creating widgets of board pieces
destroyPieces();
error="Error: unable to create game pieces. Exiting...\n";
errorMessageBox(error);
printf("%s", error);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(main_window);
SDL_Quit();
return 1;
}
setDefaultPiecesPositions();
//running the game
runGuiMain(main_window);
destoryChessGame(game);
destroyPieces();
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(main_window);
SDL_Quit();
return 0;
}
void runGuiMain(SDL_Window* main_window) {
SDL_Renderer* rend=SDL_GetRenderer(main_window);
CHESS_GUI_WINDOW current_window=GUI_MAIN_WINDOW;
CHESS_GUI_WINDOW pre_window=GUI_MAIN_WINDOW; //used if back was clicked in load window, from main OR(!) game
int messageBoxResult=0;
while (1) {
if (current_window==GUI_MAIN_WINDOW) {
pre_window=current_window;
current_window=initMainWindow(rend);
}
if (current_window==GUI_SETTINGS_WINDOW) {
current_window=settingsWindow(rend);
}
if (current_window==GUI_GAME_WINDOW) {
pre_window=current_window;
current_window=GameWindow(main_window);
//show messagebox, if user wants to save the game, in case the game is not saved
if (isGameSaved==false && (current_window==GUI_MAIN_WINDOW || current_window==GUI_QUIT)) {
SDL_HideWindow(main_window);
messageBoxResult=messageBoxBeforeExitingGame();
if(messageBoxResult==1) { //"yes" was chosen
saveWindow();
}
//messageBoxResult==2 -> "cancel" was chosen
//other possibility - "yes" was chosen but back button was pressed in the save window
if (messageBoxResult==2 ||(messageBoxResult==1 && isGameSaved==false)) {
current_window=GUI_GAME_WINDOW;
SDL_ShowWindow(main_window);
continue;
}
}
if(current_window==GUI_MAIN_WINDOW){
if(!restartGuiGame(rend)){ //restart function sets isGameSaved to true;
errorMessageBox("Error: failure returning to main menu- returning to current game. You may try again.");
printf("Error: failure returning to main menu- returning to current game. You may try again.");
current_window=GUI_GAME_WINDOW;
SDL_ShowWindow(main_window);
continue;
}
changeWindowName(main_window, "Chessprog");
SDL_ShowWindow(main_window);
}
}
if (current_window==GUI_SAVE_GAME) {
SDL_HideWindow(main_window);
current_window=saveWindow();
SDL_ShowWindow(main_window);
if (isGameSaved) { //a save was made
//change the main window name, since the game was saved
changeWindowName(main_window, "Chessprog");
}
}
if (current_window==GUI_LOAD_GAME) {
SDL_HideWindow(main_window);
current_window=loadWindow(pre_window);
SDL_ShowWindow(main_window);
if (isGameSaved) { //a load was made - the game is now "saved".
//change the main window name, since the game was saved
changeWindowName(main_window, "Chessprog");
}
}
if (current_window==GUI_QUIT) {
break;
}
}
}