Diffstat (limited to 'noncore/games/sfcave-sdl/game.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/sfcave-sdl/game.cpp | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/noncore/games/sfcave-sdl/game.cpp b/noncore/games/sfcave-sdl/game.cpp index a644696..e41e510 100644 --- a/noncore/games/sfcave-sdl/game.cpp +++ b/noncore/games/sfcave-sdl/game.cpp | |||
@@ -33,168 +33,171 @@ Game :: Game( SFCave *p, int w, int h, int diff ) | |||
33 | 33 | ||
34 | Game :: ~Game() | 34 | Game :: ~Game() |
35 | { | 35 | { |
36 | if ( terrain ) | 36 | if ( terrain ) |
37 | delete terrain; | 37 | delete terrain; |
38 | 38 | ||
39 | if ( player ) | 39 | if ( player ) |
40 | delete player; | 40 | delete player; |
41 | 41 | ||
42 | replayList.clear(); | 42 | replayList.clear(); |
43 | } | 43 | } |
44 | 44 | ||
45 | void Game :: init() | 45 | void Game :: init() |
46 | { | 46 | { |
47 | if ( replay ) | 47 | if ( replay ) |
48 | { | 48 | { |
49 | setSeed( currentSeed ); | 49 | setSeed( currentSeed ); |
50 | replayIt = replayList.begin(); | 50 | replayIt = replayList.begin(); |
51 | } | 51 | } |
52 | else | 52 | else |
53 | { | 53 | { |
54 | setSeed( -1 ); | 54 | setSeed( -1 ); |
55 | replayList.clear(); | 55 | replayList.clear(); |
56 | } | 56 | } |
57 | 57 | ||
58 | score = 0; | 58 | score = 0; |
59 | nrFrames = 0; | 59 | nrFrames = 0; |
60 | press = false; | 60 | press = false; |
61 | 61 | ||
62 | // Load highscore | 62 | // Load highscore |
63 | string key = getGameName() + "_" + getGameDifficultyText() + "_highscore"; | 63 | string key = getGameName() + "_" + getGameDifficultyText() + "_highscore"; |
64 | highScore = atoi( parent->loadSetting( key, "0" ).c_str() ); | 64 | highScore = atoi( parent->loadSetting( key, "0" ).c_str() ); |
65 | 65 | ||
66 | terrain->initTerrain(); | 66 | terrain->initTerrain(); |
67 | player->init(); | 67 | player->init(); |
68 | } | 68 | } |
69 | 69 | ||
70 | void Game :: handleKeys( SDL_KeyboardEvent &key ) | 70 | void Game :: handleKeys( SDL_KeyboardEvent &key ) |
71 | { | 71 | { |
72 | if ( !replay && key.keysym.sym == SDLK_SPACE ) | 72 | if ( !replay && key.keysym.sym == SDLK_SPACE ) |
73 | { | 73 | { |
74 | if ( key.type == SDL_KEYDOWN ) | 74 | if ( key.type == SDL_KEYDOWN ) |
75 | { | 75 | { |
76 | if ( !press ) | 76 | if ( !press ) |
77 | replayList.push_back( nrFrames ); | 77 | replayList.push_back( nrFrames ); |
78 | press = true; | 78 | press = true; |
79 | |||
80 | // if ( thrustChannel == -1 && parent->getState() == STATE_PLAYING ) | ||
81 | // thrustChannel = SoundHandler :: playSound( SND_THRUST, -1, -1, false ); | ||
82 | } | 79 | } |
83 | else | 80 | else |
84 | { | 81 | { |
85 | if ( press ) | 82 | if ( press ) |
86 | replayList.push_back( nrFrames ); | 83 | replayList.push_back( nrFrames ); |
87 | press = false; | 84 | press = false; |
88 | 85 | ||
89 | if ( thrustChannel != -1 ) | ||
90 | { | ||
91 | // SoundHandler :: stopSound( thrustChannel, true, 300 ); | ||
92 | // thrustChannel = -1; | ||
93 | } | ||
94 | } | 86 | } |
95 | } | 87 | } |
96 | } | 88 | } |
97 | 89 | ||
98 | 90 | ||
99 | 91 | ||
100 | QString Game :: getGameDifficultyText() | 92 | string Game :: getGameDifficultyText() |
101 | { | 93 | { |
102 | QString ret; | 94 | string ret; |
103 | 95 | ||
104 | if ( difficulty == MENU_DIFFICULTY_EASY ) | 96 | if ( difficulty == MENU_DIFFICULTY_EASY ) |
105 | ret = "Easy"; | 97 | ret = "Easy"; |
106 | else if ( difficulty == MENU_DIFFICULTY_NORMAL ) | 98 | else if ( difficulty == MENU_DIFFICULTY_NORMAL ) |
107 | ret = "Medium"; | 99 | ret = "Medium"; |
108 | else if ( difficulty == MENU_DIFFICULTY_HARD ) | 100 | else if ( difficulty == MENU_DIFFICULTY_HARD ) |
109 | ret = "Hard"; | 101 | ret = "Hard"; |
102 | else if ( difficulty == MENU_DIFFICULTY_CUSTOM ) | ||
103 | ret = "Custom"; | ||
110 | 104 | ||
111 | return ret; | 105 | return ret; |
112 | } | 106 | } |
113 | 107 | ||
114 | void Game :: setDifficulty( string diff ) | 108 | void Game :: setDifficulty( string diff ) |
115 | { | 109 | { |
116 | if ( diff == "Easy" ) | 110 | if ( diff == "Easy" ) |
117 | difficulty = MENU_DIFFICULTY_EASY; | 111 | difficulty = MENU_DIFFICULTY_EASY; |
118 | else if ( diff == "Medium" ) | 112 | else if ( diff == "Medium" ) |
119 | difficulty = MENU_DIFFICULTY_NORMAL; | 113 | difficulty = MENU_DIFFICULTY_NORMAL; |
120 | else if ( diff == "Hard" ) | 114 | else if ( diff == "Hard" ) |
121 | difficulty = MENU_DIFFICULTY_HARD; | 115 | difficulty = MENU_DIFFICULTY_HARD; |
116 | else if ( diff == "Custom" ) | ||
117 | difficulty = MENU_DIFFICULTY_CUSTOM; | ||
118 | |||
119 | init(); | ||
120 | } | ||
121 | |||
122 | void Game :: setDifficulty( int diff ) | ||
123 | { | ||
124 | difficulty = diff; | ||
125 | init(); | ||
122 | } | 126 | } |
123 | 127 | ||
124 | void Game :: update( int state ) | 128 | void Game :: update( int state ) |
125 | { | 129 | { |
126 | nrFrames ++; | 130 | nrFrames ++; |
127 | 131 | ||
128 | if ( score > highScore ) | 132 | if ( score > highScore ) |
129 | highScore = score; | 133 | highScore = score; |
130 | 134 | ||
131 | 135 | ||
132 | if ( state == STATE_PLAYING ) | 136 | if ( state == STATE_PLAYING ) |
133 | { | 137 | { |
134 | if ( replay ) | 138 | if ( replay ) |
135 | { | 139 | { |
136 | while( replayIt != replayList.end() && (*replayIt) == nrFrames-1 ) | 140 | while( replayIt != replayList.end() && (*replayIt) == nrFrames-1 ) |
137 | { | 141 | { |
138 | press = !press; | 142 | press = !press; |
139 | replayIt ++; | 143 | replayIt ++; |
140 | } | 144 | } |
141 | } | 145 | } |
142 | 146 | ||
143 | if ( press && thrustChannel == -1 ) | 147 | if ( press && thrustChannel == -1 ) |
144 | thrustChannel = SoundHandler :: playSound( SND_THRUST, -1, -1, false ); | 148 | thrustChannel = SoundHandler :: playSound( SND_THRUST, -1, -1, false ); |
145 | 149 | ||
146 | if ( !press &&thrustChannel != -1 ) | 150 | if ( !press &&thrustChannel != -1 ) |
147 | { | 151 | { |
148 | SoundHandler :: stopSound( thrustChannel, true, 300 ); | 152 | SoundHandler :: stopSound( thrustChannel, true, 300 ); |
149 | thrustChannel = -1; | 153 | thrustChannel = -1; |
150 | } | 154 | } |
151 | } | 155 | } |
152 | 156 | ||
153 | if ( state == STATE_CRASHING || state == STATE_CRASHED ) | 157 | if ( state == STATE_CRASHING || state == STATE_CRASHED ) |
154 | { | 158 | { |
155 | // fade out any trail marks remainin | 159 | // fade out any trail marks remainin |
156 | if ( player->updateCrashing() ) | 160 | if ( player->updateCrashing() ) |
157 | parent->changeState( STATE_CRASHED ); | 161 | parent->changeState( STATE_CRASHED ); |
158 | 162 | ||
159 | } | 163 | } |
160 | } | 164 | } |
161 | 165 | ||
162 | void Game :: preDraw( SDL_Surface *screen ) | 166 | void Game :: preDraw( SDL_Surface *screen ) |
163 | { | 167 | { |
164 | } | 168 | } |
165 | 169 | ||
166 | void Game :: draw( SDL_Surface *screen ) | 170 | void Game :: draw( SDL_Surface *screen ) |
167 | { | 171 | { |
168 | char tmp[100]; | 172 | char tmp[100]; |
169 | QString scoreText; | 173 | string scoreText; |
170 | sprintf( tmp, "Score: %06ld High Score: %06ld", score, highScore ); | 174 | sprintf( tmp, "Score: %06ld High Score: %06ld", score, highScore ); |
171 | //printf( "%s\n", (const char *)scoreText ); | ||
172 | FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 3, 10 ); | 175 | FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 3, 10 ); |
173 | 176 | ||
174 | if ( parent->getState() == STATE_CRASHED ) | 177 | if ( parent->getState() == STATE_CRASHED ) |
175 | { | 178 | { |
176 | QString crashText; | 179 | string crashText; |
177 | crashText = "Game Over"; | 180 | crashText = "Game Over"; |
178 | int x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; | 181 | int x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; |
179 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 ); | 182 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 ); |
180 | 183 | ||
181 | int fontHeight = FontHandler::FontHeight( FONT_WHITE_TEXT ); | 184 | int fontHeight = FontHandler::FontHeight( FONT_WHITE_TEXT ); |
182 | crashText = "Press Middle Button to play again"; | 185 | crashText = "Press Middle Button to play again"; |
183 | x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; | 186 | x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; |
184 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + fontHeight ); | 187 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + fontHeight ); |
185 | 188 | ||
186 | crashText = "or OK for menu"; | 189 | crashText = "or OK for menu"; |
187 | x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; | 190 | x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; |
188 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + 2*fontHeight ); | 191 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + 2*fontHeight ); |
189 | } | 192 | } |
190 | 193 | ||
191 | if ( parent->showFPS() ) | 194 | if ( parent->showFPS() ) |
192 | { | 195 | { |
193 | sprintf( tmp, "FPS : %d", parent->getFPS() ); | 196 | sprintf( tmp, "FPS : %d", parent->getFPS() ); |
194 | FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 20, 300 ); | 197 | FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 20, 300 ); |
195 | } | 198 | } |
196 | } | 199 | } |
197 | 200 | ||
198 | void Game :: stateChanged( int from, int to ) | 201 | void Game :: stateChanged( int from, int to ) |
199 | { | 202 | { |
200 | if ( from != STATE_CRASHING && to == STATE_CRASHING ) | 203 | if ( from != STATE_CRASHING && to == STATE_CRASHING ) |
@@ -202,131 +205,124 @@ void Game :: stateChanged( int from, int to ) | |||
202 | // play explosion sound | 205 | // play explosion sound |
203 | SoundHandler :: stopSound( -1, false ); | 206 | SoundHandler :: stopSound( -1, false ); |
204 | SoundHandler :: playSound( SND_EXPLOSION ); | 207 | SoundHandler :: playSound( SND_EXPLOSION ); |
205 | 208 | ||
206 | // Check and save highscore | 209 | // Check and save highscore |
207 | printf( "Got Highscore = %d\n", gotHighScore() ); | 210 | printf( "Got Highscore = %d\n", gotHighScore() ); |
208 | if ( gotHighScore() ) | 211 | if ( gotHighScore() ) |
209 | { | 212 | { |
210 | string key = getGameName() + "_" + getGameDifficultyText() + "_highscore"; | 213 | string key = getGameName() + "_" + getGameDifficultyText() + "_highscore"; |
211 | parent->saveSetting( key, getHighScore() ); | 214 | parent->saveSetting( key, getHighScore() ); |
212 | } | 215 | } |
213 | 216 | ||
214 | } | 217 | } |
215 | } | 218 | } |
216 | 219 | ||
217 | void Game :: setSeed( int seed ) | 220 | void Game :: setSeed( int seed ) |
218 | { | 221 | { |
219 | if ( seed == -1 ) | 222 | if ( seed == -1 ) |
220 | currentSeed = ((unsigned long) time((time_t *) NULL)); | 223 | currentSeed = ((unsigned long) time((time_t *) NULL)); |
221 | else | 224 | else |
222 | currentSeed = seed; | 225 | currentSeed = seed; |
223 | PutSeed( currentSeed ); | 226 | PutSeed( currentSeed ); |
224 | } | 227 | } |
225 | 228 | ||
226 | void Game :: saveReplay( QString file ) | 229 | void Game :: saveReplay( string file ) |
227 | { | 230 | { |
228 | FILE *out; | 231 | FILE *out; |
229 | out = fopen( file.c_str(), "w" ); | 232 | out = fopen( file.c_str(), "w" ); |
230 | if ( !out ) | 233 | if ( !out ) |
231 | { | 234 | { |
232 | printf( "Couldn't write to /home/root/%s\n", file.c_str() ); | 235 | printf( "Couldn't write to /home/root/%s\n", file.c_str() ); |
233 | parent->setMenuStatusText( "Couldn't save replay file" ); | 236 | parent->setMenuStatusText( "Couldn't save replay file" ); |
234 | return; | 237 | return; |
235 | } | 238 | } |
236 | 239 | ||
237 | // Build up string of values | 240 | // Build up string of values |
238 | // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>....... | 241 | // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>....... |
239 | QString val; | 242 | string val; |
240 | char tmp[20]; | 243 | char tmp[20]; |
241 | sprintf( tmp, "%d %d ", currentSeed, difficulty ); | 244 | sprintf( tmp, "%d %d ", currentSeed, difficulty ); |
242 | val = tmp; | 245 | val = tmp; |
243 | 246 | ||
244 | list<int>::iterator it = replayList.begin(); | 247 | list<int>::iterator it = replayList.begin(); |
245 | while( it != replayList.end() ) | 248 | while( it != replayList.end() ) |
246 | { | 249 | { |
247 | sprintf( tmp, "%d ", *it ); | 250 | sprintf( tmp, "%d ", *it ); |
248 | val += tmp; | 251 | val += tmp; |
249 | 252 | ||
250 | it++; | 253 | it++; |
251 | } | 254 | } |
252 | val += "\n"; | 255 | val += "\n"; |
253 | 256 | ||
254 | QString line; | 257 | string line; |
255 | sprintf( tmp, "%d\n", val.length() ); | 258 | sprintf( tmp, "%d\n", val.length() ); |
256 | line = tmp; | 259 | line = tmp; |
257 | fwrite( line.c_str(), 1, line.length(), out ); | 260 | fwrite( line.c_str(), 1, line.length(), out ); |
258 | 261 | ||
259 | fwrite( val.c_str(), 1, val.length(), out ); | 262 | fwrite( val.c_str(), 1, val.length(), out ); |
260 | 263 | ||
261 | fclose( out ); | 264 | fclose( out ); |
262 | |||
263 | printf( "Replay saved to %s\n", (const char *)file.c_str() ); | ||
264 | |||
265 | } | 265 | } |
266 | 266 | ||
267 | void Game :: loadReplay( QString file ) | 267 | void Game :: loadReplay( string file ) |
268 | { | 268 | { |
269 | 269 | ||
270 | FILE *in = fopen( (const char *)file.c_str(), "r" ); | 270 | FILE *in = fopen( (const char *)file.c_str(), "r" ); |
271 | 271 | ||
272 | if ( in == 0 ) | 272 | if ( in == 0 ) |
273 | { | 273 | { |
274 | printf( "Couldn't load replay file %s!\n", (const char *)file.c_str() ); | 274 | printf( "Couldn't load replay file %s!\n", (const char *)file.c_str() ); |
275 | parent->setMenuStatusText( "Couldn't load replay file" ); | 275 | parent->setMenuStatusText( "Couldn't load replay file" ); |
276 | return; | 276 | return; |
277 | } | 277 | } |
278 | 278 | ||
279 | // Read next line - contains the size of the options | 279 | // Read next line - contains the size of the options |
280 | char line[10+1]; | 280 | char line[10+1]; |
281 | fgets( line, 10, in ); | 281 | fgets( line, 10, in ); |
282 | 282 | ||
283 | int length = -1; | 283 | int length = -1; |
284 | sscanf( line, "%d", &length ); | 284 | sscanf( line, "%d", &length ); |
285 | char *data = new char[length+1]; | 285 | char *data = new char[length+1]; |
286 | 286 | ||
287 | fread( data, 1, length, in ); | 287 | fread( data, 1, length, in ); |
288 | // printf( "data - %s", data ); | ||
289 | 288 | ||
290 | QString sep = " "; | 289 | string sep = " "; |
291 | 290 | ||
292 | StringTokenizer st( data, sep ); | 291 | StringTokenizer st( data, sep ); |
293 | 292 | ||
294 | // print it out | 293 | // print it out |
295 | vector<string>::iterator it = st.begin(); | 294 | vector<string>::iterator it = st.begin(); |
296 | currentSeed = atoi( (*it).c_str() ); | 295 | currentSeed = atoi( (*it).c_str() ); |
297 | ++it; | 296 | ++it; |
298 | difficulty = atoi( (*it).c_str() ); | 297 | difficulty = atoi( (*it).c_str() ); |
299 | ++it; | 298 | ++it; |
300 | 299 | ||
301 | replayList.clear(); | 300 | replayList.clear(); |
302 | for ( ; it != st.end(); ++it ) | 301 | for ( ; it != st.end(); ++it ) |
303 | { | 302 | { |
304 | int v = atoi( (*it).c_str() ); | 303 | int v = atoi( (*it).c_str() ); |
305 | replayList.push_back( v ); | 304 | replayList.push_back( v ); |
306 | } | 305 | } |
307 | 306 | ||
308 | delete data; | 307 | delete data; |
309 | 308 | ||
310 | fclose( in ); | 309 | fclose( in ); |
311 | |||
312 | printf( "Replay loaded from %s\n", (const char *)file.c_str() ); | ||
313 | |||
314 | } | 310 | } |
315 | 311 | ||
316 | 312 | ||
317 | Game *Game :: createGame( SFCave *p, int w, int h, string game, string difficulty ) | 313 | Game *Game :: createGame( SFCave *p, int w, int h, string game, string difficulty ) |
318 | { | 314 | { |
319 | Game *g; | 315 | Game *g; |
320 | 316 | ||
321 | if ( game == "SFCave" ) | 317 | if ( game == "SFCave" ) |
322 | g = new SFCaveGame( p, w, h, 0 ); | 318 | g = new SFCaveGame( p, w, h, 0 ); |
323 | else if ( game == "Gates" ) | 319 | else if ( game == "Gates" ) |
324 | g = new GatesGame( p, w, h, 0 ); | 320 | g = new GatesGame( p, w, h, 0 ); |
325 | else if ( game == "Fly" ) | 321 | else if ( game == "Fly" ) |
326 | g = new FlyGame( p, w, h, 0 ); | 322 | g = new FlyGame( p, w, h, 0 ); |
327 | 323 | ||
328 | if ( g ) | 324 | if ( g ) |
329 | g->setDifficulty( difficulty ); | 325 | g->setDifficulty( difficulty ); |
330 | 326 | ||
331 | return g; | 327 | return g; |
332 | } | 328 | } |