Diffstat (limited to 'noncore/games/sfcave-sdl/terrain.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/sfcave-sdl/terrain.cpp | 313 |
1 files changed, 313 insertions, 0 deletions
diff --git a/noncore/games/sfcave-sdl/terrain.cpp b/noncore/games/sfcave-sdl/terrain.cpp new file mode 100644 index 0000000..c001a56 --- a/dev/null +++ b/noncore/games/sfcave-sdl/terrain.cpp | |||
@@ -0,0 +1,313 @@ | |||
1 | #include "SDL.h" | ||
2 | #include "SDL_rotozoom.h" | ||
3 | #include "SDL_gfxPrimitives.h" | ||
4 | |||
5 | #include "constants.h" | ||
6 | #include "terrain.h" | ||
7 | #include "random.h" | ||
8 | #include "util.h" | ||
9 | #include "starfield.h" | ||
10 | |||
11 | Terrain :: Terrain( int w, int h, bool drawTop, bool drawBottom ) | ||
12 | { | ||
13 | sWidth = w; | ||
14 | sHeight = h; | ||
15 | speed = 1; | ||
16 | segSize = sWidth/(MAPSIZE-2)+1; | ||
17 | this->drawTop = drawTop; | ||
18 | this->drawBottom = drawBottom; | ||
19 | |||
20 | stars = new StarField( true ); | ||
21 | |||
22 | SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, sWidth + 20, sHeight, 32, | ||
23 | 0x000000ff,0x0000ff00, 0x00ff0000, 0xff000000); | ||
24 | terrainSurface = SDL_DisplayFormat( tmp ); | ||
25 | SDL_FreeSurface( tmp ); | ||
26 | |||
27 | initTerrain(); | ||
28 | |||
29 | } | ||
30 | |||
31 | Terrain :: ~Terrain() | ||
32 | { | ||
33 | SDL_FreeSurface( terrainSurface ); | ||
34 | delete stars; | ||
35 | } | ||
36 | |||
37 | void Terrain :: initTerrain() | ||
38 | { | ||
39 | dir = 1; | ||
40 | offset = 0; | ||
41 | |||
42 | maxHeight = 50; | ||
43 | |||
44 | mapTop[0] = (int)(nextInt(50)) + 5; | ||
45 | mapBottom[0] = sHeight - (maxHeight - mapTop[0]); | ||
46 | for ( int i = 1 ; i < MAPSIZE ; ++i ) | ||
47 | setPoint( i ); | ||
48 | |||
49 | SDL_FillRect( terrainSurface, 0, 0 ); | ||
50 | // Draw Terrain into surface | ||
51 | Sint16 px[5]; | ||
52 | Sint16 py[5]; | ||
53 | |||
54 | for ( int i = 0 ; i < MAPSIZE ; ++i ) | ||
55 | { | ||
56 | int left = (i*segSize); | ||
57 | int right = ((i+1)*segSize); | ||
58 | px[0] = left; | ||
59 | py[0] = mapTop[i]; | ||
60 | px[1] = right; | ||
61 | py[1] = mapTop[i+1]; | ||
62 | px[2] = right; | ||
63 | py[2] = 0; | ||
64 | px[3] = left; | ||
65 | py[3] = 0; | ||
66 | |||
67 | if ( drawTop ) | ||
68 | { | ||
69 | // Only display top landscape if not running FLY_GAME | ||
70 | filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 ); | ||
71 | } | ||
72 | |||
73 | if ( drawBottom ) | ||
74 | { | ||
75 | py[0] = mapBottom[i]; | ||
76 | py[1] = mapBottom[i+1]; | ||
77 | py[2] = sHeight; | ||
78 | py[3] = sHeight; | ||
79 | filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 ); | ||
80 | } | ||
81 | |||
82 | } | ||
83 | |||
84 | |||
85 | } | ||
86 | |||
87 | void Terrain :: moveTerrain( int amountToMove ) | ||
88 | { | ||
89 | stars->move(); | ||
90 | |||
91 | offset += amountToMove; | ||
92 | speed = offset/segSize; | ||
93 | |||
94 | //printf( "offset - %d, speed - %d\n", offset, speed ); | ||
95 | if ( offset >= segSize ) | ||
96 | { | ||
97 | for ( int i = 0 ; i < (MAPSIZE)-speed ; ++i ) | ||
98 | { | ||
99 | mapTop[i] = mapTop[i+speed]; | ||
100 | mapBottom[i] = mapBottom[i+speed]; | ||
101 | } | ||
102 | |||
103 | for ( int i = (MAPSIZE)-speed ; i < MAPSIZE ; ++i ) | ||
104 | { | ||
105 | setPoint( i ); | ||
106 | } | ||
107 | |||
108 | SDL_Rect dst; | ||
109 | dst.x = offset; | ||
110 | dst.y = 0; | ||
111 | dst.w = sWidth; | ||
112 | dst.h = sHeight; | ||
113 | |||
114 | SDL_Rect dst2; | ||
115 | dst2.x = 0; | ||
116 | dst2.y = 0; | ||
117 | |||
118 | SDL_BlitSurface(terrainSurface, &dst, terrainSurface, &dst2 ); | ||
119 | |||
120 | dst.x = sWidth-5; | ||
121 | dst.y = 0; | ||
122 | dst.w = offset; | ||
123 | dst.h = sHeight; | ||
124 | SDL_FillRect( terrainSurface, &dst, 0 ); | ||
125 | for ( int i = (MAPSIZE-1) - speed -1; i < MAPSIZE-1 ; ++i ) | ||
126 | { | ||
127 | Sint16 px[4]; | ||
128 | Sint16 py[5]; | ||
129 | int left = ((i-1)*segSize); | ||
130 | int right = ((i)*segSize); | ||
131 | // printf( "left = %d, right = %d\n", left, right ); | ||
132 | |||
133 | px[0] = left; | ||
134 | py[0] = mapTop[i]; | ||
135 | px[1] = right; | ||
136 | py[1] = mapTop[i+1]; | ||
137 | px[2] = right; | ||
138 | py[2] = 0; | ||
139 | px[3] = left; | ||
140 | py[3] = 0; | ||
141 | |||
142 | if ( drawTop ) | ||
143 | { | ||
144 | // Only display top landscape if not running FLY_GAME | ||
145 | filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 ); | ||
146 | } | ||
147 | |||
148 | if ( drawBottom ) | ||
149 | { | ||
150 | py[0] = mapBottom[i]; | ||
151 | py[1] = mapBottom[i+1]; | ||
152 | py[2] = sHeight; | ||
153 | py[3] = sHeight; | ||
154 | filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 ); | ||
155 | } | ||
156 | } | ||
157 | |||
158 | offset -= speed*segSize; | ||
159 | |||
160 | } | ||
161 | |||
162 | } | ||
163 | |||
164 | void Terrain :: setPoint( int point ) | ||
165 | { | ||
166 | if ( nextInt( 100 ) >= 80 ) | ||
167 | dir *= -1; | ||
168 | |||
169 | mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 )); | ||
170 | if ( mapTop[point] < 0 ) | ||
171 | { | ||
172 | mapTop[point] = 0; | ||
173 | dir *= -1; | ||
174 | } | ||
175 | else if ( mapTop[point] >= maxHeight ) | ||
176 | { | ||
177 | mapTop[point] = maxHeight; | ||
178 | dir *= -1; | ||
179 | } | ||
180 | |||
181 | mapBottom[point] = sHeight - (maxHeight - mapTop[point]); | ||
182 | } | ||
183 | |||
184 | void Terrain :: increaseMaxHeight( int amount ) | ||
185 | { | ||
186 | maxHeight += amount; | ||
187 | } | ||
188 | |||
189 | void Terrain :: drawTerrain( SDL_Surface *screen ) | ||
190 | { | ||
191 | // Blit terrain surface onto screen | ||
192 | |||
193 | SDL_Rect dst; | ||
194 | dst.x = offset; | ||
195 | dst.y = 0; | ||
196 | dst.w = sWidth; | ||
197 | dst.h = sHeight; | ||
198 | //dst.h = maxHeight; | ||
199 | |||
200 | SDL_Rect dst2; | ||
201 | dst2.x = 0; | ||
202 | dst2.y = 0; | ||
203 | |||
204 | SDL_BlitSurface(terrainSurface, &dst, screen, &dst2 ); | ||
205 | |||
206 | stars->draw( screen ); | ||
207 | |||
208 | //dst.y = sHeight - maxHeight; | ||
209 | //dst2.y = sHeight - maxHeight; | ||
210 | //SDL_BlitSurface(terrainSurface, &dst, screen, &dst2 ); | ||
211 | |||
212 | /* | ||
213 | for ( int i = 0 ; i < MAPSIZE ; ++i ) | ||
214 | { | ||
215 | int x1 = (i*segSize) - (offset*speed); | ||
216 | int x2 = ((i+1)*segSize)-(offset*speed); | ||
217 | if ( x2 >= sWidth ) | ||
218 | x2 = sWidth-1; | ||
219 | aalineRGBA( screen, x1, mapTop[i], x2, mapTop[i+1], 0, 220, 0, 255 ); | ||
220 | aalineRGBA( screen, x1, mapBottom[i], x2, mapBottom[i+1], 0, 220, 0, 255 ); | ||
221 | } | ||
222 | */ | ||
223 | } | ||
224 | |||
225 | bool Terrain :: checkCollision( int x, int y, int h ) | ||
226 | { | ||
227 | if ( y < 0 || y > sHeight ) | ||
228 | return true; | ||
229 | // First get segment that matches x | ||
230 | SDL_LockSurface( terrainSurface ); | ||
231 | |||
232 | Uint32 c = getpixel( terrainSurface, x, y ); | ||
233 | |||
234 | SDL_UnlockSurface( terrainSurface ); | ||
235 | |||
236 | if ( c == 0 ) | ||
237 | return false; | ||
238 | else | ||
239 | return true; | ||
240 | } | ||
241 | |||
242 | |||
243 | // Test | ||
244 | #ifdef DEBUG_TERRAIN | ||
245 | SDL_Surface *screen; | ||
246 | Terrain *terrain; | ||
247 | |||
248 | void go() | ||
249 | { | ||
250 | /* Initialize SDL */ | ||
251 | if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) | ||
252 | { | ||
253 | fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | ||
254 | exit(1); | ||
255 | } | ||
256 | atexit(SDL_Quit); | ||
257 | |||
258 | int videoflags = SDL_SWSURFACE ; | ||
259 | |||
260 | if ( (screen=SDL_SetVideoMode(280, 320,32,videoflags)) == NULL ) | ||
261 | { | ||
262 | fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",WIDTH,HEIGHT,SDL_GetError()); | ||
263 | exit(2); | ||
264 | } | ||
265 | |||
266 | terrain = new Terrain( 240, 320 ); | ||
267 | |||
268 | bool done = false; | ||
269 | while ( !done ) | ||
270 | { | ||
271 | SDL_FillRect( screen, 0, 0 ); | ||
272 | terrain->drawTerrain( screen ); | ||
273 | terrain->moveTerrain( 5 ); | ||
274 | |||
275 | SDL_Flip( screen ); | ||
276 | |||
277 | SDL_Delay( 5 ); | ||
278 | |||
279 | SDL_Event event; | ||
280 | while ( SDL_PollEvent(&event) ) | ||
281 | { | ||
282 | switch (event.type) | ||
283 | { | ||
284 | case SDL_KEYDOWN: | ||
285 | // Escape keypress quits the app | ||
286 | if ( event.key.keysym.sym != SDLK_ESCAPE ) | ||
287 | { | ||
288 | terrain->moveTerrain( 5 ); | ||
289 | break; | ||
290 | } | ||
291 | case SDL_QUIT: | ||
292 | done = 1; | ||
293 | break; | ||
294 | default: | ||
295 | break; | ||
296 | } | ||
297 | } | ||
298 | } | ||
299 | } | ||
300 | |||
301 | |||
302 | |||
303 | |||
304 | #ifdef __cplusplus | ||
305 | extern "C" | ||
306 | #endif | ||
307 | int main( int argc, char *argv[] ) | ||
308 | { | ||
309 | go(); | ||
310 | } | ||
311 | |||
312 | #endif | ||
313 | |||