/**
* 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;
}