1 /*
2 Solar Conquest
3 Copyright (C) 2006 Greg Beaman
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 using namespace std;
21
22 struct Vector
23 {
24 float x;
25 float y;
26 };
27
CreateVector(float x,float y)28 Vector CreateVector(float x, float y)
29 {
30 Vector v;
31 v.x = x;
32 v.y = y;
33 return v;
34 }
35
AddVectors(Vector v1,Vector v2)36 Vector AddVectors(Vector v1, Vector v2)
37 {
38 Vector v3;
39 v3.x = v1.x + v2.x;
40 v3.y = v1.y + v2.y;
41 return v3;
42 }
43
SubtractVectors(Vector v1,Vector v2)44 Vector SubtractVectors(Vector v1, Vector v2)
45 {
46 Vector v3;
47 v3.x = v1.x - v2.x;
48 v3.y = v1.y - v2.y;
49 return v3;
50 }
51
MultiplyVectors(Vector v1,Vector v2)52 Vector MultiplyVectors(Vector v1, Vector v2)
53 {
54 Vector v3;
55 v3.x = v1.x * v2.x;
56 v3.y = v1.y * v2.y;
57 return v3;
58 }
59
ScaleVector(Vector v1,float scale)60 Vector ScaleVector(Vector v1, float scale)
61 {
62 Vector v3;
63 v3.x = v1.x * scale;
64 v3.y = v1.y * scale;
65 return v3;
66 }
67
CapAngle(float angle)68 float CapAngle(float angle)
69 {
70 while (angle < -6.28)
71 angle += 6.28;
72 while (angle > 6.28)
73 angle -= 6.28;
74 return angle;
75 }
76
CapAngles(Vector angles)77 Vector CapAngles(Vector angles)
78 {
79 Vector v;
80 v.x = CapAngle(angles.x);
81 v.y = CapAngle(angles.y);
82 return v;
83 }
84
85 //floating point version of abs
absf(float num)86 float absf(float num)
87 {
88 if (num < 0)
89 num *= -1;
90 return num;
91 }
92
Distance(Vector v1,Vector v2)93 float Distance(Vector v1, Vector v2)
94 {
95 float d = sqrt((((v1.x-v2.x)*(v1.x-v2.x))+(v1.y-v2.y)*(v1.y-v2.y)));
96 return d;
97 }
98
Distance(float x1,float y1,float x2,float y2)99 float Distance(float x1, float y1, float x2, float y2)
100 {
101 float d = sqrt((((x1-x2)*(x1-x2))+(y1-y2)*(y1-y2)));
102 return d;
103 }
104
PointInBoxCenter(Vector boxCenter,float width,float height,Vector point)105 bool PointInBoxCenter(Vector boxCenter, float width, float height, Vector point)
106 {
107 if ((point.x < boxCenter.x-width) || (point.x > boxCenter.x+width))
108 return false;
109 if ((point.y < boxCenter.y-height) || (point.y > boxCenter.y+height))
110 return false;
111 return true;
112 }
113
114 //Wrappers in case lookup tables get used
ToDegrees(float angle)115 float ToDegrees(float angle) { return angle * (180/3.14); }
ToRadians(float angle)116 float ToRadians(float angle) { return angle * (3.14/180); }
117
GetProperAngle(float radians)118 float GetProperAngle(float radians)
119 {
120 float num = radians;
121 while (num >= (3.14*2))
122 num -= (3.14*2);
123 while (num < 0)
124 num = (3.14*2) + num;
125 return num;
126 }
GetNegativeAngle(float radians)127 float GetNegativeAngle(float radians)
128 {
129 float num = radians;
130 while (num > (3.14))
131 num -= (3.14*2);
132 return num;
133 }
134
GetSin(float angle)135 float GetSin(float angle) { return sin(angle); }
GetCos(float angle)136 float GetCos(float angle) { return cos(angle); }
137
checkGLError()138 GLenum checkGLError()
139 {
140 GLenum error = glGetError();
141 #ifdef _DEBUG
142 if (error == GL_NO_ERROR)
143 return error;
144 if (error == GL_INVALID_ENUM)
145 return error;
146 if (error == GL_INVALID_VALUE)
147 return error;
148 if (error == GL_INVALID_OPERATION)
149 return error;
150 if (error == GL_STACK_OVERFLOW)
151 return error;
152 if (error == GL_STACK_UNDERFLOW)
153 return error;
154 if (error == GL_OUT_OF_MEMORY)
155 return error;
156 #endif
157 return error;
158 }
159
FileExists(char * fileName)160 bool FileExists(char* fileName)
161 {
162 if (!fileName)
163 return false;
164
165 FILE* file;
166 file = fopen(fileName,"r");
167
168 if (!file)
169 return false;
170
171 fclose(file);
172
173 return true;
174 }
175
LoadBMPImage(char * fileName)176 SDL_Surface* LoadBMPImage(char* fileName)
177 {
178 if (!fileName)
179 return NULL;
180
181 FILE* file;
182 file = fopen(fileName,"r");
183
184 if (!file)
185 return NULL;
186
187 fclose(file);
188 return SDL_LoadBMP(fileName);
189 }
190
XPixelToTexCoord(int x,float pxWidth)191 float XPixelToTexCoord(int x, float pxWidth)
192 {
193 float rx;
194 rx = x * pxWidth;
195 return rx;
196 // return x;
197 }
198
YPixelToTexCoord(int y,int imgHeight,float pxHeight)199 float YPixelToTexCoord(int y, int imgHeight, float pxHeight)
200 {
201 float ry;
202 ry = (imgHeight-y) * pxHeight;
203 // return ry;
204 return y * pxHeight;
205 }
206
207 int lastFpsUpdate = 0;
208 int frames = 0;
209 int framesPerSecond = 0;
210
UpdateFPS()211 void UpdateFPS()
212 {
213 frames++;
214 if ((SDL_GetTicks()-lastFpsUpdate) >= 1000)
215 {
216 framesPerSecond = frames;
217 frames = 0;
218 lastFpsUpdate = SDL_GetTicks();
219 }
220 }
221
222 //Just an explenation for some of the funkyness here, there seems to be a difference between Windows gcvt and Unix gcvt, but since we only care about
223 //the first decimal point in the game speed, the speed is multiplied by 10 and made into an int on save, and multiplied by 0.1 on load. Just fyi.
LoadGameSettings()224 void LoadGameSettings()
225 {
226 if (!FileExists(".solarconquest.rc"))
227 {
228 g_screenWidth = 640;
229 g_screenHeight = 480;
230 g_screenDepth = 16;
231
232 g_fullscreen = false;
233 g_soundEnabled = true;
234 g_gameSpeed = 1.0;
235 return;
236 }
237
238 ifstream file;
239 file.open(".solarconquest.rc");
240
241 char txt[MAX_STRING_SIZE];
242
243 file.getline(txt,MAX_STRING_SIZE-2);
244 g_screenWidth = atoi(txt);
245 file.getline(txt,MAX_STRING_SIZE-2);
246 g_screenHeight = atoi(txt);
247 file.getline(txt,MAX_STRING_SIZE-2);
248 g_screenDepth = atoi(txt);
249
250 file.getline(txt,MAX_STRING_SIZE-2);
251 if (atoi(txt) == 1)
252 g_fullscreen = true;
253 else
254 g_fullscreen = false;
255
256 file.getline(txt,MAX_STRING_SIZE-2);
257 if (atoi(txt) == 1)
258 g_soundEnabled = true;
259 else
260 g_soundEnabled = false;
261
262 file.getline(txt,MAX_STRING_SIZE-2);
263 g_gameSpeed = atof(txt) * 0.1;
264 }
265
SaveGameSettings()266 void SaveGameSettings()
267 {
268 ofstream file;
269 file.open(".solarconquest.rc");
270
271 char num[MAX_STRING_SIZE];
272
273 sprintf(num,"%d\n%d\n%d\n",g_screenWidth,g_screenHeight,g_screenDepth);
274 file.write(num,strlen(num));
275
276 if (g_fullscreen)
277 file.write("1\n",strlen("1\n"));
278 else
279 file.write("0\n",strlen("0\n"));
280
281 if (g_soundEnabled)
282 file.write("1\n",strlen("1\n"));
283 else
284 file.write("0\n",strlen("0\n"));
285
286 // gcvt(g_gameSpeed,3,num);
287 int ftoi = (int)(g_gameSpeed*10);
288 sprintf(num,"%d\n",ftoi);
289 file.write(num,strlen(num));
290 }