Diffstat (limited to 'noncore/games/sfcave-sdl/starfield.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/sfcave-sdl/starfield.cpp | 74 |
1 files changed, 73 insertions, 1 deletions
diff --git a/noncore/games/sfcave-sdl/starfield.cpp b/noncore/games/sfcave-sdl/starfield.cpp index c1f2d73..82edfc1 100644 --- a/noncore/games/sfcave-sdl/starfield.cpp +++ b/noncore/games/sfcave-sdl/starfield.cpp | |||
@@ -152,25 +152,25 @@ void StarField :: move( ) | |||
152 | 152 | ||
153 | //Check speed limits y | 153 | //Check speed limits y |
154 | if (vel_y[i] > top_star_speed) | 154 | if (vel_y[i] > top_star_speed) |
155 | vel_y[i] = top_star_speed; | 155 | vel_y[i] = top_star_speed; |
156 | else if (vel_y[i] < -top_star_speed) | 156 | else if (vel_y[i] < -top_star_speed) |
157 | vel_y[i] = -top_star_speed; | 157 | vel_y[i] = -top_star_speed; |
158 | 158 | ||
159 | 159 | ||
160 | 160 | ||
161 | //Move Star | 161 | //Move Star |
162 | pos_x[i] += vel_x[i]; | 162 | pos_x[i] += vel_x[i]; |
163 | pos_y[i] += vel_y[i]; | 163 | pos_y[i] += vel_y[i]; |
164 | 164 | ||
165 | if (pos_x[i] < 0) | 165 | if (pos_x[i] < 0) |
166 | pos_x[i] = pos_x[i] + 240; | 166 | pos_x[i] = pos_x[i] + 240; |
167 | 167 | ||
168 | if (pos_x[i] > 240) | 168 | if (pos_x[i] > 240) |
169 | pos_x[i] = pos_x[i] - 240; | 169 | pos_x[i] = pos_x[i] - 240; |
170 | if (pos_y[i] < 0) | 170 | if (pos_y[i] < 0) |
171 | pos_y[i] = pos_y[i] + 320; | 171 | pos_y[i] = pos_y[i] + 320; |
172 | 172 | ||
173 | if (pos_y[i] > 320) | 173 | if (pos_y[i] > 320) |
174 | pos_y[i] = pos_y[i] - 320; | 174 | pos_y[i] = pos_y[i] - 320; |
175 | } | 175 | } |
176 | } | 176 | } |
@@ -212,12 +212,84 @@ void StarField :: draw( SDL_Surface *screen, int w, int h ) | |||
212 | 212 | ||
213 | Uint32 c = getpixel( screen, pos_x[i], pos_y[i] ); | 213 | Uint32 c = getpixel( screen, pos_x[i], pos_y[i] ); |
214 | 214 | ||
215 | 215 | ||
216 | if ( c == 0 ) | 216 | if ( c == 0 ) |
217 | lineRGBA( screen, pos_x[i], pos_y[i], pos_x [i]+ vel_x[i], pos_y[i] + vel_y[i], star_color[i], star_color[i], star_color[i], 255 ); | 217 | lineRGBA( screen, pos_x[i], pos_y[i], pos_x [i]+ vel_x[i], pos_y[i] + vel_y[i], star_color[i], star_color[i], star_color[i], 255 ); |
218 | 218 | ||
219 | //*** NOTE : if the velocity of the stars never changes then the values such as 'pos_x[i] + vel_x[i]' could be precalculated for each star *** | 219 | //*** NOTE : if the velocity of the stars never changes then the values such as 'pos_x[i] + vel_x[i]' could be precalculated for each star *** |
220 | } | 220 | } |
221 | SDL_UnlockSurface( screen ); | 221 | SDL_UnlockSurface( screen ); |
222 | } | 222 | } |
223 | } | 223 | } |
224 | |||
225 | |||
226 | |||
227 | // Test | ||
228 | #ifdef DEBUG_STARS | ||
229 | SDL_Surface *screen; | ||
230 | StarField *stars; | ||
231 | |||
232 | void go() | ||
233 | { | ||
234 | /* Initialize SDL */ | ||
235 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) | ||
236 | { | ||
237 | fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | ||
238 | exit(1); | ||
239 | } | ||
240 | atexit(SDL_Quit); | ||
241 | |||
242 | int videoflags = SDL_SWSURFACE ; | ||
243 | |||
244 | if ( (screen=SDL_SetVideoMode(240, 320,32,videoflags)) == NULL ) | ||
245 | { | ||
246 | fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",240,320,SDL_GetError()); | ||
247 | exit(2); | ||
248 | } | ||
249 | |||
250 | stars = new StarField( false, 200 ); | ||
251 | |||
252 | bool done = false; | ||
253 | while ( !done ) | ||
254 | { | ||
255 | SDL_FillRect( screen, 0, 0 ); | ||
256 | stars->draw( screen ); | ||
257 | stars->move( ); | ||
258 | |||
259 | SDL_Flip( screen ); | ||
260 | |||
261 | SDL_Delay( 10 ); | ||
262 | |||
263 | SDL_Event event; | ||
264 | while ( SDL_PollEvent(&event) ) | ||
265 | { | ||
266 | switch (event.type) | ||
267 | { | ||
268 | case SDL_KEYDOWN: | ||
269 | // Escape keypress quits the app | ||
270 | if ( event.key.keysym.sym != SDLK_ESCAPE ) | ||
271 | { | ||
272 | break; | ||
273 | } | ||
274 | case SDL_QUIT: | ||
275 | done = 1; | ||
276 | break; | ||
277 | default: | ||
278 | break; | ||
279 | } | ||
280 | } | ||
281 | } | ||
282 | } | ||
283 | |||
284 | |||
285 | |||
286 | |||
287 | #ifdef __cplusplus | ||
288 | extern "C" | ||
289 | #endif | ||
290 | int main( int argc, char *argv[] ) | ||
291 | { | ||
292 | go(); | ||
293 | } | ||
294 | |||
295 | #endif | ||