[go: up one dir, main page]

Menu

[r21]: / src / main.cc  Maximize  Restore  History

Download this file

395 lines (320 with data), 10.8 kB

  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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/**
* Nitrito 3D
* Created:
* Last Modified: 28/03/06
*/
#include "nitrito.h"
World *world;
Coche *coche;
Camera *cam;
Timer timer;
Speaker snd;
Console console;
Menu menu;
Race race;
SDL_Surface *screen;
freetype::font_data font;
freetype::font_data font_big;
freetype::font_data console_font;
ObjLoader LoaderClass;
Options options;
Raton raton;
float gvar_1,gvar_2;
void set_options()
{
options.done = FALSE;
options.camera_mode = 3;
options.weather = W_CLEAR;
options.screen_w = 640;
options.screen_h = 480;
options.bpp = 16;
options.fullscreen = FALSE;
options.mipmaps = TRUE;
options.gravity = 2.5;
options.tiempo_bala = FALSE;
options.view_far = 300;
options.view_close = 0.1;
options.joystick = FALSE;
options.audio_rate = 44100;
options.t_bala_time_speed = 0.5;
options.normal_time_speed = 1.5;
options.console_max_lines = 30;
options.num_coches = 2;
options.motion_blur = TRUE;
options.data_path = "../data/";
options.music_vol = 0.5;
options.sfx_vol = 0.9;
options.texture_quality = 1;
options.shadows = 0;
}
/**
* Set the video mode from de arguments.
* @param arg Command-line video parameter.
* @param w Video width.
* @param h Video height.
*/
void set_video_mode (string arg, int &w, int &h) {
string sw = "", sh = "";
bool flag = false;
for (unsigned int i = 13; i < arg.length(); i++)
if (arg[i] == 'x') flag = true;
else if (!flag) sw += arg[i];
else if (flag) sh += arg[i];
w = atoi(sw.c_str ());
h = atoi(sh.c_str ());
if (w != 640 && w != 800 && w != 1024) {
fprintf (stderr, "* Incorrect Mode: %dx%d *\n", w, h);
w = 640; //Default width
}
else if (h != 480 && h != 600 && h != 768) {
fprintf (stderr, "* Incorrect Mode: %dx%d *\n", w, h);
h = 480; //Default height
}
}
/**
* Load options from a file.
*/
void set_options_from_file() {
string aux, filename = "../data/nitrito.cfg";
ifstream f;
f.open(filename.c_str ());
if(!f.is_open())
{
fprintf ( stderr, "Failed Opening File: %s\n", filename.c_str ());
f.clear();
string homedir = getenv("HOME");
filename = homedir + "/.nitrito/nitrito.cfg";
fprintf ( stderr, "Opening %s...\n", filename.c_str ());
f.open(filename.c_str ());
}
if(!f.is_open()) {
fprintf ( stderr, "Failed Opening File: %s\n", filename.c_str ());
}
else {
getline(f, aux);
getline(f, aux);
f >> aux >> options.camera_mode;
f >> aux >> options.weather;
f >> aux >> options.screen_w;
f >> aux >> options.screen_h;
f >> aux >> options.bpp;
f >> aux >> options.fullscreen;
f >> aux >> options.mipmaps;
f >> aux >> options.gravity;
f >> aux >> options.tiempo_bala;
f >> aux >> options.view_far;
f >> aux >> options.joystick;
f >> aux >> options.audio_rate;
f >> aux >> options.t_bala_time_speed;
f >> aux >> options.normal_time_speed;
f >> aux >> options.console_max_lines;
f >> aux >> options.motion_blur;
f >> aux >> options.data_path;
f >> aux >> options.music_vol;
f >> aux >> options.sfx_vol;
f >> aux >> options.texture_quality;
f >> aux >> options.shadows;
f.close();
}
}
/**
* Set options from the command-line arguments.
* @param argc Number of arguments.
* @param argv String Vector of arguments.
*/
void set_options_from_args (int argc, char **argv) {
if (argc > 1) {
for (int i=0; i < argc; i++) {
if (strcmp(argv[i],"--help") == 0) {
printf("NITRITO 3D - Version (%s)\n",NVERSION);
printf("Parameters:\n"
" --video-mode= [640x480 | 800x600 | 1024x768]\n"
" -f[on|off] or --fullscreen=[on|off]\n"
" -d Debug Mode.\n"
);
exit (0);
}
else if (strncmp (argv[i], "--video-mode=", 13) == 0) {
set_video_mode (argv[i], options.screen_w, options.screen_h);
}
else if (strcmp (argv[i], "--fullscreen=on") == 0 ||
strcmp (argv[i], "-fon") == 0 )
options.fullscreen = TRUE;
else if (strcmp (argv[i], "--fullscreen=off") == 0 ||
strcmp (argv[i], "-foff") == 0 )
options.fullscreen = FALSE;
else if (strcmp (argv[i], "-d") == 0 ) {
cout << "[Debug Mode Activated]" << endl;
}
}
}
}
char* add_data_path(string filename)
{
filename = options.data_path + filename;
return (char*) filename.c_str();
}
void check_extensions()
{
char *ext = (char*)glGetString( GL_EXTENSIONS );
if( strstr( ext, "GL_ARB_multitexture" ) == NULL )
{
console.printf("Error: GL_ARB_multitexture no soportada, vaya mierda.");
}
else
{
glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)SDL_GL_GetProcAddress("glActiveTextureARB");
glMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC)SDL_GL_GetProcAddress("glMultiTexCoord2fARB");
glClientActiveTextureARB = (PFNGLCLIENTACTIVETEXTUREARBPROC)SDL_GL_GetProcAddress("glClientActiveTextureARB");
if( !glActiveTextureARB || !glMultiTexCoord2fARB || !glClientActiveTextureARB )
{
console.printf("Una o mas funciones de GL_ARB_multitexture no fueron encontradas.");
}
}
if( strstr( ext, "GL_ARB_shadow" ) == NULL )
{
console.printf("Extension GL_ARB_shadow no soportada.");
console.printf("Sin shadow no hay sombra.");
console.printf("Desactivando sombras...");
options.shadows = 0;
}
if( strstr( ext, "GL_EXT_framebuffer_object" ) == NULL )
{
console.printf("Extension GL_EXT_framebuffer_object no soportada.");
console.printf("Sin el framebuffer las sombras son demasiado feas..");
console.printf("Desactivando sombras...");
options.shadows = 0;
}
else
{
glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC) SDL_GL_GetProcAddress("glGenFramebuffersEXT");
glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC) SDL_GL_GetProcAddress("glBindFramebufferEXT");
glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC) SDL_GL_GetProcAddress("glFramebufferTexture2DEXT");
glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC) SDL_GL_GetProcAddress("glCheckFramebufferStatusEXT");
}
}
int main (int argc, char **argv)
{
long int frames=0;
set_options();
set_options_from_file ();
set_options_from_args (argc, argv);
console.printf("Consola cargada, 'help' para lista de comandos.");
console.printf("Inicializando...");
if (SDL_Init (SDL_INIT_VIDEO)) {
fprintf ( stderr, "SDL_Init error: %s\n", SDL_GetError () );
exit ( 1 );
}
if(options.joystick)
{
console.printf("Inicializando joystick.");
if (SDL_Init (SDL_INIT_JOYSTICK)) {
fprintf (stderr, "SDL_Init error: %s\n", SDL_GetError ());
exit (1);
console.printf("Error al inicializar el Joystick.");
}
else
{
SDL_Joystick *joystick;
SDL_JoystickEventState (SDL_ENABLE);
joystick = SDL_JoystickOpen (0);
}
}
if(options.fullscreen)
{
console.printf("Inicializando video en modo pantalla completa...");
screen = SDL_SetVideoMode ( options.screen_w, //Width
options.screen_h, //Height
options.bpp, //Bbp
SDL_OPENGL |SDL_DOUBLEBUF | SDL_FULLSCREEN //Flags
);
if(!screen) {
fprintf ( stderr, "SDL_SetVideoMode error: %s\n", SDL_GetError ());
SDL_Quit ();
exit (2);
}
}
else
{
console.printf("Inicializando video en modo ventana...");
screen = SDL_SetVideoMode (options.screen_w, //Width
options.screen_h, //Height
options.bpp, //bbp
SDL_OPENGL | SDL_DOUBLEBUF //Flags
);
if(!screen) {
fprintf ( stderr, "SDL_SetVideoMode error: %s\n", SDL_GetError ());
SDL_Quit ();
exit (2);
}
}
{
GLint bits[6];
glGetIntegerv(GL_RED_BITS, &bits[0]);
glGetIntegerv(GL_GREEN_BITS, &bits[1]);
glGetIntegerv(GL_BLUE_BITS, &bits[2]);
glGetIntegerv(GL_ALPHA_BITS, &bits[3]);
glGetIntegerv(GL_DEPTH_BITS, &bits[4]);
glGetIntegerv(GL_STENCIL_BITS, &bits[5]);
console.printf("Ventana creada bits: RGBA(%d,%d,%d,%d), profundidad(%d) stencil(%d).",
bits[0], bits[1], bits[2], bits[3], bits[4], bits[5]);
}
SDL_WM_SetCaption ("Nitrito: Furgo Edition", "Nitrito");
SDL_ShowCursor(FALSE);
SDL_EnableUNICODE(0);
check_extensions();
console.printf("Inicializando sonido...");
alutInit(NULL, 0);
if (alGetError() != AL_NO_ERROR) console.printf("Eroor al inicializar sonido.");
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
ReSizeGLScene (options.screen_w, options.screen_h );
glShadeModel (GL_SMOOTH);
glClearDepth (1.0f);
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LEQUAL);
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable (GL_CULL_FACE); //cara de culo
glEnable (GL_TEXTURE_2D);
glEnable (GL_BLEND);
glEnable(GL_NORMALIZE);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.1,4.0);
glClear(GL_ACCUM_BUFFER_BIT);
set_opengl_ambient();
console.printf("Buscando datos en \"%s\".",options.data_path.c_str());
console.printf("Cargando texturas...");
load_textures();
world = new World;
coche = new Coche[1];
cam = new Camera;
menu.draw();
SDL_GL_SwapBuffers ();
console.printf("Cargando fuentes...");
font.init(add_data_path("fuente.ttf"), 12);
font_big.init(add_data_path("fuente.ttf"), 18);
console_font.init(add_data_path("fuente2.ttf"), 12);
console.printf("Cargando sonidos...");
snd.load_sounds();
SDL_Delay(100);
gvar_1=0;
gvar_2=0;
while (!options.done)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
handle_input ();
if(menu.is_open) menu.draw();
else if(race.is_open) race.race_loop();
SDL_GL_SwapBuffers (); // Swap Buffers
frames++;
//SDL_Delay(10);
}
//printf("FPS = %f\n", frames/(SDL_GetTicks()/1000.f));
//printf("Coche tiene %d triangulos\n", coche[0].chasis.NumTriangle);
//coche[0].destroy();
//world->destroy();
//int size;
//glGetIntegerv( GL_MAX_TEXTURE_SIZE, & size ) ;
//printf("%s",glGetString(GL_VERSION));
SDL_Quit ();
return 0;
}