1 // Brain Party
2 // Copyright (C) 2010 Paul Hudson (http://www.tuxradar.com/brainparty)
3 
4 // Brain Party is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 3
7 // of the License, or (at your option) any later version.
8 
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 
18 #ifndef __BPGAME_H__
19 #define __BPGAME_H__
20 
21 #define SAFE_DELETE(a) { delete(a); (a) = 0; }
22 #define TIMERREDRAW 1000
23 
24 #include <vector>
25 #include <map>
26 #include <string>
27 #include <cstring>
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 
32 #include "SDL.h"
33 #include "SDL_opengl.h"
34 #include "SDL_mixer.h"
35 
36 #include "Texture.h"
37 #include "Colour.h"
38 #include "BPPoint.h"
39 #include "MessageBox.h"
40 #include "BPList.h"
41 #include "Minigame.h"
42 #include "MiniGameContainer.h"
43 #include "TestResultContainer.h"
44 #include "SpriteFont.h"
45 #include "BGObject.h"
46 
47 enum GameStates { FIRST_RUN, MAIN_MENU, ABOUT, PLAY_MENU, PRACTISE_MENU, PLAYING, TEST_STATUS, TEST_RESULTS, PROFESSOR, OPTIONS, HELP, HISTORY, CREDITS, STORE, BRAINBOOST, MARATHON, MAIN_MENU_PLAY, PLAY_MAIN_MENU, PLAY_PRACTISE, PRACTISE_PLAY, PLAY_HISTORY, HISTORY_CREDITS, CREDITS_HISTORY, HISTORY_PLAY, TEST_RESULTS_PLAY, PRACTISE_PROFESSOR, MAIN_MENU_OPTIONS, OPTIONS_MAIN_MENU, MAIN_MENU_HELP, HELP_MAIN_MENU };
48 enum MiniGameType { BALLOONBLASTER, BOMBHUNT, BPSAYS, BUBBLETROUBLE, CARDMATCH, CONNEX, CUPSNBALLS, DICEOFF, FLASHCOUNTING, FLASHLIGHT, IQTEST, JEWELFLIP, JEWELJAM, MARBLEDROP, MEMORYBLOX, MEMORYBOX, MEMORYMATHS, MINESWEEP, MOONJUMP, NEXTINLINE, NUMBERSNAKE, ODDONEOUT, PATCHMATCH, PERFECTPATHS, ROUTEFINDER, RPS, SCRAMBLED, SETFINDER, SHARPSHOOTER, SHORTCIRCUITSUDOKU, SHUFFLEPUZZLER, STRANGERDANGER, SYMBOLICLOGIC, UNDERTHEHAT, UNTANGLER, WORDSMASH };
49 enum FontSizes { TINY = 16, XSMALL = 19, SMALL = 22, NORMAL = 24, LARGE = 30, XLARGE = 40, XXLARGE = 50, MEGA = 72, BRAINWEIGHT = 82, ALMOSTEPIC = 90, EPIC = 96, LARGEST = 120 };
50 enum Colours { WHITE, BLACK, NOCOLOUR };
51 
52 class BPMiniGame;
53 class SpriteFont;
54 
55 struct cmp_str {
operatorcmp_str56 	bool operator()(char const *a, char const *b) const {
57 		return std::strcmp(a, b) < 0;
58 	}
59 };
60 
61 class BPGame {
62 public:
63     	int GLViewRenderbuffer;
64 	int GLViewFramebuffer;
65 
66 	int TickCount; // number of ticks ever
67 	int ElapsedTickCount; // number of ticks since last update
68 	float ElapsedSeconds;
69 
70 	int CheatMainTaps;
71 	int CheatOptionsTaps;
72 
73 	bool EnableSound;
74 	bool EnableMusic;
75 
76 	int NumUnlockedGames;
77 
78 	BPPoint EmptyPoint;
79 
80 	Colour* Black;
81 	Colour* White;
82 	Colour* TransparentWhite;
83 	Colour* Blue;
84 	Colour* Cyan;
85 	Colour* Green;
86 	Colour* ConnexGreen;
87 	Colour* Orange;
88 	Colour* Red;
89 	Colour* LightRed;
90 	Colour* DarkRed;
91 	Colour* Yellow;
92 	Colour* DarkGrey;
93 
94 	static Texture* sfcLogo;
95 	static bool ShowingMessageBox;
96 	static bool ShowingClearScores;
97 	static SpriteFont* sfcMessageBoxTitle;
98 	static SpriteFont* sfcMessageBoxText;
99 
100 	Texture* sfcMainMenu;
101 	Texture* sfcPlayMenu;
102 	Texture* sfcPractiseMenu;
103 	Texture* sfcPractiseMenuHider;
104 	Texture* sfcProfessorTalk;
105 	Texture* sfcCredits;
106 	Texture* sfcOptions;
107 	Texture* sfcOptionsSoundsOff;
108 	Texture* sfcOptionsMusicOff;
109 	Texture* sfcHistoryBackground;
110 	Texture* sfcMarathonMode;
111 	Texture* sfcBrainBoost;
112 	Texture* sfcFirstRun;
113 	Texture* sfcHelp;
114 
115 	Texture* sfcLoading;
116 
117 	Texture* sfcSecretHi;
118 	Texture* sfcSecretLo;
119 
120 	Texture* sfcResultsFail;
121 	Texture* sfcResultsBronze;
122 	Texture* sfcResultsSilver;
123 	Texture* sfcResultsGold;
124 	Texture* sfcResultsPlatinum;
125 
126 	Texture* sfcBottomBar;
127 	Texture* sfcMinigameHelp;
128 	Texture* sfcMinigameBack;
129 	Texture* sfcQuitTest;
130 
131 	Texture* sfcWhite;
132 
133 	Texture* sfcMiniGameUnknown;
134 
135 	Texture* sfcTestBackground;
136 	Texture* sfcResultsBackground;
137 	Texture* sfcResultsBackground2;
138 	map<int, Texture*> sfcResults90;
139 
140 	BPList<Texture*> sfcBGObjectTypes;
141 	BPList<BGObject*> BackgroundObjects;
142 	int LastBGObject;
143 
144 	BPList<Texture*> sfcStarTypes;
145 
146 	Mix_Music* Music;
147 	map<const char*, Mix_Chunk*, cmp_str> Sounds;
148 
149 	Texture* sfcCorrect;
150 	Texture* sfcWrong;
151 
152 	BPPList<Texture*> sfcTestStatus;
153 	BPPList<TestResultContainer*> TestResultContainers;
154 
155 	GameStates GameState;
156 	int LastStateChange;
157 	bool GameStateJustChanged; // used to skip one frame of anim updates to avoid jumping after long game changes such as starting a minigame
158 
159 	float AnimCounter;
160 	float PractiseAnimCounter;
161 
162 	BPMiniGame* ActiveMiniGame;
163 	string* CurrentMiniGame;
164 
165 	int PractisePageNumber;
166 	bool PractisePageMoveLeft; // true if items should be moving right to left
167 
168 	bool FirstRun;
169 	bool Secret1;
170 	bool Secret2;
171 	bool Secret3;
172 	bool Secret4;
173 
174 	void Init(int width, int height);
175 	void Update(float elapsed);
176 	void Draw();
177 
178 	BPPoint TouchEvent;
179 
180 	BPPList<const char*> TestBrainJobs;
181 	BPPList<const char*> PeopleNames;
182 
183 	map<MiniGameType, BPMiniGame_Container*> MiniGames;
184 
185 	BPList<int> AllTestScores;
186 	BPList<int> ShowScores;
187 	BPList<MiniGameType> TestMiniGames;
188 	BPList<int> TestScores;
189 
190 	SpriteFont* sfcTestWeight;
191 	SpriteFont* sfcTestJob;
192 	SpriteFont* sfcHighestBrainWeight;
193 
194 	int TestPosition;
195 	bool InTestMode;
196 
197 	int CurrentBrainWeight;
198 	int HighestBrainWeight;
199 	int TestBrainWeight;
200 	const char* TestBrainJob;
201 
202 	float Acceleration[3];
203 
204 	void TouchStart(float x, float y);
205 	void TouchStop(float x, float y);
206 	void TouchDrag(float x, float y);
207 	void Accelerate(float x, float y, float z);
208 
209 	Texture* LoadBitmap(const char* filename, int actualwidth = 100, int actualheight = 100);
210 	Mix_Chunk* LoadSound(const char* filename, const char* extension = "wav");
211 	void PlaySound(const char* sound, bool preload_only = false, bool force_play = false);
212 	void AddTestScore(int score);
213 	void AddPlatinumGame();
214 
215 	void FillRectangle(Colour col, float x, float y, float width, float height);
216 	void DrawImage(Texture* tex, float x, float y);
217 	void DrawImage(Texture* tex, float x, float y, Colour &col);
218 	void DrawImage(Texture* tex, float x, float y, float rotation, float scale, Colour &col);
219 	static void AllocString(SpriteFont** tex, const char* str, FontSizes size, float width, float height, Alignments align, bool bold = false);
220 	void DrawString(SpriteFont* tex, Colour &col, float x, float y);
221 	void DrawString(SpriteFont* tex, Colours col, float x, float y);
222 	void DrawString(SpriteFont* tex, float x, float y);
223 	void Clear(Colour* col);
224 	bool PointOverRect(int x1, int y1, int x2, int y2, int width, int height);
225 	bool RectOverRect(int x1, int y1, int width1, int height1, int x2, int y2, int width2, int height2);
226 	int RandomRange(int min, int max);
227 	int RandomRangeExcept(int min, int max, int except);
228 	string SeparateThousands(int number);
229 	string* TicksToTime(int ticks);
230 	int DivRem(int Num, int Div, int* Rem);
231 	void DrawLine(int fromx, int fromy, int tox, int toy, Colour* col, float width);
232 	float Hermite(float value1, float tangent1, float value2, float tangent2, float amount);
233 	float Lerp(float value1, float value2, float amount);
234 	float SmoothStep(float value1, float value2, float amount);
235 	float SmoothStep2(float value1, float value2, float amount);
236 	Colour ColourSmoothStep (Colour &from, Colour &to, float amount);
237 	float Clamp(float value, float minval, float maxval);
238 	bool StartsWithVowel(string* word);
239 	const char* GetName();
240 	Colour* ColorLerp (Colour* from, Colour* to, float amount);
241 
242 	void LoadSettings();
243 	void SaveSettings();
244 
245 	void RenderMainMenu(float xoff, float yoff);
246 	void RenderOptionsMenu();
247 	void RenderPlayMenu(float xoff, float yoff);
248 	void RenderPractiseMenu();
249 	void RenderProfessor();
250 	void RenderPlaying();
251 	void RenderTestStatus();
252 	void RenderTestResults(float xoff, float yoff);
253 	void RenderHistory(float xoff, float yoff);
254 	void RenderBrainBoost();
255 	void RenderMarathon();
256 	void RenderCredits(float xoff, float yoff);
257 	void RenderHelp(float xoff, float yoff);
258 
259 	void CalculateTestResults();
260 	void CreateMiniGame(MiniGameType game, bool marathon, bool practise);
261 	void SetGameState(GameStates state);
262 	void PositionPractiseGames();
263 
264 	void RunTest();
265 	void NextTest();
266 	void CancelTest();
267 
268 	void PlayMusic(const char* name);
269 	void StopMusic();
270 
271 	void PromptResetScores();
272 	void ExecuteResetScores();
273 
274 	bool FileExists(const char* filename);
275 
IsNull(void * somepointer)276 	static bool IsNull(void* somepointer) { return (somepointer == NULL); }
277 
278 	vector<string> Split(const string& s);
279 };
280 
281 #endif
282