-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
executable file
·203 lines (186 loc) · 7.55 KB
/
main.cpp
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
#include <csignal>
#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <SDL_gfxPrimitives.h>
#include "headers/menu.h"
#include "headers/image.h"
#include "headers/gamegui.h"
int main(int argc, char *argv[])
{
int width = 800;
int height = 602;
bool gameRunning = true;
SDL_Event event; // dump event polls into this
// Initialize SDL, NOPARACHUTE means SDL will handle fatal interrupts which
// we use to exit on Ctrl+C.
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
// Initialize SDL_ttf
if (TTF_Init() < 0)
{
fprintf(stderr, "TTF_Init: %s\n", TTF_GetError());
return 1;
}
// Exit on Ctrl+C
signal(SIGINT, SIG_DFL);
// Cleanup on exit
atexit(SDL_Quit);
atexit(TTF_Quit);
// What we draw on the screen
SDL_Surface* screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
if (screen == NULL)
{
fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
width, height, SDL_GetError());
return 1;
}
// Window title and icon
//Image icon("images/icon1.bmp", true);
Image icon("images/icon2.bmp", true); // a second possible icon found at http://www.merchantcircle.com/static/imgs/answers_homeTopicThumb.png
SDL_WM_SetIcon(icon.surface(), NULL);
SDL_WM_SetCaption("Sprouts", "Sprouts");
TTF_Font* font = TTF_OpenFont("images/LiberationSerif-Bold.ttf", 14);
try
{
bool inMenu = true;
Menu menu(screen);
menu.init();
GameGUI game(screen, font);
while (gameRunning)
{
while (gameRunning && SDL_PollEvent(&event))
{
try
{
switch (event.type)
{
case SDL_QUIT: // Alt+F4, X, ...
gameRunning = false;
break;
case SDL_ACTIVEEVENT:
break;
case SDL_KEYDOWN:
break;
case SDL_KEYUP:
// Escape cancels line and does nothing (at the moment) in the menu
if (event.key.keysym.sym == SDLK_ESCAPE)
{
if (inMenu)
menu.cancel();
else
game.cancel();
}
// Q returns to menu
else if (event.key.keysym.sym == SDLK_q && !inMenu)
{
game = GameGUI(screen, font);
inMenu = true;
menu.init();
}
break;
case SDL_MOUSEBUTTONDOWN:
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT)
{
// If it's in the menu, send the click event and
// then see if they clicked to start the game. If
// so, switch to game mode by setting inMenu to
// false and draw the game with the specified
// number of nodes to the screen.
if (inMenu)
{
ClickType status = menu.click(Coord(event.button.x, event.button.y));
// Exit the program.
if (status == EXIT)
{
gameRunning = false;
}
// Start the game with the specified number of nodes
else if (status == GAME)
{
inMenu = false;
game.init(menu.mode(), menu.nodes(), menu.nodeRadius(), menu.lineThick());
}
}
else
{
State state = game.click(Coord(event.button.x, event.button.y));
if (state == GameEnd)
{
//cout << "Game has ended!" << endl;
SDL_Delay(200);
inMenu = true;
menu.over(game.playerTurn());
game = GameGUI(screen, font);
}
}
}
break;
case SDL_MOUSEMOTION:
if (inMenu)
menu.cursor(Coord(event.motion.x, event.motion.y));
else
game.cursor(Coord(event.motion.x, event.motion.y));
break;
default:
break;
}
}
// Catch all of the errors that might have occurred in the game. If
// these are thrown, there's a GUI problem since the GUI shouldn't
// allow invalid moves. That, or the A-Checker has a bug that saying
// a move is invalid when it shouldn't be.
catch (const InvalidLine& e)
{
cout << "Error: Tried to add an invalid line. " << e << endl;
}
catch (const InvalidNode& e)
{
cout << "Error: Could not find start and/or end node in the line" << endl;
}
catch (const InvalidMiddle& e)
{
cout << "Error: Could not find where to place middle node in the line. " << e << endl;
}
catch (const AreasOutdated& e)
{
cout << "Error: Tried to find if connectable with outdated areas" << endl;
}
catch (const InvalidCorner& e)
{
cout << "Error: Tried to add a middle node on a corner" << endl;
}
catch (const NodeEntryCollision& e)
{
cout << "Error: Tried to enter a node twice from the same direction" << endl;
}
// Note that NotConnectable is only thrown when Game game(true), setting extraChecks to true
catch (const NotConnectable& e)
{
cout << "Error: Tried to connect two nodes that shouldn't be connectable" << endl;
}
}
// Don't use all the CPU
SDL_Delay(20);
}
}
catch (const ImageNotLoaded& e)
{
cout << "Error: could not load image: " << e << endl;
}
catch (...)
{
cout << "Error: Unhandled Exception" << endl;
}
TTF_CloseFont(font);
SDL_FreeSurface(screen);
SDL_Quit();
TTF_Quit();
return 0;
}