author | andyq <andyq> | 2003-02-28 23:56:03 (UTC) |
---|---|---|
committer | andyq <andyq> | 2003-02-28 23:56:03 (UTC) |
commit | 7dd36872a3eb63eb1c3c7a17549f3eeba92f1b32 (patch) (unidiff) | |
tree | b0628e9a3c75129d24301e30c44c55a23f03d963 | |
parent | 64423f1852a13178f23365f2bba995ef2c355b3b (diff) | |
download | opie-7dd36872a3eb63eb1c3c7a17549f3eeba92f1b32.zip opie-7dd36872a3eb63eb1c3c7a17549f3eeba92f1b32.tar.gz opie-7dd36872a3eb63eb1c3c7a17549f3eeba92f1b32.tar.bz2 |
Changed keyboard handling to accept UP, ENTER and RETURN for moving ship
-rw-r--r-- | noncore/games/sfcave-sdl/game.cpp | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/noncore/games/sfcave-sdl/game.cpp b/noncore/games/sfcave-sdl/game.cpp index e41e510..1ee0230 100644 --- a/noncore/games/sfcave-sdl/game.cpp +++ b/noncore/games/sfcave-sdl/game.cpp | |||
@@ -1,328 +1,332 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <time.h> | 2 | #include <time.h> |
3 | 3 | ||
4 | #include <SDL.h> | 4 | #include <SDL.h> |
5 | #include <SDL_image.h> | 5 | #include <SDL_image.h> |
6 | 6 | ||
7 | #include "font.h" | 7 | #include "font.h" |
8 | 8 | ||
9 | #include "constants.h" | 9 | #include "constants.h" |
10 | #include "game.h" | 10 | #include "game.h" |
11 | #include "player.h" | 11 | #include "player.h" |
12 | #include "random.h" | 12 | #include "random.h" |
13 | #include "sound.h" | 13 | #include "sound.h" |
14 | #include "stringtokenizer.h" | 14 | #include "stringtokenizer.h" |
15 | 15 | ||
16 | #include "sfcave_game.h" | 16 | #include "sfcave_game.h" |
17 | #include "gates_game.h" | 17 | #include "gates_game.h" |
18 | #include "fly_game.h" | 18 | #include "fly_game.h" |
19 | #include "starfield.h" | 19 | #include "starfield.h" |
20 | 20 | ||
21 | Game :: Game( SFCave *p, int w, int h, int diff ) | 21 | Game :: Game( SFCave *p, int w, int h, int diff ) |
22 | { | 22 | { |
23 | parent = p; | 23 | parent = p; |
24 | sHeight = h; | 24 | sHeight = h; |
25 | sWidth = w; | 25 | sWidth = w; |
26 | difficulty = diff; | 26 | difficulty = diff; |
27 | replayIt = 0; | 27 | replayIt = 0; |
28 | replay = false; | 28 | replay = false; |
29 | terrain = 0; | 29 | terrain = 0; |
30 | player = 0; | 30 | player = 0; |
31 | thrustChannel = -1; | 31 | thrustChannel = -1; |
32 | } | 32 | } |
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 && |
73 | (key.keysym.sym == SDLK_SPACE || | ||
74 | key.keysym.sym == SDLK_KP_ENTER || | ||
75 | key.keysym.sym == SDLK_RETURN || | ||
76 | key.keysym.sym == SDLK_UP) ) | ||
73 | { | 77 | { |
74 | if ( key.type == SDL_KEYDOWN ) | 78 | if ( key.type == SDL_KEYDOWN ) |
75 | { | 79 | { |
76 | if ( !press ) | 80 | if ( !press ) |
77 | replayList.push_back( nrFrames ); | 81 | replayList.push_back( nrFrames ); |
78 | press = true; | 82 | press = true; |
79 | } | 83 | } |
80 | else | 84 | else |
81 | { | 85 | { |
82 | if ( press ) | 86 | if ( press ) |
83 | replayList.push_back( nrFrames ); | 87 | replayList.push_back( nrFrames ); |
84 | press = false; | 88 | press = false; |
85 | 89 | ||
86 | } | 90 | } |
87 | } | 91 | } |
88 | } | 92 | } |
89 | 93 | ||
90 | 94 | ||
91 | 95 | ||
92 | string Game :: getGameDifficultyText() | 96 | string Game :: getGameDifficultyText() |
93 | { | 97 | { |
94 | string ret; | 98 | string ret; |
95 | 99 | ||
96 | if ( difficulty == MENU_DIFFICULTY_EASY ) | 100 | if ( difficulty == MENU_DIFFICULTY_EASY ) |
97 | ret = "Easy"; | 101 | ret = "Easy"; |
98 | else if ( difficulty == MENU_DIFFICULTY_NORMAL ) | 102 | else if ( difficulty == MENU_DIFFICULTY_NORMAL ) |
99 | ret = "Medium"; | 103 | ret = "Medium"; |
100 | else if ( difficulty == MENU_DIFFICULTY_HARD ) | 104 | else if ( difficulty == MENU_DIFFICULTY_HARD ) |
101 | ret = "Hard"; | 105 | ret = "Hard"; |
102 | else if ( difficulty == MENU_DIFFICULTY_CUSTOM ) | 106 | else if ( difficulty == MENU_DIFFICULTY_CUSTOM ) |
103 | ret = "Custom"; | 107 | ret = "Custom"; |
104 | 108 | ||
105 | return ret; | 109 | return ret; |
106 | } | 110 | } |
107 | 111 | ||
108 | void Game :: setDifficulty( string diff ) | 112 | void Game :: setDifficulty( string diff ) |
109 | { | 113 | { |
110 | if ( diff == "Easy" ) | 114 | if ( diff == "Easy" ) |
111 | difficulty = MENU_DIFFICULTY_EASY; | 115 | difficulty = MENU_DIFFICULTY_EASY; |
112 | else if ( diff == "Medium" ) | 116 | else if ( diff == "Medium" ) |
113 | difficulty = MENU_DIFFICULTY_NORMAL; | 117 | difficulty = MENU_DIFFICULTY_NORMAL; |
114 | else if ( diff == "Hard" ) | 118 | else if ( diff == "Hard" ) |
115 | difficulty = MENU_DIFFICULTY_HARD; | 119 | difficulty = MENU_DIFFICULTY_HARD; |
116 | else if ( diff == "Custom" ) | 120 | else if ( diff == "Custom" ) |
117 | difficulty = MENU_DIFFICULTY_CUSTOM; | 121 | difficulty = MENU_DIFFICULTY_CUSTOM; |
118 | 122 | ||
119 | init(); | 123 | init(); |
120 | } | 124 | } |
121 | 125 | ||
122 | void Game :: setDifficulty( int diff ) | 126 | void Game :: setDifficulty( int diff ) |
123 | { | 127 | { |
124 | difficulty = diff; | 128 | difficulty = diff; |
125 | init(); | 129 | init(); |
126 | } | 130 | } |
127 | 131 | ||
128 | void Game :: update( int state ) | 132 | void Game :: update( int state ) |
129 | { | 133 | { |
130 | nrFrames ++; | 134 | nrFrames ++; |
131 | 135 | ||
132 | if ( score > highScore ) | 136 | if ( score > highScore ) |
133 | highScore = score; | 137 | highScore = score; |
134 | 138 | ||
135 | 139 | ||
136 | if ( state == STATE_PLAYING ) | 140 | if ( state == STATE_PLAYING ) |
137 | { | 141 | { |
138 | if ( replay ) | 142 | if ( replay ) |
139 | { | 143 | { |
140 | while( replayIt != replayList.end() && (*replayIt) == nrFrames-1 ) | 144 | while( replayIt != replayList.end() && (*replayIt) == nrFrames-1 ) |
141 | { | 145 | { |
142 | press = !press; | 146 | press = !press; |
143 | replayIt ++; | 147 | replayIt ++; |
144 | } | 148 | } |
145 | } | 149 | } |
146 | 150 | ||
147 | if ( press && thrustChannel == -1 ) | 151 | if ( press && thrustChannel == -1 ) |
148 | thrustChannel = SoundHandler :: playSound( SND_THRUST, -1, -1, false ); | 152 | thrustChannel = SoundHandler :: playSound( SND_THRUST, -1, -1, false ); |
149 | 153 | ||
150 | if ( !press &&thrustChannel != -1 ) | 154 | if ( !press &&thrustChannel != -1 ) |
151 | { | 155 | { |
152 | SoundHandler :: stopSound( thrustChannel, true, 300 ); | 156 | SoundHandler :: stopSound( thrustChannel, true, 300 ); |
153 | thrustChannel = -1; | 157 | thrustChannel = -1; |
154 | } | 158 | } |
155 | } | 159 | } |
156 | 160 | ||
157 | if ( state == STATE_CRASHING || state == STATE_CRASHED ) | 161 | if ( state == STATE_CRASHING || state == STATE_CRASHED ) |
158 | { | 162 | { |
159 | // fade out any trail marks remainin | 163 | // fade out any trail marks remainin |
160 | if ( player->updateCrashing() ) | 164 | if ( player->updateCrashing() ) |
161 | parent->changeState( STATE_CRASHED ); | 165 | parent->changeState( STATE_CRASHED ); |
162 | 166 | ||
163 | } | 167 | } |
164 | } | 168 | } |
165 | 169 | ||
166 | void Game :: preDraw( SDL_Surface *screen ) | 170 | void Game :: preDraw( SDL_Surface *screen ) |
167 | { | 171 | { |
168 | } | 172 | } |
169 | 173 | ||
170 | void Game :: draw( SDL_Surface *screen ) | 174 | void Game :: draw( SDL_Surface *screen ) |
171 | { | 175 | { |
172 | char tmp[100]; | 176 | char tmp[100]; |
173 | string scoreText; | 177 | string scoreText; |
174 | sprintf( tmp, "Score: %06ld High Score: %06ld", score, highScore ); | 178 | sprintf( tmp, "Score: %06ld High Score: %06ld", score, highScore ); |
175 | FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 3, 10 ); | 179 | FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 3, 10 ); |
176 | 180 | ||
177 | if ( parent->getState() == STATE_CRASHED ) | 181 | if ( parent->getState() == STATE_CRASHED ) |
178 | { | 182 | { |
179 | string crashText; | 183 | string crashText; |
180 | crashText = "Game Over"; | 184 | crashText = "Game Over"; |
181 | int x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; | 185 | int x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; |
182 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 ); | 186 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 ); |
183 | 187 | ||
184 | int fontHeight = FontHandler::FontHeight( FONT_WHITE_TEXT ); | 188 | int fontHeight = FontHandler::FontHeight( FONT_WHITE_TEXT ); |
185 | crashText = "Press Middle Button to play again"; | 189 | crashText = "Press Middle Button to play again"; |
186 | 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; |
187 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + fontHeight ); | 191 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + fontHeight ); |
188 | 192 | ||
189 | crashText = "or OK for menu"; | 193 | crashText = "or OK for menu"; |
190 | x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; | 194 | x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; |
191 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + 2*fontHeight ); | 195 | FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + 2*fontHeight ); |
192 | } | 196 | } |
193 | 197 | ||
194 | if ( parent->showFPS() ) | 198 | if ( parent->showFPS() ) |
195 | { | 199 | { |
196 | sprintf( tmp, "FPS : %d", parent->getFPS() ); | 200 | sprintf( tmp, "FPS : %d", parent->getFPS() ); |
197 | FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 20, 300 ); | 201 | FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 20, 300 ); |
198 | } | 202 | } |
199 | } | 203 | } |
200 | 204 | ||
201 | void Game :: stateChanged( int from, int to ) | 205 | void Game :: stateChanged( int from, int to ) |
202 | { | 206 | { |
203 | if ( from != STATE_CRASHING && to == STATE_CRASHING ) | 207 | if ( from != STATE_CRASHING && to == STATE_CRASHING ) |
204 | { | 208 | { |
205 | // play explosion sound | 209 | // play explosion sound |
206 | SoundHandler :: stopSound( -1, false ); | 210 | SoundHandler :: stopSound( -1, false ); |
207 | SoundHandler :: playSound( SND_EXPLOSION ); | 211 | SoundHandler :: playSound( SND_EXPLOSION ); |
208 | 212 | ||
209 | // Check and save highscore | 213 | // Check and save highscore |
210 | printf( "Got Highscore = %d\n", gotHighScore() ); | 214 | printf( "Got Highscore = %d\n", gotHighScore() ); |
211 | if ( gotHighScore() ) | 215 | if ( gotHighScore() ) |
212 | { | 216 | { |
213 | string key = getGameName() + "_" + getGameDifficultyText() + "_highscore"; | 217 | string key = getGameName() + "_" + getGameDifficultyText() + "_highscore"; |
214 | parent->saveSetting( key, getHighScore() ); | 218 | parent->saveSetting( key, getHighScore() ); |
215 | } | 219 | } |
216 | 220 | ||
217 | } | 221 | } |
218 | } | 222 | } |
219 | 223 | ||
220 | void Game :: setSeed( int seed ) | 224 | void Game :: setSeed( int seed ) |
221 | { | 225 | { |
222 | if ( seed == -1 ) | 226 | if ( seed == -1 ) |
223 | currentSeed = ((unsigned long) time((time_t *) NULL)); | 227 | currentSeed = ((unsigned long) time((time_t *) NULL)); |
224 | else | 228 | else |
225 | currentSeed = seed; | 229 | currentSeed = seed; |
226 | PutSeed( currentSeed ); | 230 | PutSeed( currentSeed ); |
227 | } | 231 | } |
228 | 232 | ||
229 | void Game :: saveReplay( string file ) | 233 | void Game :: saveReplay( string file ) |
230 | { | 234 | { |
231 | FILE *out; | 235 | FILE *out; |
232 | out = fopen( file.c_str(), "w" ); | 236 | out = fopen( file.c_str(), "w" ); |
233 | if ( !out ) | 237 | if ( !out ) |
234 | { | 238 | { |
235 | printf( "Couldn't write to /home/root/%s\n", file.c_str() ); | 239 | printf( "Couldn't write to /home/root/%s\n", file.c_str() ); |
236 | parent->setMenuStatusText( "Couldn't save replay file" ); | 240 | parent->setMenuStatusText( "Couldn't save replay file" ); |
237 | return; | 241 | return; |
238 | } | 242 | } |
239 | 243 | ||
240 | // Build up string of values | 244 | // Build up string of values |
241 | // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>....... | 245 | // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>....... |
242 | string val; | 246 | string val; |
243 | char tmp[20]; | 247 | char tmp[20]; |
244 | sprintf( tmp, "%d %d ", currentSeed, difficulty ); | 248 | sprintf( tmp, "%d %d ", currentSeed, difficulty ); |
245 | val = tmp; | 249 | val = tmp; |
246 | 250 | ||
247 | list<int>::iterator it = replayList.begin(); | 251 | list<int>::iterator it = replayList.begin(); |
248 | while( it != replayList.end() ) | 252 | while( it != replayList.end() ) |
249 | { | 253 | { |
250 | sprintf( tmp, "%d ", *it ); | 254 | sprintf( tmp, "%d ", *it ); |
251 | val += tmp; | 255 | val += tmp; |
252 | 256 | ||
253 | it++; | 257 | it++; |
254 | } | 258 | } |
255 | val += "\n"; | 259 | val += "\n"; |
256 | 260 | ||
257 | string line; | 261 | string line; |
258 | sprintf( tmp, "%d\n", val.length() ); | 262 | sprintf( tmp, "%d\n", val.length() ); |
259 | line = tmp; | 263 | line = tmp; |
260 | fwrite( line.c_str(), 1, line.length(), out ); | 264 | fwrite( line.c_str(), 1, line.length(), out ); |
261 | 265 | ||
262 | fwrite( val.c_str(), 1, val.length(), out ); | 266 | fwrite( val.c_str(), 1, val.length(), out ); |
263 | 267 | ||
264 | fclose( out ); | 268 | fclose( out ); |
265 | } | 269 | } |
266 | 270 | ||
267 | void Game :: loadReplay( string file ) | 271 | void Game :: loadReplay( string file ) |
268 | { | 272 | { |
269 | 273 | ||
270 | FILE *in = fopen( (const char *)file.c_str(), "r" ); | 274 | FILE *in = fopen( (const char *)file.c_str(), "r" ); |
271 | 275 | ||
272 | if ( in == 0 ) | 276 | if ( in == 0 ) |
273 | { | 277 | { |
274 | printf( "Couldn't load replay file %s!\n", (const char *)file.c_str() ); | 278 | printf( "Couldn't load replay file %s!\n", (const char *)file.c_str() ); |
275 | parent->setMenuStatusText( "Couldn't load replay file" ); | 279 | parent->setMenuStatusText( "Couldn't load replay file" ); |
276 | return; | 280 | return; |
277 | } | 281 | } |
278 | 282 | ||
279 | // Read next line - contains the size of the options | 283 | // Read next line - contains the size of the options |
280 | char line[10+1]; | 284 | char line[10+1]; |
281 | fgets( line, 10, in ); | 285 | fgets( line, 10, in ); |
282 | 286 | ||
283 | int length = -1; | 287 | int length = -1; |
284 | sscanf( line, "%d", &length ); | 288 | sscanf( line, "%d", &length ); |
285 | char *data = new char[length+1]; | 289 | char *data = new char[length+1]; |
286 | 290 | ||
287 | fread( data, 1, length, in ); | 291 | fread( data, 1, length, in ); |
288 | 292 | ||
289 | string sep = " "; | 293 | string sep = " "; |
290 | 294 | ||
291 | StringTokenizer st( data, sep ); | 295 | StringTokenizer st( data, sep ); |
292 | 296 | ||
293 | // print it out | 297 | // print it out |
294 | vector<string>::iterator it = st.begin(); | 298 | vector<string>::iterator it = st.begin(); |
295 | currentSeed = atoi( (*it).c_str() ); | 299 | currentSeed = atoi( (*it).c_str() ); |
296 | ++it; | 300 | ++it; |
297 | difficulty = atoi( (*it).c_str() ); | 301 | difficulty = atoi( (*it).c_str() ); |
298 | ++it; | 302 | ++it; |
299 | 303 | ||
300 | replayList.clear(); | 304 | replayList.clear(); |
301 | for ( ; it != st.end(); ++it ) | 305 | for ( ; it != st.end(); ++it ) |
302 | { | 306 | { |
303 | int v = atoi( (*it).c_str() ); | 307 | int v = atoi( (*it).c_str() ); |
304 | replayList.push_back( v ); | 308 | replayList.push_back( v ); |
305 | } | 309 | } |
306 | 310 | ||
307 | delete data; | 311 | delete data; |
308 | 312 | ||
309 | fclose( in ); | 313 | fclose( in ); |
310 | } | 314 | } |
311 | 315 | ||
312 | 316 | ||
313 | Game *Game :: createGame( SFCave *p, int w, int h, string game, string difficulty ) | 317 | Game *Game :: createGame( SFCave *p, int w, int h, string game, string difficulty ) |
314 | { | 318 | { |
315 | Game *g; | 319 | Game *g; |
316 | 320 | ||
317 | if ( game == "SFCave" ) | 321 | if ( game == "SFCave" ) |
318 | g = new SFCaveGame( p, w, h, 0 ); | 322 | g = new SFCaveGame( p, w, h, 0 ); |
319 | else if ( game == "Gates" ) | 323 | else if ( game == "Gates" ) |
320 | g = new GatesGame( p, w, h, 0 ); | 324 | g = new GatesGame( p, w, h, 0 ); |
321 | else if ( game == "Fly" ) | 325 | else if ( game == "Fly" ) |
322 | g = new FlyGame( p, w, h, 0 ); | 326 | g = new FlyGame( p, w, h, 0 ); |
323 | 327 | ||
324 | if ( g ) | 328 | if ( g ) |
325 | g->setDifficulty( difficulty ); | 329 | g->setDifficulty( difficulty ); |
326 | 330 | ||
327 | return g; | 331 | return g; |
328 | } | 332 | } |