-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
286 lines (237 loc) · 6.22 KB
/
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "Common.h"
#include <SDL2/SDL.h>
#include "Game.h"
#include "SharedVars.h"
#include "GLCommon.h"
#include "SoundManager/SoundManager.h"
static bool done = false;
static bool active = true;
static bool keys[SDL_NUM_SCANCODES];
static int screenwidth = 800;
static int screenheight = 600;
static bool fullscreen = false;
static char *programtitle = "Flyby";
static float sdlgamma = 1.0f;
static SDL_Window *window;
static SDL_GLContext glContext;
void KillGLFrame()
{
CloseGL();
SDL_GL_DeleteContext( glContext );
SDL_Quit();
}
bool SDL_GL_LoadExtensions( const char *glExtensions )
{
if ( !strstr( glExtensions, "GL_ARB_multitexture" ) )
return false;
#ifndef NO_GLEXT
// ARB_multitexture functions
glActiveTexture = (PFNGLACTIVETEXTUREARBPROC)SDL_GL_GetProcAddress( "glActiveTextureARB" );
glClientActiveTexture = (PFNGLCLIENTACTIVETEXTUREARBPROC)SDL_GL_GetProcAddress( "glClientActiveTextureARB" );
glMultiTexCoord2fv = (PFNGLMULTITEXCOORD2FVARBPROC)SDL_GL_GetProcAddress( "glMultiTexCoord2fvARB" );
glMultiTexCoord3fv = (PFNGLMULTITEXCOORD3FVARBPROC)SDL_GL_GetProcAddress( "glMultiTexCoord3fvARB" );
if ( !glActiveTexture || !glClientActiveTexture || !glMultiTexCoord2fv || !glMultiTexCoord3fv )
return false;
#endif
return true;
}
void SDL_GL_SwapBuffers()
{
SDL_GL_SwapWindow( window );
}
void SDL_SetGamma( float redGamma, float greenGamma, float blueGamma )
{
Uint16 red[256];
Uint16 green[256];
Uint16 blue[256];
SDL_CalculateGammaRamp(redGamma, red);
SDL_CalculateGammaRamp(greenGamma, green);
SDL_CalculateGammaRamp(blueGamma, blue);
SDL_SetWindowGammaRamp(window, red, green, blue);
}
bool CreateGLFrame( char *title, int width, int height, int bpp, bool fullscreen )
{
unsigned int flags;
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Could not initialize SDL\n" );
return false;
}
flags = SDL_WINDOW_OPENGL;
if ( fullscreen )
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
if ( !window )
{
printf( "Could not set video mode\n" );
return false;
}
glContext = SDL_GL_CreateContext( window );
if (!glContext)
{
printf( "Could not create OpenGL context\n" );
return false;
}
SDL_SetGamma( sdlgamma, sdlgamma, sdlgamma );
SDL_ShowCursor( SDL_DISABLE );
glSwapBuffers = SDL_GL_SwapBuffers;
glLoadExtensions = SDL_GL_LoadExtensions;
ResizeGLScene( width, height );
if ( !InitGL() )
{
printf( "OpenGL initialization failed\n" );
KillGLFrame();
return false;
}
return true;
}
void HandleEvent( SDL_Event *event )
{
switch( event->type )
{
case SDL_SYSWMEVENT:
// syscommand (screensaver, monitor power saving mode, etc)
break;
case SDL_QUIT:
done = 1;
break;
case SDL_MOUSEMOTION:
break;
case SDL_KEYDOWN:
keys[ event->key.keysym.scancode ] = true;
break;
case SDL_KEYUP:
keys[ event->key.keysym.scancode ] = false;
break;
case SDL_WINDOWEVENT:
if (event->window.event == SDL_WINDOWEVENT_RESIZED)
ResizeGLScene( event->window.data1, event->window.data2 );
break;
}
}
void ReadSettings( char *filename )
{
FILE *f;
int w = 0, h = 0, full = 0, subdiv = 0;
float gamm = 0, volume = 0, music = 0;
if ( !( f = fopen( filename, "r" ) ) )
{
printf( "Could not open settings file %s\n", filename );
return;
}
fscanf( f, "width %i\n", &w );
fscanf( f, "height %i\n", &h );
fscanf( f, "fullscreen %i\n", &full );
fscanf( f, "gamma %f\n", &gamm );
fscanf( f, "volume %f\n", &volume );
fscanf( f, "musicvolume %f\n", &music );
fscanf( f, "subdivisions %i\n", &subdiv );
if ( w && h )
{
screenwidth = w;
screenheight = h;
}
fullscreen = (full != 0);
if ( gamm )
sdlgamma = gamm;
s_volume = volume;
s_musicvolume = music;
if ( subdiv )
bsp_subdivisions = subdiv;
fclose( f );
}
int main( int argc, char **argv )
{
SDL_Event event;
unsigned int prevtime;
ReadSettings( "settings.cfg" );
if ( !CreateGLFrame( programtitle, screenwidth, screenheight, 32, fullscreen ) )
{
printf( "Could not open OpenGL window\n" );
return 1;
}
InitGame();
prevtime = SDL_GetTicks();
while ( !done )
{
// Update time
realtime = SDL_GetTicks();
int timedelta = realtime - prevtime;
if ( hyperdrive > 0 )
timedelta *= hyperdrive;
else if ( hyperdrive < 0 )
timedelta /= -hyperdrive;
time = time + timedelta;
frametime = (float)timedelta / 1000.0f;
// Poll SDL events
while ( SDL_PollEvent( &event ) )
{
HandleEvent( &event );
}
// Process input (TODO: Create a better system for input handling)
if ( keys[SDL_SCANCODE_ESCAPE] )
{
done = true;
}
Vector3D forward, right, up;
cam.angles.AnglesToAxis( forward, right, up );
if ( keys[SDL_SCANCODE_F] )
{
keys[SDL_SCANCODE_F] = false;
hyperdrive = (hyperdrive > 0) ? 0 : 4;
}
if ( keys[SDL_SCANCODE_H] )
{
keys[SDL_SCANCODE_H] = false;
hyperdrive = (hyperdrive < 0) ? 0 : -4;
}
if ( keys[SDL_SCANCODE_UP] )
cam.angles.x += 100 * frametime;
if ( keys[SDL_SCANCODE_DOWN] )
cam.angles.x -= 100 * frametime;
if ( keys[SDL_SCANCODE_LEFT] )
cam.angles.y += 100 * frametime;
if ( keys[SDL_SCANCODE_RIGHT] )
cam.angles.y -= 100 * frametime;
if ( keys[SDL_SCANCODE_W] )
cam.origin = cam.origin + forward * 360 * frametime;
if ( keys[SDL_SCANCODE_S] )
cam.origin = cam.origin - forward * 360 * frametime;
if ( keys[SDL_SCANCODE_A] )
cam.origin = cam.origin - right * 360 * frametime;
if ( keys[SDL_SCANCODE_D] )
cam.origin = cam.origin + right * 360 * frametime;
if ( keys[SDL_SCANCODE_Q] )
cam.origin.y += 360 * frametime;
if ( keys[SDL_SCANCODE_Z] )
cam.origin.y -= 360 * frametime;
if ( keys[SDL_SCANCODE_EQUALS] )
{
keys[SDL_SCANCODE_EQUALS] = false;
sdlgamma += 0.1f;
SDL_SetGamma( sdlgamma, sdlgamma, sdlgamma );
}
if ( keys[SDL_SCANCODE_MINUS] )
{
keys[SDL_SCANCODE_MINUS] = false;
sdlgamma -= 0.1f;
SDL_SetGamma( sdlgamma, sdlgamma, sdlgamma );
}
SoundManager::Instance()->Update();
// Update the game
UpdateFrame();
// Render the screen
if ( active )
{
InitDrawing();
DrawFrame();
FinalizeDrawing();
}
prevtime = realtime;
}
CloseGame();
KillGLFrame();
return 0;
}