summaryrefslogtreecommitdiff
authorandyq <andyq>2003-01-21 20:37:00 (UTC)
committer andyq <andyq>2003-01-21 20:37:00 (UTC)
commit0a6563fcc2f49857c581d9def24407a3a4ef526c (patch) (unidiff)
treef1b82a4bd7582ef2cb722cffb87eecff1e1f96e6
parent50b5915b48fc5cbacf23e4d2b75d7a266f141a4a (diff)
downloadopie-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 (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave-sdl/animatedimage.cpp8
-rw-r--r--noncore/games/sfcave-sdl/animatedimage.h2
-rw-r--r--noncore/games/sfcave-sdl/bfont.cpp7
-rw-r--r--noncore/games/sfcave-sdl/constants.h29
-rw-r--r--noncore/games/sfcave-sdl/fly_game.cpp13
-rw-r--r--noncore/games/sfcave-sdl/font.cpp24
-rw-r--r--noncore/games/sfcave-sdl/font.h2
-rw-r--r--noncore/games/sfcave-sdl/game.cpp50
-rw-r--r--noncore/games/sfcave-sdl/game.h17
-rw-r--r--noncore/games/sfcave-sdl/gates_game.cpp15
-rw-r--r--noncore/games/sfcave-sdl/help.cpp9
-rw-r--r--noncore/games/sfcave-sdl/menu.cpp43
-rw-r--r--noncore/games/sfcave-sdl/menu.h10
-rw-r--r--noncore/games/sfcave-sdl/player.cpp152
-rw-r--r--noncore/games/sfcave-sdl/player.h14
-rw-r--r--noncore/games/sfcave-sdl/settings.cpp98
-rw-r--r--noncore/games/sfcave-sdl/settings.h4
-rw-r--r--noncore/games/sfcave-sdl/sfcave.cpp527
-rw-r--r--noncore/games/sfcave-sdl/sfcave.h28
-rw-r--r--noncore/games/sfcave-sdl/sfcave_game.cpp14
-rw-r--r--noncore/games/sfcave-sdl/sound.cpp15
-rw-r--r--noncore/games/sfcave-sdl/starfield.cpp74
-rw-r--r--noncore/games/sfcave-sdl/terrain.cpp22
-rw-r--r--noncore/games/sfcave-sdl/util.cpp19
-rw-r--r--noncore/games/sfcave-sdl/util.h4
25 files changed, 791 insertions, 409 deletions
diff --git a/noncore/games/sfcave-sdl/animatedimage.cpp b/noncore/games/sfcave-sdl/animatedimage.cpp
index d9d6ff6..441c647 100644
--- a/noncore/games/sfcave-sdl/animatedimage.cpp
+++ b/noncore/games/sfcave-sdl/animatedimage.cpp
@@ -1,68 +1,68 @@
1#include "SDL.h" 1#include "SDL.h"
2#include "SDL_image.h" 2#include "SDL_image.h"
3 3
4#include "constants.h" 4#include "constants.h"
5#include "animatedimage.h" 5#include "animatedimage.h"
6 6
7AnimatedImage :: AnimatedImage( QString file, int nFrames ) 7AnimatedImage :: AnimatedImage( string file, int nFrames )
8{ 8{
9 nrFrames = nFrames; 9 nrFrames = nFrames;
10 currentFrame = 0; 10 currentFrame = 0;
11 11
12 // Load image 12 // Load image
13 image = IMG_Load( (const char *)file.c_str() ); 13 image = IMG_Load( (const char *)file.c_str() );
14 if ( !image ) 14 if ( !image )
15 { 15 {
16 nrFrames = 0; 16 nrFrames = 0;
17 image = 0; 17 image = 0;
18 return; 18 return;
19 } 19 }
20 20
21 SDL_SetColorKey(image, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( image->format, 0, 0, 0 ) ); 21 SDL_SetColorKey(image, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( image->format, 0, 0, 0 ) );
22 //image = SDL_DisplayFormat( tmp );
23
24 //SDL_FreeSurface( tmp );
25 frameWidth = image->w/nrFrames; 22 frameWidth = image->w/nrFrames;
26 frameHeight = image->h; 23 frameHeight = image->h;
27} 24}
28 25
29AnimatedImage :: ~AnimatedImage() 26AnimatedImage :: ~AnimatedImage()
30{ 27{
31 if ( image != 0 ) 28 if ( image != 0 )
32 SDL_FreeSurface( image ); 29 SDL_FreeSurface( image );
33} 30}
34 31
35bool AnimatedImage :: nextFrame() 32bool AnimatedImage :: nextFrame()
36{ 33{
37 bool rc = true; 34 bool rc = true;
38 currentFrame ++; 35 currentFrame ++;
39 if ( currentFrame >= nrFrames ) 36 if ( currentFrame >= nrFrames )
40 { 37 {
41 currentFrame --; 38 currentFrame --;
42 rc = false; 39 rc = false;
43 } 40 }
44 41
45 return rc; 42 return rc;
46} 43}
47 44
48void AnimatedImage :: draw( SDL_Surface *screen, int x, int y ) 45void AnimatedImage :: draw( SDL_Surface *screen, int x, int y )
49{ 46{
47 if ( !image )
48 return;
49
50 SDL_Rect dst; 50 SDL_Rect dst;
51 dst.x = currentFrame * frameWidth; 51 dst.x = currentFrame * frameWidth;
52 dst.y = 0; 52 dst.y = 0;
53 dst.w = frameWidth; 53 dst.w = frameWidth;
54 dst.h = frameHeight; 54 dst.h = frameHeight;
55 55
56 SDL_Rect dst2; 56 SDL_Rect dst2;
57 dst2.x = x - (frameWidth/2); 57 dst2.x = x - (frameWidth/2);
58 dst2.y = y - (frameHeight/2);; 58 dst2.y = y - (frameHeight/2);;
59 SDL_BlitSurface( image, &dst, screen, &dst2 ); 59 SDL_BlitSurface( image, &dst, screen, &dst2 );
60} 60}
61 61
62bool AnimatedImage :: AtEnd() 62bool AnimatedImage :: AtEnd()
63{ 63{
64 if ( currentFrame +1 >= nrFrames || image == 0 ) 64 if ( currentFrame +1 >= nrFrames || image == 0 )
65 return true; 65 return true;
66 return false; 66 return false;
67} 67}
68 68
diff --git a/noncore/games/sfcave-sdl/animatedimage.h b/noncore/games/sfcave-sdl/animatedimage.h
index 1b38e6d..3c03f52 100644
--- a/noncore/games/sfcave-sdl/animatedimage.h
+++ b/noncore/games/sfcave-sdl/animatedimage.h
@@ -1,25 +1,25 @@
1#ifndef __ANIMATED_IMAGE_H 1#ifndef __ANIMATED_IMAGE_H
2#define __ANIMATED_IMAGE_H 2#define __ANIMATED_IMAGE_H
3 3
4#include "SDL.h" 4#include "SDL.h"
5 5
6class AnimatedImage 6class AnimatedImage
7{ 7{
8public: 8public:
9 AnimatedImage( QString file, int nFrames ); 9 AnimatedImage( string file, int nFrames );
10 ~AnimatedImage(); 10 ~AnimatedImage();
11 11
12 bool nextFrame(); 12 bool nextFrame();
13 void draw( SDL_Surface *screen, int x, int y ); 13 void draw( SDL_Surface *screen, int x, int y );
14 bool AtEnd(); 14 bool AtEnd();
15 void reset() { currentFrame = 0; } 15 void reset() { currentFrame = 0; }
16private: 16private:
17 17
18 SDL_Surface *image; 18 SDL_Surface *image;
19 int nrFrames; 19 int nrFrames;
20 int currentFrame; 20 int currentFrame;
21 21
22 int frameWidth; 22 int frameWidth;
23 int frameHeight; 23 int frameHeight;
24}; 24};
25#endif 25#endif
diff --git a/noncore/games/sfcave-sdl/bfont.cpp b/noncore/games/sfcave-sdl/bfont.cpp
index 0f29104..7dec8f5 100644
--- a/noncore/games/sfcave-sdl/bfont.cpp
+++ b/noncore/games/sfcave-sdl/bfont.cpp
@@ -1,390 +1,385 @@
1/***********************************************************/ 1/***********************************************************/
2/* */ 2/* */
3/* BFONT.c v. 1.0.2 - Billi Font Library by Diego Billi */ 3/* BFONT.c v. 1.0.2 - Billi Font Library by Diego Billi */
4/* BFONT++ C++ port by Gianluigi Davassi */ 4/* BFONT++ C++ port by Gianluigi Davassi */
5/***********************************************************/ 5/***********************************************************/
6#include "iostream" 6#include "iostream"
7using namespace std; 7using namespace std;
8#include "string.h" 8#include "string.h"
9#include "stdlib.h" 9#include "stdlib.h"
10#include "stdarg.h" 10#include "stdarg.h"
11 11
12#include "SDL_image.h" 12#include "SDL_image.h"
13#include "bfont.h" 13#include "bfont.h"
14 14
15void BFont::InitFont() 15void BFont::InitFont()
16{ 16{
17 int x = 0, i = '!'; 17 int x = 0, i = '!';
18 Uint32 sentry = GetPixel(0,0); 18 Uint32 sentry = GetPixel(0,0);
19 19
20 if (SDL_MUSTLOCK(Surface)) 20 if (SDL_MUSTLOCK(Surface))
21 SDL_LockSurface(Surface); 21 SDL_LockSurface(Surface);
22 22
23 while ( x < (Surface->w-1)) 23 while ( x < (Surface->w-1))
24 { 24 {
25 if(GetPixel(x,0) != sentry) 25 if(GetPixel(x,0) != sentry)
26 { 26 {
27 Chars[i].x = x; 27 Chars[i].x = x;
28 Chars[i].y = 1; 28 Chars[i].y = 1;
29 Chars[i].h = Surface->h; 29 Chars[i].h = Surface->h;
30 for (; GetPixel(x, 0) != sentry && x < (Surface->w); ++x) ; 30 for (; GetPixel(x, 0) != sentry && x < (Surface->w); ++x) ;
31 Chars[i].w = (x - Chars[i].x); 31 Chars[i].w = (x - Chars[i].x);
32 i++; 32 i++;
33 } else 33 } else
34 { x++; } 34 { x++; }
35 } 35 }
36 Chars[' '].x = 0; 36 Chars[' '].x = 0;
37 Chars[' '].y = 0; 37 Chars[' '].y = 0;
38 Chars[' '].h = Surface->h; 38 Chars[' '].h = Surface->h;
39 Chars[' '].w = Chars['!'].w; 39 Chars[' '].w = Chars['!'].w;
40 40
41 if (SDL_MUSTLOCK(Surface)) 41 if (SDL_MUSTLOCK(Surface))
42 SDL_UnlockSurface(Surface); 42 SDL_UnlockSurface(Surface);
43 43
44 h = Surface->h; 44 h = Surface->h;
45 45
46 SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, GetPixel(0, Surface->h-1)); 46 SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, GetPixel(0, Surface->h-1));
47} 47}
48 48
49 49
50/* Load the font and stores it in the BFont_Info structure */ 50/* Load the font and stores it in the BFont_Info structure */
51void BFont::LoadFont (const char *filename) 51void BFont::LoadFont (const char *filename)
52{ 52{
53 SDL_Surface *surface(NULL); 53 SDL_Surface *surface(NULL);
54 int x; 54 int x;
55 // tutta roba inutile in C++.... :-) 55 // tutta roba inutile in C++.... :-)
56 /* BFont_Info *Font=NULL; 56 /* BFont_Info *Font=NULL;
57 Font = (BFont_Info *) malloc(sizeof(BFont_Info));*/ 57 Font = (BFont_Info *) malloc(sizeof(BFont_Info));*/
58 58
59 if ((filename != NULL ) && (this != NULL)) 59 if ((filename != NULL ) && (this != NULL))
60 { 60 {
61 surface = IMG_Load( filename ); 61 surface = IMG_Load( filename );
62 62
63 if (surface != NULL) 63 if (surface != NULL)
64 { 64 {
65 Surface = surface; 65 Surface = surface;
66 for (x=0; x<256; x++) 66 for (x=0; x<256; x++)
67 { 67 {
68 Chars[x].x = 0; 68 Chars[x].x = 0;
69 Chars[x].y = 0; 69 Chars[x].y = 0;
70 Chars[x].h = 0; 70 Chars[x].h = 0;
71 Chars[x].w = 0; 71 Chars[x].w = 0;
72 } 72 }
73 InitFont(); // Init the font 73 InitFont(); // Init the font
74 } 74 }
75 } 75 }
76 else
77 {
78 cerr << "Error! The font has not been loaded!" << endl;
79 }
80} 76}
81 77
82BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b) 78BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b)
83{ 79{
84 int x,y; 80 int x,y;
85 81
86 BFont *newfont; 82 BFont *newfont;
87 SDL_Surface *surface = NULL; 83 SDL_Surface *surface = NULL;
88 84
89 Uint32 pixel; 85 Uint32 pixel;
90 Uint8 old_r, old_g, old_b; 86 Uint8 old_r, old_g, old_b;
91 Uint8 new_r, new_g, new_b; 87 Uint8 new_r, new_g, new_b;
92 Uint32 color_key; 88 Uint32 color_key;
93 89
94 newfont = new BFont(NULL); 90 newfont = new BFont(NULL);
95 91
96 if (newfont != NULL) { 92 if (newfont != NULL) {
97 93
98 newfont->h = h; 94 newfont->h = h;
99 95
100 for (x=0; x<256; x++) { 96 for (x=0; x<256; x++) {
101 newfont->Chars[x].x = Chars[x].x; 97 newfont->Chars[x].x = Chars[x].x;
102 newfont->Chars[x].y = Chars[x].y; 98 newfont->Chars[x].y = Chars[x].y;
103 newfont->Chars[x].h = Chars[x].h; 99 newfont->Chars[x].h = Chars[x].h;
104 newfont->Chars[x].w = Chars[x].w; 100 newfont->Chars[x].w = Chars[x].w;
105 } 101 }
106 102
107 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, Surface->w, Surface->h, 32, 103 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, Surface->w, Surface->h, 32,
108 0x000000ff,0x0000ff00, 0x00ff0000, 0xff000000); 104 0x000000ff,0x0000ff00, 0x00ff0000, 0xff000000);
109 if (surface != NULL) { 105 if (surface != NULL) {
110 106
111 if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface); 107 if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface);
112 if (SDL_MUSTLOCK(Surface)) SDL_LockSurface(Surface); 108 if (SDL_MUSTLOCK(Surface)) SDL_LockSurface(Surface);
113 109
114 color_key = xGetPixel(Surface, 0, Surface->h-1); 110 color_key = xGetPixel(Surface, 0, Surface->h-1);
115 111
116 printf("looking...\n");
117 for( x=0; x < Surface->w; x++) { 112 for( x=0; x < Surface->w; x++) {
118 for( y=0; y < Surface->h; y++) { 113 for( y=0; y < Surface->h; y++) {
119 old_r = old_g = old_b = 0; 114 old_r = old_g = old_b = 0;
120 pixel = xGetPixel(Surface,x,y); 115 pixel = xGetPixel(Surface,x,y);
121 116
122 if (pixel != color_key) { 117 if (pixel != color_key) {
123 SDL_GetRGB(pixel, Surface->format, &old_r,&old_g,&old_b); 118 SDL_GetRGB(pixel, Surface->format, &old_r,&old_g,&old_b);
124 119
125 new_r = (Uint8) ((old_r * r) / 255); 120 new_r = (Uint8) ((old_r * r) / 255);
126 new_g = (Uint8) ((old_g * g) / 255); 121 new_g = (Uint8) ((old_g * g) / 255);
127 new_b = (Uint8) ((old_b * b) / 255); 122 new_b = (Uint8) ((old_b * b) / 255);
128 123
129 pixel = SDL_MapRGB(surface->format,new_r,new_g,new_b); 124 pixel = SDL_MapRGB(surface->format,new_r,new_g,new_b);
130 } 125 }
131 PutPixel(surface,x,y,pixel); 126 PutPixel(surface,x,y,pixel);
132 } 127 }
133 } 128 }
134 printf("unlooking...\n"); 129
135 if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); 130 if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);
136 if (SDL_MUSTLOCK(Surface)) SDL_UnlockSurface(Surface); 131 if (SDL_MUSTLOCK(Surface)) SDL_UnlockSurface(Surface);
137 132
138 SDL_SetColorKey(surface, SDL_SRCCOLORKEY, color_key); 133 SDL_SetColorKey(surface, SDL_SRCCOLORKEY, color_key);
139 } 134 }
140 135
141 newfont->Surface = surface; 136 newfont->Surface = surface;
142 } 137 }
143 return newfont; 138 return newfont;
144} 139}
145 140
146 141
147/* Puts a single char on the surface with the specified font */ 142/* Puts a single char on the surface with the specified font */
148int BFont::PutChar(SDL_Surface *screen, int x, int y, char c) 143int BFont::PutChar(SDL_Surface *screen, int x, int y, char c)
149{ 144{
150 int r=0; 145 int r=0;
151 SDL_Rect dest; 146 SDL_Rect dest;
152 147
153 dest.w = CharWidth(' '); 148 dest.w = CharWidth(' ');
154 dest.h = FontHeight(); 149 dest.h = FontHeight();
155 dest.x = x; 150 dest.x = x;
156 dest.y = y; 151 dest.y = y;
157 152
158 if (c != ' ') 153 if (c != ' ')
159 SDL_BlitSurface( Surface, &Chars[c], screen, &dest); 154 SDL_BlitSurface( Surface, &Chars[c], screen, &dest);
160 155
161 r = dest.w; 156 r = dest.w;
162 return r; 157 return r;
163} 158}
164 159
165 160
166void BFont::PutString(SDL_Surface *screen, int x, int y, const char *text) 161void BFont::PutString(SDL_Surface *screen, int x, int y, const char *text)
167{ 162{
168 int i(0); 163 int i(0);
169 while (text[i]!='\0') 164 while (text[i]!='\0')
170 { 165 {
171 x += PutChar(screen,x,y,text[i]); 166 x += PutChar(screen,x,y,text[i]);
172 i++; 167 i++;
173 } 168 }
174} 169}
175 170
176int BFont::TextWidth(const char *text) 171int BFont::TextWidth(const char *text)
177{ 172{
178 int i(0),x(0); 173 int i(0),x(0);
179 174
180 while (text[i]!='\0') 175 while (text[i]!='\0')
181 { 176 {
182 x += CharWidth(text[i]); 177 x += CharWidth(text[i]);
183 i++; 178 i++;
184 } 179 }
185 return x; 180 return x;
186} 181}
187 182
188 183
189/* counts the spaces of the strings */ 184/* counts the spaces of the strings */
190int BFont::count (const char *text) 185int BFont::count (const char *text)
191{ 186{
192 char *p(NULL); 187 char *p(NULL);
193 int pos(-1),i(0); 188 int pos(-1),i(0);
194 189
195 /* Calculate the space occupied by the text without spaces */ 190 /* Calculate the space occupied by the text without spaces */
196 while ((p=strchr(&text[pos+1],' ')) != NULL) 191 while ((p=strchr(&text[pos+1],' ')) != NULL)
197 { 192 {
198 i++; 193 i++;
199 pos = p - text; 194 pos = p - text;
200 } 195 }
201 return i; 196 return i;
202} 197}
203 198
204 199
205void BFont::JustifiedPutString( SDL_Surface *screen, int y, const char *text) 200void BFont::JustifiedPutString( SDL_Surface *screen, int y, const char *text)
206{ 201{
207 int spaces(0),gap,single_gap,dif; 202 int spaces(0),gap,single_gap,dif;
208 char *strtmp,*p; 203 char *strtmp,*p;
209 int pos(-1),xpos(0); 204 int pos(-1),xpos(0);
210 205
211 206
212 if (strchr(text,' ') == NULL) 207 if (strchr(text,' ') == NULL)
213 { 208 {
214 PutString(screen, 0, y, text); 209 PutString(screen, 0, y, text);
215 } 210 }
216 else { 211 else {
217 gap = (screen->w-1) - TextWidth(text); 212 gap = (screen->w-1) - TextWidth(text);
218 213
219 if (gap <= 0) { 214 if (gap <= 0) {
220 PutString(screen, 0,y,text); 215 PutString(screen, 0,y,text);
221 } else { 216 } else {
222 spaces = count(text); 217 spaces = count(text);
223 dif = gap % spaces; 218 dif = gap % spaces;
224 single_gap = (gap - dif) / spaces; 219 single_gap = (gap - dif) / spaces;
225 xpos=0; 220 xpos=0;
226 pos = -1; 221 pos = -1;
227 while ( spaces > 0 ) 222 while ( spaces > 0 )
228 { 223 {
229 p = strstr(&text[pos+1]," "); 224 p = strstr(&text[pos+1]," ");
230 strtmp = NULL; 225 strtmp = NULL;
231 strtmp = (char *) calloc ( (p - &text[pos+1]) + 1,sizeof(char)); 226 strtmp = (char *) calloc ( (p - &text[pos+1]) + 1,sizeof(char));
232 if (strtmp != NULL) 227 if (strtmp != NULL)
233 { 228 {
234 strncpy (strtmp, &text[pos+1], (p - &text[pos+1])); 229 strncpy (strtmp, &text[pos+1], (p - &text[pos+1]));
235 PutString(screen, xpos, y, strtmp); 230 PutString(screen, xpos, y, strtmp);
236 xpos = xpos + TextWidth(strtmp) + single_gap + CharWidth(' '); 231 xpos = xpos + TextWidth(strtmp) + single_gap + CharWidth(' ');
237 if (dif >= 0) 232 if (dif >= 0)
238 { 233 {
239 xpos ++; 234 xpos ++;
240 dif--; 235 dif--;
241 } 236 }
242 pos = p - text; 237 pos = p - text;
243 spaces--; 238 spaces--;
244 free(strtmp); 239 free(strtmp);
245 } 240 }
246 } 241 }
247 strtmp = NULL; 242 strtmp = NULL;
248 strtmp = (char *) calloc ( strlen( &text[pos+1]) + 1,sizeof(char)); 243 strtmp = (char *) calloc ( strlen( &text[pos+1]) + 1,sizeof(char));
249 244
250 if (strtmp != NULL) { 245 if (strtmp != NULL) {
251 strncpy (strtmp, &text[pos+1], strlen( &text[pos+1])); 246 strncpy (strtmp, &text[pos+1], strlen( &text[pos+1]));
252 PutString(screen, xpos, y, strtmp); 247 PutString(screen, xpos, y, strtmp);
253 free(strtmp); 248 free(strtmp);
254 } 249 }
255 } 250 }
256 } 251 }
257} 252}
258 253
259 254
260void BFont::CenteredPutString(SDL_Surface *screen, int y, const char *text) 255void BFont::CenteredPutString(SDL_Surface *screen, int y, const char *text)
261{ 256{
262 printf( "xpos - %d, %d <%s>\n", screen->w/2-TextWidth(text)/2, TextWidth(text), text ); 257 printf( "xpos - %d, %d <%s>\n", screen->w/2-TextWidth(text)/2, TextWidth(text), text );
263 PutString( screen, screen->w/2-TextWidth(text)/2, y, text); 258 PutString( screen, screen->w/2-TextWidth(text)/2, y, text);
264} 259}
265 260
266 261
267void BFont::RightPutString(SDL_Surface *screen, int y, const char *text) 262void BFont::RightPutString(SDL_Surface *screen, int y, const char *text)
268{ 263{
269 PutString( screen, screen->w - TextWidth(text) - 1, y, text); 264 PutString( screen, screen->w - TextWidth(text) - 1, y, text);
270} 265}
271 266
272void BFont::LeftPutString(SDL_Surface *screen, int y, const char *text) 267void BFont::LeftPutString(SDL_Surface *screen, int y, const char *text)
273{ 268{
274 PutString( screen, 0, y, text); 269 PutString( screen, 0, y, text);
275} 270}
276 271
277/******/ 272/******/
278 273
279void BFont::PrintString (SDL_Surface *screen, int x, int y, char *fmt, ...) 274void BFont::PrintString (SDL_Surface *screen, int x, int y, char *fmt, ...)
280{ 275{
281 va_list args; 276 va_list args;
282 char *temp; 277 char *temp;
283 va_start (args,fmt); 278 va_start (args,fmt);
284 279
285 if ( (temp = (char *) malloc(1000+1)) != NULL) { 280 if ( (temp = (char *) malloc(1000+1)) != NULL) {
286 vsprintf(temp,fmt,args); 281 vsprintf(temp,fmt,args);
287 PutString(screen, x, y, temp); 282 PutString(screen, x, y, temp);
288 free (temp); 283 free (temp);
289 } 284 }
290 va_end(args); 285 va_end(args);
291} 286}
292 287
293void BFont::CenteredPrintString(SDL_Surface *screen, int y, char *fmt, ...) 288void BFont::CenteredPrintString(SDL_Surface *screen, int y, char *fmt, ...)
294{ 289{
295 va_list args; 290 va_list args;
296 char *temp; 291 char *temp;
297 va_start (args,fmt); 292 va_start (args,fmt);
298 293
299 if ( (temp = (char *) malloc(1000+1)) != NULL) { 294 if ( (temp = (char *) malloc(1000+1)) != NULL) {
300 vsprintf(temp,fmt,args); 295 vsprintf(temp,fmt,args);
301 CenteredPutString(screen, y, temp); 296 CenteredPutString(screen, y, temp);
302 free (temp); 297 free (temp);
303 } 298 }
304 va_end(args); 299 va_end(args);
305} 300}
306 301
307void BFont::RightPrintString(SDL_Surface *screen, int y, char *fmt, ...) 302void BFont::RightPrintString(SDL_Surface *screen, int y, char *fmt, ...)
308{ 303{
309 va_list args; 304 va_list args;
310 char *temp; 305 char *temp;
311 va_start (args,fmt); 306 va_start (args,fmt);
312 307
313 if ( (temp = (char *) malloc(1000+1)) != NULL) { 308 if ( (temp = (char *) malloc(1000+1)) != NULL) {
314 vsprintf(temp,fmt,args); 309 vsprintf(temp,fmt,args);
315 RightPutString(screen, y, temp); 310 RightPutString(screen, y, temp);
316 free (temp); 311 free (temp);
317 } 312 }
318 va_end(args); 313 va_end(args);
319} 314}
320 315
321void BFont::LeftPrintString( SDL_Surface *screen, int y, char *fmt, ...) 316void BFont::LeftPrintString( SDL_Surface *screen, int y, char *fmt, ...)
322{ 317{
323 va_list args; 318 va_list args;
324 char *temp; 319 char *temp;
325 va_start (args,fmt); 320 va_start (args,fmt);
326 321
327 if ( (temp = (char *) malloc(1000+1)) != NULL) { 322 if ( (temp = (char *) malloc(1000+1)) != NULL) {
328 vsprintf(temp,fmt,args); 323 vsprintf(temp,fmt,args);
329 LeftPutString(screen, y, temp); 324 LeftPutString(screen, y, temp);
330 free (temp); 325 free (temp);
331 } 326 }
332 va_end(args); 327 va_end(args);
333} 328}
334 329
335void BFont::JustifiedPrintString( SDL_Surface *screen, int y, char *fmt, ...) 330void BFont::JustifiedPrintString( SDL_Surface *screen, int y, char *fmt, ...)
336{ 331{
337 va_list args; 332 va_list args;
338 char *temp; 333 char *temp;
339 va_start (args,fmt); 334 va_start (args,fmt);
340 335
341 if ( (temp = (char *) malloc(1000+1)) != NULL) { 336 if ( (temp = (char *) malloc(1000+1)) != NULL) {
342 vsprintf(temp,fmt,args); 337 vsprintf(temp,fmt,args);
343 JustifiedPutString( screen, y,temp); 338 JustifiedPutString( screen, y,temp);
344 free (temp); 339 free (temp);
345 } 340 }
346 va_end(args); 341 va_end(args);
347} 342}
348 343
349 344
350void BFont::PutPixel(SDL_Surface *surface, int x, int y, Uint32 pixel) 345void BFont::PutPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
351{ 346{
352 int bpp = surface->format->BytesPerPixel; 347 int bpp = surface->format->BytesPerPixel;
353 /* Here p is the address to the pixel we want to set */ 348 /* Here p is the address to the pixel we want to set */
354 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; 349 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
355 350
356 switch(bpp) { 351 switch(bpp) {
357 case 1: 352 case 1:
358 *p = pixel; 353 *p = pixel;
359 break; 354 break;
360 355
361 case 2: 356 case 2:
362 *(Uint16 *)p = pixel; 357 *(Uint16 *)p = pixel;
363 break; 358 break;
364 359
365 case 3: 360 case 3:
366 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { 361 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
367 p[0] = (pixel >> 16) & 0xff; 362 p[0] = (pixel >> 16) & 0xff;
368 p[1] = (pixel >> 8) & 0xff; 363 p[1] = (pixel >> 8) & 0xff;
369 p[2] = pixel & 0xff; 364 p[2] = pixel & 0xff;
370 } else { 365 } else {
371 p[0] = pixel & 0xff; 366 p[0] = pixel & 0xff;
372 p[1] = (pixel >> 8) & 0xff; 367 p[1] = (pixel >> 8) & 0xff;
373 p[2] = (pixel >> 16) & 0xff; 368 p[2] = (pixel >> 16) & 0xff;
374 } 369 }
375 break; 370 break;
376 371
377 case 4: 372 case 4:
378 *(Uint32 *)p = pixel; 373 *(Uint32 *)p = pixel;
379 break; 374 break;
380 } 375 }
381} 376}
382 377
383Uint32 BFont :: xGetPixel(SDL_Surface *surface, int x, int y) 378Uint32 BFont :: xGetPixel(SDL_Surface *surface, int x, int y)
384{ 379{
385 int bpp = surface->format->BytesPerPixel; 380 int bpp = surface->format->BytesPerPixel;
386 /* Here p is the address to the pixel we want to retrieve */ 381 /* Here p is the address to the pixel we want to retrieve */
387 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; 382 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
388 383
389 switch(bpp) { 384 switch(bpp) {
390 case 1: 385 case 1:
diff --git a/noncore/games/sfcave-sdl/constants.h b/noncore/games/sfcave-sdl/constants.h
index 8fadae4..f10764e 100644
--- a/noncore/games/sfcave-sdl/constants.h
+++ b/noncore/games/sfcave-sdl/constants.h
@@ -1,67 +1,84 @@
1#ifndef __CONSTANTS_H 1#ifndef __CONSTANTS_H
2#define __CONSTANTS_H 2#define __CONSTANTS_H
3 3
4#include <string> 4#include <string>
5using namespace std; 5using namespace std;
6#define QString string
7 6
8#ifdef QWS 7#ifdef QWS
9#define IMAGES_PATH "/opt/QtPalmtop/pics/sfcave/data/" 8#define IMAGES_PATH "/opt/QtPalmtop/pics/sfcave/data/"
10#define SOUND_PATH "/opt/QtPalmtop/sounds/sfcave/" 9#define SOUND_PATH "/opt/QtPalmtop/sounds/sfcave/"
11#else 10#else
12#define IMAGES_PATH "./images/" 11#define IMAGES_PATH "./images/"
13#define SOUND_PATH "./sounds/" 12#define SOUND_PATH "./sounds/"
14#endif 13#endif
15 14
16// Width and height of app 15// Width and height of app
17 #define WIDTH240 16 #define WIDTH240
18 #define HEIGHT320 17 #define HEIGHT320
19 18
20//Number of map segments 19//Number of map segments
21#define MAPSIZE 51 20#define MAPSIZE 51
22 21
23// Maximum number of blocks visible on screen at any one time 22// Maximum number of blocks visible on screen at any one time
24#define BLOCKSIZE 6 23#define BLOCKSIZE 6
25 24
26// length of players trail 25// length of players trail
27#define TRAILSIZE 60 26#define TRAILSIZE 60
28 27
29// Game States 28// Game States
30 #define STATE_QUIT -1 29 #define STATE_QUIT -1
31 #define STATE_PLAYING 0 30 #define STATE_PLAYING 0
32 #define STATE_CRASHING 1 31 #define STATE_CRASHING 1
33 #define STATE_CRASHED 2 32 #define STATE_CRASHED 2
34 #define STATE_NEWGAME 3 33 #define STATE_NEWGAME 3
35 #define STATE_MENU 4 34 #define STATE_MENU 4
36 #define STATE_REPLAY 5 35 #define STATE_REPLAY 5
37#define STATE_HELP 6 36#define STATE_HELP 6
38 37
39// Menu Options 38// Menu Options
40 #define MENU_STARTGAME 1 39 #define MENU_STARTGAME 1
41 #define MENU_REPLAYS 2 40 #define MENU_REPLAYS 2
42 #define MENU_OPTIONS 3 41 #define MENU_OPTIONS 3
43 #define MENU_HELP 4 42 #define MENU_HELP 4
44 #define MENU_QUIT 5 43 #define MENU_QUIT 5
45 #define MENU_PLAY_REPLAY 6 44 #define MENU_PLAY_REPLAY 6
46 #define MENU_LOAD_REPLAY 7 45 #define MENU_LOAD_REPLAY 7
47 #define MENU_SAVE_REPLAY 8 46 #define MENU_SAVE_REPLAY 8
48 #define MENU_BACK 9 47 #define MENU_BACK 9
49 #define MENU_GAME_TYPE 10 48 #define MENU_GAME_TYPE 10
50 #define MENU_DIFFICULTY 11 49 #define MENU_DIFFICULTY 11
51 #define MENU_CLEAR_SCORES 12 50 #define MENU_CLEAR_SCORES 12
52 #define MENU_GAME_SFCAVE13 51 #define MENU_GAME_SFCAVE13
53 #define MENU_GAME_GATES 14 52 #define MENU_GAME_GATES 14
54 #define MENU_GAME_FLY 15 53 #define MENU_GAME_FLY 15
55 #define MENU_DIFFICULTY_EASY16 54 #define MENU_DIFFICULTY_EASY16
56 #define MENU_DIFFICULTY_NORMAL17 55 #define MENU_DIFFICULTY_NORMAL17
57 #define MENU_DIFFICULTY_HARD18 56 #define MENU_DIFFICULTY_HARD18
58 #define MENU_SOUNDS 19 57 #define MENU_DIFFICULTY_HARD18
59 #define MENU_SOUND_ON 20 58 #define MENU_DIFFICULTY_CUSTOM19
60 #define MENU_SOUND_OFF 21 59 #define MENU_SOUNDS 20
61 #define MENU_MUSIC_ON 22 60 #define MENU_SOUND_ON 21
62 #define MENU_MUSIC_OFF 23 61 #define MENU_SOUND_OFF 22
62 #define MENU_MUSIC_ON 23
63 #define MENU_MUSIC_OFF 24
64#define MENU_CUSTOM_THRUST 25
65#define MENU_CUSTOM_GRAVITY 26
66#define MENU_CUSTOM_MAXSPEEDUP 27
67#define MENU_CUSTOM_MAXSPEEDDOWN 28
68#define MENU_CUSTOM_INCREASE 29
69#define MENU_CUSTOM_DECREASE 30
70#define MENU_CUSTOM_SAVE 31
71#define MENU_CUSTOM_CANCEL 32
63 72
64// Sounds 73// Sounds
65 #define SND_EXPLOSION 0 74 #define SND_EXPLOSION 0
66 #define SND_THRUST 1 75 #define SND_THRUST 1
76 #define INGAME_MUSIC SOUND_PATH "ingame.mod"
77
78// Constants for player values
79#define PLAYER_THRUST 0
80#define PLAYER_GRAVITY 1
81#define PLAYER_MAX_SPEED_UP 2
82#define PLAYER_MAX_SPEED_DOWN 3
83
67#endif 84#endif
diff --git a/noncore/games/sfcave-sdl/fly_game.cpp b/noncore/games/sfcave-sdl/fly_game.cpp
index f5ab401..7605c3f 100644
--- a/noncore/games/sfcave-sdl/fly_game.cpp
+++ b/noncore/games/sfcave-sdl/fly_game.cpp
@@ -1,103 +1,112 @@
1#include "SDL_gfxPrimitives.h" 1#include "SDL_gfxPrimitives.h"
2 2
3#include "constants.h" 3#include "constants.h"
4#include "fly_game.h" 4#include "fly_game.h"
5#include "random.h" 5#include "random.h"
6 6
7FlyGame :: FlyGame( SFCave *p, int w, int h, int diff ) 7FlyGame :: FlyGame( SFCave *p, int w, int h, int diff )
8 : Game( p, w, h, diff ) 8 : Game( p, w, h, diff )
9{ 9{
10 gameName = "Fly"; 10 gameName = "Fly";
11 difficulty = MENU_DIFFICULTY_EASY; 11 difficulty = MENU_DIFFICULTY_EASY;
12 12
13 terrain = new FlyTerrain( w, h ); 13 terrain = new FlyTerrain( w, h );
14 player = new Player( w, h ); 14 player = new Player( w, h );
15 highScore = 0; 15 highScore = 0;
16} 16}
17 17
18FlyGame :: ~FlyGame() 18FlyGame :: ~FlyGame()
19{ 19{
20 // terrain and player get deleted by parent class
20} 21}
21 22
22void FlyGame :: init() 23void FlyGame :: init()
23{ 24{
24 Game :: init(); 25 Game :: init();
25 26
26 switch( difficulty ) 27 switch( difficulty )
27 { 28 {
28 case MENU_DIFFICULTY_EASY: 29 case MENU_DIFFICULTY_EASY:
29 player->setMovementInfo( 0.3, 0.2, 1.5, 1.5 ); 30 player->setMovementInfo( 0.3, 0.2, 1.5, 1.5 );
30 break; 31 break;
31 case MENU_DIFFICULTY_NORMAL: 32 case MENU_DIFFICULTY_NORMAL:
32 player->setMovementInfo( 0.35, 0.4, 2.5, 3 ); 33 player->setMovementInfo( 0.35, 0.4, 2.5, 3 );
33 break; 34 break;
34 case MENU_DIFFICULTY_HARD: 35 case MENU_DIFFICULTY_HARD:
35 player->setMovementInfo( 0.4, 0.6, 4, 5 ); 36 player->setMovementInfo( 0.4, 0.6, 4, 5 );
36 break; 37 break;
38 case MENU_DIFFICULTY_CUSTOM:
39 {
40 double thrust = parent->loadDoubleSetting( "Fly_custom_player_thrust", 0.3 );
41 double gravity = parent->loadDoubleSetting( "Fly_custom_player_gravity", 0.2 );
42 double maxUp = parent->loadDoubleSetting( "Fly_custom_player_maxupspeed", 1.5 );
43 double maxDown = parent->loadDoubleSetting( "Fly_custom_player_maxdownspeed", 1.5 );
44 player->setMovementInfo( thrust, gravity, maxUp, maxDown );
45 break;
46 }
37 } 47 }
38 48
39 startScoring = false; 49 startScoring = false;
40} 50}
41 51
42void FlyGame :: update( int state ) 52void FlyGame :: update( int state )
43{ 53{
44 Game::update( state ); 54 Game::update( state );
45 55
46 if ( state == STATE_PLAYING ) 56 if ( state == STATE_PLAYING )
47 { 57 {
48 58
49 if ( nrFrames % 3 == 0 ) 59 if ( nrFrames % 3 == 0 )
50 { 60 {
51 int diff = terrain->getMapBottom( 10 ) - player->getY(); 61 int diff = terrain->getMapBottom( 10 ) - player->getY();
52 int tmpScore = ((FlyTerrain *)terrain)->getScore( 1, diff ); 62 int tmpScore = ((FlyTerrain *)terrain)->getScore( 1, diff );
53 // printf( "diff - %d score - %d\n", diff, tmpScore ); 63
54 if ( !startScoring ) 64 if ( !startScoring )
55 { 65 {
56 if ( tmpScore > 0 ) 66 if ( tmpScore > 0 )
57 startScoring = true; 67 startScoring = true;
58 } 68 }
59 69
60 if ( startScoring ) 70 if ( startScoring )
61 { 71 {
62 // Update score 72 // Update score
63 // get distance between landscape and ship 73 // get distance between landscape and ship
64 74
65 // the closer the difference is to 0 means more points 75 // the closer the difference is to 0 means more points
66 score += tmpScore; 76 score += tmpScore;
67 } 77 }
68 } 78 }
69 79
70 if ( checkCollisions() ) 80 if ( checkCollisions() )
71 { 81 {
72 // printf( "Crashed!\n" );
73 parent->changeState( STATE_CRASHING ); 82 parent->changeState( STATE_CRASHING );
74 return; 83 return;
75 } 84 }
76 85
77 // Game logic goes here 86 // Game logic goes here
78 terrain->moveTerrain( 5 ); 87 terrain->moveTerrain( 5 );
79 player->move( press ); 88 player->move( press );
80 } 89 }
81} 90}
82 91
83void FlyGame :: draw( SDL_Surface *screen ) 92void FlyGame :: draw( SDL_Surface *screen )
84{ 93{
85 Game::preDraw( screen ); 94 Game::preDraw( screen );
86 95
87 // Screen drawing goes here 96 // Screen drawing goes here
88 terrain->drawTerrain( screen ); 97 terrain->drawTerrain( screen );
89 98
90 player->draw( screen ); 99 player->draw( screen );
91 100
92 Game::draw( screen ); 101 Game::draw( screen );
93} 102}
94 103
95 104
96bool FlyGame :: checkCollisions() 105bool FlyGame :: checkCollisions()
97{ 106{
98 bool ret = false; 107 bool ret = false;
99 108
100 // Check collision with landscape 109 // Check collision with landscape
101 110
102 return terrain->checkCollision( player->getX(), player->getY(), player->getHeight() ); 111 return terrain->checkCollision( player->getX(), player->getY(), player->getHeight() );
103} 112}
diff --git a/noncore/games/sfcave-sdl/font.cpp b/noncore/games/sfcave-sdl/font.cpp
index 2976d48..1988252 100644
--- a/noncore/games/sfcave-sdl/font.cpp
+++ b/noncore/games/sfcave-sdl/font.cpp
@@ -1,72 +1,84 @@
1#include "font.h" 1#include "font.h"
2 2
3#include "constants.h" 3#include "constants.h"
4 4
5BFont *FontHandler :: menuSelFont; 5BFont *FontHandler :: menuSelFont;
6BFont *FontHandler :: menuUnSelFont; 6BFont *FontHandler :: menuUnSelFont;
7BFont *FontHandler :: whiteFont; 7BFont *FontHandler :: whiteFont;
8BFont *FontHandler :: colouredFont; 8BFont *FontHandler :: colouredFont;
9BFont *FontHandler :: helpFont; 9BFont *FontHandler :: helpFont;
10 10
11void FontHandler :: init() 11bool FontHandler :: init()
12{ 12{
13 // Load font images 13 // Load font images
14 // Convert to fonts 14 // Convert to fonts
15 menuSelFont = new BFont( IMAGES_PATH "sel_menu_font.bmp" ); 15 menuSelFont = new BFont( IMAGES_PATH "sel_menu_font.bmp" );
16 menuUnSelFont = new BFont( IMAGES_PATH "unsel_menu_font.bmp" ); 16 menuUnSelFont = new BFont( IMAGES_PATH "unsel_menu_font.bmp" );
17 whiteFont = new BFont( IMAGES_PATH "score_font.bmp" ); 17 whiteFont = new BFont( IMAGES_PATH "score_font.bmp" );
18 helpFont = new BFont( IMAGES_PATH "help_font.bmp" ); 18 helpFont = new BFont( IMAGES_PATH "help_font.bmp" );
19 colouredFont = 0; 19 colouredFont = 0;
20
21 // Check if we are installed correctly (we need fonts to function)
22 if ( menuSelFont == 0 || menuUnSelFont == 0 || whiteFont == 0 || helpFont == 0 )
23 {
24 printf( "One or more fonts are not installed correctly\n" );
25 return false;
26 }
27
28 return true;
20} 29}
21 30
22void FontHandler :: cleanUp() 31void FontHandler :: cleanUp()
23{ 32{
24 delete menuSelFont; 33 if ( menuSelFont )
25 delete menuUnSelFont; 34 delete menuSelFont;
26 delete whiteFont; 35 if ( menuUnSelFont )
27 delete helpFont; 36 delete menuUnSelFont;
28 37 if ( whiteFont )
38 delete whiteFont;
39 if ( helpFont )
40 delete helpFont;
29 if ( colouredFont ) 41 if ( colouredFont )
30 delete colouredFont; 42 delete colouredFont;
31} 43}
32 44
33int FontHandler :: TextWidth( int font, const char *text ) 45int FontHandler :: TextWidth( int font, const char *text )
34{ 46{
35 return getFont( font )->TextWidth( text ); 47 return getFont( font )->TextWidth( text );
36} 48}
37 49
38int FontHandler :: FontHeight( int font ) 50int FontHandler :: FontHeight( int font )
39{ 51{
40 return getFont( font )->FontHeight(); 52 return getFont( font )->FontHeight();
41} 53}
42 54
43void FontHandler :: draw( SDL_Surface *screen, int font, const char *text, int x, int y ) 55void FontHandler :: draw( SDL_Surface *screen, int font, const char *text, int x, int y )
44{ 56{
45 if ( x == -1 ) 57 if ( x == -1 )
46 getFont( font )->CenteredPutString( screen, y, text ); 58 getFont( font )->CenteredPutString( screen, y, text );
47 else 59 else
48 getFont( font )->PutString( screen, x, y, text ); 60 getFont( font )->PutString( screen, x, y, text );
49} 61}
50 62
51void FontHandler :: changeColor( int font, int r, int g, int b ) 63void FontHandler :: changeColor( int font, int r, int g, int b )
52{ 64{
53 if ( colouredFont ) 65 if ( colouredFont )
54 delete colouredFont; 66 delete colouredFont;
55 67
56 colouredFont = getFont( font )->SetFontColor( r, g, b ); 68 colouredFont = getFont( font )->SetFontColor( r, g, b );
57} 69}
58 70
59 71
60BFont *FontHandler :: getFont( int font ) 72BFont *FontHandler :: getFont( int font )
61{ 73{
62 if ( font == FONT_MENU_HIGHLIGHTED ) 74 if ( font == FONT_MENU_HIGHLIGHTED )
63 return menuSelFont; 75 return menuSelFont;
64 else if ( font == FONT_MENU_UNHIGHLIGHTED ) 76 else if ( font == FONT_MENU_UNHIGHLIGHTED )
65 return menuUnSelFont; 77 return menuUnSelFont;
66 else if ( font == FONT_COLOURED_TEXT ) 78 else if ( font == FONT_COLOURED_TEXT )
67 return colouredFont; 79 return colouredFont;
68 else if ( font == FONT_HELP_FONT ) 80 else if ( font == FONT_HELP_FONT )
69 return helpFont; 81 return helpFont;
70 else 82 else
71 return whiteFont; 83 return whiteFont;
72} 84}
diff --git a/noncore/games/sfcave-sdl/font.h b/noncore/games/sfcave-sdl/font.h
index e5bb707..5f0674a 100644
--- a/noncore/games/sfcave-sdl/font.h
+++ b/noncore/games/sfcave-sdl/font.h
@@ -1,33 +1,33 @@
1#ifndef __FONT_H 1#ifndef __FONT_H
2#define __FONT_H 2#define __FONT_H
3 3
4#include "SDL.h" 4#include "SDL.h"
5#include "bfont.h" 5#include "bfont.h"
6 6
7 #define FONT_MENU_HIGHLIGHTED 1 7 #define FONT_MENU_HIGHLIGHTED 1
8 #define FONT_MENU_UNHIGHLIGHTED 2 8 #define FONT_MENU_UNHIGHLIGHTED 2
9 #define FONT_WHITE_TEXT 3 9 #define FONT_WHITE_TEXT 3
10 #define FONT_COLOURED_TEXT 4 10 #define FONT_COLOURED_TEXT 4
11 #define FONT_HELP_FONT 5 11 #define FONT_HELP_FONT 5
12 12
13class FontHandler 13class FontHandler
14{ 14{
15public: 15public:
16 static void init(); 16 static bool init();
17 static void cleanUp(); 17 static void cleanUp();
18 18
19 static int TextWidth( int font, const char *text ); 19 static int TextWidth( int font, const char *text );
20 static int FontHeight( int font ); 20 static int FontHeight( int font );
21 static void draw( SDL_Surface *screen, int font, const char *text, int x, int y ); 21 static void draw( SDL_Surface *screen, int font, const char *text, int x, int y );
22 static void changeColor( int font, int r, int g, int b ); 22 static void changeColor( int font, int r, int g, int b );
23 23
24 static BFont *getFont( int font ); 24 static BFont *getFont( int font );
25private: 25private:
26 static BFont *menuSelFont; 26 static BFont *menuSelFont;
27 static BFont *menuUnSelFont; 27 static BFont *menuUnSelFont;
28 static BFont *whiteFont; 28 static BFont *whiteFont;
29 static BFont *colouredFont; 29 static BFont *colouredFont;
30 static BFont *helpFont; 30 static BFont *helpFont;
31}; 31};
32 32
33#endif 33#endif
diff --git a/noncore/games/sfcave-sdl/game.cpp b/noncore/games/sfcave-sdl/game.cpp
index a644696..e41e510 100644
--- a/noncore/games/sfcave-sdl/game.cpp
+++ b/noncore/games/sfcave-sdl/game.cpp
@@ -1,332 +1,328 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <time.h> 2#include <time.h>
3 3
4#include <SDL.h> 4#include <SDL.h>
5#include <SDL_image.h> 5#include <SDL_image.h>
6 6
7#include "font.h" 7#include "font.h"
8 8
9#include "constants.h" 9#include "constants.h"
10#include "game.h" 10#include "game.h"
11#include "player.h" 11#include "player.h"
12#include "random.h" 12#include "random.h"
13#include "sound.h" 13#include "sound.h"
14#include "stringtokenizer.h" 14#include "stringtokenizer.h"
15 15
16#include "sfcave_game.h" 16#include "sfcave_game.h"
17#include "gates_game.h" 17#include "gates_game.h"
18#include "fly_game.h" 18#include "fly_game.h"
19#include "starfield.h" 19#include "starfield.h"
20 20
21Game :: Game( SFCave *p, int w, int h, int diff ) 21Game :: Game( SFCave *p, int w, int h, int diff )
22{ 22{
23 parent = p; 23 parent = p;
24 sHeight = h; 24 sHeight = h;
25 sWidth = w; 25 sWidth = w;
26 difficulty = diff; 26 difficulty = diff;
27 replayIt = 0; 27 replayIt = 0;
28 replay = false; 28 replay = false;
29 terrain = 0; 29 terrain = 0;
30 player = 0; 30 player = 0;
31 thrustChannel = -1; 31 thrustChannel = -1;
32} 32}
33 33
34Game :: ~Game() 34Game :: ~Game()
35{ 35{
36 if ( terrain ) 36 if ( terrain )
37 delete terrain; 37 delete terrain;
38 38
39 if ( player ) 39 if ( player )
40 delete player; 40 delete player;
41 41
42 replayList.clear(); 42 replayList.clear();
43} 43}
44 44
45void Game :: init() 45void Game :: init()
46{ 46{
47 if ( replay ) 47 if ( replay )
48 { 48 {
49 setSeed( currentSeed ); 49 setSeed( currentSeed );
50 replayIt = replayList.begin(); 50 replayIt = replayList.begin();
51 } 51 }
52 else 52 else
53 { 53 {
54 setSeed( -1 ); 54 setSeed( -1 );
55 replayList.clear(); 55 replayList.clear();
56 } 56 }
57 57
58 score = 0; 58 score = 0;
59 nrFrames = 0; 59 nrFrames = 0;
60 press = false; 60 press = false;
61 61
62 // Load highscore 62 // Load highscore
63 string key = getGameName() + "_" + getGameDifficultyText() + "_highscore"; 63 string key = getGameName() + "_" + getGameDifficultyText() + "_highscore";
64 highScore = atoi( parent->loadSetting( key, "0" ).c_str() ); 64 highScore = atoi( parent->loadSetting( key, "0" ).c_str() );
65 65
66 terrain->initTerrain(); 66 terrain->initTerrain();
67 player->init(); 67 player->init();
68} 68}
69 69
70void Game :: handleKeys( SDL_KeyboardEvent &key ) 70void Game :: handleKeys( SDL_KeyboardEvent &key )
71{ 71{
72 if ( !replay && key.keysym.sym == SDLK_SPACE ) 72 if ( !replay && key.keysym.sym == SDLK_SPACE )
73 { 73 {
74 if ( key.type == SDL_KEYDOWN ) 74 if ( key.type == SDL_KEYDOWN )
75 { 75 {
76 if ( !press ) 76 if ( !press )
77 replayList.push_back( nrFrames ); 77 replayList.push_back( nrFrames );
78 press = true; 78 press = true;
79
80 // if ( thrustChannel == -1 && parent->getState() == STATE_PLAYING )
81 // thrustChannel = SoundHandler :: playSound( SND_THRUST, -1, -1, false );
82 } 79 }
83 else 80 else
84 { 81 {
85 if ( press ) 82 if ( press )
86 replayList.push_back( nrFrames ); 83 replayList.push_back( nrFrames );
87 press = false; 84 press = false;
88 85
89 if ( thrustChannel != -1 )
90 {
91 // SoundHandler :: stopSound( thrustChannel, true, 300 );
92 // thrustChannel = -1;
93 }
94 } 86 }
95 } 87 }
96} 88}
97 89
98 90
99 91
100QString Game :: getGameDifficultyText() 92string Game :: getGameDifficultyText()
101{ 93{
102 QString ret; 94 string ret;
103 95
104 if ( difficulty == MENU_DIFFICULTY_EASY ) 96 if ( difficulty == MENU_DIFFICULTY_EASY )
105 ret = "Easy"; 97 ret = "Easy";
106 else if ( difficulty == MENU_DIFFICULTY_NORMAL ) 98 else if ( difficulty == MENU_DIFFICULTY_NORMAL )
107 ret = "Medium"; 99 ret = "Medium";
108 else if ( difficulty == MENU_DIFFICULTY_HARD ) 100 else if ( difficulty == MENU_DIFFICULTY_HARD )
109 ret = "Hard"; 101 ret = "Hard";
102 else if ( difficulty == MENU_DIFFICULTY_CUSTOM )
103 ret = "Custom";
110 104
111 return ret; 105 return ret;
112} 106}
113 107
114void Game :: setDifficulty( string diff ) 108void Game :: setDifficulty( string diff )
115{ 109{
116 if ( diff == "Easy" ) 110 if ( diff == "Easy" )
117 difficulty = MENU_DIFFICULTY_EASY; 111 difficulty = MENU_DIFFICULTY_EASY;
118 else if ( diff == "Medium" ) 112 else if ( diff == "Medium" )
119 difficulty = MENU_DIFFICULTY_NORMAL; 113 difficulty = MENU_DIFFICULTY_NORMAL;
120 else if ( diff == "Hard" ) 114 else if ( diff == "Hard" )
121 difficulty = MENU_DIFFICULTY_HARD; 115 difficulty = MENU_DIFFICULTY_HARD;
116 else if ( diff == "Custom" )
117 difficulty = MENU_DIFFICULTY_CUSTOM;
118
119 init();
120}
121
122void Game :: setDifficulty( int diff )
123{
124 difficulty = diff;
125 init();
122} 126}
123 127
124void Game :: update( int state ) 128void Game :: update( int state )
125{ 129{
126 nrFrames ++; 130 nrFrames ++;
127 131
128 if ( score > highScore ) 132 if ( score > highScore )
129 highScore = score; 133 highScore = score;
130 134
131 135
132 if ( state == STATE_PLAYING ) 136 if ( state == STATE_PLAYING )
133 { 137 {
134 if ( replay ) 138 if ( replay )
135 { 139 {
136 while( replayIt != replayList.end() && (*replayIt) == nrFrames-1 ) 140 while( replayIt != replayList.end() && (*replayIt) == nrFrames-1 )
137 { 141 {
138 press = !press; 142 press = !press;
139 replayIt ++; 143 replayIt ++;
140 } 144 }
141 } 145 }
142 146
143 if ( press && thrustChannel == -1 ) 147 if ( press && thrustChannel == -1 )
144 thrustChannel = SoundHandler :: playSound( SND_THRUST, -1, -1, false ); 148 thrustChannel = SoundHandler :: playSound( SND_THRUST, -1, -1, false );
145 149
146 if ( !press &&thrustChannel != -1 ) 150 if ( !press &&thrustChannel != -1 )
147 { 151 {
148 SoundHandler :: stopSound( thrustChannel, true, 300 ); 152 SoundHandler :: stopSound( thrustChannel, true, 300 );
149 thrustChannel = -1; 153 thrustChannel = -1;
150 } 154 }
151 } 155 }
152 156
153 if ( state == STATE_CRASHING || state == STATE_CRASHED ) 157 if ( state == STATE_CRASHING || state == STATE_CRASHED )
154 { 158 {
155 // fade out any trail marks remainin 159 // fade out any trail marks remainin
156 if ( player->updateCrashing() ) 160 if ( player->updateCrashing() )
157 parent->changeState( STATE_CRASHED ); 161 parent->changeState( STATE_CRASHED );
158 162
159 } 163 }
160} 164}
161 165
162void Game :: preDraw( SDL_Surface *screen ) 166void Game :: preDraw( SDL_Surface *screen )
163{ 167{
164} 168}
165 169
166void Game :: draw( SDL_Surface *screen ) 170void Game :: draw( SDL_Surface *screen )
167{ 171{
168 char tmp[100]; 172 char tmp[100];
169 QString scoreText; 173 string scoreText;
170 sprintf( tmp, "Score: %06ld High Score: %06ld", score, highScore ); 174 sprintf( tmp, "Score: %06ld High Score: %06ld", score, highScore );
171 //printf( "%s\n", (const char *)scoreText );
172 FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 3, 10 ); 175 FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 3, 10 );
173 176
174 if ( parent->getState() == STATE_CRASHED ) 177 if ( parent->getState() == STATE_CRASHED )
175 { 178 {
176 QString crashText; 179 string crashText;
177 crashText = "Game Over"; 180 crashText = "Game Over";
178 int x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; 181 int x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2;
179 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 ); 182 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 );
180 183
181 int fontHeight = FontHandler::FontHeight( FONT_WHITE_TEXT ); 184 int fontHeight = FontHandler::FontHeight( FONT_WHITE_TEXT );
182 crashText = "Press Middle Button to play again"; 185 crashText = "Press Middle Button to play again";
183 x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; 186 x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2;
184 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + fontHeight ); 187 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + fontHeight );
185 188
186 crashText = "or OK for menu"; 189 crashText = "or OK for menu";
187 x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; 190 x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2;
188 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + 2*fontHeight ); 191 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 + 2*fontHeight );
189 } 192 }
190 193
191 if ( parent->showFPS() ) 194 if ( parent->showFPS() )
192 { 195 {
193 sprintf( tmp, "FPS : %d", parent->getFPS() ); 196 sprintf( tmp, "FPS : %d", parent->getFPS() );
194 FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 20, 300 ); 197 FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 20, 300 );
195 } 198 }
196} 199}
197 200
198void Game :: stateChanged( int from, int to ) 201void Game :: stateChanged( int from, int to )
199{ 202{
200 if ( from != STATE_CRASHING && to == STATE_CRASHING ) 203 if ( from != STATE_CRASHING && to == STATE_CRASHING )
201 { 204 {
202 // play explosion sound 205 // play explosion sound
203 SoundHandler :: stopSound( -1, false ); 206 SoundHandler :: stopSound( -1, false );
204 SoundHandler :: playSound( SND_EXPLOSION ); 207 SoundHandler :: playSound( SND_EXPLOSION );
205 208
206 // Check and save highscore 209 // Check and save highscore
207 printf( "Got Highscore = %d\n", gotHighScore() ); 210 printf( "Got Highscore = %d\n", gotHighScore() );
208 if ( gotHighScore() ) 211 if ( gotHighScore() )
209 { 212 {
210 string key = getGameName() + "_" + getGameDifficultyText() + "_highscore"; 213 string key = getGameName() + "_" + getGameDifficultyText() + "_highscore";
211 parent->saveSetting( key, getHighScore() ); 214 parent->saveSetting( key, getHighScore() );
212 } 215 }
213 216
214 } 217 }
215} 218}
216 219
217void Game :: setSeed( int seed ) 220void Game :: setSeed( int seed )
218{ 221{
219 if ( seed == -1 ) 222 if ( seed == -1 )
220 currentSeed = ((unsigned long) time((time_t *) NULL)); 223 currentSeed = ((unsigned long) time((time_t *) NULL));
221 else 224 else
222 currentSeed = seed; 225 currentSeed = seed;
223 PutSeed( currentSeed ); 226 PutSeed( currentSeed );
224} 227}
225 228
226void Game :: saveReplay( QString file ) 229void Game :: saveReplay( string file )
227{ 230{
228 FILE *out; 231 FILE *out;
229 out = fopen( file.c_str(), "w" ); 232 out = fopen( file.c_str(), "w" );
230 if ( !out ) 233 if ( !out )
231 { 234 {
232 printf( "Couldn't write to /home/root/%s\n", file.c_str() ); 235 printf( "Couldn't write to /home/root/%s\n", file.c_str() );
233 parent->setMenuStatusText( "Couldn't save replay file" ); 236 parent->setMenuStatusText( "Couldn't save replay file" );
234 return; 237 return;
235 } 238 }
236 239
237 // Build up string of values 240 // Build up string of values
238 // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>....... 241 // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>.......
239 QString val; 242 string val;
240 char tmp[20]; 243 char tmp[20];
241 sprintf( tmp, "%d %d ", currentSeed, difficulty ); 244 sprintf( tmp, "%d %d ", currentSeed, difficulty );
242 val = tmp; 245 val = tmp;
243 246
244 list<int>::iterator it = replayList.begin(); 247 list<int>::iterator it = replayList.begin();
245 while( it != replayList.end() ) 248 while( it != replayList.end() )
246 { 249 {
247 sprintf( tmp, "%d ", *it ); 250 sprintf( tmp, "%d ", *it );
248 val += tmp; 251 val += tmp;
249 252
250 it++; 253 it++;
251 } 254 }
252 val += "\n"; 255 val += "\n";
253 256
254 QString line; 257 string line;
255 sprintf( tmp, "%d\n", val.length() ); 258 sprintf( tmp, "%d\n", val.length() );
256 line = tmp; 259 line = tmp;
257 fwrite( line.c_str(), 1, line.length(), out ); 260 fwrite( line.c_str(), 1, line.length(), out );
258 261
259 fwrite( val.c_str(), 1, val.length(), out ); 262 fwrite( val.c_str(), 1, val.length(), out );
260 263
261 fclose( out ); 264 fclose( out );
262
263 printf( "Replay saved to %s\n", (const char *)file.c_str() );
264
265} 265}
266 266
267void Game :: loadReplay( QString file ) 267void Game :: loadReplay( string file )
268{ 268{
269 269
270 FILE *in = fopen( (const char *)file.c_str(), "r" ); 270 FILE *in = fopen( (const char *)file.c_str(), "r" );
271 271
272 if ( in == 0 ) 272 if ( in == 0 )
273 { 273 {
274 printf( "Couldn't load replay file %s!\n", (const char *)file.c_str() ); 274 printf( "Couldn't load replay file %s!\n", (const char *)file.c_str() );
275 parent->setMenuStatusText( "Couldn't load replay file" ); 275 parent->setMenuStatusText( "Couldn't load replay file" );
276 return; 276 return;
277 } 277 }
278 278
279 // Read next line - contains the size of the options 279 // Read next line - contains the size of the options
280 char line[10+1]; 280 char line[10+1];
281 fgets( line, 10, in ); 281 fgets( line, 10, in );
282 282
283 int length = -1; 283 int length = -1;
284 sscanf( line, "%d", &length ); 284 sscanf( line, "%d", &length );
285 char *data = new char[length+1]; 285 char *data = new char[length+1];
286 286
287 fread( data, 1, length, in ); 287 fread( data, 1, length, in );
288// printf( "data - %s", data );
289 288
290 QString sep = " "; 289 string sep = " ";
291 290
292 StringTokenizer st( data, sep ); 291 StringTokenizer st( data, sep );
293 292
294 // print it out 293 // print it out
295 vector<string>::iterator it = st.begin(); 294 vector<string>::iterator it = st.begin();
296 currentSeed = atoi( (*it).c_str() ); 295 currentSeed = atoi( (*it).c_str() );
297 ++it; 296 ++it;
298 difficulty = atoi( (*it).c_str() ); 297 difficulty = atoi( (*it).c_str() );
299 ++it; 298 ++it;
300 299
301 replayList.clear(); 300 replayList.clear();
302 for ( ; it != st.end(); ++it ) 301 for ( ; it != st.end(); ++it )
303 { 302 {
304 int v = atoi( (*it).c_str() ); 303 int v = atoi( (*it).c_str() );
305 replayList.push_back( v ); 304 replayList.push_back( v );
306 } 305 }
307 306
308 delete data; 307 delete data;
309 308
310 fclose( in ); 309 fclose( in );
311
312 printf( "Replay loaded from %s\n", (const char *)file.c_str() );
313
314} 310}
315 311
316 312
317Game *Game :: createGame( SFCave *p, int w, int h, string game, string difficulty ) 313Game *Game :: createGame( SFCave *p, int w, int h, string game, string difficulty )
318{ 314{
319 Game *g; 315 Game *g;
320 316
321 if ( game == "SFCave" ) 317 if ( game == "SFCave" )
322 g = new SFCaveGame( p, w, h, 0 ); 318 g = new SFCaveGame( p, w, h, 0 );
323 else if ( game == "Gates" ) 319 else if ( game == "Gates" )
324 g = new GatesGame( p, w, h, 0 ); 320 g = new GatesGame( p, w, h, 0 );
325 else if ( game == "Fly" ) 321 else if ( game == "Fly" )
326 g = new FlyGame( p, w, h, 0 ); 322 g = new FlyGame( p, w, h, 0 );
327 323
328 if ( g ) 324 if ( g )
329 g->setDifficulty( difficulty ); 325 g->setDifficulty( difficulty );
330 326
331 return g; 327 return g;
332} 328}
diff --git a/noncore/games/sfcave-sdl/game.h b/noncore/games/sfcave-sdl/game.h
index 56fa6a1..087f848 100644
--- a/noncore/games/sfcave-sdl/game.h
+++ b/noncore/games/sfcave-sdl/game.h
@@ -1,82 +1,81 @@
1#ifndef __GAME_H 1#ifndef __GAME_H
2#define __GAME_H 2#define __GAME_H
3 3
4#include <list> 4#include <list>
5using namespace std; 5using namespace std;
6 6
7#include "sfcave.h" 7#include "sfcave.h"
8 8
9class Terrain; 9class Terrain;
10class Player; 10class Player;
11 11
12class Game 12class Game
13{ 13{
14public: 14public:
15 Game( SFCave *p, int w, int h, int diff ); 15 Game( SFCave *p, int w, int h, int diff );
16 virtual ~Game(); 16 virtual ~Game();
17 17
18 virtual void init( ); 18 virtual void init( );
19 virtual void update( int state ); 19 virtual void update( int state );
20 virtual void preDraw( SDL_Surface * ); 20 virtual void preDraw( SDL_Surface * );
21 virtual void draw( SDL_Surface * ); 21 virtual void draw( SDL_Surface * );
22 22
23 virtual void stateChanged( int from, int to ); 23 virtual void stateChanged( int from, int to );
24 24
25 void setReplay( bool val ) { replay = val; } 25 void setReplay( bool val ) { replay = val; }
26 26
27 void handleKeys( SDL_KeyboardEvent &key ); 27 void handleKeys( SDL_KeyboardEvent &key );
28 QString getGameName() { return gameName; } 28 string getGameName() { return gameName; }
29 int getDifficulty() { return difficulty; } 29 int getDifficulty() { return difficulty; }
30 QString getGameDifficultyText(); 30 string getGameDifficultyText();
31 void setDifficulty( int diff ) { difficulty = diff; } 31 void setDifficulty( int diff );
32 void setDifficulty( string diff ); 32 void setDifficulty( string diff );
33 33
34 long getScore() { return score; } 34 long getScore() { return score; }
35 long getHighScore() { return highScore; } 35 long getHighScore() { return highScore; }
36 void increaseScore( long val ) { score += val; } 36 void increaseScore( long val ) { score += val; }
37 void clearScore() { score = 0; } 37 void clearScore() { score = 0; }
38 bool gotHighScore() { return (score >= highScore); } 38 bool gotHighScore() { return (score >= highScore); }
39 bool isReplayAvailable() { return replayList.size() > 0; } 39 bool isReplayAvailable() { return replayList.size() > 0; }
40 40
41 Terrain *getTerrain() { return terrain; } 41 Terrain *getTerrain() { return terrain; }
42 Player *getPlayer() { return player; }
42 43
43 void setSeed( int seed ); 44 void setSeed( int seed );
44 void loadReplay( QString file ); 45 void loadReplay( string file );
45 void saveReplay( QString file ); 46 void saveReplay( string file );
46 47
47 static Game *createGame( SFCave *p, int w, int h, string game, string difficulty ); 48 static Game *createGame( SFCave *p, int w, int h, string game, string difficulty );
48 49
49protected: 50protected:
50 QString gameName; 51 string gameName;
51 52
52 int thrustChannel; 53 int thrustChannel;
53 54
54 int difficulty; 55 int difficulty;
55 56
56 SFCave *parent; 57 SFCave *parent;
57 Terrain *terrain; 58 Terrain *terrain;
58 Player *player; 59 Player *player;
59 60
60 int nrFrames; 61 int nrFrames;
61 62
62 bool press; 63 bool press;
63 64
64 int sHeight; 65 int sHeight;
65 int sWidth; 66 int sWidth;
66 long score; 67 long score;
67 long highScore; 68 long highScore;
68 69
69 // Stuff for the replays 70 // Stuff for the replays
70 int currentSeed; 71 int currentSeed;
71 72
72// QListIterator<int> *replayIt;
73 list<int> replayList; 73 list<int> replayList;
74 list<int>::iterator replayIt; 74 list<int>::iterator replayIt;
75 //QList<int> replayList;
76 bool replay; 75 bool replay;
77 QString replayFile; 76 string replayFile;
78 77
79private: 78private:
80}; 79};
81 80
82#endif 81#endif
diff --git a/noncore/games/sfcave-sdl/gates_game.cpp b/noncore/games/sfcave-sdl/gates_game.cpp
index 1a9bc89..762801d 100644
--- a/noncore/games/sfcave-sdl/gates_game.cpp
+++ b/noncore/games/sfcave-sdl/gates_game.cpp
@@ -1,188 +1,201 @@
1#include "SDL_gfxPrimitives.h" 1#include "SDL_gfxPrimitives.h"
2 2
3#include "constants.h" 3#include "constants.h"
4#include "gates_game.h" 4#include "gates_game.h"
5#include "random.h" 5#include "random.h"
6 6
7GatesGame :: GatesGame( SFCave *p, int w, int h, int diff ) 7GatesGame :: GatesGame( SFCave *p, int w, int h, int diff )
8 : Game( p, w, h, diff ) 8 : Game( p, w, h, diff )
9{ 9{
10 gameName = "Gates"; 10 gameName = "Gates";
11 difficulty = MENU_DIFFICULTY_EASY; 11 difficulty = MENU_DIFFICULTY_EASY;
12 blockUpdateRate = 200; 12 blockUpdateRate = 200;
13 13
14 terrain = new Terrain( w, h ); 14 terrain = new Terrain( w, h );
15 player = new Player( w, h ); 15 player = new Player( w, h );
16 highScore = 0; 16 highScore = 0;
17} 17}
18 18
19GatesGame :: ~GatesGame() 19GatesGame :: ~GatesGame()
20{ 20{
21 // terrain and player get deleted by parent class
21} 22}
22 23
23void GatesGame :: init() 24void GatesGame :: init()
24{ 25{
25 Game :: init(); 26 Game :: init();
26 27
27 blockHeight = 80; 28 blockHeight = 80;
28 blockWidth = 20; 29 blockWidth = 20;
29 lastGateBottomY = 0; 30 lastGateBottomY = 0;
30 31
31 gateDistance = 75; 32 gateDistance = 75;
32 nextGate = nextInt( 50 ) + gateDistance; 33 nextGate = nextInt( 50 ) + gateDistance;
33 gapHeight = 75; 34 gapHeight = 75;
34 35
35 switch( difficulty ) 36 switch( difficulty )
36 { 37 {
37 case MENU_DIFFICULTY_EASY: 38 case MENU_DIFFICULTY_EASY:
38 gapHeight = 75; 39 gapHeight = 75;
39 player->setMovementInfo( 0.4, 0.6, 4, 5 ); 40 player->setMovementInfo( 0.4, 0.6, 4, 5 );
40 break; 41 break;
41 case MENU_DIFFICULTY_NORMAL: 42 case MENU_DIFFICULTY_NORMAL:
42 gapHeight = 50; 43 gapHeight = 50;
43 player->setMovementInfo( 0.4, 0.6, 4, 5 ); 44 player->setMovementInfo( 0.4, 0.6, 4, 5 );
44 break; 45 break;
45 case MENU_DIFFICULTY_HARD: 46 case MENU_DIFFICULTY_HARD:
46 gapHeight = 25; 47 gapHeight = 25;
47 player->setMovementInfo( 0.6, 0.8, 6, 7 ); 48 player->setMovementInfo( 0.6, 0.8, 6, 7 );
48 break; 49 break;
50 case MENU_DIFFICULTY_CUSTOM:
51 {
52 // Read custom difficulty settings for this game
53 gapHeight = parent->loadIntSetting( "Gates_custom_gapHeight", 75 );
54
55 double thrust = parent->loadDoubleSetting( "Gates_custom_player_thrust", 0.4 );
56 double gravity = parent->loadDoubleSetting( "Gates_custom_player_gravity", 0.6 );
57 double maxUp = parent->loadDoubleSetting( "Gates_custom_player_maxupspeed", 4.0 );
58 double maxDown = parent->loadDoubleSetting( "Gates_custom_player_maxdownspeed", 5.0 );
59 player->setMovementInfo( thrust, gravity, maxUp, maxDown );
60
61 break;
62 }
49 } 63 }
50 64
51 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 65 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
52 blocks[i].y( -1 ); 66 blocks[i].y( -1 );
53} 67}
54 68
55void GatesGame :: update( int state ) 69void GatesGame :: update( int state )
56{ 70{
57 Game::update( state ); 71 Game::update( state );
58 72
59 // Game logic goes here 73 // Game logic goes here
60 if ( state == STATE_PLAYING ) 74 if ( state == STATE_PLAYING )
61 { 75 {
62 if ( nrFrames % 3 == 0 ) 76 if ( nrFrames % 3 == 0 )
63 score ++; 77 score ++;
64 78
65 if ( nrFrames % 500 == 0 ) 79 if ( nrFrames % 500 == 0 )
66 { 80 {
67 if ( gapHeight > 75 ) 81 if ( gapHeight > 75 )
68 gapHeight -= 5; 82 gapHeight -= 5;
69 } 83 }
70 84
71 // Slightly random gap distance 85 // Slightly random gap distance
72 if ( nrFrames >= nextGate ) 86 if ( nrFrames >= nextGate )
73 { 87 {
74 nextGate = nrFrames + nextInt( 50 ) + gateDistance; 88 nextGate = nrFrames + nextInt( 50 ) + gateDistance;
75 addGate(); 89 addGate();
76 } 90 }
77 91
78 if ( checkCollisions() ) 92 if ( checkCollisions() )
79 { 93 {
80 // printf( "Crashed!\n" );
81 parent->changeState( STATE_CRASHING ); 94 parent->changeState( STATE_CRASHING );
82 return; 95 return;
83 } 96 }
84 97
85 terrain->moveTerrain( 5 ); 98 terrain->moveTerrain( 5 );
86 moveBlocks( 5 ); 99 moveBlocks( 5 );
87 player->move( press ); 100 player->move( press );
88 } 101 }
89} 102}
90 103
91void GatesGame :: draw( SDL_Surface *screen ) 104void GatesGame :: draw( SDL_Surface *screen )
92{ 105{
93 Game::preDraw( screen ); 106 Game::preDraw( screen );
94 107
95 if ( parent->getState() == STATE_PLAYING ) 108 if ( parent->getState() == STATE_PLAYING )
96 { 109 {
97 // Screen drawing goes here 110 // Screen drawing goes here
98 terrain->drawTerrain( screen ); 111 terrain->drawTerrain( screen );
99 112
100 player->draw( screen ); 113 player->draw( screen );
101 114
102 drawBlocks( screen ); 115 drawBlocks( screen );
103 } 116 }
104 else 117 else
105 { 118 {
106 // Screen drawing goes here 119 // Screen drawing goes here
107 terrain->drawTerrain( screen ); 120 terrain->drawTerrain( screen );
108 121
109 drawBlocks( screen ); 122 drawBlocks( screen );
110 123
111 player->draw( screen ); 124 player->draw( screen );
112 } 125 }
113 126
114 Game::draw( screen ); 127 Game::draw( screen );
115} 128}
116 129
117 130
118void GatesGame :: addGate() 131void GatesGame :: addGate()
119{ 132{
120 printf( "gapHeight = %d\n", gapHeight ); 133 printf( "gapHeight = %d\n", gapHeight );
121 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 134 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
122 { 135 {
123 if ( blocks[i].y() == -1 ) 136 if ( blocks[i].y() == -1 )
124 { 137 {
125 int x1 = sWidth; 138 int x1 = sWidth;
126 int y1 = terrain->getMapTop(50); 139 int y1 = terrain->getMapTop(50);
127 int b1Height = nextInt(terrain->getMapBottom( 50 ) - terrain->getMapTop(50) - gapHeight); 140 int b1Height = nextInt(terrain->getMapBottom( 50 ) - terrain->getMapTop(50) - gapHeight);
128 141
129 // See if height between last gate and this one is too big 142 // See if height between last gate and this one is too big
130 if ( b1Height - 100 > lastGateBottomY ) 143 if ( b1Height - 100 > lastGateBottomY )
131 b1Height -= 25; 144 b1Height -= 25;
132 else if ( b1Height + 100 < lastGateBottomY ) 145 else if ( b1Height + 100 < lastGateBottomY )
133 b1Height += 25; 146 b1Height += 25;
134 lastGateBottomY = b1Height; 147 lastGateBottomY = b1Height;
135 148
136 149
137 int x2 = sWidth; 150 int x2 = sWidth;
138 int y2 = y1 + b1Height + gapHeight; 151 int y2 = y1 + b1Height + gapHeight;
139 int b2Height = terrain->getMapBottom( 50 ) - y2; 152 int b2Height = terrain->getMapBottom( 50 ) - y2;
140 153
141 154
142 blocks[i].setRect( x1, y1, blockWidth, b1Height ); 155 blocks[i].setRect( x1, y1, blockWidth, b1Height );
143 blocks[i+1].setRect( x2, y2, blockWidth, b2Height ); 156 blocks[i+1].setRect( x2, y2, blockWidth, b2Height );
144 157
145 break; 158 break;
146 } 159 }
147 } 160 }
148} 161}
149 162
150void GatesGame :: moveBlocks( int amountToMove ) 163void GatesGame :: moveBlocks( int amountToMove )
151{ 164{
152 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 165 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
153 { 166 {
154 if ( blocks[i].y() != -1 ) 167 if ( blocks[i].y() != -1 )
155 { 168 {
156 blocks[i].moveBy( -amountToMove, 0 ); 169 blocks[i].moveBy( -amountToMove, 0 );
157 if ( blocks[i].x() + blocks[i].y() < 0 ) 170 if ( blocks[i].x() + blocks[i].y() < 0 )
158 blocks[i].y( -1 ); 171 blocks[i].y( -1 );
159 } 172 }
160 } 173 }
161} 174}
162 175
163void GatesGame :: drawBlocks( SDL_Surface *screen ) 176void GatesGame :: drawBlocks( SDL_Surface *screen )
164{ 177{
165 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 178 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
166 { 179 {
167 if ( blocks[i].y() != -1 ) 180 if ( blocks[i].y() != -1 )
168 { 181 {
169 SDL_Rect r = blocks[i].getRect(); 182 SDL_Rect r = blocks[i].getRect();
170 SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 100, 100, 255 ) ); 183 SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 100, 100, 255 ) );
171 } 184 }
172 } 185 }
173} 186}
174 187
175bool GatesGame :: checkCollisions() 188bool GatesGame :: checkCollisions()
176{ 189{
177 // Check collisions with blocks 190 // Check collisions with blocks
178 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 191 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
179 { 192 {
180 if ( blocks[i].y() != -1 ) 193 if ( blocks[i].y() != -1 )
181 { 194 {
182 if ( blocks[i].intersects( player->getPos() ) ) 195 if ( blocks[i].intersects( player->getPos() ) )
183 return true; 196 return true;
184 } 197 }
185 } 198 }
186 // Check collision with landscape 199 // Check collision with landscape
187 return terrain->checkCollision( player->getX(), player->getY(), player->getHeight() ); 200 return terrain->checkCollision( player->getX(), player->getY(), player->getHeight() );
188} 201}
diff --git a/noncore/games/sfcave-sdl/help.cpp b/noncore/games/sfcave-sdl/help.cpp
index 91c62da..f1728f6 100644
--- a/noncore/games/sfcave-sdl/help.cpp
+++ b/noncore/games/sfcave-sdl/help.cpp
@@ -1,237 +1,236 @@
1#include "SDL.h" 1#include "SDL.h"
2#include "constants.h" 2#include "constants.h"
3 3
4#include "font.h" 4#include "font.h"
5#include "help.h" 5#include "help.h"
6#include "sfcave.h" 6#include "sfcave.h"
7#include "starfield.h" 7#include "starfield.h"
8 8
9Help :: Help( SFCave *p ) 9Help :: Help( SFCave *p )
10{ 10{
11 parent = p; 11 parent = p;
12 stars = new StarField( false, 200 ); 12 stars = new StarField( false, 200 );
13 13
14 loadText(); 14 loadText();
15 15
16 init(); 16 init();
17} 17}
18 18
19Help :: ~Help() 19Help :: ~Help()
20{ 20{
21 delete stars; 21 delete stars;
22} 22}
23 23
24void Help :: handleKeys( SDL_KeyboardEvent &key ) 24void Help :: handleKeys( SDL_KeyboardEvent &key )
25{ 25{
26 if ( key.type == SDL_KEYDOWN ) 26 if ( key.type == SDL_KEYDOWN )
27 { 27 {
28 if ( key.keysym.sym == SDLK_SPACE ) 28 if ( key.keysym.sym == SDLK_SPACE )
29 parent->changeState( STATE_MENU ); 29 parent->changeState( STATE_MENU );
30 else if ( key.keysym.sym == SDLK_DOWN ) 30 else if ( key.keysym.sym == SDLK_DOWN )
31 textSpeed = 5; 31 textSpeed = 5;
32 else if ( key.keysym.sym == SDLK_UP ) 32 else if ( key.keysym.sym == SDLK_UP )
33 { 33 {
34 if ( textSpeed > 0 ) 34 if ( textSpeed > 0 )
35 textSpeed = 0; 35 textSpeed = 0;
36 else textSpeed = 1; 36 else textSpeed = 1;
37 } 37 }
38 38
39 } 39 }
40 else if ( key.type == SDL_KEYUP ) 40 else if ( key.type == SDL_KEYUP )
41 { 41 {
42 if ( key.keysym.sym == SDLK_DOWN ) 42 if ( key.keysym.sym == SDLK_DOWN )
43 textSpeed = 1; 43 textSpeed = 1;
44 } 44 }
45} 45}
46void Help :: init() 46void Help :: init()
47{ 47{
48 startPos = 320; 48 startPos = 320;
49 currLine = 0; 49 currLine = 0;
50 textSpeed = 1; 50 textSpeed = 1;
51 51
52 // Create our coloured font 52 // Create our coloured font
53 FontHandler :: changeColor( FONT_HELP_FONT, 0, 0, 255 ); 53 FontHandler :: changeColor( FONT_HELP_FONT, 0, 0, 255 );
54} 54}
55 55
56void Help :: draw( SDL_Surface *screen ) 56void Help :: draw( SDL_Surface *screen )
57{ 57{
58 stars->draw( screen ); 58 stars->draw( screen );
59 59
60
61 list<string>::iterator it = textList.begin(); 60 list<string>::iterator it = textList.begin();
62 61
63 // Move to start of text 62 // Move to start of text
64 for ( int i = 0 ; i < currLine && it != textList.end() ; ++i ) 63 for ( int i = 0 ; i < currLine && it != textList.end() ; ++i )
65 it++; 64 it++;
66 65
67 int pos = startPos; 66 int pos = startPos;
68 while ( pos < 320 && it != textList.end() ) 67 while ( pos < 320 && it != textList.end() )
69 { 68 {
70 // get next line 69 // get next line
71 string text = *it; 70 string text = *it;
72 71
73 // draw text 72 // draw text
74 FontHandler::draw( screen, FONT_COLOURED_TEXT, text.c_str(), -1, pos ); 73 FontHandler::draw( screen, FONT_COLOURED_TEXT, text.c_str(), -1, pos );
75 pos += FontHandler::FontHeight( FONT_COLOURED_TEXT ); 74 pos += FontHandler::FontHeight( FONT_COLOURED_TEXT );
76 it ++; 75 it ++;
77 } 76 }
78 77
79} 78}
80 79
81void Help :: update() 80void Help :: update()
82{ 81{
83 stars->move(); 82 stars->move();
84 83
85 startPos -= textSpeed; 84 startPos -= textSpeed;
86 if ( startPos <= -FontHandler::FontHeight( FONT_COLOURED_TEXT ) ) 85 if ( startPos <= -FontHandler::FontHeight( FONT_COLOURED_TEXT ) )
87 { 86 {
88 startPos = 0; 87 startPos = 0;
89 currLine ++; 88 currLine ++;
90 89
91 if ( currLine > textList.size() ) 90 if ( currLine > textList.size() )
92 { 91 {
93 startPos = 320; 92 startPos = 320;
94 currLine = 0; 93 currLine = 0;
95 } 94 }
96 } 95 }
97 96
98} 97}
99 98
100void Help :: loadText() 99void Help :: loadText()
101{ 100{
102 textList.push_back( "SFCave" ); 101 textList.push_back( "SFCave" );
103 textList.push_back( "Written By AndyQ" ); 102 textList.push_back( "Written By AndyQ" );
104 textList.push_back( "" ); 103 textList.push_back( "" );
105 textList.push_back( "Instructions" ); 104 textList.push_back( "Instructions" );
106 textList.push_back( "To return to the menu" ); 105 textList.push_back( "To return to the menu" );
107 textList.push_back( "press the space or " ); 106 textList.push_back( "press the space or " );
108 textList.push_back( "middle button." ); 107 textList.push_back( "middle button." );
109 textList.push_back( "" ); 108 textList.push_back( "" );
110 textList.push_back( "To speed up the text" ); 109 textList.push_back( "To speed up the text" );
111 textList.push_back( "hold the down button" ); 110 textList.push_back( "hold the down button" );
112 textList.push_back( "(releasing will return" ); 111 textList.push_back( "(releasing will return" );
113 textList.push_back( "to normal speed)" ); 112 textList.push_back( "to normal speed)" );
114 textList.push_back( "" ); 113 textList.push_back( "" );
115 textList.push_back( "" ); 114 textList.push_back( "" );
116 textList.push_back( "SFCave is a flying game" ); 115 textList.push_back( "SFCave is a flying game" );
117 textList.push_back( "writtin originally for the" ); 116 textList.push_back( "writtin originally for the" );
118 textList.push_back( "Sharp Zaurus." ); 117 textList.push_back( "Sharp Zaurus." );
119 textList.push_back( "" ); 118 textList.push_back( "" );
120 textList.push_back( "The aim is to stay alive" ); 119 textList.push_back( "The aim is to stay alive" );
121 textList.push_back( "for as long as possible," ); 120 textList.push_back( "for as long as possible," );
122 textList.push_back( "and get the highest score" ); 121 textList.push_back( "and get the highest score" );
123 textList.push_back( "you can." ); 122 textList.push_back( "you can." );
124 textList.push_back( "" ); 123 textList.push_back( "" );
125 textList.push_back( "There are currently three" ); 124 textList.push_back( "There are currently three" );
126 textList.push_back( "game types - SFCave," ); 125 textList.push_back( "game types - SFCave," );
127 textList.push_back( "Gates, and Fly." ); 126 textList.push_back( "Gates, and Fly." );
128 textList.push_back( "" ); 127 textList.push_back( "" );
129 textList.push_back( "SFCave is a remake of" ); 128 textList.push_back( "SFCave is a remake of" );
130 textList.push_back( "the classic SFCave game." ); 129 textList.push_back( "the classic SFCave game." );
131 textList.push_back( "Fly through the cavern" ); 130 textList.push_back( "Fly through the cavern" );
132 textList.push_back( "avoiding all the blocks" ); 131 textList.push_back( "avoiding all the blocks" );
133 textList.push_back( "that just happen to be" ); 132 textList.push_back( "that just happen to be" );
134 textList.push_back( "hanging in mid-air" ); 133 textList.push_back( "hanging in mid-air" );
135 textList.push_back( "" ); 134 textList.push_back( "" );
136 textList.push_back( "Gates is similar to" ); 135 textList.push_back( "Gates is similar to" );
137 textList.push_back( "SFCave but instead of" ); 136 textList.push_back( "SFCave but instead of" );
138 textList.push_back( "avoiding blocks you must" ); 137 textList.push_back( "avoiding blocks you must" );
139 textList.push_back( "fly through gates without" ); 138 textList.push_back( "fly through gates without" );
140 textList.push_back( "crashing." ); 139 textList.push_back( "crashing." );
141 textList.push_back( "" ); 140 textList.push_back( "" );
142 textList.push_back( "Fly is a different kettle of" ); 141 textList.push_back( "Fly is a different kettle of" );
143 textList.push_back( "fish altogether. Instead," ); 142 textList.push_back( "fish altogether. Instead," );
144 textList.push_back( "you are flying in the " ); 143 textList.push_back( "you are flying in the " );
145 textList.push_back( "open air above a" ); 144 textList.push_back( "open air above a" );
146 textList.push_back( "scrolling landscape and" ); 145 textList.push_back( "scrolling landscape and" );
147 textList.push_back( "the aim is to fly as close" ); 146 textList.push_back( "the aim is to fly as close" );
148 textList.push_back( "to the land as possible." ); 147 textList.push_back( "to the land as possible." );
149 textList.push_back( "The closer to the land" ); 148 textList.push_back( "The closer to the land" );
150 textList.push_back( "you fly the more points" ); 149 textList.push_back( "you fly the more points" );
151 textList.push_back( "you score. But beware," ); 150 textList.push_back( "you score. But beware," );
152 textList.push_back( "fly too high above the" ); 151 textList.push_back( "fly too high above the" );
153 textList.push_back( "land and points get" ); 152 textList.push_back( "land and points get" );
154 textList.push_back( "deducted." ); 153 textList.push_back( "deducted." );
155 textList.push_back( "" ); 154 textList.push_back( "" );
156 textList.push_back( "How to play" ); 155 textList.push_back( "How to play" );
157 textList.push_back( "Press the space or middle" ); 156 textList.push_back( "Press the space or middle" );
158 textList.push_back( "button (Zaurus only) to " ); 157 textList.push_back( "button (Zaurus only) to " );
159 textList.push_back( "apply thrust (makes you" ); 158 textList.push_back( "apply thrust (makes you" );
160 textList.push_back( "go up) and release it" ); 159 textList.push_back( "go up) and release it" );
161 textList.push_back( "to go down." ); 160 textList.push_back( "to go down." );
162 textList.push_back( "" ); 161 textList.push_back( "" );
163 textList.push_back( "Have fun" ); 162 textList.push_back( "Have fun" );
164 textList.push_back( "AndyQ" ); 163 textList.push_back( "AndyQ" );
165} 164}
166 165
167// Test 166// Test
168#ifdef DEBUG_HELP 167#ifdef DEBUG_HELP
169SDL_Surface *screen; 168SDL_Surface *screen;
170Help *help; 169Help *help;
171 170
172void go() 171void go()
173{ 172{
174 FontHandler :: init(); 173 FontHandler :: init();
175 174
176 /* Initialize SDL */ 175 /* Initialize SDL */
177 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) 176 if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
178 { 177 {
179 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 178 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
180 exit(1); 179 exit(1);
181 } 180 }
182 atexit(SDL_Quit); 181 atexit(SDL_Quit);
183 182
184 int videoflags = SDL_SWSURFACE ; 183 int videoflags = SDL_SWSURFACE ;
185 184
186 if ( (screen=SDL_SetVideoMode(240, 320,32,videoflags)) == NULL ) 185 if ( (screen=SDL_SetVideoMode(240, 320,32,videoflags)) == NULL )
187 { 186 {
188 fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",240,320,SDL_GetError()); 187 fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",240,320,SDL_GetError());
189 exit(2); 188 exit(2);
190 } 189 }
191 190
192 help = new Help(); 191 help = new Help();
193 192
194 bool done = false; 193 bool done = false;
195 while ( !done ) 194 while ( !done )
196 { 195 {
197 SDL_FillRect( screen, 0, 0 ); 196 SDL_FillRect( screen, 0, 0 );
198 help->draw( screen ); 197 help->draw( screen );
199 help->update( ); 198 help->update( );
200 199
201 SDL_Flip( screen ); 200 SDL_Flip( screen );
202 201
203 SDL_Delay( 10 ); 202 SDL_Delay( 10 );
204 203
205 SDL_Event event; 204 SDL_Event event;
206 while ( SDL_PollEvent(&event) ) 205 while ( SDL_PollEvent(&event) )
207 { 206 {
208 switch (event.type) 207 switch (event.type)
209 { 208 {
210 case SDL_KEYDOWN: 209 case SDL_KEYDOWN:
211 // Escape keypress quits the app 210 // Escape keypress quits the app
212 if ( event.key.keysym.sym != SDLK_ESCAPE ) 211 if ( event.key.keysym.sym != SDLK_ESCAPE )
213 { 212 {
214 break; 213 break;
215 } 214 }
216 case SDL_QUIT: 215 case SDL_QUIT:
217 done = 1; 216 done = 1;
218 break; 217 break;
219 default: 218 default:
220 break; 219 break;
221 } 220 }
222 } 221 }
223 } 222 }
224 } 223 }
225 224
226 225
227 226
228 227
229#ifdef __cplusplus 228#ifdef __cplusplus
230extern "C" 229extern "C"
231#endif 230#endif
232int main( int argc, char *argv[] ) 231int main( int argc, char *argv[] )
233{ 232{
234 go(); 233 go();
235} 234}
236 235
237#endif 236#endif
diff --git a/noncore/games/sfcave-sdl/menu.cpp b/noncore/games/sfcave-sdl/menu.cpp
index 0a7366f..a4a4216 100644
--- a/noncore/games/sfcave-sdl/menu.cpp
+++ b/noncore/games/sfcave-sdl/menu.cpp
@@ -1,337 +1,360 @@
1#include <SDL_image.h> 1#include <SDL_image.h>
2#include "SDL_rotozoom.h" 2#include "SDL_rotozoom.h"
3 3
4#include "constants.h" 4#include "constants.h"
5#include "sfcave.h" 5#include "sfcave.h"
6#include "game.h" 6#include "game.h"
7#include "menu.h" 7#include "menu.h"
8#include "font.h" 8#include "font.h"
9#include "starfield.h" 9#include "starfield.h"
10 10
11MenuOption :: MenuOption( QString text, int id ) 11MenuOption :: MenuOption( string text, int id )
12{ 12{
13 menuText = text; 13 menuText = text;
14 menuId = id; 14 menuId = id;
15 nextMenu = 0; 15 nextMenu = 0;
16 highlighted = false; 16 highlighted = false;
17} 17}
18 18
19MenuOption :: ~MenuOption() 19MenuOption :: ~MenuOption()
20{ 20{
21} 21}
22 22
23 23
24int MenuOption :: draw( SDL_Surface *screen, int y ) 24int MenuOption :: draw( SDL_Surface *screen, int y )
25{ 25{
26 if ( highlighted ) 26 if ( highlighted )
27 { 27 {
28 int x = (240 - FontHandler::TextWidth( FONT_MENU_HIGHLIGHTED, (const char *)menuText.c_str() ))/2; 28 int x = (240 - FontHandler::TextWidth( FONT_MENU_HIGHLIGHTED, (const char *)menuText.c_str() ))/2;
29 FontHandler::draw( screen, FONT_MENU_HIGHLIGHTED, (const char *)menuText.c_str(), x, y ); 29 FontHandler::draw( screen, FONT_MENU_HIGHLIGHTED, (const char *)menuText.c_str(), x, y );
30 return FontHandler::FontHeight( FONT_MENU_HIGHLIGHTED ); 30 return FontHandler::FontHeight( FONT_MENU_HIGHLIGHTED );
31 } 31 }
32 else 32 else
33 { 33 {
34 int x = (240 - FontHandler::TextWidth( FONT_MENU_UNHIGHLIGHTED, (const char *)menuText.c_str() ))/2; 34 int x = (240 - FontHandler::TextWidth( FONT_MENU_UNHIGHLIGHTED, (const char *)menuText.c_str() ))/2;
35 FontHandler::draw( screen, FONT_MENU_UNHIGHLIGHTED, (const char *)menuText.c_str(), x, y ); 35 FontHandler::draw( screen, FONT_MENU_UNHIGHLIGHTED, (const char *)menuText.c_str(), x, y );
36 return FontHandler::FontHeight( FONT_MENU_UNHIGHLIGHTED ); 36 return FontHandler::FontHeight( FONT_MENU_UNHIGHLIGHTED );
37 } 37 }
38} 38}
39 39
40void MenuOption :: setNextMenu( Menu *item, bool down ) 40void MenuOption :: setNextMenu( Menu *item, bool down )
41{ 41{
42 nextMenu = item; 42 nextMenu = item;
43 downMenuTree = down; 43 downMenuTree = down;
44} 44}
45 45
46 46
47 47
48//----------------- Menu Class ------------- 48//----------------- Menu Class -------------
49 49
50SDL_Surface * Menu :: sfcaveTextImage; 50SDL_Surface * Menu :: sfcaveTextImage;
51Menu *Menu :: mainMenu; 51Menu *Menu :: mainMenu;
52Menu *Menu :: currentMenu; 52Menu *Menu :: currentMenu;
53 53
54// This is the Master Menu constructor 54// This is the Master Menu constructor
55Menu :: Menu( SFCave *p ) 55Menu :: Menu( SFCave *p )
56{ 56{
57 parent = p; 57 parent = p;
58 parentMenu = 0; 58 parentMenu = 0;
59 statusText = ""; 59 statusText = "";
60 60
61 //listItems.setAutoDelete( TRUE ); 61 //listItems.setAutoDelete( TRUE );
62 62
63 SDL_Surface *tmp = IMG_Load( IMAGES_PATH "sfcave_text.bmp" ); 63 SDL_Surface *tmp = IMG_Load( IMAGES_PATH "sfcave_text.bmp" );
64 sfcaveTextImage = SDL_CreateRGBSurface(SDL_SWSURFACE, tmp->w, tmp->h, 32, 64 sfcaveTextImage = SDL_CreateRGBSurface(SDL_SWSURFACE, tmp->w, tmp->h, 32,
65 0x000000ff,0x0000ff00, 0x00ff0000, 0xff000000); 65 0x000000ff,0x0000ff00, 0x00ff0000, 0xff000000);
66 SDL_BlitSurface(tmp, NULL, sfcaveTextImage, NULL); 66 SDL_BlitSurface(tmp, NULL, sfcaveTextImage, NULL);
67 SDL_FreeSurface(tmp); 67 SDL_FreeSurface(tmp);
68 68
69 // Create menu structure 69 // Create menu structure
70 // Top level menu has 5 items - Start Game, Replays, Options, Help, and Quit 70 // Top level menu has 5 items - Start Game, Replays, Options, Help, and Quit
71 // Replays, Option menu items hav submenus 71 // Replays, Option menu items hav submenus
72 MenuOption *replayMenu = 0; 72 MenuOption *replayMenu = 0;
73 MenuOption *optionsMenu = 0; 73 MenuOption *optionsMenu = 0;
74 MenuOption *item = 0; 74 MenuOption *item = 0;
75 addMenuOption( "Start Game", MENU_STARTGAME ); 75 addMenuOption( "Start Game", MENU_STARTGAME );
76 replayMenu = addMenuOption( "Replays", MENU_REPLAYS ); 76 replayMenu = addMenuOption( "Replays", MENU_REPLAYS );
77 optionsMenu = addMenuOption( "Options", MENU_OPTIONS ); 77 optionsMenu = addMenuOption( "Options", MENU_OPTIONS );
78 addMenuOption( "Help", MENU_HELP ); 78 addMenuOption( "Help", MENU_HELP );
79 addMenuOption( "Quit", MENU_QUIT ); 79 addMenuOption( "Quit", MENU_QUIT );
80 80
81 // Now deal with the Replays Menu 81 // Now deal with the Replays Menu
82 Menu *replay = new Menu( this ); 82 Menu *replay = new Menu( this );
83 replay->addMenuOption( "Play Replay", MENU_PLAY_REPLAY ); 83 replay->addMenuOption( "Play Replay", MENU_PLAY_REPLAY );
84 replay->addMenuOption( "Load Replay", MENU_LOAD_REPLAY ); 84 replay->addMenuOption( "Load Replay", MENU_LOAD_REPLAY );
85 replay->addMenuOption( "Save Replay", MENU_SAVE_REPLAY ); 85 replay->addMenuOption( "Save Replay", MENU_SAVE_REPLAY );
86 item = replay->addMenuOption( "Back", MENU_BACK ); 86 item = replay->addMenuOption( "Back", MENU_BACK );
87 item->setNextMenu( this, false ); 87 item->setNextMenu( this, false );
88 replayMenu->setNextMenu( replay ); 88 replayMenu->setNextMenu( replay );
89 89
90 // Now deal with the Options MenucurrentMenu->currentMenuOption->setHighlighted( false ); 90 // Now deal with the Options MenucurrentMenu->currentMenuOption->setHighlighted( false );
91 listItems.front()->highlight( true ); 91 listItems.front()->highlight( true );
92 92
93 Menu *options = new Menu( this ); 93 Menu *options = new Menu( this );
94 MenuOption *typeMenu = 0; 94 MenuOption *typeMenu = 0;
95 MenuOption *difficultyMenu = 0; 95 MenuOption *difficultyMenu = 0;
96 MenuOption *soundsMenu = 0; 96 MenuOption *soundsMenu = 0;
97 typeMenu = options->addMenuOption( "Game Type", MENU_GAME_TYPE ); 97 typeMenu = options->addMenuOption( "Game Type", MENU_GAME_TYPE );
98 difficultyMenu = options->addMenuOption( "Difficulty", MENU_DIFFICULTY ); 98 difficultyMenu = options->addMenuOption( "Difficulty", MENU_DIFFICULTY );
99 soundsMenu = options->addMenuOption( "Sound", MENU_SOUNDS ); 99 soundsMenu = options->addMenuOption( "Sound", MENU_SOUNDS );
100 options->addMenuOption( "Clear Scores", MENU_CLEAR_SCORES ); 100 options->addMenuOption( "Clear Scores", MENU_CLEAR_SCORES );
101 item = options->addMenuOption( "Back", MENU_BACK ); 101 item = options->addMenuOption( "Back", MENU_BACK );
102 item->setNextMenu( this, false ); 102 item->setNextMenu( this, false );
103 optionsMenu->setNextMenu( options ); 103 optionsMenu->setNextMenu( options );
104 104
105 // Game Type menu 105 // Game Type menu
106 Menu *gameType = new Menu( options ); 106 Menu *gameType = new Menu( options );
107 item = gameType->addMenuOption( "SFCave", MENU_GAME_SFCAVE ); 107 item = gameType->addMenuOption( "SFCave", MENU_GAME_SFCAVE );
108 item->setNextMenu( options ); 108 item->setNextMenu( options );
109 item = gameType->addMenuOption( "Gates", MENU_GAME_GATES ); 109 item = gameType->addMenuOption( "Gates", MENU_GAME_GATES );
110 item->setNextMenu( options ); 110 item->setNextMenu( options );
111 item = gameType->addMenuOption( "Fly", MENU_GAME_FLY ); 111 item = gameType->addMenuOption( "Fly", MENU_GAME_FLY );
112 item->setNextMenu( options ); 112 item->setNextMenu( options );
113 item = gameType->addMenuOption( "Back", MENU_BACK ); 113 item = gameType->addMenuOption( "Back", MENU_BACK );
114 item->setNextMenu( options ); 114 item->setNextMenu( options );
115 typeMenu->setNextMenu( gameType ); 115 typeMenu->setNextMenu( gameType );
116 116
117 // Game Difficulty menu 117 // Game Difficulty menu
118 MenuOption *customMenu = 0;
118 Menu *gameDifficulty = new Menu( options ); 119 Menu *gameDifficulty = new Menu( options );
119 item = gameDifficulty->addMenuOption( "Easy", MENU_DIFFICULTY_EASY ); 120 item = gameDifficulty->addMenuOption( "Easy", MENU_DIFFICULTY_EASY );
120 item->setNextMenu( options, false ); 121 item->setNextMenu( options, false );
121 item = gameDifficulty->addMenuOption( "Normal", MENU_DIFFICULTY_NORMAL ); 122 item = gameDifficulty->addMenuOption( "Normal", MENU_DIFFICULTY_NORMAL );
122 item->setNextMenu( options, false ); 123 item->setNextMenu( options, false );
123 item = gameDifficulty->addMenuOption( "Hard", MENU_DIFFICULTY_HARD ); 124 item = gameDifficulty->addMenuOption( "Hard", MENU_DIFFICULTY_HARD );
124 item->setNextMenu( options, false ); 125 item->setNextMenu( options, false );
126 customMenu = gameDifficulty->addMenuOption( "Custom", MENU_DIFFICULTY_CUSTOM );
125 item = gameDifficulty->addMenuOption( "Back", MENU_BACK ); 127 item = gameDifficulty->addMenuOption( "Back", MENU_BACK );
126 item->setNextMenu( options, false ); 128 item->setNextMenu( options, false );
127 difficultyMenu->setNextMenu( gameDifficulty ); 129 difficultyMenu->setNextMenu( gameDifficulty );
128 130
129 // Sounds Menu 131 // Sounds Menu
130 Menu *sounds = new Menu( options ); 132 Menu *sounds = new Menu( options );
131 sounds->addMenuOption( "Sound On", MENU_SOUND_ON ); 133 sounds->addMenuOption( "Sound On", MENU_SOUND_ON );
132 sounds->addMenuOption( "Sound Off", MENU_SOUND_OFF ); 134 sounds->addMenuOption( "Sound Off", MENU_SOUND_OFF );
133 sounds->addMenuOption( "Music On", MENU_MUSIC_ON ); 135 sounds->addMenuOption( "Music On", MENU_MUSIC_ON );
134 sounds->addMenuOption( "Music Off", MENU_MUSIC_OFF ); 136 sounds->addMenuOption( "Music Off", MENU_MUSIC_OFF );
135 item = sounds->addMenuOption( "Back", MENU_BACK ); 137 item = sounds->addMenuOption( "Back", MENU_BACK );
136 item->setNextMenu( options, false ); 138 item->setNextMenu( options, false );
137 soundsMenu->setNextMenu( sounds ); 139 soundsMenu->setNextMenu( sounds );
140
141 // Custom Menu
142 Menu *custom = new Menu( gameDifficulty );
143 Menu *updown = new Menu( custom );
144 item = custom->addMenuOption( "Thrust", MENU_CUSTOM_THRUST );
145 item->setNextMenu( updown );
146 item = custom->addMenuOption( "Gravity", MENU_CUSTOM_GRAVITY );
147 item->setNextMenu( updown );
148 item = custom->addMenuOption( "Max Speed Up", MENU_CUSTOM_MAXSPEEDUP );
149 item->setNextMenu( updown );
150 item = custom->addMenuOption( "Max Speed Down", MENU_CUSTOM_MAXSPEEDDOWN );
151 item->setNextMenu( updown );
152 item = custom->addMenuOption( "Back", MENU_BACK );
153 item->setNextMenu( gameDifficulty, false );
154 customMenu->setNextMenu( custom );
155
156 // Up down menu
157 item = updown->addMenuOption( "Increase", MENU_CUSTOM_INCREASE );
158 item = updown->addMenuOption( "Decrease", MENU_CUSTOM_DECREASE );
159 item = updown->addMenuOption( "Save", MENU_CUSTOM_SAVE );
160 item->setNextMenu( custom, false );
161 item = updown->addMenuOption( "Cancel", MENU_CUSTOM_CANCEL );
162 item->setNextMenu( custom, false );
138 163
139 // Set static variables for menu selection up 164 // Set static variables for menu selection up
140 mainMenu = this; 165 mainMenu = this;
141 currentMenuOption = 0; 166 currentMenuOption = 0;
142 167
143 resetToTopMenu(); 168 resetToTopMenu();
144 169
145 angle = 0; 170 angle = 0;
146 171
147 stars = new StarField; 172 stars = new StarField;
148} 173}
149 174
150// This is a private constructor used for creating sub menus - only called by the Master Menu 175// This is a private constructor used for creating sub menus - only called by the Master Menu
151Menu :: Menu( Menu *p ) 176Menu :: Menu( Menu *p )
152{ 177{
153 parentMenu = p; 178 parentMenu = p;
154 //listItems.setAutoDelete( TRUE ); 179 //listItems.setAutoDelete( TRUE );
155 currentMenuOption = 0; 180 currentMenuOption = 0;
156} 181}
157 182
158Menu :: ~Menu() 183Menu :: ~Menu()
159{ 184{
160 if ( this == mainMenu ) 185 if ( this == mainMenu )
161 { 186 {
162 SDL_FreeSurface( sfcaveTextImage ); 187 SDL_FreeSurface( sfcaveTextImage );
163 delete stars; 188 delete stars;
164 } 189 }
165} 190}
166 191
167void Menu :: draw( SDL_Surface *screen ) 192void Menu :: draw( SDL_Surface *screen )
168{ 193{
169 // draw stafield 194 // draw stafield
170 stars->draw( screen ); 195 stars->draw( screen );
171 stars->move( ); 196 stars->move( );
172 197
173 // Draw the spinning SFCave logo 198 // Draw the spinning SFCave logo
174 SDL_Surface *rotozoom_pic; 199 SDL_Surface *rotozoom_pic;
175 SDL_Rect dest; 200 SDL_Rect dest;
176 201
177 angle += 2; 202 angle += 2;
178 if ( angle > 359 ) 203 if ( angle > 359 )
179 angle = 0; 204 angle = 0;
180 if ((rotozoom_pic=rotozoomSurface (sfcaveTextImage, angle*1, 1, SMOOTHING_ON))!=NULL) 205 if ((rotozoom_pic=rotozoomSurface (sfcaveTextImage, angle*1, 1, SMOOTHING_ON))!=NULL)
181 { 206 {
182 dest.x = (screen->w - rotozoom_pic->w)/2; 207 dest.x = (screen->w - rotozoom_pic->w)/2;
183 dest.y = 10; 208 dest.y = 10;
184 dest.w = rotozoom_pic->w; 209 dest.w = rotozoom_pic->w;
185 dest.h = rotozoom_pic->h; 210 dest.h = rotozoom_pic->h;
186 SDL_BlitSurface( rotozoom_pic, NULL, screen, &dest ); 211 SDL_BlitSurface( rotozoom_pic, NULL, screen, &dest );
187 SDL_FreeSurface(rotozoom_pic); 212 SDL_FreeSurface(rotozoom_pic);
188 } 213 }
189 214
190 // Draw what game is selected 215 // Draw what game is selected
191 char text[100]; 216 char text[100];
192 sprintf( text, "Current Game: %s", (const char *)parent->getCurrentGame()->getGameName().c_str() ); 217 sprintf( text, "Current Game: %s", (const char *)parent->getCurrentGame()->getGameName().c_str() );
193 //Menu::scoreFont.PutString( screen, (240 - Menu::scoreFont.TextWidth( (const char *)text ))/2, 120, (const char *)text ); 218 //Menu::scoreFont.PutString( screen, (240 - Menu::scoreFont.TextWidth( (const char *)text ))/2, 120, (const char *)text );
194 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)text, (240 - FontHandler::TextWidth( FONT_WHITE_TEXT,(const char *)text ))/2, 120 ); 219 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)text, (240 - FontHandler::TextWidth( FONT_WHITE_TEXT,(const char *)text ))/2, 120 );
195 sprintf( text, "Difficulty: %s", (const char *)parent->getCurrentGame()->getGameDifficultyText().c_str() ); 220 sprintf( text, "Difficulty: %s", (const char *)parent->getCurrentGame()->getGameDifficultyText().c_str() );
196 //Menu::scoreFont.PutString( screen, (240 - Menu::scoreFont.TextWidth( (const char *)text ))/2, 120 + Menu::scoreFont.FontHeight()+2, (const char *)text ); 221 //Menu::scoreFont.PutString( screen, (240 - Menu::scoreFont.TextWidth( (const char *)text ))/2, 120 + Menu::scoreFont.FontHeight()+2, (const char *)text );
197 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)text, (240 - FontHandler::TextWidth( FONT_WHITE_TEXT,(const char *)text ))/2, 120 + FontHandler::FontHeight( FONT_WHITE_TEXT ) ); 222 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)text, (240 - FontHandler::TextWidth( FONT_WHITE_TEXT,(const char *)text ))/2, 120 + FontHandler::FontHeight( FONT_WHITE_TEXT ) );
198 223
199 if ( statusText != "" ) 224 if ( statusText != "" )
200 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)statusText.c_str(), (240 - FontHandler::TextWidth( FONT_WHITE_TEXT,(const char *)statusText.c_str() ))/2, 120 + (2*FontHandler::FontHeight( FONT_WHITE_TEXT ))+6 ); 225 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)statusText.c_str(), (240 - FontHandler::TextWidth( FONT_WHITE_TEXT,(const char *)statusText.c_str() ))/2, 120 + (2*FontHandler::FontHeight( FONT_WHITE_TEXT ))+6 );
201 // Menu::scoreFont.PutString( screen, (240 - Menu::scoreFont.TextWidth( (const char *)statusText ))/2, 120 + (Menu::scoreFont.FontHeight()*2)+6, (const char *)statusText ); 226 // Menu::scoreFont.PutString( screen, (240 - Menu::scoreFont.TextWidth( (const char *)statusText ))/2, 120 + (Menu::scoreFont.FontHeight()*2)+6, (const char *)statusText );
202 227
203 228
204 // Loop round each menu option and draw it 229 // Loop round each menu option and draw it
205 int y = 155; 230 int y = 155;
206 list<MenuOption *>::iterator it; 231 list<MenuOption *>::iterator it;
207 for ( it = currentMenu->listItems.begin(); it != currentMenu->listItems.end() ; ++it ) 232 for ( it = currentMenu->listItems.begin(); it != currentMenu->listItems.end() ; ++it )
208 { 233 {
209 y += (*it)->draw( screen, y ) + 2; 234 y += (*it)->draw( screen, y ) + 2;
210 } 235 }
211} 236}
212 237
213int Menu :: handleKeys( SDL_KeyboardEvent &key ) 238int Menu :: handleKeys( SDL_KeyboardEvent &key )
214{ 239{
215 if ( key.type != SDL_KEYDOWN ) 240 if ( key.type != SDL_KEYDOWN )
216 return -1; 241 return -1;
217 242
218 statusText = "";
219 switch( key.keysym.sym ) 243 switch( key.keysym.sym )
220 { 244 {
221 case SDLK_DOWN: 245 case SDLK_DOWN:
222 { 246 {
223 // Move to next menu item 247 // Move to next menu item
224 currentMenu->currentMenuOption->highlight( false ); 248 currentMenu->currentMenuOption->highlight( false );
225 249
226 list<MenuOption *>::iterator it; 250 list<MenuOption *>::iterator it;
227 for ( it = currentMenu->listItems.begin(); it != currentMenu->listItems.end() ; ++it ) 251 for ( it = currentMenu->listItems.begin(); it != currentMenu->listItems.end() ; ++it )
228 { 252 {
229 if ( (*it) == currentMenu->currentMenuOption ) 253 if ( (*it) == currentMenu->currentMenuOption )
230 { 254 {
231 it++; 255 it++;
232 break; 256 break;
233 } 257 }
234 } 258 }
235 259
236 if ( it == currentMenu->listItems.end() ) 260 if ( it == currentMenu->listItems.end() )
237 it = currentMenu->listItems.begin(); 261 it = currentMenu->listItems.begin();
238 262
239 currentMenu->currentMenuOption = *it; 263 currentMenu->currentMenuOption = *it;
240 currentMenu->currentMenuOption->highlight( true ); 264 currentMenu->currentMenuOption->highlight( true );
241 265
242 break; 266 break;
243 } 267 }
244 case SDLK_UP: 268 case SDLK_UP:
245 { 269 {
246 // Move to previous menu item 270 // Move to previous menu item
247 currentMenu->currentMenuOption->highlight( false ); 271 currentMenu->currentMenuOption->highlight( false );
248 list<MenuOption *>::iterator it; 272 list<MenuOption *>::iterator it;
249 bool reachedBeginning = false; 273 bool reachedBeginning = false;
250 for ( it = (currentMenu->listItems).end()--; ; --it ) 274 for ( it = (currentMenu->listItems).end()--; ; --it )
251 { 275 {
252 if ( (*it) == currentMenu->currentMenuOption ) 276 if ( (*it) == currentMenu->currentMenuOption )
253 { 277 {
254 278
255 if ( it == currentMenu->listItems.begin( ) ) 279 if ( it == currentMenu->listItems.begin( ) )
256 { 280 {
257 reachedBeginning = true; 281 reachedBeginning = true;
258 break; 282 break;
259 } 283 }
260 else 284 else
261 it--; 285 it--;
262 break; 286 break;
263 } 287 }
264 288
265 } 289 }
266 290
267 if ( reachedBeginning ) 291 if ( reachedBeginning )
268 currentMenu->currentMenuOption = currentMenu->listItems.back(); 292 currentMenu->currentMenuOption = currentMenu->listItems.back();
269 else 293 else
270 currentMenu->currentMenuOption = *it; 294 currentMenu->currentMenuOption = *it;
271 295
272 currentMenu->currentMenuOption->highlight( true ); 296 currentMenu->currentMenuOption->highlight( true );
273 297
274 break; 298 break;
275 } 299 }
276 case SDLK_LEFT: 300 case SDLK_LEFT:
277 if ( currentMenu->parentMenu != 0 ) 301 if ( currentMenu->parentMenu != 0 )
278 { 302 {
303 statusText = "";
279 currentMenu = currentMenu->parentMenu; 304 currentMenu = currentMenu->parentMenu;
280 printf( "HERE\n" );
281 305
282 return -1; 306 return -1;
283 } 307 }
284 break; 308 break;
285 309
286 case SDLK_RETURN: 310 case SDLK_RETURN:
287 case SDLK_SPACE: 311 case SDLK_SPACE:
288 { 312 {
313 statusText = "";
289 // select menu item 314 // select menu item
290 int id = currentMenu->currentMenuOption->getMenuId(); 315 int id = currentMenu->currentMenuOption->getMenuId();
291 // // if the current item has a child menu then move to that menu 316
317 // if the current item has a child menu then move to that menu
292 Menu *next = currentMenu->currentMenuOption->getNextMenu(); 318 Menu *next = currentMenu->currentMenuOption->getNextMenu();
293 if ( next != 0 ) 319 if ( next != 0 )
294 { 320 {
295 bool down = currentMenu->currentMenuOption->isDownMenuTree(); 321 bool down = currentMenu->currentMenuOption->isDownMenuTree();
296 currentMenu = next; 322 currentMenu = next;
297 if ( down ) 323 if ( down )
298 initCurrentMenu(); 324 initCurrentMenu();
299 // return -1;
300 }
301 // else
302 {
303 return id;
304 } 325 }
305 326
327 return id;
328
306 break; 329 break;
307 } 330 }
308 331
309 default: 332 default:
310 break; 333 break;
311 } 334 }
312 335
313 return -1; 336 return -1;
314} 337}
315 338
316MenuOption *Menu :: addMenuOption( QString text, int id ) 339MenuOption *Menu :: addMenuOption( string text, int id )
317{ 340{
318 MenuOption *item = new MenuOption( text, id ); 341 MenuOption *item = new MenuOption( text, id );
319 342
320 listItems.push_back( item ); 343 listItems.push_back( item );
321 344
322 return item; 345 return item;
323} 346}
324 347
325void Menu :: resetToTopMenu() 348void Menu :: resetToTopMenu()
326{ 349{
327 currentMenu = mainMenu; 350 currentMenu = mainMenu;
328 initCurrentMenu(); 351 initCurrentMenu();
329} 352}
330 353
331void Menu :: initCurrentMenu() 354void Menu :: initCurrentMenu()
332{ 355{
333 if ( currentMenu->currentMenuOption != 0 ) 356 if ( currentMenu->currentMenuOption != 0 )
334 currentMenu->currentMenuOption->highlight( false ); 357 currentMenu->currentMenuOption->highlight( false );
335 currentMenu->currentMenuOption = currentMenu->listItems.front(); 358 currentMenu->currentMenuOption = currentMenu->listItems.front();
336 currentMenu->currentMenuOption->highlight( true ); 359 currentMenu->currentMenuOption->highlight( true );
337} 360}
diff --git a/noncore/games/sfcave-sdl/menu.h b/noncore/games/sfcave-sdl/menu.h
index 08f7528..6a5ef40 100644
--- a/noncore/games/sfcave-sdl/menu.h
+++ b/noncore/games/sfcave-sdl/menu.h
@@ -1,71 +1,71 @@
1#ifndef __MENU_H 1#ifndef __MENU_H
2#define __MENU_H 2#define __MENU_H
3 3
4#include <list> 4#include <list>
5using namespace std; 5using namespace std;
6 6
7#include <SDL.h> 7#include <SDL.h>
8 8
9class SFCave; 9class SFCave;
10class StarField; 10class StarField;
11class Menu; 11class Menu;
12 12
13class MenuOption 13class MenuOption
14{ 14{
15public: 15public:
16 MenuOption( QString text, int id ); 16 MenuOption( string text, int id );
17 ~MenuOption(); 17 ~MenuOption();
18 18
19 void highlight( bool val ) { highlighted = val; } 19 void highlight( bool val ) { highlighted = val; }
20 int draw( SDL_Surface *screen, int y ); 20 int draw( SDL_Surface *screen, int y );
21 void setNextMenu( Menu *item, bool down = true ); 21 void setNextMenu( Menu *item, bool down = true );
22 Menu *getNextMenu() { return nextMenu; } 22 Menu *getNextMenu() { return nextMenu; }
23 int getMenuId() { return menuId; } 23 int getMenuId() { return menuId; }
24 bool isDownMenuTree() { return downMenuTree; } 24 bool isDownMenuTree() { return downMenuTree; }
25 25
26private: 26private:
27 int menuId; 27 int menuId;
28 QString menuText; 28 string menuText;
29 bool highlighted; 29 bool highlighted;
30 bool downMenuTree; 30 bool downMenuTree;
31 31
32 Menu *nextMenu; 32 Menu *nextMenu;
33}; 33};
34 34
35class Menu 35class Menu
36{ 36{
37public: 37public:
38 Menu( SFCave *p ); 38 Menu( SFCave *p );
39 ~Menu(); 39 ~Menu();
40 40
41 void draw( SDL_Surface *screen ); 41 void draw( SDL_Surface *screen );
42 int handleKeys( SDL_KeyboardEvent & ); 42 int handleKeys( SDL_KeyboardEvent & );
43 MenuOption *addMenuOption( QString text, int id ); 43 MenuOption *addMenuOption( string text, int id );
44 void resetToTopMenu(); 44 void resetToTopMenu();
45 void initCurrentMenu(); 45 void initCurrentMenu();
46 46
47 void setStatusText( QString text ) { statusText = text; } 47 void setStatusText( string text ) { statusText = text; }
48 48
49protected: 49protected:
50 50
51private: 51private:
52 static SDL_Surface * sfcaveTextImage; 52 static SDL_Surface * sfcaveTextImage;
53 int angle; 53 int angle;
54 54
55 static Menu *mainMenu; 55 static Menu *mainMenu;
56 static Menu *currentMenu; 56 static Menu *currentMenu;
57 Menu *parentMenu; 57 Menu *parentMenu;
58 58
59 StarField *stars; 59 StarField *stars;
60 60
61 QString statusText; 61 string statusText;
62 62
63 SFCave *parent; 63 SFCave *parent;
64 list<MenuOption *> listItems; 64 list<MenuOption *> listItems;
65 MenuOption *currentMenuOption; 65 MenuOption *currentMenuOption;
66 66
67 Menu( Menu* p ); 67 Menu( Menu* p );
68}; 68};
69 69
70 70
71#endif 71#endif
diff --git a/noncore/games/sfcave-sdl/player.cpp b/noncore/games/sfcave-sdl/player.cpp
index 830ee78..2d52ae2 100644
--- a/noncore/games/sfcave-sdl/player.cpp
+++ b/noncore/games/sfcave-sdl/player.cpp
@@ -1,162 +1,284 @@
1#include <SDL.h> 1#include <SDL.h>
2#include "SDL_gfxPrimitives.h" 2#include "SDL_gfxPrimitives.h"
3 3
4#include "constants.h" 4#include "constants.h"
5#include "player.h" 5#include "player.h"
6#include "random.h" 6#include "random.h"
7#include "animatedimage.h" 7#include "animatedimage.h"
8 8
9Player :: Player( int w, int h ) 9Player :: Player( int w, int h )
10{ 10{
11 sWidth = w; 11 sWidth = w;
12 sHeight = h; 12 sHeight = h;
13 13
14 thrustUp = 0.4; 14 thrust = 0.4;
15 thrustDown = 0.6; 15 gravity = 0.6;
16 maxUpSpeed = 4.0; 16 maxUpSpeed = 4.0;
17 maxDownSpeed = 5.0; 17 maxDownSpeed = 5.0;
18 18
19 explosion = new AnimatedImage( IMAGES_PATH "explosion.bmp", 15 ); 19 explosion = new AnimatedImage( IMAGES_PATH "explosion.bmp", 15 );
20 init(); 20 init();
21} 21}
22 22
23Player :: ~Player() 23Player :: ~Player()
24{ 24{
25 if ( explosion ) 25 if ( explosion )
26 delete explosion; 26 delete explosion;
27} 27}
28 28
29void Player :: init() 29void Player :: init()
30{ 30{
31 // Set player position 31 // Set player position
32 pos.x( 50 ); 32 pos.x( 50 );
33 pos.y( sWidth/2 ); 33 pos.y( sWidth/2 );
34 pos.h( 2 ); 34 pos.h( 2 );
35 pos.w( 4 ); 35 pos.w( 4 );
36 thrust = 0; 36 thrust = 0;
37 crashing = false; 37 crashing = false;
38 crashLineLength = 0; 38 crashLineLength = 0;
39 crashed = false; 39 crashed = false;
40 explosion->reset(); 40 explosion->reset();
41 allFaded = false; 41 allFaded = false;
42 expNextFrame = false; 42 expNextFrame = false;
43 43
44 // Reset Trail 44 // Reset Trail
45 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 45 for ( int i = 0 ; i < TRAILSIZE ; ++i )
46 { 46 {
47 trail[i].x( -1 ); 47 trail[i].x( -1 );
48 trail[i].y( 0 ); 48 trail[i].y( 0 );
49 trail[i].w( 2 ); 49 trail[i].w( 2 );
50 trail[i].h( 2 ); 50 trail[i].h( 2 );
51 } 51 }
52} 52}
53 53
54void Player :: draw( SDL_Surface *screen ) 54void Player :: draw( SDL_Surface *screen )
55{ 55{
56 if ( !crashing ) 56 if ( !crashing )
57 { 57 {
58 // Draw Player 58 // Draw Player
59 // ellipseRGBA( screen, pos.x(), pos.y(), pos.x()+ pos.width(), pos.y()+pos.height(), 0, 255, 255, 255 ); 59 // ellipseRGBA( screen, pos.x(), pos.y(), pos.x()+ pos.width(), pos.y()+pos.height(), 0, 255, 255, 255 );
60 filledEllipseRGBA( screen, pos.x() + pos.w(), pos.y(), pos.w(), pos.h(), 0, 255, 255, 255 ); 60 filledEllipseRGBA( screen, pos.x() + pos.w(), pos.y(), pos.w(), pos.h(), 0, 255, 255, 255 );
61 61
62 // Draw Trail 62 // Draw Trail
63 drawTrails( screen ); 63 drawTrails( screen );
64 } 64 }
65 else 65 else
66 { 66 {
67 drawTrails( screen ); 67 drawTrails( screen );
68 68
69 if ( !crashed ) 69 if ( !crashed )
70 explosion->draw( screen, pos.x(), pos.y() ); 70 explosion->draw( screen, pos.x(), pos.y() );
71 } 71 }
72} 72}
73 73
74void Player :: drawTrails( SDL_Surface *screen ) 74void Player :: drawTrails( SDL_Surface *screen )
75{ 75{
76 if ( allFaded && crashing ) 76 if ( allFaded && crashing )
77 return; 77 return;
78 78
79 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 79 for ( int i = 0 ; i < TRAILSIZE ; ++i )
80 { 80 {
81 if ( trail[i].x() >= 0 ) 81 if ( trail[i].x() >= 0 )
82 { 82 {
83 // int r = (int) ((255.0/pos.x()) * (trail[i].x));
84 // int g = (int) ((150.0/pos.x()) * (trail[i].x));
85 int c = (int)((150.0/50) * (50.0 - (pos.x() - trail[i].x() ) )); 83 int c = (int)((150.0/50) * (50.0 - (pos.x() - trail[i].x() ) ));
86 // SDL_FillRect( screen, &trail[i], SDL_MapRGBA( screen->format, r, g, 0, 0 ) ); //(int)(1.5*c), 0, 255 ) );
87 boxRGBA( screen, trail[i].x(), trail[i].y(), trail[i].x() + 2, trail[i].y() + 2, 255, (int)(1.5*c), 0, c ); 84 boxRGBA( screen, trail[i].x(), trail[i].y(), trail[i].x() + 2, trail[i].y() + 2, 255, (int)(1.5*c), 0, c );
88 } 85 }
89 } 86 }
90} 87}
91 88
92void Player :: move( bool up ) 89void Player :: move( bool up )
93{ 90{
94 // Find enpty trail and move others 91 // Find enpty trail and move others
95 moveTrails(); 92 moveTrails();
96 93
97 if ( up ) 94 if ( up )
98 thrust -= thrustUp; 95 currentThrust -= thrust;
99 else 96 else
100 thrust += thrustDown; 97 currentThrust += gravity;
101 98
102 if ( thrust > maxDownSpeed ) 99 if ( currentThrust > maxDownSpeed )
103 thrust = maxDownSpeed; 100 currentThrust = maxDownSpeed;
104 else if ( thrust < -maxUpSpeed ) 101 else if ( currentThrust < -maxUpSpeed )
105 thrust = -maxUpSpeed; 102 currentThrust = -maxUpSpeed;
106 103
107 pos.moveBy( 0, (int)(thrust) ); 104 pos.moveBy( 0, (int)(currentThrust) );
108} 105}
109 106
110void Player :: moveTrails() 107void Player :: moveTrails()
111{ 108{
112 bool done = false; 109 bool done = false;
113 bool stillVisible = false; 110 bool stillVisible = false;
114 111
115 // Dont do anything here if all faded when were crashing 112 // Dont do anything here if all faded when were crashing
116 if ( allFaded && crashing ) 113 if ( allFaded && crashing )
117 return; 114 return;
118 115
119 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 116 for ( int i = 0 ; i < TRAILSIZE ; ++i )
120 { 117 {
121 if ( trail[i].x() < 0 ) 118 if ( trail[i].x() < 0 )
122 { 119 {
123 stillVisible = true; 120 stillVisible = true;
124 if ( !crashing && !done ) 121 if ( !crashing && !done )
125 { 122 {
126 trail[i].x( pos.x() - 5 ); 123 trail[i].x( pos.x() - 5 );
127 trail[i].y( pos.y() ); 124 trail[i].y( pos.y() );
128 done = true; 125 done = true;
129 } 126 }
130 } 127 }
131 else 128 else
132 trail[i].x( trail[i].x() - 1 ); 129 trail[i].x( trail[i].x() - 1 );
133 } 130 }
134 131
135 if ( !stillVisible ) 132 if ( !stillVisible )
136 allFaded = true; 133 allFaded = true;
137} 134}
138 135
139bool Player :: updateCrashing() 136bool Player :: updateCrashing()
140{ 137{
141 crashing = true; 138 crashing = true;
142 139
143 moveTrails(); 140 moveTrails();
144 if ( expNextFrame ) 141 if ( expNextFrame )
145 { 142 {
146 expNextFrame = false; 143 expNextFrame = false;
147 crashed = !explosion->nextFrame(); 144 crashed = !explosion->nextFrame();
148 } 145 }
149 else 146 else
150 expNextFrame = true; 147 expNextFrame = true;
151 148
152 return crashed; 149 return crashed;
153} 150}
154 151
155void Player :: setMovementInfo( double up, double down, double maxUp, double maxDown ) 152void Player :: setMovementInfo( double up, double grav, double maxUp, double maxDown )
156{ 153{
157 thrustUp = up; 154 thrust = up;
158 thrustDown = down; 155 gravity = grav;
159 maxUpSpeed = maxUp; 156 maxUpSpeed = maxUp;
160 maxDownSpeed = maxDown; 157 maxDownSpeed = maxDown;
161} 158}
162 159
160
161void Player :: incValue( int valueType )
162{
163 switch( valueType )
164 {
165 case PLAYER_THRUST:
166 thrust += 0.1;
167 break;
168 case PLAYER_GRAVITY:
169 gravity += 0.1;
170 break;
171 case PLAYER_MAX_SPEED_UP:
172 maxUpSpeed += 0.1;
173 break;
174 case PLAYER_MAX_SPEED_DOWN:
175 maxDownSpeed += 0.1;
176 break;
177 }
178}
179
180void Player :: decValue( int valueType )
181{
182 switch( valueType )
183 {
184 case PLAYER_THRUST:
185 thrust -= 0.1;
186 break;
187 case PLAYER_GRAVITY:
188 gravity -= 0.1;
189 break;
190 case PLAYER_MAX_SPEED_UP:
191 maxUpSpeed -= 0.1;
192 break;
193 case PLAYER_MAX_SPEED_DOWN:
194 maxDownSpeed -= 0.1;
195 break;
196 }
197}
198
199void Player :: setValue( int valueType, double val )
200{
201 switch( valueType )
202 {
203 case PLAYER_THRUST:
204 thrust = val;
205 break;
206 case PLAYER_GRAVITY:
207 gravity = val;
208 break;
209 case PLAYER_MAX_SPEED_UP:
210 maxUpSpeed = val;
211 break;
212 case PLAYER_MAX_SPEED_DOWN:
213 maxDownSpeed = val;
214 break;
215 }
216}
217
218double Player :: getValue( int valueType )
219{
220 double val;
221 switch( valueType )
222 {
223 case PLAYER_THRUST:
224 val = thrust;
225 break;
226 case PLAYER_GRAVITY:
227 val = gravity;
228 break;
229 case PLAYER_MAX_SPEED_UP:
230 val = maxUpSpeed;
231 break;
232 case PLAYER_MAX_SPEED_DOWN:
233 val = maxDownSpeed;
234 break;
235 }
236
237 return val;
238}
239
240string Player :: getValueTypeString( int valueType )
241{
242 string val;
243 switch( valueType )
244 {
245 case PLAYER_THRUST:
246 val = "thrust";
247 break;
248 case PLAYER_GRAVITY:
249 val = "gravity";
250 break;
251 case PLAYER_MAX_SPEED_UP:
252 val = "maxupspeed";
253 break;
254 case PLAYER_MAX_SPEED_DOWN:
255 val = "maxdownspeed";
256 break;
257 }
258
259 return val;
260}
261
262string Player :: getValueString( int valueType )
263{
264 char val[50];
265 switch( valueType )
266 {
267 case PLAYER_THRUST:
268 sprintf( val, "Thrust - %lf", thrust );
269 break;
270 case PLAYER_GRAVITY:
271 sprintf( val, "Gravity - %lf", gravity );
272 break;
273 case PLAYER_MAX_SPEED_UP:
274 sprintf( val, "Max Speed Up - %lf", maxUpSpeed );
275 break;
276 case PLAYER_MAX_SPEED_DOWN:
277 sprintf( val, "Max Speed Down - %lf", maxDownSpeed );
278 break;
279 }
280
281 string ret = val;
282 return ret;
283}
284
diff --git a/noncore/games/sfcave-sdl/player.h b/noncore/games/sfcave-sdl/player.h
index e4c904a..595c25b 100644
--- a/noncore/games/sfcave-sdl/player.h
+++ b/noncore/games/sfcave-sdl/player.h
@@ -1,50 +1,56 @@
1#ifndef __PLAYER_H 1#ifndef __PLAYER_H
2#define __PLAYER_H 2#define __PLAYER_H
3 3
4#include "rect.h" 4#include "rect.h"
5 5
6class SDL_Surface; 6class SDL_Surface;
7class AnimatedImage; 7class AnimatedImage;
8 8
9class Player 9class Player
10{ 10{
11public: 11public:
12 Player( int w, int h ); 12 Player( int w, int h );
13 ~Player(); 13 ~Player();
14 14
15 void init(); 15 void init();
16 void draw( SDL_Surface *screen ); 16 void draw( SDL_Surface *screen );
17 void drawTrails( SDL_Surface *screen ); 17 void drawTrails( SDL_Surface *screen );
18 void move( bool up ); 18 void move( bool up );
19 void moveTrails(); 19 void moveTrails();
20 Rect getPos() { return pos; } 20 Rect getPos() { return pos; }
21 int getX() { return pos.x(); } 21 int getX() { return pos.x(); }
22 int getY() { return pos.y(); } 22 int getY() { return pos.y(); }
23 int getHeight() { return pos.h(); } 23 int getHeight() { return pos.h(); }
24 bool updateCrashing(); 24 bool updateCrashing();
25 void setMovementInfo( double up, double down, double maxUp, double maxDown ); 25 void setMovementInfo( double up, double grav, double maxUp, double maxDown );
26 void incValue( int valType );
27 void decValue( int valType );
28 double getValue( int valueType );
29 string getValueString( int valueType );
30 string getValueTypeString( int valueType );
31 void setValue( int valueType, double val );
26 32
27private: 33private:
28 AnimatedImage *explosion; 34 AnimatedImage *explosion;
29 35
30 int sWidth; 36 int sWidth;
31 int sHeight; 37 int sHeight;
32 38
33 bool expNextFrame; 39 bool expNextFrame;
34 bool allFaded; 40 bool allFaded;
35 bool crashing; 41 bool crashing;
36 bool crashed; 42 bool crashed;
37 int crashLineLength; 43 int crashLineLength;
38 Rect pos; 44 Rect pos;
39 double thrust; 45 double currentThrust;
40 46
41 double thrustUp; 47 double thrust;
42 double thrustDown; 48 double gravity;
43 double maxUpSpeed; 49 double maxUpSpeed;
44 double maxDownSpeed; 50 double maxDownSpeed;
45 51
46 Rect trail[TRAILSIZE]; 52 Rect trail[TRAILSIZE];
47 53
48}; 54};
49 55
50#endif 56#endif
diff --git a/noncore/games/sfcave-sdl/settings.cpp b/noncore/games/sfcave-sdl/settings.cpp
index 914c4ec..20cce4f 100644
--- a/noncore/games/sfcave-sdl/settings.cpp
+++ b/noncore/games/sfcave-sdl/settings.cpp
@@ -1,273 +1,271 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <sys/stat.h> 3#include <sys/stat.h>
4#include <vector> 4#include <vector>
5 5
6#include "settings.h" 6#include "settings.h"
7 7
8// Defined in util.h
9string getHomeDir();
8 10
9#define DEFAULT_DIR "." 11#define DEFAULT_DIR "."
10#define DEFAULT_FILE "Settings.cfg" 12#define DEFAULT_FILE "Settings.cfg"
11#define MAX_LINE_SIZE 2048 13#define MAX_LINE_SIZE 2048
12 14
13 15
14Settings::Settings( char * env_file, char * env_dir ) 16Settings::Settings( char * env_file, char * env_dir )
15{ 17{
16 // Store the correct environment directory 18 // Store the correct environment directory
17 if (env_dir == NULL) 19 if (env_dir == NULL)
18 { 20 {
19 char * homeDir = getenv( "HOME" );; 21 envFile = getHomeDir();
20 22 envFile.append("/");
21 if ( homeDir )
22 {
23 envFile.append(homeDir);
24 envFile.append("/");
25 }
26 else
27 printf( "Environment var HOME not set!\n" );
28 23
29 envFile.append(DEFAULT_DIR); 24 envFile.append(DEFAULT_DIR);
30 } 25 }
31 else 26 else
32 envFile.append(env_dir); 27 envFile.append(env_dir);
33 28
34 envFile.append("/"); 29 envFile.append("/");
35 30
36 // Store the correct environment file 31 // Store the correct environment file
37 if (env_file == NULL) 32 if (env_file == NULL)
38 envFile.append(DEFAULT_FILE); 33 envFile.append(DEFAULT_FILE);
39 else 34 else
40 envFile.append(env_file); 35 envFile.append(env_file);
41} 36}
42 37
43Settings::Settings() 38Settings::Settings()
44{ 39{
45 char * homeDir = getenv("HOME"); 40 envFile = getHomeDir();
46 41 envFile.append("/");
47 if ( homeDir)
48 {
49 envFile.append(homeDir);
50 envFile.append("/");
51 }
52 else
53 printf( "Environment var HOME not set!\n" );
54 42
55 envFile.append(DEFAULT_DIR); 43 envFile.append(DEFAULT_DIR);
56 envFile.append("/"); 44 envFile.append("/");
57 45
58 envFile.append(DEFAULT_FILE); 46 envFile.append(DEFAULT_FILE);
59} 47}
60 48
61Settings::~Settings() 49Settings::~Settings()
62{ 50{
63} 51}
64 52
65bool Settings::readSetting(const string key_str,int& result) 53bool Settings::readSetting(const string key_str,int& result)
66{ 54{
67 string Buffer; 55 string Buffer;
68 if (readSetting(key_str,Buffer)) 56 if (readSetting(key_str,Buffer))
69 { 57 {
70 result = atoi(Buffer.c_str()); 58 result = atoi(Buffer.c_str());
71 return true; 59 return true;
72 } 60 }
73 else 61 else
74 return false; 62 return false;
75} 63}
76 64
77bool Settings::readSetting(const string key_str,unsigned int& result) 65bool Settings::readSetting(const string key_str,unsigned int& result)
78{ 66{
79 string Buffer; 67 string Buffer;
80 if (readSetting(key_str,Buffer)) 68 if (readSetting(key_str,Buffer))
81 { 69 {
82 result = atoi(Buffer.c_str()); 70 result = atoi(Buffer.c_str());
83 return true; 71 return true;
84 } 72 }
85 else 73 else
86 return false; 74 return false;
87} 75}
88 76
89bool Settings::readSetting(const string key_str,long int& result) 77bool Settings::readSetting(const string key_str,long int& result)
90{ 78{
91 string Buffer; 79 string Buffer;
92 if (readSetting(key_str,Buffer)) 80 if (readSetting(key_str,Buffer))
93 { 81 {
94 result = atol(Buffer.c_str()); 82 result = atol(Buffer.c_str());
95 return true; 83 return true;
96 } 84 }
97 else 85 else
98 return false; 86 return false;
99} 87}
100 88
101bool Settings::readSetting(const string key_str,unsigned long& result) 89bool Settings::readSetting(const string key_str,unsigned long& result)
102{ 90{
103 string Buffer; 91 string Buffer;
104 if (readSetting(key_str,Buffer)) 92 if (readSetting(key_str,Buffer))
105 { 93 {
106 result = atol(Buffer.c_str()); 94 result = atol(Buffer.c_str());
107 return true; 95 return true;
108 } 96 }
109 else 97 else
110 return false; 98 return false;
111} 99}
112 100
101bool Settings::readSetting(const string key_str,double& result)
102{
103 string Buffer;
104 if (readSetting(key_str,Buffer))
105 {
106 result = atof( Buffer.c_str() );
107 return true;
108 }
109 else
110 return false;
111}
112
113bool Settings::readSetting(const string key_str,bool& result) 113bool Settings::readSetting(const string key_str,bool& result)
114{ 114{
115 string Buffer; 115 string Buffer;
116 if (readSetting(key_str,Buffer)) 116 if (readSetting(key_str,Buffer))
117 { 117 {
118 result = (Buffer == "true"); 118 result = (Buffer == "true");
119 return true; 119 return true;
120 } 120 }
121 else 121 else
122 return false; 122 return false;
123} 123}
124 124
125bool Settings::readSetting(const string key_str,string& results) 125bool Settings::readSetting(const string key_str,string& results)
126{ 126{
127 // This function will read a string from the env file that corresponds to the 127 // This function will read a string from the env file that corresponds to the
128 // key key_str passed in. 128 // key key_str passed in.
129 FILE * fd = 0; 129 FILE * fd = 0;
130 char buf[MAX_LINE_SIZE]; 130 char buf[MAX_LINE_SIZE];
131 bool ret_flag = false; 131 bool ret_flag = false;
132 char* key; 132 char* key;
133 char* value; 133 char* value;
134 134
135 // open file 135 // open file
136 fd = fopen(envFile.c_str(), "r"); 136 fd = fopen(envFile.c_str(), "r");
137 137
138 if (fd) 138 if (fd)
139 { 139 {
140 while (fgets(buf, MAX_LINE_SIZE-1, fd)) 140 while (fgets(buf, MAX_LINE_SIZE-1, fd))
141 { 141 {
142 key = strtok(buf, "\t"); 142 key = strtok(buf, "\t");
143 value = strtok(NULL, "\n"); 143 value = strtok(NULL, "\n");
144 // find key in file 144 // find key in file
145 if (!strcasecmp(key,key_str.c_str())) 145 if (!strcasecmp(key,key_str.c_str()))
146 { 146 {
147 results = value; 147 results = value;
148 ret_flag = true; 148 ret_flag = true;
149 } 149 }
150 } 150 }
151 fclose(fd); 151 fclose(fd);
152 } 152 }
153 153
154 return(ret_flag); 154 return(ret_flag);
155} 155}
156 156
157void Settings::writeSetting(const string key_str,const bool value) 157void Settings::writeSetting(const string key_str,const bool value)
158{ 158{
159 value ?writeSetting(key_str,string("true")) :writeSetting(key_str,string("false")); 159 value ?writeSetting(key_str,string("true")) :writeSetting(key_str,string("false"));
160} 160}
161 161
162void Settings::writeSetting(const string key_str,const double value)
163{
164 char Buffer[30];
165
166 sprintf(Buffer,"%lf",value);
167 writeSetting(key_str,string(Buffer));
168}
169
162void Settings::writeSetting(const string key_str,const int value) 170void Settings::writeSetting(const string key_str,const int value)
163{ 171{
164 char Buffer[30]; 172 char Buffer[30];
165 173
166 sprintf(Buffer,"%i",value); 174 sprintf(Buffer,"%i",value);
167 writeSetting(key_str,string(Buffer)); 175 writeSetting(key_str,string(Buffer));
168} 176}
169 177
170void Settings::writeSetting(const string key_str,const unsigned int value) 178void Settings::writeSetting(const string key_str,const unsigned int value)
171{ 179{
172 char Buffer[30]; 180 char Buffer[30];
173 181
174 sprintf(Buffer,"%i",value); 182 sprintf(Buffer,"%i",value);
175 writeSetting(key_str,string(Buffer)); 183 writeSetting(key_str,string(Buffer));
176} 184}
177 185
178void Settings::writeSetting(const string key_str,const long int value) 186void Settings::writeSetting(const string key_str,const long int value)
179{ 187{
180 char Buffer[30]; 188 char Buffer[30];
181 189
182 sprintf(Buffer,"%li",value); 190 sprintf(Buffer,"%li",value);
183 writeSetting(key_str,string(Buffer)); 191 writeSetting(key_str,string(Buffer));
184} 192}
185 193
186void Settings::writeSetting(const string key_str,const unsigned long value) 194void Settings::writeSetting(const string key_str,const unsigned long value)
187{ 195{
188 char Buffer[30]; 196 char Buffer[30];
189 197
190 sprintf(Buffer,"%lu",value); 198 sprintf(Buffer,"%lu",value);
191 writeSetting(key_str,string(Buffer)); 199 writeSetting(key_str,string(Buffer));
192} 200}
193 201
194void Settings::writeSetting(const string key_str,const string value) 202void Settings::writeSetting(const string key_str,const string value)
195{ 203{
196 // This function will write a value for the key key_str. If the key_str 204 // This function will write a value for the key key_str. If the key_str
197 // already exists then it will be overwritten. 205 // already exists then it will be overwritten.
198
199 std::vector<string> FileEntries;
200 FILE *fd=NULL,*ftemp=NULL; 206 FILE *fd=NULL,*ftemp=NULL;
201 char * dir_str;
202 char * dir_ptr;
203 char buf[MAX_LINE_SIZE]; 207 char buf[MAX_LINE_SIZE];
204 char tempname[12];
205 208
206 dir_str = strdup(envFile.c_str()); 209 string tmp = getHomeDir() + "/tmpsfcave.dat";
207 printf( "dir = %s, file - %s\n", dir_str, envFile.c_str() ); 210
208 if (dir_str) 211 // if file exists we need to save contents
212 fd = fopen( envFile.c_str(), "r" );
213 ftemp = fopen( tmp.c_str(), "w" );
214 if ( fd != NULL && ftemp != NULL )
209 { 215 {
210 // remove file from the directory string 216 while (fgets(buf, MAX_LINE_SIZE-1, fd))
211 dir_ptr = strrchr(dir_str, (int)'/');
212 if (dir_ptr)
213 { 217 {
214 *dir_ptr = 0; 218 if ( strncmp( buf, key_str.c_str(), key_str.size() ) != 0 )
215 219 fprintf( ftemp, "%s", buf );
216 // make the directory path if it does not exist 220 }
217 // mkdir(dir_str, 777 ); 221 fclose(fd);
222 }
218 223
219 // if file exists we need to save contents 224 if ( ftemp != NULL )
220 if ((fd = fopen(envFile.c_str(), "r")) != NULL) 225 {
221 { 226 fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str());
222 while (fgets(buf, MAX_LINE_SIZE-1, fd)) 227 fclose( ftemp );
223 FileEntries.push_back(string(buf));
224 fclose(fd);
225 }
226 228
227 char *home = getenv( "HOME" ); 229 remove(envFile.c_str());
228 string tmp; 230 rename( tmp.c_str(), envFile.c_str() );
229 if ( home ) 231 }
230 tmp = home + string( "/" ) + "tmpsfcave.dat"; 232/*
231 else 233 string tmp = getHomeDir() + "/tmpsfcave.dat";
232 tmp = "./tmpsfcave.dat";
233 strcpy(tempname,tmp.c_str() ); 234 strcpy(tempname,tmp.c_str() );
234 printf( "tmp - %s\n", tempname ); 235 printf( "tmp - %s\n", tempname );
235 if ((ftemp = fopen(tempname,"w")) != NULL) 236 if ((ftemp = fopen(tempname,"w")) != NULL)
236 { 237 {
237 char *key1,*key2; 238 char *key1,*key2;
238 char buff1[80],buff2[80]; 239 char buff1[80],buff2[80];
239 240
240 strncpy(buff1,key_str.c_str(),80); 241 strncpy(buff1,key_str.c_str(),80);
241 key1 = strtok(buff1,"\t"); 242 key1 = strtok(buff1,"\t");
242 for (std::vector<string>::iterator iter = FileEntries.begin(); iter < FileEntries.end(); iter++) 243 for (std::vector<string>::iterator iter = FileEntries.begin(); iter < FileEntries.end(); iter++)
243 { 244 {
244 strncpy(buff2,(*iter).c_str(),80); 245 strncpy(buff2,(*iter).c_str(),80);
245 key2 = strtok(buff2,"\t"); 246 key2 = strtok(buff2,"\t");
246 // IF not the key string then write out to file 247 // IF not the key string then write out to file
247 if (strcmp(key1,key2) != 0) 248 if (strcmp(key1,key2) != 0)
248 { 249 {
249 fprintf(ftemp,"%s",iter->c_str()); 250 fprintf(ftemp,"%s",iter->c_str());
250 fflush(ftemp); 251 fflush(ftemp);
251 } 252 }
252 } 253 }
253 254
254 fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str()); 255 fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str());
255 fflush(ftemp); 256 fflush(ftemp);
256 fclose(ftemp); 257 fclose(ftemp);
257
258 remove(envFile.c_str());
259
260 rename( tempname, envFile.c_str() );
261 } 258 }
262 else 259 else
263 printf( "Can't open file %s\n", envFile.c_str() ); 260 printf( "Can't open file %s\n", envFile.c_str() );
264 } 261 }
265 262
266 delete dir_str; 263 delete dir_str;
267 } 264 }
265*/
268} 266}
269 267
270void Settings::deleteFile(void) 268void Settings::deleteFile(void)
271{ 269{
272 remove(envFile.c_str()); 270 remove(envFile.c_str());
273} 271}
diff --git a/noncore/games/sfcave-sdl/settings.h b/noncore/games/sfcave-sdl/settings.h
index 5e828ed..a3af999 100644
--- a/noncore/games/sfcave-sdl/settings.h
+++ b/noncore/games/sfcave-sdl/settings.h
@@ -1,52 +1,54 @@
1#ifndef __SETTINGS_H 1#ifndef __SETTINGS_H
2#define __SETTINGS_H 2#define __SETTINGS_H
3 3
4// This class will create a .<name> directory in the users home directory or 4// This class will create a .<name> directory in the users home directory or
5// a directory for the users choice. It will then manage a set of key values 5// a directory for the users choice. It will then manage a set of key values
6// that the programmer can search for. This allows programmers to save a users 6// that the programmer can search for. This allows programmers to save a users
7// settings and then retrieve then at a latter time. It currently supports 7// settings and then retrieve then at a latter time. It currently supports
8// upto 1024 different settings. 8// upto 1024 different settings.
9// Two constructors are provided. They will dertermine what directory to look 9// Two constructors are provided. They will dertermine what directory to look
10// for the settings file and what the name of the file is. If the directory is 10// for the settings file and what the name of the file is. If the directory is
11// not specified then a default directory of .<DEFAULT_DIR> will be created in 11// not specified then a default directory of .<DEFAULT_DIR> will be created in
12// the users home directory. A file will be created in this directory. The name 12// the users home directory. A file will be created in this directory. The name
13// will be the one specified by the caller. If none is specified then 13// will be the one specified by the caller. If none is specified then
14// DEFAULT_FILE will be created. 14// DEFAULT_FILE will be created.
15// To retrieve and store strings into the file readSetting and writeSetting 15// To retrieve and store strings into the file readSetting and writeSetting
16// should be called. 16// should be called.
17 17
18#include <string> 18#include <string>
19using namespace std; 19using namespace std;
20 20
21class Settings 21class Settings
22{ 22{
23 23
24public: 24public:
25 25
26 Settings( char * env_file = 0, char * env_dir = 0 ); 26 Settings( char * env_file = 0, char * env_dir = 0 );
27 Settings(); 27 Settings();
28 ~Settings(); 28 ~Settings();
29 29
30 bool readSetting(const string key_str,string& results); 30 bool readSetting(const string key_str,string& results);
31 bool readSetting(const string key_str,int& result); 31 bool readSetting(const string key_str,int& result);
32 bool readSetting(const string key_str,unsigned int& result); 32 bool readSetting(const string key_str,unsigned int& result);
33 bool readSetting(const string key_str,long int& result); 33 bool readSetting(const string key_str,long int& result);
34 bool readSetting(const string key_str,unsigned long& result); 34 bool readSetting(const string key_str,unsigned long& result);
35 bool readSetting(const string key_str,double & result);
35 bool readSetting(const string key_str,bool& result); 36 bool readSetting(const string key_str,bool& result);
36 37
37 void writeSetting(const string key_str,const string value); 38 void writeSetting(const string key_str,const string value);
38 void writeSetting(const string key_str,const int value); 39 void writeSetting(const string key_str,const int value);
39 void writeSetting(const string key_str,const unsigned int result); 40 void writeSetting(const string key_str,const unsigned int result);
40 void writeSetting(const string key_str,const long int result); 41 void writeSetting(const string key_str,const long int result);
41 void writeSetting(const string key_str,const unsigned long result); 42 void writeSetting(const string key_str,const unsigned long result);
43 void writeSetting(const string key_str,const double value);
42 void writeSetting(const string key_str,const bool value); 44 void writeSetting(const string key_str,const bool value);
43 45
44 void deleteFile(void); 46 void deleteFile(void);
45 47
46private: 48private:
47 49
48 string envFile; 50 string envFile;
49}; 51};
50 52
51 53
52#endif 54#endif
diff --git a/noncore/games/sfcave-sdl/sfcave.cpp b/noncore/games/sfcave-sdl/sfcave.cpp
index 8d376a1..dbd788c 100644
--- a/noncore/games/sfcave-sdl/sfcave.cpp
+++ b/noncore/games/sfcave-sdl/sfcave.cpp
@@ -1,546 +1,633 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h> 2#include <stdlib.h>
3 3
4#include <time.h> 4#include <time.h>
5#include <sys/timeb.h> 5#include <sys/timeb.h>
6 6
7#include "SDL.h" 7#include "SDL.h"
8#include "SDL_gfxPrimitives.h" 8#include "SDL_gfxPrimitives.h"
9 9
10#include "constants.h" 10#include "constants.h"
11 11
12#include "sound.h" 12#include "sound.h"
13#include "menu.h" 13#include "menu.h"
14#include "help.h" 14#include "help.h"
15#include "game.h" 15#include "game.h"
16#include "terrain.h" 16#include "terrain.h"
17#include "random.h" 17#include "random.h"
18#include "sfcave.h" 18#include "sfcave.h"
19#include "font.h" 19#include "font.h"
20#include "settings.h" 20#include "settings.h"
21#include "util.h" 21#include "util.h"
22 22
23#include "sfcave_game.h" 23#include "sfcave_game.h"
24#include "gates_game.h" 24#include "gates_game.h"
25#include "fly_game.h" 25#include "fly_game.h"
26 26
27void start( int argc, char *argv[] ) 27void start( int argc, char *argv[] )
28{ 28{
29 FontHandler::init(); 29 SFCave *app = new SFCave( argc, argv );
30 SFCave app( argc, argv ); 30 app->mainEventLoop();
31 FontHandler::cleanUp(); 31 delete app;
32} 32}
33 33
34#ifdef __cplusplus 34#ifdef __cplusplus
35extern "C" 35extern "C"
36#endif 36#endif
37int main(int argc, char *argv[]) 37int main(int argc, char *argv[])
38{ 38{
39 start( argc, argv ); 39 start( argc, argv );
40 return 0; 40 return 0;
41} 41}
42 42
43 43
44SFCave :: SFCave( int argc, char *argv[] ) 44SFCave :: SFCave( int argc, char *argv[] )
45{ 45{
46 setupOK = false;
47
48 // Load settings
46 string diff = loadSetting( "GameDifficulty", "Easy" ); 49 string diff = loadSetting( "GameDifficulty", "Easy" );
47 string game = loadSetting( "GameType", "SFCave" ); 50 string game = loadSetting( "GameType", "SFCave" );
48 musicPath = loadSetting( "MusicPath", SOUND_PATH ); 51 musicPath = loadSetting( "MusicPath", SOUND_PATH );
49 printf( "musicPath %s\n", musicPath.c_str() );
50 musicType = loadSetting( "MusicType", "mod,ogg" ); 52 musicType = loadSetting( "MusicType", "mod,ogg" );
53 bool soundOn = loadBoolSetting( "SoundOn", true );
54 bool musicOn = loadBoolSetting( "MusicOn", true );
51 if ( musicPath[musicPath.size()-1] != '/' ) 55 if ( musicPath[musicPath.size()-1] != '/' )
52 musicPath += "/"; 56 musicPath += "/";
57 printf( "musicPath %s\n", musicPath.c_str() );
53 58
54 // Init main SDL Library 59 // Init main SDL Library
55 initSDL( argc, argv ); 60 initSDL( argc, argv );
56 61
62 // Init font handler
63 if ( !FontHandler::init() )
64 {
65 printf( "Unable to initialise fonts!\n" );
66 return;
67 }
68
57 // Init SoundHandler 69 // Init SoundHandler
58 if ( !SoundHandler :: init() ) 70 if ( !SoundHandler :: init() )
59 printf("Unable to open audio!\n"); 71 printf("Unable to open audio!\n");
60 72
73 SoundHandler :: setSoundsOn( soundOn );
74 SoundHandler :: setMusicOn( musicOn );
75
61 currentGame = Game::createGame( this, WIDTH, HEIGHT, game, diff ); 76 currentGame = Game::createGame( this, WIDTH, HEIGHT, game, diff );
62 if ( !currentGame ) 77 if ( !currentGame )
63 currentGame = new SFCaveGame( this, WIDTH, HEIGHT, 0 ); 78 currentGame = new SFCaveGame( this, WIDTH, HEIGHT, 0 );
64 currentGame->setSeed(-1); 79 currentGame->setSeed(-1);
80
81 // Create menu
65 menu = new Menu( this ); 82 menu = new Menu( this );
66 83
84 // Create help screen
67 help = new Help( this ); 85 help = new Help( this );
68 86
69 maxFPS = 50; 87 maxFPS = 50;
70 showFps = false; 88 showFps = false;
71 mainEventLoop(); 89
72 90 setupOK = true;
73 SoundHandler :: cleanUp();
74 SDL_Quit();
75} 91}
76 92
77SFCave :: ~SFCave() 93SFCave :: ~SFCave()
78{ 94{
79 if ( currentGame ) 95 if ( currentGame )
80 delete currentGame; 96 delete currentGame;
81 97
82 if ( menu ) 98 if ( menu )
83 delete menu; 99 delete menu;
84 100
85 SDL_FreeSurface( screen ); 101 if ( help )
86} 102 delete help;
87
88 103
89void SFCave :: drawGameScreen( ) 104 SDL_FreeSurface( screen );
90{ 105 FontHandler::cleanUp();
91 //ClearScreen(screen, "Titletext"); 106 SoundHandler :: cleanUp();
92 107
108 SDL_Quit();
93} 109}
94 110
111
95void SFCave :: initSDL( int argc, char *argv[] ) 112void SFCave :: initSDL( int argc, char *argv[] )
96{ 113{
97 const SDL_VideoInfo *info; 114 const SDL_VideoInfo *info;
98 Uint8 video_bpp; 115 Uint8 video_bpp;
99 Uint32 videoflags; 116 Uint32 videoflags;
100 117
101 118 // Initialize SDL
102
103 /* Initialize SDL */
104 if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ) { 119 if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ) {
105 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 120 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
106 exit(1); 121 exit(1);
107 } 122 }
108 atexit(SDL_Quit);
109 123
110 /* Alpha blending doesn't work well at 8-bit color */
111 video_bpp = 16; 124 video_bpp = 16;
112 125
113 if ( !SDL_VideoModeOK(WIDTH, HEIGHT, 16, SDL_DOUBLEBUF) ) 126 if ( !SDL_VideoModeOK(WIDTH, HEIGHT, video_bpp, SDL_DOUBLEBUF) )
114 printf( "No double buffering\n" ); 127 printf( "No double buffering\n" );
115 128
116 videoflags = SDL_HWSURFACE | SDL_SRCALPHA;//|| SDL_DOUBLEBUF;// | SDL_SRCALPHA | SDL_RESIZABLE; 129 videoflags = SDL_HWSURFACE | SDL_SRCALPHA;
117 while ( argc > 1 ) { 130 while ( argc > 1 )
131 {
118 --argc; 132 --argc;
119 if ( strcmp(argv[argc-1], "-bpp") == 0 ) { 133 if ( strcmp(argv[argc-1], "-bpp") == 0 )
134 {
120 video_bpp = atoi(argv[argc]); 135 video_bpp = atoi(argv[argc]);
121 --argc; 136 --argc;
122 } else 137 }
123 if ( strcmp(argv[argc], "-hw") == 0 ) { 138 else if ( strcmp(argv[argc], "-hw") == 0 )
139 {
124 videoflags |= SDL_HWSURFACE; 140 videoflags |= SDL_HWSURFACE;
125 } else 141 }
126 if ( strcmp(argv[argc], "-warp") == 0 ) { 142 else if ( strcmp(argv[argc], "-warp") == 0 )
143 {
127 videoflags |= SDL_HWPALETTE; 144 videoflags |= SDL_HWPALETTE;
128 } else 145 }
129 if ( strcmp(argv[argc], "-fullscreen") == 0 ) { 146 else if ( strcmp(argv[argc], "-fullscreen") == 0 )
147 {
130 videoflags |= SDL_FULLSCREEN; 148 videoflags |= SDL_FULLSCREEN;
131 } else { 149 }
150 else if ( strcmp(argv[argc], "-h") == 0 )
151 {
132 fprintf(stderr, 152 fprintf(stderr,
133 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", 153 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
134 argv[0]); 154 argv[0]);
135 exit(1); 155 exit(1);
136 } 156 }
137 } 157 }
138 158
139 /* Set 240x320 video mode */ 159 // Set 240x320 video mode
140 if ( (screen=SDL_SetVideoMode(WIDTH,HEIGHT,video_bpp,videoflags)) == NULL ) { 160 if ( (screen = SDL_SetVideoMode( WIDTH,HEIGHT,video_bpp,videoflags )) == NULL )
141 fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",WIDTH,HEIGHT,SDL_GetError()); 161 {
162 printf( "Couldn't set %ix%i video mode: %s\n",WIDTH,HEIGHT,SDL_GetError() );
142 exit(2); 163 exit(2);
143 } 164 }
144 165
145 /* Use alpha blending */ 166 // Use alpha blending
146 SDL_SetAlpha(screen, SDL_RLEACCEL, 0); 167 //SDL_SetAlpha(screen, SDL_RLEACCEL, 0);
147 168
148 /* Set title for window */ 169 // Set title for window
149 SDL_WM_SetCaption("SFCave","SFCave"); 170 SDL_WM_SetCaption("SFCave","SFCave");
150} 171}
151 172
152void SFCave :: mainEventLoop() 173void SFCave :: mainEventLoop()
153{ 174{
154 SDL_Event event; 175 if ( !setupOK )
155 int done; 176 return;
156 177
157 /* Wait for a keystroke */ 178 // Wait for a keystroke
158 done = 0; 179 finish = false;
159 state = 0; 180 state = 0;
160 state = STATE_CRASHED; 181 state = STATE_CRASHED;
161 changeState( STATE_MENU ); 182 changeState( STATE_MENU );
162 183
163 int FPS = 0; 184 FPS = 0;
164 bool limitFPS = true;
165 actualFPS = 0; 185 actualFPS = 0;
166 long time1 = 0; 186 time1 = 0;
167 long start; 187
168 long end; 188 limitFPS = true;
169 //long nrTimes = 0; 189 while ( !finish )
170 struct timeb tp;
171 while ( !done )
172 { 190 {
173 // calc FPS 191 // calc FPS
174 ftime( &tp ); 192 calcFPS();
175 start =(tp.time%10000)*10000 + tp.millitm;
176 // printf( "start = %ld, time1 - %d, st-tm - %d, tp.time - %ld\n", start, time1, start-time1, (tp.time%1000)*1000 );
177 if ( start - time1 >= 1000 )
178 {
179 actualFPS = FPS;
180 // printf( "%d FPS = %d\n", nrTimes++, actualFPS );
181 FPS = 0;
182 time1 = start;
183 }
184 else
185 FPS ++;
186 193
187 SDL_FillRect( screen, 0, 0 ); 194 SDL_FillRect( screen, 0, 0 );
188 switch( state )
189 {
190 case STATE_MENU:
191 SDL_FillRect( screen, 0, 0 );
192 menu->draw( screen );
193 break;
194 case STATE_HELP:
195 SDL_FillRect( screen, 0, 0 );
196 help->update();
197 help->draw( screen );
198 break;
199 case STATE_NEWGAME:
200 printf( "STATE_NEWGAME\n" );
201 currentGame->setReplay( false );
202 currentGame->init();
203 changeState( STATE_PLAYING );
204 break;
205 195
206 case STATE_REPLAY: 196 handleGameState( );
207 printf( "STATE_NEWGAME\n" );
208 currentGame->setReplay( true );
209 currentGame->init();
210 changeState( STATE_PLAYING );
211 break;
212 197
213 case STATE_PLAYING: 198 SDL_Flip( screen );
214 case STATE_CRASHING:
215 currentGame->update( state );
216 currentGame->draw( screen );
217 break;
218
219 case STATE_CRASHED:
220 currentGame->update( state );
221 currentGame->draw( screen );
222
223 // Display Game Over message
224 break;
225
226 case STATE_QUIT:
227 done = 1;
228 break;
229 }
230
231 /* Show */
232 // if ( state != STATE_CRASHED )
233 SDL_Flip( screen );
234 // SDL_UpdateRect(screen, 0, 0, 0, 0);
235 199
236 if ( limitFPS ) 200 if ( limitFPS )
237 { 201 FPSDelay();
238 /* Slow down polling - limit to x FPS*/
239 ftime( &tp );
240 end = abs((tp.time%10000)*10000 + tp.millitm);
241 if ( end-start < (1000/maxFPS) )
242 {
243 // printf( "end - %ld, timetaken for frame = %ld, sleeping for %ld %d\n", end, end-start, (1000/maxFPS)-(end-start), actualFPS );
244 if ( (1000/maxFPS)-(end-start) > 500 )
245 {
246 // Should never happen but in case it does sleep for 5 seconds
247 printf( "WARNING WILL ROBINSON! delay = %ld - start %ld, end %ld\n", (1000/maxFPS)-(end-start), start, end );
248 SDL_Delay( 5 );
249 }
250 else
251 SDL_Delay((1000/maxFPS)-(end-start) );
252 }
253 }
254 else 202 else
255 SDL_Delay( 5 ); 203 SDL_Delay( 5 );
256 204
257 /* Check for events */ 205 handleEvents();
258 while ( SDL_PollEvent(&event) ) 206 }
207}
208
209
210void SFCave :: handleGameState()
211{
212 switch( state )
213 {
214 case STATE_MENU:
215 SDL_FillRect( screen, 0, 0 );
216 menu->draw( screen );
217 break;
218 case STATE_HELP:
219 SDL_FillRect( screen, 0, 0 );
220 help->update();
221 help->draw( screen );
222 break;
223 case STATE_NEWGAME:
224 currentGame->setReplay( false );
225 currentGame->init();
226 changeState( STATE_PLAYING );
227 break;
228
229 case STATE_REPLAY:
230 currentGame->setReplay( true );
231 currentGame->init();
232 changeState( STATE_PLAYING );
233 break;
234
235 case STATE_PLAYING:
236 case STATE_CRASHING:
237 case STATE_CRASHED:
238 currentGame->update( state );
239 currentGame->draw( screen );
240 break;
241
242 case STATE_QUIT:
243 finish = true;
244 break;
245 }
246}
247
248void SFCave :: handleEvents()
249{
250 SDL_Event event;
251
252 // Check for events
253 while ( SDL_PollEvent(&event) )
254 {
255 switch (event.type)
259 { 256 {
260 switch (event.type) 257 case SDL_KEYDOWN:
258 case SDL_KEYUP:
261 { 259 {
262 case SDL_KEYDOWN: 260 // Escape keypress quits the app
263 case SDL_KEYUP: 261 if ( event.key.keysym.sym == SDLK_ESCAPE )
264 // Escape keypress quits the app 262 {
265 if ( event.key.keysym.sym != SDLK_ESCAPE ) 263 finish = true;
264 break;
265 }
266
267 if ( state == STATE_MENU )
268 {
269 int rc = menu->handleKeys( event.key );
270 if ( rc != -1 )
271 handleMenuSelect( rc );
272 }
273 else if ( state == STATE_HELP )
274 {
275 help->handleKeys( event.key );
276 }
277 else if ( state == STATE_CRASHED )
278 {
279 if ( event.type == SDL_KEYDOWN )
266 { 280 {
267 // printf( "Key Pressed was %d %s\n", event.key.keysym.sym, SDL_GetKeyName( event.key.keysym.sym ) ); 281 if ( event.key.keysym.sym == SDLK_UP ||
268 282 event.key.keysym.sym == SDLK_DOWN ||
269 if ( state == STATE_MENU ) 283 event.key.keysym.sym == SDLK_SPACE )
284 changeState( STATE_NEWGAME );
285 else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == 0 )
270 { 286 {
271 int rc = menu->handleKeys( event.key ); 287 changeState( STATE_MENU );
272 if ( rc != -1 ) 288 menu->resetToTopMenu();
273 handleMenuSelect( rc );
274 } 289 }
275 else if ( state == STATE_HELP ) 290 else if ( event.key.keysym.sym == SDLK_r )
276 { 291 {
277 help->handleKeys( event.key ); 292 changeState( STATE_REPLAY );
278 } 293 }
279 else if ( state == STATE_CRASHED ) 294 else if ( event.key.keysym.sym == SDLK_s )
280 { 295 {
281 if ( event.type == SDL_KEYDOWN ) 296 SoundHandler :: playSound( SND_EXPLOSION );
282 {
283 if ( event.key.keysym.sym == SDLK_UP ||
284 event.key.keysym.sym == SDLK_DOWN ||
285 event.key.keysym.sym == SDLK_SPACE )
286 changeState( STATE_NEWGAME );
287 else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == 0 )
288 {
289 changeState( STATE_MENU );
290 menu->resetToTopMenu();
291 }
292 else if ( event.key.keysym.sym == SDLK_r )
293 {
294 changeState( STATE_REPLAY );
295 }
296 else if ( event.key.keysym.sym == SDLK_s )
297 {
298 SoundHandler :: playSound( SND_EXPLOSION );
299 }
300 }
301 } 297 }
302 else
303 {
304 switch ( event.key.keysym.sym )
305 {
306 case SDLK_f:
307 printf( "showFPS - %d\n", showFps );
308 if ( event.type == SDL_KEYDOWN )
309 showFps = !showFps;
310 break;
311 case SDLK_l:
312 if ( event.type == SDL_KEYDOWN )
313 limitFPS = !limitFPS;
314 break;
315
316 case SDLK_p:
317 if ( event.type == SDL_KEYDOWN )
318 {
319 maxFPS ++;
320 printf( "maxFPS - %d\n", maxFPS );
321 }
322 break;
323
324 case SDLK_o:
325 if ( event.type == SDL_KEYDOWN )
326 {
327 maxFPS --;
328 printf( "maxFPS - %d\n", maxFPS );
329 }
330 break;
331
332 case SDLK_n:
333 currentGame->getTerrain()->offset++;
334 break;
335
336 default:
337 currentGame->handleKeys( event.key );
338 break;
339 }
340 }
341
342 break;
343 } 298 }
299 }
300 else
301 {
302 switch ( event.key.keysym.sym )
303 {
304 case SDLK_f:
305 if ( event.type == SDL_KEYDOWN )
306 showFps = !showFps;
307 break;
308 case SDLK_l:
309 if ( event.type == SDL_KEYDOWN )
310 limitFPS = !limitFPS;
311 break;
344 312
313 default:
314 currentGame->handleKeys( event.key );
315 break;
316 }
317 }
345 318
346 case SDL_QUIT: 319 break;
347 done = 1;
348 break;
349 default:
350 break;
351 } 320 }
321
322 case SDL_QUIT:
323 finish = true;
324 break;
325 default:
326 break;
352 } 327 }
353 } 328 }
354} 329}
355 330
356void SFCave :: changeState( int s ) 331void SFCave :: changeState( int s )
357{ 332{
358 if ( state != s ) 333 if ( state != s )
359 currentGame->stateChanged( state, s ); 334 currentGame->stateChanged( state, s );
360 335
361 if ( state == STATE_MENU && s == STATE_HELP ) 336 if ( state == STATE_MENU && s == STATE_HELP )
362 help->init(); 337 help->init();
363 if ( state == STATE_CRASHED && s == STATE_MENU ) 338 if ( state == STATE_CRASHED && s == STATE_MENU )
364 { 339 {
365 SoundHandler :: stopMusic( true ); 340 SoundHandler :: stopMusic( true );
366 341
367 string musicFile = chooseRandomFile( musicPath, musicType ); 342 string musicFile = chooseRandomFile( musicPath, musicType );
368 printf("playing music %s\n", musicFile.c_str() );
369 SoundHandler :: setMusicVolume( 128 ); 343 SoundHandler :: setMusicVolume( 128 );
370 SoundHandler :: playMusic( musicFile ); 344 SoundHandler :: playMusic( musicFile );
371 } 345 }
372 else if ( state == STATE_MENU && (s == STATE_NEWGAME || s == STATE_REPLAY) ) 346 else if ( state == STATE_MENU && (s == STATE_NEWGAME || s == STATE_REPLAY) )
373 { 347 {
374 SoundHandler :: stopMusic( ); 348 SoundHandler :: stopMusic( );
375 349
376 // Start the in game music 350 // Start the in game music
377 string musicFile = SOUND_PATH "ingame.mod"; 351 string musicFile = INGAME_MUSIC;
378 SoundHandler :: playMusic( musicFile ); 352 SoundHandler :: playMusic( musicFile );
379 SoundHandler :: setMusicVolume( 25 ); 353 SoundHandler :: setMusicVolume( 25 );
380 } 354 }
381 355
382 state = s; 356 state = s;
383} 357}
384 358
385 359
386void SFCave :: handleMenuSelect( int menuId ) 360void SFCave :: handleMenuSelect( int menuId )
387{ 361{
388 switch( menuId ) 362 switch( menuId )
389 { 363 {
390 case MENU_STARTGAME: 364 case MENU_STARTGAME:
391 changeState( STATE_NEWGAME ); 365 changeState( STATE_NEWGAME );
392 break; 366 break;
393 367
394 case MENU_HELP: 368 case MENU_HELP:
395 changeState( STATE_HELP ); 369 changeState( STATE_HELP );
396 break; 370 break;
397 371
398 case MENU_QUIT: 372 case MENU_QUIT:
399 changeState( STATE_QUIT ); 373 changeState( STATE_QUIT );
400 break; 374 break;
401 375
402 case MENU_PLAY_REPLAY: 376 case MENU_PLAY_REPLAY:
403 if ( currentGame->isReplayAvailable() ) 377 if ( currentGame->isReplayAvailable() )
404 changeState( STATE_REPLAY ); 378 changeState( STATE_REPLAY );
405 else 379 else
406 setMenuStatusText( "No replay available yet" ); 380 setMenuStatusText( "No replay available yet" );
407 break; 381 break;
408 382
409 case MENU_LOAD_REPLAY: 383 case MENU_LOAD_REPLAY:
410 { 384 {
411#ifdef QWS 385 string replayFile = getHomeDir() + "/" + currentGame->getGameName() + ".replay";
412 QString replayFile = getenv( "HOME" );
413#else
414 QString replayFile = ".";
415#endif
416 replayFile += string( "/" ) + currentGame->getGameName() + ".replay";
417 386
418 currentGame->loadReplay( replayFile ); 387 currentGame->loadReplay( replayFile );
419 388
420 break; 389 break;
421 } 390 }
422 391
423 case MENU_SAVE_REPLAY: 392 case MENU_SAVE_REPLAY:
424 { 393 {
425 394
426 if ( currentGame->isReplayAvailable() ) 395 if ( currentGame->isReplayAvailable() )
427 { 396 {
428#ifdef QWS 397 string replayFile = getHomeDir() + "/" + currentGame->getGameName() + ".replay";
429 QString replayFile = getenv( "HOME" );
430#else
431 QString replayFile = ".";
432#endif
433 replayFile += string( "/" ) + currentGame->getGameName() + ".replay";
434 398
435 currentGame->saveReplay( replayFile ); 399 currentGame->saveReplay( replayFile );
436 } 400 }
437 else 401 else
438 setMenuStatusText( "No replay available yet" ); 402 setMenuStatusText( "No replay available yet" );
439 403
440 break; 404 break;
441 } 405 }
442 case MENU_CLEAR_SCORES: 406 case MENU_CLEAR_SCORES:
443 break; 407 break;
444 408
445 case MENU_GAME_SFCAVE: 409 case MENU_GAME_SFCAVE:
446 if ( currentGame->getGameName() != "SFCave" ) 410 if ( currentGame->getGameName() != "SFCave" )
447 { 411 {
448 int diff = currentGame->getDifficulty(); 412 int diff = currentGame->getDifficulty();
449 delete currentGame; 413 delete currentGame;
450 currentGame = new SFCaveGame( this, WIDTH, HEIGHT, 0 ); 414 currentGame = new SFCaveGame( this, WIDTH, HEIGHT, 0 );
451 currentGame->setDifficulty( diff ); 415 currentGame->setDifficulty( diff );
452 416
453 saveSetting( "GameType", "SFCave" ); 417 saveSetting( "GameType", "SFCave" );
454 } 418 }
455 break; 419 break;
456 420
457 case MENU_GAME_GATES: 421 case MENU_GAME_GATES:
458 if ( currentGame->getGameName() != "Gates" ) 422 if ( currentGame->getGameName() != "Gates" )
459 { 423 {
460 int diff = currentGame->getDifficulty(); 424 int diff = currentGame->getDifficulty();
461 delete currentGame; 425 delete currentGame;
462 currentGame = new GatesGame( this, WIDTH, HEIGHT, 0 ); 426 currentGame = new GatesGame( this, WIDTH, HEIGHT, 0 );
463 currentGame->setDifficulty( diff ); 427 currentGame->setDifficulty( diff );
464 428
465 saveSetting( "GameType", "Gates" ); 429 saveSetting( "GameType", "Gates" );
466 } 430 }
467 break; 431 break;
468 break; 432 break;
469 433
470 case MENU_GAME_FLY: 434 case MENU_GAME_FLY:
471 if ( currentGame->getGameName() != "Fly" ) 435 if ( currentGame->getGameName() != "Fly" )
472 { 436 {
473 int diff = currentGame->getDifficulty(); 437 int diff = currentGame->getDifficulty();
474 delete currentGame; 438 delete currentGame;
475 currentGame = new FlyGame( this, WIDTH, HEIGHT, 0 ); 439 currentGame = new FlyGame( this, WIDTH, HEIGHT, 0 );
476 currentGame->setDifficulty( diff ); 440 currentGame->setDifficulty( diff );
477 441
478 saveSetting( "GameType", "Fly" ); 442 saveSetting( "GameType", "Fly" );
479 } 443 }
480 break; 444 break;
481 445
482 case MENU_DIFFICULTY_EASY: 446 case MENU_DIFFICULTY_EASY:
483 currentGame->setDifficulty( MENU_DIFFICULTY_EASY ); 447 currentGame->setDifficulty( MENU_DIFFICULTY_EASY );
484 saveSetting( "GameDifficulty", "Easy" ); 448 saveSetting( "GameDifficulty", "Easy" );
485 break; 449 break;
486 450
487 case MENU_DIFFICULTY_NORMAL: 451 case MENU_DIFFICULTY_NORMAL:
488 currentGame->setDifficulty( MENU_DIFFICULTY_NORMAL ); 452 currentGame->setDifficulty( MENU_DIFFICULTY_NORMAL );
489 saveSetting( "GameDifficulty", "Medium" ); 453 saveSetting( "GameDifficulty", "Medium" );
490 break; 454 break;
491 455
492 case MENU_DIFFICULTY_HARD: 456 case MENU_DIFFICULTY_HARD:
493 currentGame->setDifficulty( MENU_DIFFICULTY_HARD ); 457 currentGame->setDifficulty( MENU_DIFFICULTY_HARD );
494 saveSetting( "GameDifficulty", "Hard" ); 458 saveSetting( "GameDifficulty", "Hard" );
495 break; 459 break;
496 460
461 case MENU_DIFFICULTY_CUSTOM:
462 currentGame->setDifficulty( MENU_DIFFICULTY_CUSTOM );
463 saveSetting( "GameDifficulty", "Custom" );
464 break;
465
497 case MENU_SOUND_ON: 466 case MENU_SOUND_ON:
498 SoundHandler :: setSoundsOn( true ); 467 SoundHandler :: setSoundsOn( true );
468 saveSetting( "SoundOn", "true" );
499 break; 469 break;
500 470
501 case MENU_SOUND_OFF: 471 case MENU_SOUND_OFF:
502 SoundHandler :: setSoundsOn( false ); 472 SoundHandler :: setSoundsOn( false );
473 saveSetting( "SoundOn", "false" );
503 break; 474 break;
504 475
505 case MENU_MUSIC_ON: 476 case MENU_MUSIC_ON:
506 SoundHandler :: setMusicOn( true ); 477 SoundHandler :: setMusicOn( true );
478 saveSetting( "MusicOn", "true" );
507 break; 479 break;
508 480
509 case MENU_MUSIC_OFF: 481 case MENU_MUSIC_OFF:
510 SoundHandler :: setMusicOn( false ); 482 SoundHandler :: setMusicOn( false );
483 saveSetting( "MusicOn", "false" );
511 break; 484 break;
512 485
486 case MENU_CUSTOM_THRUST:
487 customPlayerMenuVal = PLAYER_THRUST;
488 origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
489 setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
490 break;
491 case MENU_CUSTOM_GRAVITY:
492 customPlayerMenuVal = PLAYER_GRAVITY;
493 origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
494 setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
495 break;
496 case MENU_CUSTOM_MAXSPEEDUP:
497 customPlayerMenuVal = PLAYER_MAX_SPEED_UP;
498 origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
499 setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
500 break;
501 case MENU_CUSTOM_MAXSPEEDDOWN:
502 customPlayerMenuVal = PLAYER_MAX_SPEED_DOWN;
503 origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
504 setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
505 break;
506 case MENU_CUSTOM_INCREASE:
507 currentGame->getPlayer()->incValue( customPlayerMenuVal );
508 setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
509 break;
510 case MENU_CUSTOM_DECREASE:
511 currentGame->getPlayer()->decValue( customPlayerMenuVal );
512 setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
513 break;
514 case MENU_CUSTOM_SAVE:
515 {
516 // save settings
517 string key = currentGame->getGameName() + "_custom_player_" + currentGame->getPlayer()->getValueTypeString( customPlayerMenuVal );
518 saveSetting( key, currentGame->getPlayer()->getValue( customPlayerMenuVal ) );
519
520 break;
521 }
522 case MENU_CUSTOM_CANCEL:
523 currentGame->getPlayer()->setValue( customPlayerMenuVal, origValue );
524 break;
525
513 default: 526 default:
514 break; 527 break;
515 } 528 }
516} 529}
517 530
518void SFCave :: setMenuStatusText( string statusText ) 531void SFCave :: setMenuStatusText( string statusText )
519{ 532{
520 menu->setStatusText( statusText ); 533 menu->setStatusText( statusText );
521} 534}
522 535
523 536
524void SFCave :: saveSetting( string key, string val ) 537void SFCave :: saveSetting( string key, string val )
525{ 538{
526 Settings cfg( "sfcave-sdl" ); 539 Settings cfg( "sfcave-sdl" );
527 cfg.writeSetting( key, val ); 540 cfg.writeSetting( key, val );
528} 541}
529 542
530void SFCave :: saveSetting( string key, int val ) 543void SFCave :: saveSetting( string key, int val )
531{ 544{
532 Settings cfg( "sfcave-sdl" ); 545 Settings cfg( "sfcave-sdl" );
533 cfg.writeSetting( key, val ); 546 cfg.writeSetting( key, val );
534} 547}
535 548
549void SFCave :: saveSetting( string key, long val )
550{
551 Settings cfg( "sfcave-sdl" );
552 cfg.writeSetting( key, val );
553}
554
555void SFCave :: saveSetting( string key, double val )
556{
557 Settings cfg( "sfcave-sdl" );
558 cfg.writeSetting( key, val );
559}
560
536string SFCave :: loadSetting( string key, string defaultVal ) 561string SFCave :: loadSetting( string key, string defaultVal )
537{ 562{
538 string val; 563 string val;
539 Settings cfg( "sfcave-sdl" ); 564 Settings cfg( "sfcave-sdl" );
540 cfg.readSetting( key, val ); 565 cfg.readSetting( key, val );
541 566
542 if ( val == "" ) 567 if ( val == "" )
543 val = defaultVal; 568 val = defaultVal;
544 569
545 return val; 570 return val;
546} 571}
572
573bool SFCave :: loadBoolSetting( string key, bool defaultVal )
574{
575 bool val = defaultVal;
576 Settings cfg( "sfcave-sdl" );
577 cfg.readSetting( key, val );
578
579 return val;
580}
581
582int SFCave :: loadIntSetting( string key, int defaultVal )
583{
584 int val = defaultVal;
585 Settings cfg( "sfcave-sdl" );
586 cfg.readSetting( key, val );
587
588 return val;
589}
590
591double SFCave :: loadDoubleSetting( string key, double defaultVal )
592{
593 double val = defaultVal;
594 Settings cfg( "sfcave-sdl" );
595 cfg.readSetting( key, val );
596
597 return val;
598}
599
600
601void SFCave :: calcFPS()
602{
603 struct timeb tp;
604 ftime( &tp );
605 start =(tp.time%10000)*10000 + tp.millitm;
606 if ( start - time1 >= 1000 )
607 {
608 actualFPS = FPS;
609 FPS = 0;
610 time1 = start;
611 }
612 else
613 FPS ++;
614}
615
616void SFCave :: FPSDelay()
617{
618 struct timeb tp;
619 // Slow down polling - limit to x FPS
620 ftime( &tp );
621 end = abs((tp.time%10000)*10000 + tp.millitm);
622 if ( end-start < (1000/maxFPS) )
623 {
624 if ( (1000/maxFPS)-(end-start) > 500 )
625 {
626 // Should never happen but in case it does sleep for 5 seconds
627 printf( "WARNING WILL ROBINSON! delay = %ld - start %ld, end %ld\n", (1000/maxFPS)-(end-start), start, end );
628 SDL_Delay( 5 );
629 }
630 else
631 SDL_Delay((1000/maxFPS)-(end-start) );
632 }
633}
diff --git a/noncore/games/sfcave-sdl/sfcave.h b/noncore/games/sfcave-sdl/sfcave.h
index 96c2334..c707919 100644
--- a/noncore/games/sfcave-sdl/sfcave.h
+++ b/noncore/games/sfcave-sdl/sfcave.h
@@ -1,50 +1,72 @@
1#ifndef __SFCAVE_H 1#ifndef __SFCAVE_H
2#define __SFCAVE_H 2#define __SFCAVE_H
3 3
4#include "SDL.h" 4#include "SDL.h"
5 5
6#include "terrain.h" 6#include "terrain.h"
7 7
8class Game; 8class Game;
9class Menu; 9class Menu;
10class Help; 10class Help;
11 11
12class SFCave 12class SFCave
13{ 13{
14public: 14public:
15 SFCave( int argc, char *argv[] ); 15 SFCave( int argc, char *argv[] );
16 ~SFCave(); 16 ~SFCave();
17 17
18 void drawGameScreen();
19 void initSDL( int argc, char *argv[] ); 18 void initSDL( int argc, char *argv[] );
20 void mainEventLoop(); 19 void mainEventLoop();
21 20
22 void setCrashed( bool val ); 21 void setCrashed( bool val );
23 void changeState( int s ); 22 void changeState( int s );
24 int getState() { return state; } 23 int getState() { return state; }
25 Game *getCurrentGame() { return currentGame; } 24 Game *getCurrentGame() { return currentGame; }
26 int getFPS() { return actualFPS; } 25 int getFPS() { return actualFPS; }
27 bool showFPS() { return showFps; } 26 bool showFPS() { return showFps; }
28 27
29 void setMenuStatusText( string statusText ); 28 void setMenuStatusText( string statusText );
30 29
31 void saveSetting( string key, string val ); 30 void saveSetting( string key, string val );
32 void saveSetting( string key, int val ); 31 void saveSetting( string key, int val );
32 void saveSetting( string key, long val );
33 void saveSetting( string key, double val );
33 string loadSetting( string key, string defaultVal = "" ); 34 string loadSetting( string key, string defaultVal = "" );
35 bool loadBoolSetting( string key, bool defaultVal);
36 int loadIntSetting( string key, int defaultVal );
37 double loadDoubleSetting( string key, double defaultVal );
38
34private: 39private:
35 SDL_Surface *screen; 40 SDL_Surface *screen;
41 bool setupOK;
36 42
37 Game *currentGame; 43 Game *currentGame;
38 Menu *menu; 44 Menu *menu;
39 Help *help; 45 Help *help;
40 int state; 46 int state;
41 int maxFPS;
42 int actualFPS;
43 bool showFps; 47 bool showFps;
44 string musicPath; 48 string musicPath;
45 string musicType; 49 string musicType;
50 bool finish;
51
52 bool limitFPS;
53 int maxFPS;
54 int actualFPS;
55 int FPS;
56 long time1;
57 long start;
58 long end;
59
60 // This is used when the user is setting the custom
61 // values in the menu
62 int customPlayerMenuVal;
63 double origValue;
46 64
47 void handleMenuSelect( int menuId ); 65 void handleMenuSelect( int menuId );
66 void handleGameState();
67 void handleEvents();
68 void calcFPS();
69 void FPSDelay();
48}; 70};
49 71
50#endif 72#endif
diff --git a/noncore/games/sfcave-sdl/sfcave_game.cpp b/noncore/games/sfcave-sdl/sfcave_game.cpp
index 72c5ce3..1b00e14 100644
--- a/noncore/games/sfcave-sdl/sfcave_game.cpp
+++ b/noncore/games/sfcave-sdl/sfcave_game.cpp
@@ -1,167 +1,179 @@
1#include "SDL_gfxPrimitives.h" 1#include "SDL_gfxPrimitives.h"
2 2
3#include "constants.h" 3#include "constants.h"
4#include "sfcave_game.h" 4#include "sfcave_game.h"
5#include "random.h" 5#include "random.h"
6 6
7SFCaveGame :: SFCaveGame( SFCave *p, int w, int h, int diff ) 7SFCaveGame :: SFCaveGame( SFCave *p, int w, int h, int diff )
8 : Game( p, w, h, diff ) 8 : Game( p, w, h, diff )
9{ 9{
10 gameName = "SFCave"; 10 gameName = "SFCave";
11 difficulty = MENU_DIFFICULTY_EASY; 11 difficulty = MENU_DIFFICULTY_EASY;
12 blockUpdateRate = 200; 12 blockUpdateRate = 200;
13 13
14 terrain = new Terrain( w, h ); 14 terrain = new Terrain( w, h );
15 player = new Player( w, h ); 15 player = new Player( w, h );
16 highScore = 0; 16 highScore = 0;
17} 17}
18 18
19SFCaveGame :: ~SFCaveGame() 19SFCaveGame :: ~SFCaveGame()
20{ 20{
21} 21}
22 22
23void SFCaveGame :: init() 23void SFCaveGame :: init()
24{ 24{
25 Game :: init(); 25 Game :: init();
26 26
27 blockDistance = 50; 27 blockDistance = 50;
28 blockHeight = 80; 28 blockHeight = 80;
29 blockWidth = 20; 29 blockWidth = 20;
30 30
31 switch( difficulty ) 31 switch( difficulty )
32 { 32 {
33 case MENU_DIFFICULTY_EASY: 33 case MENU_DIFFICULTY_EASY:
34 blockDistance = 50; 34 blockDistance = 50;
35 break; 35 break;
36 case MENU_DIFFICULTY_NORMAL: 36 case MENU_DIFFICULTY_NORMAL:
37 blockDistance = 40; 37 blockDistance = 40;
38 break; 38 break;
39 case MENU_DIFFICULTY_HARD: 39 case MENU_DIFFICULTY_HARD:
40 blockDistance = 30; 40 blockDistance = 30;
41 break; 41 break;
42 case MENU_DIFFICULTY_CUSTOM:
43 {
44 // Read custom difficulty settings for this game
45 blockDistance = parent->loadIntSetting( "SFCave_custom_blockdistance", 50 );
46
47 double thrust = parent->loadDoubleSetting( "SFCave_custom_player_thrust", 0.4 );
48 double gravity = parent->loadDoubleSetting( "SFCave_custom_player_gravity", 0.6 );
49 double maxUp = parent->loadDoubleSetting( "SFCave_custom_player_maxupspeed", 4.0 );
50 double maxDown = parent->loadDoubleSetting( "SFCave_custom_player_maxdownspeed", 5.0 );
51 player->setMovementInfo( thrust, gravity, maxUp, maxDown );
52
53 break;
54 }
42 } 55 }
43 56
44 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 57 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
45 blocks[i].y( -1 ); 58 blocks[i].y( -1 );
46} 59}
47 60
48void SFCaveGame :: update( int state ) 61void SFCaveGame :: update( int state )
49{ 62{
50 Game::update( state ); 63 Game::update( state );
51 64
52 if ( state == STATE_PLAYING ) 65 if ( state == STATE_PLAYING )
53 { 66 {
54 if ( nrFrames % 3 == 0 ) 67 if ( nrFrames % 3 == 0 )
55 score ++; 68 score ++;
56 69
57 if ( nrFrames % 200 == 0 ) 70 if ( nrFrames % 200 == 0 )
58 { 71 {
59 if ( terrain->getMaxHeight() < sHeight - 100 ) 72 if ( terrain->getMaxHeight() < sHeight - 100 )
60 { 73 {
61 terrain->increaseMaxHeight( 10 ); 74 terrain->increaseMaxHeight( 10 );
62 75
63 // Reduce block height 76 // Reduce block height
64 if ( terrain->getMaxHeight() > sHeight - 150 ) 77 if ( terrain->getMaxHeight() > sHeight - 150 )
65 blockHeight -= 5; 78 blockHeight -= 5;
66 } 79 }
67 } 80 }
68 81
69 if ( checkCollisions() ) 82 if ( checkCollisions() )
70 { 83 {
71 // printf( "Crashed!\n" );
72 parent->changeState( STATE_CRASHING ); 84 parent->changeState( STATE_CRASHING );
73 return; 85 return;
74 } 86 }
75 87
76 if ( nrFrames % blockDistance == 0 ) 88 if ( nrFrames % blockDistance == 0 )
77 addBlock(); 89 addBlock();
78 90
79 // Game logic goes here 91 // Game logic goes here
80 terrain->moveTerrain( 5 ); 92 terrain->moveTerrain( 5 );
81 moveBlocks( 5 ); 93 moveBlocks( 5 );
82 player->move( press ); 94 player->move( press );
83 } 95 }
84} 96}
85 97
86void SFCaveGame :: draw( SDL_Surface *screen ) 98void SFCaveGame :: draw( SDL_Surface *screen )
87{ 99{
88 Game::preDraw( screen ); 100 Game::preDraw( screen );
89 101
90 if ( parent->getState() == STATE_PLAYING ) 102 if ( parent->getState() == STATE_PLAYING )
91 { 103 {
92 // Screen drawing goes here 104 // Screen drawing goes here
93 terrain->drawTerrain( screen ); 105 terrain->drawTerrain( screen );
94 106
95 player->draw( screen ); 107 player->draw( screen );
96 108
97 drawBlocks( screen ); 109 drawBlocks( screen );
98 } 110 }
99 else 111 else
100 { 112 {
101 // Screen drawing goes here 113 // Screen drawing goes here
102 terrain->drawTerrain( screen ); 114 terrain->drawTerrain( screen );
103 115
104 drawBlocks( screen ); 116 drawBlocks( screen );
105 117
106 player->draw( screen ); 118 player->draw( screen );
107 } 119 }
108 120
109 Game::draw( screen ); 121 Game::draw( screen );
110} 122}
111 123
112void SFCaveGame :: addBlock() 124void SFCaveGame :: addBlock()
113{ 125{
114 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 126 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
115 { 127 {
116 if ( blocks[i].y() == -1 ) 128 if ( blocks[i].y() == -1 )
117 { 129 {
118 int x = sWidth; 130 int x = sWidth;
119 131
120 int y = terrain->getMapTop( MAPSIZE-1 ) + (int)(nextInt(terrain->getMapBottom( MAPSIZE-1 ) - terrain->getMapTop( MAPSIZE-1 ) - blockHeight)); 132 int y = terrain->getMapTop( MAPSIZE-1 ) + (int)(nextInt(terrain->getMapBottom( MAPSIZE-1 ) - terrain->getMapTop( MAPSIZE-1 ) - blockHeight));
121 133
122 blocks[i].setRect( x, y, blockWidth, blockHeight ); 134 blocks[i].setRect( x, y, blockWidth, blockHeight );
123 135
124 break; 136 break;
125 } 137 }
126 } 138 }
127} 139}
128 140
129void SFCaveGame :: moveBlocks( int amountToMove ) 141void SFCaveGame :: moveBlocks( int amountToMove )
130{ 142{
131 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 143 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
132 { 144 {
133 if ( blocks[i].y() != -1 ) 145 if ( blocks[i].y() != -1 )
134 { 146 {
135 blocks[i].moveBy( -amountToMove, 0 ); 147 blocks[i].moveBy( -amountToMove, 0 );
136 if ( blocks[i].x() + blocks[i].y() < 0 ) 148 if ( blocks[i].x() + blocks[i].y() < 0 )
137 blocks[i].y( -1 ); 149 blocks[i].y( -1 );
138 } 150 }
139 } 151 }
140} 152}
141 153
142void SFCaveGame :: drawBlocks( SDL_Surface *screen ) 154void SFCaveGame :: drawBlocks( SDL_Surface *screen )
143{ 155{
144 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 156 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
145 { 157 {
146 if ( blocks[i].y() != -1 ) 158 if ( blocks[i].y() != -1 )
147 { 159 {
148 SDL_Rect r = blocks[i].getRect(); 160 SDL_Rect r = blocks[i].getRect();
149 SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 100, 100, 255 ) ); 161 SDL_FillRect( screen, &r, SDL_MapRGB( screen->format, 100, 100, 255 ) );
150 } 162 }
151 } 163 }
152} 164}
153 165
154bool SFCaveGame :: checkCollisions() 166bool SFCaveGame :: checkCollisions()
155{ 167{
156 // Check collisions with blocks 168 // Check collisions with blocks
157 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 169 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
158 { 170 {
159 if ( blocks[i].y() != -1 ) 171 if ( blocks[i].y() != -1 )
160 { 172 {
161 if ( blocks[i].intersects( player->getPos() ) ) 173 if ( blocks[i].intersects( player->getPos() ) )
162 return true; 174 return true;
163 } 175 }
164 } 176 }
165 // Check collision with landscape 177 // Check collision with landscape
166 return terrain->checkCollision( player->getX(), player->getY(), player->getHeight() ); 178 return terrain->checkCollision( player->getX(), player->getY(), player->getHeight() );
167} 179}
diff --git a/noncore/games/sfcave-sdl/sound.cpp b/noncore/games/sfcave-sdl/sound.cpp
index 5fda859..855f2e6 100644
--- a/noncore/games/sfcave-sdl/sound.cpp
+++ b/noncore/games/sfcave-sdl/sound.cpp
@@ -1,154 +1,157 @@
1#include "constants.h" 1#include "constants.h"
2#include "sound.h" 2#include "sound.h"
3 3
4Mix_Chunk *SoundHandler :: sounds[NR_SOUNDS]; 4Mix_Chunk *SoundHandler :: sounds[NR_SOUNDS];
5Mix_Music *SoundHandler :: music; 5Mix_Music *SoundHandler :: music;
6int SoundHandler :: soundChannels[NR_SOUNDS]; 6int SoundHandler :: soundChannels[NR_SOUNDS];
7bool SoundHandler :: soundOn; 7bool SoundHandler :: soundOn;
8bool SoundHandler :: musicOn; 8bool SoundHandler :: musicOn;
9 9
10bool SoundHandler :: init( ) 10bool SoundHandler :: init( )
11{ 11{
12 // We're going to be requesting certain things from our audio 12 // We're going to be requesting certain things from our audio
13 // device, so we set them up beforehand 13 // device, so we set them up beforehand
14 int audio_rate = 22050; 14 int audio_rate = 22050;
15 Uint16 audio_format = AUDIO_S16; //AUDIO_S16; /* 16-bit stereo */ 15 Uint16 audio_format = AUDIO_S16; //AUDIO_S16; /* 16-bit stereo */
16 int audio_channels = 2; 16 int audio_channels = 2;
17 int audio_buffers = 1024;//4096; 17 int audio_buffers = 1024;//4096;
18 18
19 // This is where we open up our audio device. Mix_OpenAudio takes 19 // This is where we open up our audio device. Mix_OpenAudio takes
20 // as its parameters the audio format we'd /like/ to have. 20 // as its parameters the audio format we'd /like/ to have.
21 if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) 21 if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers))
22 { 22 {
23 printf("Unable to open audio!\n"); 23 printf("Unable to open audio!\n");
24 return false; 24 return false;
25 } 25 }
26 26
27 // We're going to pre-load the sound effects that we need right here 27 // We're going to pre-load the sound effects that we need right here
28 sounds[SND_EXPLOSION] = Mix_LoadWAV( SOUND_PATH "explosion.wav"); 28 sounds[SND_EXPLOSION] = Mix_LoadWAV( SOUND_PATH "explosion.wav");
29 sounds[SND_THRUST] = Mix_LoadWAV( SOUND_PATH "thrust.wav"); 29 sounds[SND_THRUST] = Mix_LoadWAV( SOUND_PATH "thrust.wav");
30 30
31 music = 0; 31 music = 0;
32 32
33 soundOn = true; 33 soundOn = true;
34 musicOn = true;
34 35
35 return true; 36 return true;
36} 37}
37 38
38void SoundHandler :: cleanUp() 39void SoundHandler :: cleanUp()
39{ 40{
40 // Free audio sounds 41 // Free audio sounds
41 Mix_FreeChunk( sounds[SND_EXPLOSION] ); 42 if ( sounds[SND_EXPLOSION] )
42 Mix_FreeChunk( sounds[SND_THRUST] ); 43 Mix_FreeChunk( sounds[SND_EXPLOSION] );
44 if ( sounds[SND_THRUST] )
45 Mix_FreeChunk( sounds[SND_THRUST] );
43 46
44 if ( music ) 47 if ( music )
45 Mix_FreeMusic( music ); 48 Mix_FreeMusic( music );
46 49
47 Mix_CloseAudio(); 50 Mix_CloseAudio();
48} 51}
49 52
50int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBeforeFinished ) 53int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBeforeFinished )
51{ 54{
52 if ( !soundOn ) 55 if ( !soundOn )
53 return -1; 56 return -1;
54 57
55 if ( soundNr >= NR_SOUNDS ) 58 if ( soundNr >= NR_SOUNDS || !sounds[soundNr] )
56 return -1; 59 return -1;
57 60
58 Mix_Chunk *chunk = sounds[soundNr]; 61 Mix_Chunk *chunk = sounds[soundNr];
59 if( channel == -1 || !Mix_Playing( channel ) ) 62 if( channel == -1 || !Mix_Playing( channel ) )
60 channel = Mix_PlayChannel(-1, sounds[soundNr], nrLoops); 63 channel = Mix_PlayChannel(-1, sounds[soundNr], nrLoops);
61 64
62 Mix_Volume( channel, MIX_MAX_VOLUME ); 65 Mix_Volume( channel, MIX_MAX_VOLUME );
63 return channel; 66 return channel;
64} 67}
65 68
66void SoundHandler :: stopSound( int channel, bool fadeOut, int nrMilliSecs ) 69void SoundHandler :: stopSound( int channel, bool fadeOut, int nrMilliSecs )
67{ 70{
68 if ( !soundOn ) 71 if ( !soundOn )
69 return; 72 return;
70 73
71 if ( !fadeOut ) 74 if ( !fadeOut )
72 Mix_HaltChannel( channel ); 75 Mix_HaltChannel( channel );
73 else 76 else
74 { 77 {
75 Mix_FadeOutChannel( channel, nrMilliSecs ); 78 Mix_FadeOutChannel( channel, nrMilliSecs );
76 } 79 }
77} 80}
78 81
79void SoundHandler :: playMusic( string musicFile ) 82void SoundHandler :: playMusic( string musicFile )
80{ 83{
81 if ( !soundOn ) 84 if ( !soundOn )
82 return; 85 return;
83 86
84 // If music already exists - stop it playing if necessary and free it up 87 // If music already exists - stop it playing if necessary and free it up
85 if ( music ) 88 if ( music )
86 { 89 {
87 stopMusic(); 90 stopMusic();
88 Mix_FreeMusic( music ); 91 Mix_FreeMusic( music );
89 } 92 }
90 93
91 // Load music 94 // Load music
92 music = Mix_LoadMUS( musicFile.c_str() ); 95 music = Mix_LoadMUS( musicFile.c_str() );
93 if(!music) 96 if(!music)
94 { 97 {
95 printf("Mix_LoadMUS(%s): %s\n", musicFile.c_str(), Mix_GetError()); 98 printf("Mix_LoadMUS(%s): %s\n", musicFile.c_str(), Mix_GetError());
96 // this might be a critical error... 99 // this might be a critical error...
97 } 100 }
98 101
99 playMusic(); 102 playMusic();
100} 103}
101 104
102void SoundHandler :: playMusic( bool fade ) 105void SoundHandler :: playMusic( bool fade )
103{ 106{
104 if ( !soundOn ) 107 if ( !musicOn )
105 return; 108 return;
106 109
107 if ( music ) 110 if ( music )
108 { 111 {
109 Mix_VolumeMusic( MIX_MAX_VOLUME ); 112 Mix_VolumeMusic( MIX_MAX_VOLUME );
110 Mix_RewindMusic(); 113 Mix_RewindMusic();
111 114
112 if ( fade ) 115 if ( fade )
113 Mix_FadeInMusic( music, -1, 1000 ); 116 Mix_FadeInMusic( music, -1, 1000 );
114 else 117 else
115 Mix_PlayMusic( music, -1 ); 118 Mix_PlayMusic( music, -1 );
116 119
117 } 120 }
118} 121}
119 122
120void SoundHandler :: stopMusic( bool fadeOut ) 123void SoundHandler :: stopMusic( bool fadeOut )
121{ 124{
122 if ( !music || !Mix_PlayingMusic() ) 125 if ( !music || !Mix_PlayingMusic() )
123 return; 126 return;
124 127
125 if ( fadeOut && Mix_FadingMusic() == MIX_NO_FADING ) 128 if ( fadeOut && Mix_FadingMusic() == MIX_NO_FADING )
126 { 129 {
127 Mix_FadeOutMusic( 1000 ); 130 Mix_FadeOutMusic( 1000 );
128 } 131 }
129 else 132 else
130 { 133 {
131 Mix_HaltMusic(); 134 Mix_HaltMusic();
132 } 135 }
133 136
134} 137}
135 138
136void SoundHandler :: setMusicVolume( int vol ) 139void SoundHandler :: setMusicVolume( int vol )
137{ 140{
138 Mix_VolumeMusic( vol ); 141 Mix_VolumeMusic( vol );
139} 142}
140 143
141void SoundHandler :: setSoundsOn( bool val ) 144void SoundHandler :: setSoundsOn( bool val )
142{ 145{
143 soundOn = val; 146 soundOn = val;
144} 147}
145 148
146void SoundHandler :: setMusicOn( bool val ) 149void SoundHandler :: setMusicOn( bool val )
147{ 150{
148 musicOn = val; 151 musicOn = val;
149 152
150 if ( !musicOn ) 153 if ( !musicOn )
151 stopMusic(); 154 stopMusic();
152 else 155 else
153 playMusic( true ); 156 playMusic( true );
154} 157}
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
@@ -1,223 +1,295 @@
1#include "SDL.h" 1#include "SDL.h"
2#include "SDL_gfxPrimitives.h" 2#include "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
225
226
227// Test
228#ifdef DEBUG_STARS
229SDL_Surface *screen;
230StarField *stars;
231
232void 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
288extern "C"
289#endif
290int main( int argc, char *argv[] )
291{
292 go();
293}
294
295#endif
diff --git a/noncore/games/sfcave-sdl/terrain.cpp b/noncore/games/sfcave-sdl/terrain.cpp
index c001a56..b243f45 100644
--- a/noncore/games/sfcave-sdl/terrain.cpp
+++ b/noncore/games/sfcave-sdl/terrain.cpp
@@ -1,313 +1,297 @@
1#include "SDL.h" 1#include "SDL.h"
2#include "SDL_rotozoom.h" 2#include "SDL_rotozoom.h"
3#include "SDL_gfxPrimitives.h" 3#include "SDL_gfxPrimitives.h"
4 4
5#include "constants.h" 5#include "constants.h"
6#include "terrain.h" 6#include "terrain.h"
7#include "random.h" 7#include "random.h"
8#include "util.h" 8#include "util.h"
9#include "starfield.h" 9#include "starfield.h"
10 10
11Terrain :: Terrain( int w, int h, bool drawTop, bool drawBottom ) 11Terrain :: Terrain( int w, int h, bool drawTop, bool drawBottom )
12{ 12{
13 sWidth = w; 13 sWidth = w;
14 sHeight = h; 14 sHeight = h;
15 speed = 1; 15 speed = 1;
16 segSize = sWidth/(MAPSIZE-2)+1; 16 segSize = sWidth/(MAPSIZE-2)+1;
17 this->drawTop = drawTop; 17 this->drawTop = drawTop;
18 this->drawBottom = drawBottom; 18 this->drawBottom = drawBottom;
19 19
20 stars = new StarField( true ); 20 stars = new StarField( true );
21 21
22 SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, sWidth + 20, sHeight, 32, 22 SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, sWidth + 20, sHeight, 32,
23 0x000000ff,0x0000ff00, 0x00ff0000, 0xff000000); 23 0x000000ff,0x0000ff00, 0x00ff0000, 0xff000000);
24 terrainSurface = SDL_DisplayFormat( tmp ); 24 terrainSurface = SDL_DisplayFormat( tmp );
25 SDL_FreeSurface( tmp ); 25 SDL_FreeSurface( tmp );
26 26
27 initTerrain(); 27 initTerrain();
28 28
29} 29}
30 30
31Terrain :: ~Terrain() 31Terrain :: ~Terrain()
32{ 32{
33 SDL_FreeSurface( terrainSurface ); 33 SDL_FreeSurface( terrainSurface );
34 delete stars; 34 delete stars;
35} 35}
36 36
37void Terrain :: initTerrain() 37void Terrain :: initTerrain()
38{ 38{
39 dir = 1; 39 dir = 1;
40 offset = 0; 40 offset = 0;
41 41
42 maxHeight = 50; 42 maxHeight = 50;
43 43
44 mapTop[0] = (int)(nextInt(50)) + 5; 44 mapTop[0] = (int)(nextInt(50)) + 5;
45 mapBottom[0] = sHeight - (maxHeight - mapTop[0]); 45 mapBottom[0] = sHeight - (maxHeight - mapTop[0]);
46 for ( int i = 1 ; i < MAPSIZE ; ++i ) 46 for ( int i = 1 ; i < MAPSIZE ; ++i )
47 setPoint( i ); 47 setPoint( i );
48 48
49 SDL_FillRect( terrainSurface, 0, 0 ); 49 SDL_FillRect( terrainSurface, 0, 0 );
50
50 // Draw Terrain into surface 51 // Draw Terrain into surface
51 Sint16 px[5]; 52 Sint16 px[5];
52 Sint16 py[5]; 53 Sint16 py[5];
53 54
54 for ( int i = 0 ; i < MAPSIZE ; ++i ) 55 for ( int i = 0 ; i < MAPSIZE ; ++i )
55 { 56 {
56 int left = (i*segSize); 57 int left = (i*segSize);
57 int right = ((i+1)*segSize); 58 int right = ((i+1)*segSize);
58 px[0] = left; 59 px[0] = left;
59 py[0] = mapTop[i]; 60 py[0] = mapTop[i];
60 px[1] = right; 61 px[1] = right;
61 py[1] = mapTop[i+1]; 62 py[1] = mapTop[i+1];
62 px[2] = right; 63 px[2] = right;
63 py[2] = 0; 64 py[2] = 0;
64 px[3] = left; 65 px[3] = left;
65 py[3] = 0; 66 py[3] = 0;
66 67
67 if ( drawTop ) 68 if ( drawTop )
68 { 69 {
69 // Only display top landscape if not running FLY_GAME 70 // Only display top landscape if not running FLY_GAME
70 filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 ); 71 filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 );
71 } 72 }
72 73
73 if ( drawBottom ) 74 if ( drawBottom )
74 { 75 {
75 py[0] = mapBottom[i]; 76 py[0] = mapBottom[i];
76 py[1] = mapBottom[i+1]; 77 py[1] = mapBottom[i+1];
77 py[2] = sHeight; 78 py[2] = sHeight;
78 py[3] = sHeight; 79 py[3] = sHeight;
79 filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 ); 80 filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 );
80 } 81 }
81 82
82 } 83 }
83 84
84 85
85} 86}
86 87
87void Terrain :: moveTerrain( int amountToMove ) 88void Terrain :: moveTerrain( int amountToMove )
88{ 89{
89 stars->move(); 90 stars->move();
90 91
91 offset += amountToMove; 92 offset += amountToMove;
92 speed = offset/segSize; 93 speed = offset/segSize;
93 94
94 //printf( "offset - %d, speed - %d\n", offset, speed );
95 if ( offset >= segSize ) 95 if ( offset >= segSize )
96 { 96 {
97 for ( int i = 0 ; i < (MAPSIZE)-speed ; ++i ) 97 for ( int i = 0 ; i < (MAPSIZE)-speed ; ++i )
98 { 98 {
99 mapTop[i] = mapTop[i+speed]; 99 mapTop[i] = mapTop[i+speed];
100 mapBottom[i] = mapBottom[i+speed]; 100 mapBottom[i] = mapBottom[i+speed];
101 } 101 }
102 102
103 for ( int i = (MAPSIZE)-speed ; i < MAPSIZE ; ++i ) 103 for ( int i = (MAPSIZE)-speed ; i < MAPSIZE ; ++i )
104 { 104 {
105 setPoint( i ); 105 setPoint( i );
106 } 106 }
107 107
108 SDL_Rect dst; 108 SDL_Rect dst;
109 dst.x = offset; 109 dst.x = offset;
110 dst.y = 0; 110 dst.y = 0;
111 dst.w = sWidth; 111 dst.w = sWidth;
112 dst.h = sHeight; 112 dst.h = sHeight;
113 113
114 SDL_Rect dst2; 114 SDL_Rect dst2;
115 dst2.x = 0; 115 dst2.x = 0;
116 dst2.y = 0; 116 dst2.y = 0;
117 117
118 SDL_BlitSurface(terrainSurface, &dst, terrainSurface, &dst2 ); 118 SDL_BlitSurface(terrainSurface, &dst, terrainSurface, &dst2 );
119 119
120 dst.x = sWidth-5; 120 dst.x = sWidth-5;
121 dst.y = 0; 121 dst.y = 0;
122 dst.w = offset; 122 dst.w = offset;
123 dst.h = sHeight; 123 dst.h = sHeight;
124 SDL_FillRect( terrainSurface, &dst, 0 ); 124 SDL_FillRect( terrainSurface, &dst, 0 );
125 for ( int i = (MAPSIZE-1) - speed -1; i < MAPSIZE-1 ; ++i ) 125 for ( int i = (MAPSIZE-1) - speed -1; i < MAPSIZE-1 ; ++i )
126 { 126 {
127 Sint16 px[4]; 127 Sint16 px[4];
128 Sint16 py[5]; 128 Sint16 py[5];
129 int left = ((i-1)*segSize); 129 int left = ((i-1)*segSize);
130 int right = ((i)*segSize); 130 int right = ((i)*segSize);
131 // printf( "left = %d, right = %d\n", left, right ); 131 // printf( "left = %d, right = %d\n", left, right );
132 132
133 px[0] = left; 133 px[0] = left;
134 py[0] = mapTop[i]; 134 py[0] = mapTop[i];
135 px[1] = right; 135 px[1] = right;
136 py[1] = mapTop[i+1]; 136 py[1] = mapTop[i+1];
137 px[2] = right; 137 px[2] = right;
138 py[2] = 0; 138 py[2] = 0;
139 px[3] = left; 139 px[3] = left;
140 py[3] = 0; 140 py[3] = 0;
141 141
142 if ( drawTop ) 142 if ( drawTop )
143 { 143 {
144 // Only display top landscape if not running FLY_GAME 144 // Only display top landscape if not running FLY_GAME
145 filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 ); 145 filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 );
146 } 146 }
147 147
148 if ( drawBottom ) 148 if ( drawBottom )
149 { 149 {
150 py[0] = mapBottom[i]; 150 py[0] = mapBottom[i];
151 py[1] = mapBottom[i+1]; 151 py[1] = mapBottom[i+1];
152 py[2] = sHeight; 152 py[2] = sHeight;
153 py[3] = sHeight; 153 py[3] = sHeight;
154 filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 ); 154 filledPolygonRGBA( terrainSurface, px, py, 4, 0, 190, 0, 255 );
155 } 155 }
156 } 156 }
157 157
158 offset -= speed*segSize; 158 offset -= speed*segSize;
159 159
160 } 160 }
161 161
162} 162}
163 163
164void Terrain :: setPoint( int point ) 164void Terrain :: setPoint( int point )
165{ 165{
166 if ( nextInt( 100 ) >= 80 ) 166 if ( nextInt( 100 ) >= 80 )
167 dir *= -1; 167 dir *= -1;
168 168
169 mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 )); 169 mapTop[point] = mapTop[point-1] + (dir * nextInt( 5 ));
170 if ( mapTop[point] < 0 ) 170 if ( mapTop[point] < 0 )
171 { 171 {
172 mapTop[point] = 0; 172 mapTop[point] = 0;
173 dir *= -1; 173 dir *= -1;
174 } 174 }
175 else if ( mapTop[point] >= maxHeight ) 175 else if ( mapTop[point] >= maxHeight )
176 { 176 {
177 mapTop[point] = maxHeight; 177 mapTop[point] = maxHeight;
178 dir *= -1; 178 dir *= -1;
179 } 179 }
180 180
181 mapBottom[point] = sHeight - (maxHeight - mapTop[point]); 181 mapBottom[point] = sHeight - (maxHeight - mapTop[point]);
182} 182}
183 183
184void Terrain :: increaseMaxHeight( int amount ) 184void Terrain :: increaseMaxHeight( int amount )
185{ 185{
186 maxHeight += amount; 186 maxHeight += amount;
187} 187}
188 188
189void Terrain :: drawTerrain( SDL_Surface *screen ) 189void Terrain :: drawTerrain( SDL_Surface *screen )
190{ 190{
191 // Blit terrain surface onto screen 191 // Blit terrain surface onto screen
192 192
193 SDL_Rect dst; 193 SDL_Rect dst;
194 dst.x = offset; 194 dst.x = offset;
195 dst.y = 0; 195 dst.y = 0;
196 dst.w = sWidth; 196 dst.w = sWidth;
197 dst.h = sHeight; 197 dst.h = sHeight;
198 //dst.h = maxHeight;
199 198
200 SDL_Rect dst2; 199 SDL_Rect dst2;
201 dst2.x = 0; 200 dst2.x = 0;
202 dst2.y = 0; 201 dst2.y = 0;
203 202
204 SDL_BlitSurface(terrainSurface, &dst, screen, &dst2 ); 203 SDL_BlitSurface(terrainSurface, &dst, screen, &dst2 );
205 204
206 stars->draw( screen ); 205 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} 206}
224 207
225bool Terrain :: checkCollision( int x, int y, int h ) 208bool Terrain :: checkCollision( int x, int y, int h )
226{ 209{
227 if ( y < 0 || y > sHeight ) 210 if ( y < 0 || y > sHeight )
228 return true; 211 return true;
212
229 // First get segment that matches x 213 // First get segment that matches x
230 SDL_LockSurface( terrainSurface ); 214 SDL_LockSurface( terrainSurface );
231 215
232 Uint32 c = getpixel( terrainSurface, x, y ); 216 Uint32 c = getpixel( terrainSurface, x, y );
233 217
234 SDL_UnlockSurface( terrainSurface ); 218 SDL_UnlockSurface( terrainSurface );
235 219
236 if ( c == 0 ) 220 if ( c == 0 )
237 return false; 221 return false;
238 else 222 else
239 return true; 223 return true;
240} 224}
241 225
242 226
243// Test 227// Test
244#ifdef DEBUG_TERRAIN 228#ifdef DEBUG_TERRAIN
245SDL_Surface *screen; 229SDL_Surface *screen;
246Terrain *terrain; 230Terrain *terrain;
247 231
248void go() 232void go()
249{ 233{
250 /* Initialize SDL */ 234 // Initialize SDL
251 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) 235 if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
252 { 236 {
253 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 237 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
254 exit(1); 238 exit(1);
255 } 239 }
256 atexit(SDL_Quit); 240 atexit(SDL_Quit);
257 241
258 int videoflags = SDL_SWSURFACE ; 242 int videoflags = SDL_SWSURFACE ;
259 243
260 if ( (screen=SDL_SetVideoMode(280, 320,32,videoflags)) == NULL ) 244 if ( (screen=SDL_SetVideoMode(280, 320,32,videoflags)) == NULL )
261 { 245 {
262 fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",WIDTH,HEIGHT,SDL_GetError()); 246 fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",WIDTH,HEIGHT,SDL_GetError());
263 exit(2); 247 exit(2);
264 } 248 }
265 249
266 terrain = new Terrain( 240, 320 ); 250 terrain = new Terrain( 240, 320 );
267 251
268 bool done = false; 252 bool done = false;
269 while ( !done ) 253 while ( !done )
270 { 254 {
271 SDL_FillRect( screen, 0, 0 ); 255 SDL_FillRect( screen, 0, 0 );
272 terrain->drawTerrain( screen ); 256 terrain->drawTerrain( screen );
273 terrain->moveTerrain( 5 ); 257 terrain->moveTerrain( 5 );
274 258
275 SDL_Flip( screen ); 259 SDL_Flip( screen );
276 260
277 SDL_Delay( 5 ); 261 SDL_Delay( 5 );
278 262
279 SDL_Event event; 263 SDL_Event event;
280 while ( SDL_PollEvent(&event) ) 264 while ( SDL_PollEvent(&event) )
281 { 265 {
282 switch (event.type) 266 switch (event.type)
283 { 267 {
284 case SDL_KEYDOWN: 268 case SDL_KEYDOWN:
285 // Escape keypress quits the app 269 // Escape keypress quits the app
286 if ( event.key.keysym.sym != SDLK_ESCAPE ) 270 if ( event.key.keysym.sym != SDLK_ESCAPE )
287 { 271 {
288 terrain->moveTerrain( 5 ); 272 terrain->moveTerrain( 5 );
289 break; 273 break;
290 } 274 }
291 case SDL_QUIT: 275 case SDL_QUIT:
292 done = 1; 276 done = 1;
293 break; 277 break;
294 default: 278 default:
295 break; 279 break;
296 } 280 }
297 } 281 }
298 } 282 }
299 } 283 }
300 284
301 285
302 286
303 287
304#ifdef __cplusplus 288#ifdef __cplusplus
305extern "C" 289extern "C"
306#endif 290#endif
307int main( int argc, char *argv[] ) 291int main( int argc, char *argv[] )
308{ 292{
309 go(); 293 go();
310} 294}
311 295
312#endif 296#endif
313 297
diff --git a/noncore/games/sfcave-sdl/util.cpp b/noncore/games/sfcave-sdl/util.cpp
index 86738ad..f73e256 100644
--- a/noncore/games/sfcave-sdl/util.cpp
+++ b/noncore/games/sfcave-sdl/util.cpp
@@ -1,64 +1,75 @@
1#include "SDL.h" 1#include "SDL.h"
2 2
3#include <dirent.h> 3#include <dirent.h>
4 4
5#include <vector> 5#include <vector>
6using namespace std; 6using namespace std;
7 7
8#include "util.h" 8#include "util.h"
9#include "random.h" 9#include "random.h"
10 10
11Uint32 getpixel(SDL_Surface *surface, int x, int y) 11Uint32 getpixel(SDL_Surface *surface, int x, int y)
12{ 12{
13 int bpp = surface->format->BytesPerPixel; 13 int bpp = surface->format->BytesPerPixel;
14 /* Here p is the address to the pixel we want to retrieve */ 14 /* Here p is the address to the pixel we want to retrieve */
15 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; 15 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
16 16
17 switch(bpp) { 17 switch(bpp) {
18 case 1: 18 case 1:
19 return *p; 19 return *p;
20 20
21 case 2: 21 case 2:
22 return *(Uint16 *)p; 22 return *(Uint16 *)p;
23 23
24 case 3: 24 case 3:
25 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) 25 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
26 return p[0] << 16 | p[1] << 8 | p[2]; 26 return p[0] << 16 | p[1] << 8 | p[2];
27 else 27 else
28 return p[0] | p[1] << 8 | p[2] << 16; 28 return p[0] | p[1] << 8 | p[2] << 16;
29 29
30 case 4: 30 case 4:
31 return *(Uint32 *)p; 31 return *(Uint32 *)p;
32 32
33 default: 33 default:
34 return 0; /* shouldn't happen, but avoids warnings */ 34 return 0; /* shouldn't happen, but avoids warnings */
35 } 35 }
36} 36}
37 37
38const char *chooseRandomFile( string path, string fileType ) 38string chooseRandomFile( string path, string fileType )
39{ 39{
40 vector<string> files; 40 vector<string> files;
41 DIR *d = opendir( path.c_str() ); 41 DIR *d = opendir( path.c_str() );
42 if ( !d ) 42 if ( !d )
43 return ""; 43 return "";
44 44
45 struct dirent *item = readdir( d ); 45 struct dirent *item = readdir( d );
46 while ( item ) 46 while ( item )
47 { 47 {
48 string file = string( path ) + item->d_name; 48 string file = string( path ) + item->d_name;
49 49
50 // Rip extension from file 50 // Rip extension from file
51 int pos = file.find( ".", 1 ) + 1; 51 int pos = file.find( ".", 1 ) + 1;
52 string tmp = file.substr( pos ); 52 string tmp = file.substr( pos );
53 printf( "pos = %d, tmp =%s\n", pos, tmp.c_str() );
54 if ( tmp.size() > 0 && fileType.find( tmp ) != -1 ) 53 if ( tmp.size() > 0 && fileType.find( tmp ) != -1 )
55 { 54 {
56 printf( "Matching <%s> - %s with <%s>\n", file.substr( pos ).c_str(), file.c_str(), fileType.c_str() );
57 files.push_back( file ); 55 files.push_back( file );
58 } 56 }
59 item = readdir( d ); 57 item = readdir( d );
60 } 58 }
61 59
62 closedir( d ); 60 closedir( d );
63 return files[nextInt( files.size() )].c_str(); 61 return files[nextInt( files.size() )];
62}
63
64
65string getHomeDir()
66{
67 string home;
68#ifdef QWS
69 home = getenv( "HOME" );
70#else
71 home = ".";
72#endif
73
74 return home;
64} 75}
diff --git a/noncore/games/sfcave-sdl/util.h b/noncore/games/sfcave-sdl/util.h
index fe3e9c0..e3aa31a 100644
--- a/noncore/games/sfcave-sdl/util.h
+++ b/noncore/games/sfcave-sdl/util.h
@@ -1,10 +1,10 @@
1#ifndef __UTIL_H 1#ifndef __UTIL_H
2#define __UTIL_H 2#define __UTIL_H
3 3
4#include <string> 4#include <string>
5using namespace std; 5using namespace std;
6 6
7Uint32 getpixel(SDL_Surface *surface, int x, int y); 7Uint32 getpixel(SDL_Surface *surface, int x, int y);
8const char *chooseRandomFile( string path, string fileType ); 8string chooseRandomFile( string path, string fileType );
9 9string getHomeDir();
10#endif 10#endif