author | andyq <andyq> | 2003-01-21 20:37:00 (UTC) |
---|---|---|
committer | andyq <andyq> | 2003-01-21 20:37:00 (UTC) |
commit | 0a6563fcc2f49857c581d9def24407a3a4ef526c (patch) (unidiff) | |
tree | f1b82a4bd7582ef2cb722cffb87eecff1e1f96e6 /noncore/games/sfcave-sdl/starfield.cpp | |
parent | 50b5915b48fc5cbacf23e4d2b75d7a266f141a4a (diff) | |
download | opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.zip opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.tar.gz opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.tar.bz2 |
Clean up of code - fixed memory leaks (most of them) and added new custom config menu
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 | |||
@@ -140,49 +140,49 @@ void StarField :: move( ) | |||
140 | newStar(i); | 140 | newStar(i); |
141 | } | 141 | } |
142 | } | 142 | } |
143 | else | 143 | else |
144 | { | 144 | { |
145 | for(int i = 0; i < nrStars ; i++) | 145 | for(int i = 0; i < nrStars ; i++) |
146 | { | 146 | { |
147 | //Check speed limits x | 147 | //Check speed limits x |
148 | if (vel_x[i] > top_star_speed) | 148 | if (vel_x[i] > top_star_speed) |
149 | vel_x[i] = top_star_speed; | 149 | vel_x[i] = top_star_speed; |
150 | else if (vel_x[i] < -top_star_speed) | 150 | else if (vel_x[i] < -top_star_speed) |
151 | vel_x[i] = -top_star_speed; | 151 | vel_x[i] = -top_star_speed; |
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 | } |
177 | } | 177 | } |
178 | 178 | ||
179 | void StarField :: draw( SDL_Surface *screen, int w, int h ) | 179 | void StarField :: draw( SDL_Surface *screen, int w, int h ) |
180 | { | 180 | { |
181 | if ( !sideways ) | 181 | if ( !sideways ) |
182 | { | 182 | { |
183 | int scrW = w / 2; | 183 | int scrW = w / 2; |
184 | int scrH = h / 2; | 184 | int scrH = h / 2; |
185 | for(int i = 0; i < nrStars; i++) | 185 | for(int i = 0; i < nrStars; i++) |
186 | { | 186 | { |
187 | int sx = (x[i] * 256) / z[i] + scrW; | 187 | int sx = (x[i] * 256) / z[i] + scrW; |
188 | int sy = (y[i] * 256) / z[i] + scrH; | 188 | int sy = (y[i] * 256) / z[i] + scrH; |
@@ -200,24 +200,96 @@ void StarField :: draw( SDL_Surface *screen, int w, int h ) | |||
200 | r.w = 3 - size; | 200 | r.w = 3 - size; |
201 | r.h = 3 - size; | 201 | r.h = 3 - size; |
202 | 202 | ||
203 | SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 255, 255, 255 ) ); | 203 | SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 255, 255, 255 ) ); |
204 | } | 204 | } |
205 | } | 205 | } |
206 | } | 206 | } |
207 | else | 207 | else |
208 | { | 208 | { |
209 | SDL_LockSurface( screen ); | 209 | SDL_LockSurface( screen ); |
210 | for(int i = 0; i < nrStars ; i++) | 210 | for(int i = 0; i < nrStars ; i++) |
211 | { | 211 | { |
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 | ||