summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/starfield.cpp
blob: c1f2d730ad445a7a225b87c956d76e032c5300b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "SDL.h"
#include "SDL_gfxPrimitives.h"

#include <stdlib.h>

#include "starfield.h"
#include "random.h"
#include "util.h"

#define VERTICAL_VELOCITY 0

StarField :: StarField( bool side, int nStars, int mx, int my, int minz, int maxz )
{
    nrStars = nStars;
	maxX = mx;
	maxY = my;
	minZ = minz;
	maxZ = maxz;

	min_brightness = 50;
	top_star_speed = 6;

	sideways = side;

	if ( !sideways )
	{
		x = new int[nrStars];
		y = new int[nrStars];
		z = new int[nrStars];

		star_color = 0;
		vel_x = 0;
		vel_y = 0;
		pos_x = 0;
		pos_y = 0;
	}
	else
	{
		star_color = new int[nrStars];
		vel_x = new int[nrStars];
		vel_y = new int[nrStars];
		pos_x = new int[nrStars];
		pos_y = new int[nrStars];

		x = 0;
		y = 0;
		z = 0;
	}

	init();
}

StarField :: ~StarField()
{
	if ( star_color )
		delete []star_color;
	if ( vel_x )
		delete []vel_x;
	if ( vel_y )
		delete []vel_y;
	if ( pos_x )
		delete []pos_x;
	if ( pos_y )
		delete []pos_y;

	if ( x )
		delete []x;
	if ( y )
		delete []y;
	if ( z )
		delete []z;
}

void StarField :: init()
{
	if ( !sideways )
	{
		for ( int i = 0 ; i < nrStars ; ++i )
		{
			newStar( i );
			z[i] = (int)(Random() * (double)(maxZ - minZ)) + minZ	;
		}
	}
	else
	{
		int brightness;

		//Initialise each star
		for(int i = 0; i < nrStars ; i++)
		{
			//Inialise velocities
			vel_x[i] = -(int)floor( (Random() * top_star_speed)+1 );
			vel_y[i] = VERTICAL_VELOCITY;

			//Initialise positions randomly
			pos_x[i] = (int)floor((Random() * 240));
			pos_y[i] = (int)floor((Random() * 320));

			//The Faster it goes, the Dimmer it is
			if (vel_x[i] != 0)
			{
				brightness = (int)(255 / fabs(vel_x[i]) );
				if (brightness < min_brightness)
					brightness = min_brightness;
			}
			else
				brightness = 255;

			star_color[i] = brightness;
		}
	}
}

void StarField :: newStar( int starNr )
{
	if ( !sideways )
	{
		x[starNr] = (int)(Random() * (double)maxX) + 1;
		y[starNr] = (int)(Random() * (double)maxY) + 1;
		z[starNr] = maxZ;

		int i = (int)(Random() * 4.0);
		if(i < 2)
			x[starNr] = -x[starNr];
		if(i == 0 || i == 2)
			y[starNr] = -y[starNr];
	}
}

void StarField :: move( )
{
	if ( !sideways )
	{
		int amountToMove = 16;
		for(int i = 0; i < nrStars; i++)
		{
			// Rotate star
			z[i] = z[i] - amountToMove;
			if(z[i] < minZ)
				newStar(i);
		}
	}
	else
	{
		for(int i = 0; i < nrStars ; i++)
		{
			//Check speed limits x
			if (vel_x[i] > top_star_speed)
				vel_x[i] = top_star_speed;
			else if (vel_x[i] < -top_star_speed)
				vel_x[i] = -top_star_speed;

			//Check speed limits y
			if (vel_y[i] > top_star_speed)
				vel_y[i] = top_star_speed;
			else if (vel_y[i] < -top_star_speed)
				vel_y[i] = -top_star_speed;



			//Move Star
			pos_x[i] += vel_x[i];
			pos_y[i] += vel_y[i];
			
			if (pos_x[i] < 0)
				pos_x[i] = pos_x[i] + 240;

			if (pos_x[i] > 240)
				pos_x[i] = pos_x[i] - 240;
			if (pos_y[i] < 0)
				pos_y[i] = pos_y[i] + 320;

			if (pos_y[i] > 320)
				pos_y[i] = pos_y[i] - 320;
		}
	}
}

void StarField :: draw( SDL_Surface *screen, int w, int h )
{
	if ( !sideways )
	{
		int scrW = w / 2;
		int scrH = h / 2;
		for(int i = 0; i < nrStars; i++)
		{
			int sx = (x[i] * 256) / z[i] + scrW;
			int sy = (y[i] * 256) / z[i] + scrH;
			if(sx < 0 || sx > w || sy < 0 || sy > h)
			{
				newStar(i);
			}
			else
			{
				int size = (z[i] * 3) / maxZ;

				SDL_Rect r;
				r.x = sx;
				r.y = sy;
				r.w = 3 - size;
				r.h = 3 - size;

				SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 255, 255, 255 ) );
			}
		}
	}
	else
	{
		SDL_LockSurface( screen );
		for(int i = 0; i < nrStars ; i++)
		{

			Uint32 c = getpixel( screen, pos_x[i], pos_y[i] );


			if ( c == 0 )
				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 );

			//*** 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 ***
		}
		SDL_UnlockSurface( screen );
	}
}