summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/starfield.cpp
Unidiff
Diffstat (limited to 'noncore/games/sfcave-sdl/starfield.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave-sdl/starfield.cpp223
1 files changed, 223 insertions, 0 deletions
diff --git a/noncore/games/sfcave-sdl/starfield.cpp b/noncore/games/sfcave-sdl/starfield.cpp
new file mode 100644
index 0000000..c1f2d73
--- a/dev/null
+++ b/noncore/games/sfcave-sdl/starfield.cpp
@@ -0,0 +1,223 @@
1#include "SDL.h"
2#include "SDL_gfxPrimitives.h"
3
4#include <stdlib.h>
5
6#include "starfield.h"
7#include "random.h"
8#include "util.h"
9
10#define VERTICAL_VELOCITY 0
11
12StarField :: StarField( bool side, int nStars, int mx, int my, int minz, int maxz )
13{
14 nrStars = nStars;
15 maxX = mx;
16 maxY = my;
17 minZ = minz;
18 maxZ = maxz;
19
20 min_brightness = 50;
21 top_star_speed = 6;
22
23 sideways = side;
24
25 if ( !sideways )
26 {
27 x = new int[nrStars];
28 y = new int[nrStars];
29 z = new int[nrStars];
30
31 star_color = 0;
32 vel_x = 0;
33 vel_y = 0;
34 pos_x = 0;
35 pos_y = 0;
36 }
37 else
38 {
39 star_color = new int[nrStars];
40 vel_x = new int[nrStars];
41 vel_y = new int[nrStars];
42 pos_x = new int[nrStars];
43 pos_y = new int[nrStars];
44
45 x = 0;
46 y = 0;
47 z = 0;
48 }
49
50 init();
51}
52
53StarField :: ~StarField()
54{
55 if ( star_color )
56 delete []star_color;
57 if ( vel_x )
58 delete []vel_x;
59 if ( vel_y )
60 delete []vel_y;
61 if ( pos_x )
62 delete []pos_x;
63 if ( pos_y )
64 delete []pos_y;
65
66 if ( x )
67 delete []x;
68 if ( y )
69 delete []y;
70 if ( z )
71 delete []z;
72}
73
74void StarField :: init()
75{
76 if ( !sideways )
77 {
78 for ( int i = 0 ; i < nrStars ; ++i )
79 {
80 newStar( i );
81 z[i] = (int)(Random() * (double)(maxZ - minZ)) + minZ;
82 }
83 }
84 else
85 {
86 int brightness;
87
88 //Initialise each star
89 for(int i = 0; i < nrStars ; i++)
90 {
91 //Inialise velocities
92 vel_x[i] = -(int)floor( (Random() * top_star_speed)+1 );
93 vel_y[i] = VERTICAL_VELOCITY;
94
95 //Initialise positions randomly
96 pos_x[i] = (int)floor((Random() * 240));
97 pos_y[i] = (int)floor((Random() * 320));
98
99 //The Faster it goes, the Dimmer it is
100 if (vel_x[i] != 0)
101 {
102 brightness = (int)(255 / fabs(vel_x[i]) );
103 if (brightness < min_brightness)
104 brightness = min_brightness;
105 }
106 else
107 brightness = 255;
108
109 star_color[i] = brightness;
110 }
111 }
112}
113
114void StarField :: newStar( int starNr )
115{
116 if ( !sideways )
117 {
118 x[starNr] = (int)(Random() * (double)maxX) + 1;
119 y[starNr] = (int)(Random() * (double)maxY) + 1;
120 z[starNr] = maxZ;
121
122 int i = (int)(Random() * 4.0);
123 if(i < 2)
124 x[starNr] = -x[starNr];
125 if(i == 0 || i == 2)
126 y[starNr] = -y[starNr];
127 }
128}
129
130void StarField :: move( )
131{
132 if ( !sideways )
133 {
134 int amountToMove = 16;
135 for(int i = 0; i < nrStars; i++)
136 {
137 // Rotate star
138 z[i] = z[i] - amountToMove;
139 if(z[i] < minZ)
140 newStar(i);
141 }
142 }
143 else
144 {
145 for(int i = 0; i < nrStars ; i++)
146 {
147 //Check speed limits x
148 if (vel_x[i] > top_star_speed)
149 vel_x[i] = top_star_speed;
150 else if (vel_x[i] < -top_star_speed)
151 vel_x[i] = -top_star_speed;
152
153 //Check speed limits y
154 if (vel_y[i] > top_star_speed)
155 vel_y[i] = top_star_speed;
156 else if (vel_y[i] < -top_star_speed)
157 vel_y[i] = -top_star_speed;
158
159
160
161 //Move Star
162 pos_x[i] += vel_x[i];
163 pos_y[i] += vel_y[i];
164
165 if (pos_x[i] < 0)
166 pos_x[i] = pos_x[i] + 240;
167
168 if (pos_x[i] > 240)
169 pos_x[i] = pos_x[i] - 240;
170 if (pos_y[i] < 0)
171 pos_y[i] = pos_y[i] + 320;
172
173 if (pos_y[i] > 320)
174 pos_y[i] = pos_y[i] - 320;
175 }
176 }
177}
178
179void StarField :: draw( SDL_Surface *screen, int w, int h )
180{
181 if ( !sideways )
182 {
183 int scrW = w / 2;
184 int scrH = h / 2;
185 for(int i = 0; i < nrStars; i++)
186 {
187 int sx = (x[i] * 256) / z[i] + scrW;
188 int sy = (y[i] * 256) / z[i] + scrH;
189 if(sx < 0 || sx > w || sy < 0 || sy > h)
190 {
191 newStar(i);
192 }
193 else
194 {
195 int size = (z[i] * 3) / maxZ;
196
197 SDL_Rect r;
198 r.x = sx;
199 r.y = sy;
200 r.w = 3 - size;
201 r.h = 3 - size;
202
203 SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 255, 255, 255 ) );
204 }
205 }
206 }
207 else
208 {
209 SDL_LockSurface( screen );
210 for(int i = 0; i < nrStars ; i++)
211 {
212
213 Uint32 c = getpixel( screen, pos_x[i], pos_y[i] );
214
215
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 );
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 ***
220 }
221 SDL_UnlockSurface( screen );
222 }
223}