summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/starfield.cpp
authorsudonix <sudonix>2004-02-26 02:25:15 (UTC)
committer sudonix <sudonix>2004-02-26 02:25:15 (UTC)
commitb339031e14a607ff18e404e0395b1c2782b92fdc (patch) (unidiff)
treeecd65299976322166ee5dfb2c30f045dd542c1e1 /noncore/games/sfcave-sdl/starfield.cpp
parentcb72ff620022306e493421006b024b971449f864 (diff)
downloadopie-b339031e14a607ff18e404e0395b1c2782b92fdc.zip
opie-b339031e14a607ff18e404e0395b1c2782b92fdc.tar.gz
opie-b339031e14a607ff18e404e0395b1c2782b92fdc.tar.bz2
SDL includes corrected, CRs in some files removed, just for consistency :)
Diffstat (limited to 'noncore/games/sfcave-sdl/starfield.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave-sdl/starfield.cpp590
1 files changed, 295 insertions, 295 deletions
diff --git a/noncore/games/sfcave-sdl/starfield.cpp b/noncore/games/sfcave-sdl/starfield.cpp
index 82edfc1..3b26895 100644
--- a/noncore/games/sfcave-sdl/starfield.cpp
+++ b/noncore/games/sfcave-sdl/starfield.cpp
@@ -1,295 +1,295 @@
1#include "SDL.h" 1#include <SDL/SDL.h>
2#include "SDL_gfxPrimitives.h" 2#include <SDL/SDL_gfxPrimitives.h>
3 3
4#include <stdlib.h> 4#include <stdlib.h>
5 5
6#include "starfield.h" 6#include "starfield.h"
7#include "random.h" 7#include "random.h"
8#include "util.h" 8#include "util.h"
9 9
10#define VERTICAL_VELOCITY 0 10#define VERTICAL_VELOCITY 0
11 11
12StarField :: StarField( bool side, int nStars, int mx, int my, int minz, int maxz ) 12StarField :: StarField( bool side, int nStars, int mx, int my, int minz, int maxz )
13{ 13{
14 nrStars = nStars; 14 nrStars = nStars;
15 maxX = mx; 15 maxX = mx;
16 maxY = my; 16 maxY = my;
17 minZ = minz; 17 minZ = minz;
18 maxZ = maxz; 18 maxZ = maxz;
19 19
20 min_brightness = 50; 20 min_brightness = 50;
21 top_star_speed = 6; 21 top_star_speed = 6;
22 22
23 sideways = side; 23 sideways = side;
24 24
25 if ( !sideways ) 25 if ( !sideways )
26 { 26 {
27 x = new int[nrStars]; 27 x = new int[nrStars];
28 y = new int[nrStars]; 28 y = new int[nrStars];
29 z = new int[nrStars]; 29 z = new int[nrStars];
30 30
31 star_color = 0; 31 star_color = 0;
32 vel_x = 0; 32 vel_x = 0;
33 vel_y = 0; 33 vel_y = 0;
34 pos_x = 0; 34 pos_x = 0;
35 pos_y = 0; 35 pos_y = 0;
36 } 36 }
37 else 37 else
38 { 38 {
39 star_color = new int[nrStars]; 39 star_color = new int[nrStars];
40 vel_x = new int[nrStars]; 40 vel_x = new int[nrStars];
41 vel_y = new int[nrStars]; 41 vel_y = new int[nrStars];
42 pos_x = new int[nrStars]; 42 pos_x = new int[nrStars];
43 pos_y = new int[nrStars]; 43 pos_y = new int[nrStars];
44 44
45 x = 0; 45 x = 0;
46 y = 0; 46 y = 0;
47 z = 0; 47 z = 0;
48 } 48 }
49 49
50 init(); 50 init();
51} 51}
52 52
53StarField :: ~StarField() 53StarField :: ~StarField()
54{ 54{
55 if ( star_color ) 55 if ( star_color )
56 delete []star_color; 56 delete []star_color;
57 if ( vel_x ) 57 if ( vel_x )
58 delete []vel_x; 58 delete []vel_x;
59 if ( vel_y ) 59 if ( vel_y )
60 delete []vel_y; 60 delete []vel_y;
61 if ( pos_x ) 61 if ( pos_x )
62 delete []pos_x; 62 delete []pos_x;
63 if ( pos_y ) 63 if ( pos_y )
64 delete []pos_y; 64 delete []pos_y;
65 65
66 if ( x ) 66 if ( x )
67 delete []x; 67 delete []x;
68 if ( y ) 68 if ( y )
69 delete []y; 69 delete []y;
70 if ( z ) 70 if ( z )
71 delete []z; 71 delete []z;
72} 72}
73 73
74void StarField :: init() 74void StarField :: init()
75{ 75{
76 if ( !sideways ) 76 if ( !sideways )
77 { 77 {
78 for ( int i = 0 ; i < nrStars ; ++i ) 78 for ( int i = 0 ; i < nrStars ; ++i )
79 { 79 {
80 newStar( i ); 80 newStar( i );
81 z[i] = (int)(Random() * (double)(maxZ - minZ)) + minZ; 81 z[i] = (int)(Random() * (double)(maxZ - minZ)) + minZ;
82 } 82 }
83 } 83 }
84 else 84 else
85 { 85 {
86 int brightness; 86 int brightness;
87 87
88 //Initialise each star 88 //Initialise each star
89 for(int i = 0; i < nrStars ; i++) 89 for(int i = 0; i < nrStars ; i++)
90 { 90 {
91 //Inialise velocities 91 //Inialise velocities
92 vel_x[i] = -(int)floor( (Random() * top_star_speed)+1 ); 92 vel_x[i] = -(int)floor( (Random() * top_star_speed)+1 );
93 vel_y[i] = VERTICAL_VELOCITY; 93 vel_y[i] = VERTICAL_VELOCITY;
94 94
95 //Initialise positions randomly 95 //Initialise positions randomly
96 pos_x[i] = (int)floor((Random() * 240)); 96 pos_x[i] = (int)floor((Random() * 240));
97 pos_y[i] = (int)floor((Random() * 320)); 97 pos_y[i] = (int)floor((Random() * 320));
98 98
99 //The Faster it goes, the Dimmer it is 99 //The Faster it goes, the Dimmer it is
100 if (vel_x[i] != 0) 100 if (vel_x[i] != 0)
101 { 101 {
102 brightness = (int)(255 / fabs(vel_x[i]) ); 102 brightness = (int)(255 / fabs(vel_x[i]) );
103 if (brightness < min_brightness) 103 if (brightness < min_brightness)
104 brightness = min_brightness; 104 brightness = min_brightness;
105 } 105 }
106 else 106 else
107 brightness = 255; 107 brightness = 255;
108 108
109 star_color[i] = brightness; 109 star_color[i] = brightness;
110 } 110 }
111 } 111 }
112} 112}
113 113
114void StarField :: newStar( int starNr ) 114void StarField :: newStar( int starNr )
115{ 115{
116 if ( !sideways ) 116 if ( !sideways )
117 { 117 {
118 x[starNr] = (int)(Random() * (double)maxX) + 1; 118 x[starNr] = (int)(Random() * (double)maxX) + 1;
119 y[starNr] = (int)(Random() * (double)maxY) + 1; 119 y[starNr] = (int)(Random() * (double)maxY) + 1;
120 z[starNr] = maxZ; 120 z[starNr] = maxZ;
121 121
122 int i = (int)(Random() * 4.0); 122 int i = (int)(Random() * 4.0);
123 if(i < 2) 123 if(i < 2)
124 x[starNr] = -x[starNr]; 124 x[starNr] = -x[starNr];
125 if(i == 0 || i == 2) 125 if(i == 0 || i == 2)
126 y[starNr] = -y[starNr]; 126 y[starNr] = -y[starNr];
127 } 127 }
128} 128}
129 129
130void StarField :: move( ) 130void StarField :: move( )
131{ 131{
132 if ( !sideways ) 132 if ( !sideways )
133 { 133 {
134 int amountToMove = 16; 134 int amountToMove = 16;
135 for(int i = 0; i < nrStars; i++) 135 for(int i = 0; i < nrStars; i++)
136 { 136 {
137 // Rotate star 137 // Rotate star
138 z[i] = z[i] - amountToMove; 138 z[i] = z[i] - amountToMove;
139 if(z[i] < minZ) 139 if(z[i] < minZ)
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
179void StarField :: draw( SDL_Surface *screen, int w, int h ) 179void 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;
189 if(sx < 0 || sx > w || sy < 0 || sy > h) 189 if(sx < 0 || sx > w || sy < 0 || sy > h)
190 { 190 {
191 newStar(i); 191 newStar(i);
192 } 192 }
193 else 193 else
194 { 194 {
195 int size = (z[i] * 3) / maxZ; 195 int size = (z[i] * 3) / maxZ;
196 196
197 SDL_Rect r; 197 SDL_Rect r;
198 r.x = sx; 198 r.x = sx;
199 r.y = sy; 199 r.y = sy;
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 224
225 225
226 226
227// Test 227// Test
228#ifdef DEBUG_STARS 228#ifdef DEBUG_STARS
229SDL_Surface *screen; 229SDL_Surface *screen;
230StarField *stars; 230StarField *stars;
231 231
232void go() 232void go()
233{ 233{
234 /* Initialize SDL */ 234 /* Initialize SDL */
235 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) 235 if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
236 { 236 {
237 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 237 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
238 exit(1); 238 exit(1);
239 } 239 }
240 atexit(SDL_Quit); 240 atexit(SDL_Quit);
241 241
242 int videoflags = SDL_SWSURFACE ; 242 int videoflags = SDL_SWSURFACE ;
243 243
244 if ( (screen=SDL_SetVideoMode(240, 320,32,videoflags)) == NULL ) 244 if ( (screen=SDL_SetVideoMode(240, 320,32,videoflags)) == NULL )
245 { 245 {
246 fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",240,320,SDL_GetError()); 246 fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",240,320,SDL_GetError());
247 exit(2); 247 exit(2);
248 } 248 }
249 249
250 stars = new StarField( false, 200 ); 250 stars = new StarField( false, 200 );
251 251
252 bool done = false; 252 bool done = false;
253 while ( !done ) 253 while ( !done )
254 { 254 {
255 SDL_FillRect( screen, 0, 0 ); 255 SDL_FillRect( screen, 0, 0 );
256 stars->draw( screen ); 256 stars->draw( screen );
257 stars->move( ); 257 stars->move( );
258 258
259 SDL_Flip( screen ); 259 SDL_Flip( screen );
260 260
261 SDL_Delay( 10 ); 261 SDL_Delay( 10 );
262 262
263 SDL_Event event; 263 SDL_Event event;
264 while ( SDL_PollEvent(&event) ) 264 while ( SDL_PollEvent(&event) )
265 { 265 {
266 switch (event.type) 266 switch (event.type)
267 { 267 {
268 case SDL_KEYDOWN: 268 case SDL_KEYDOWN:
269 // Escape keypress quits the app 269 // Escape keypress quits the app
270 if ( event.key.keysym.sym != SDLK_ESCAPE ) 270 if ( event.key.keysym.sym != SDLK_ESCAPE )
271 { 271 {
272 break; 272 break;
273 } 273 }
274 case SDL_QUIT: 274 case SDL_QUIT:
275 done = 1; 275 done = 1;
276 break; 276 break;
277 default: 277 default:
278 break; 278 break;
279 } 279 }
280 } 280 }
281 } 281 }
282 } 282 }
283 283
284 284
285 285
286 286
287#ifdef __cplusplus 287#ifdef __cplusplus
288extern "C" 288extern "C"
289#endif 289#endif
290int main( int argc, char *argv[] ) 290int main( int argc, char *argv[] )
291{ 291{
292 go(); 292 go();
293} 293}
294 294
295#endif 295#endif