Another proposal.
The goal of this function is to avoid the declaration of recurrent code in the main. Usually the next lines are always in the main, which minor changes:
Code:
raydium_window_create(640,480,RAYDIUM_RENDERING_WINDOW,"Primer aplicacion de Vicente");
/*raydium_window_create(640,480,RAYDIUM_RENDERING_FULLSCREEN,"My app");*/
raydium_texture_filter_change(RAYDIUM_TEXTURE_FILTER_BILINEAR);
raydium_window_view_perspective(60,0.01,2500); // fov 60 + near and far planes
raydium_fog_disable();
raydium_light_enable();
raydium_light_on(0);
raydium_light_conf_7f(0,50,150,200,1000000,1,0.9,0.7); // id, pos, intensity and color (RGB)
raydium_background_color_change(1,0.9,0.7,1);
raydium_sky_box_cache();
This function load a config text file with the variable data and do the rest by itself.:
This is an config text file:
Code:
width=800 #window width (default: 800)
height=600 #window height (default: 600)
windowtype=window #window style: window or fullscreen (default: window )
title=Titulo de pantalla #title of the window,application (default: Raydium application 0.1)
filter=aniso #texture filter: none, bilinear, trilinear, aniso (default: trilinear)
fov=60 #view angle, ie fov (default: 60)
near=0.001 #distance to the nearets object(plane) draw (default: 0.001)
far=2500 #distance to the further object(plane) draw (default: 2500)
fog=on #fog: on/enable or off/disable (default: off)
lighting=on #lighting: on/enable or off/disable (default: on )
light0=0,50,150,200,1000000,1,0.9,0.7 #light 0 parameters (default: 0,50,150,200,1000000,1,0.9,0.7)
background=1,0.9,0.7,1 #background color (default: 1,0.9,0.7,1)
So, using the prvious proposal and this one, an skel.c file could ends like this:
Code:
/*
Raydium - CQFD Corp.
http://raydium.org/
License: GPL - GNU General Public License, see "gpl.txt" file.
*/
// This file is a default skeleton. Replace all [ ... ] sections with yours.
// Have fun !
#include "raydium/index.c"
void display(void)
{
raydium_camera_first_person_free_allinone();
if(raydium_key_last==1027)
exit(0);
/* [ draw here ] */ raydium_ode_draw_all(0);
raydium_rendering_finish();
}
int main(int argc, char **argv)
{
raydium_init_args(argc,argv);
raydium_init_load_from_file("ejemplo.config");
/* [ place base scene here ] */ raydium_ode_ground_set_name("cocorobix.tri");
raydium_callback(&display);
return(0);
}
// EOF
And here is a preliminar version of the function (with spanish comments):
Code:
void raydium_init_load_from_file(char *filename)
{
FILE *fp;
int ret;
char var[255];
char val_s[255];
GLfloat val_f[255];
int size;
int tmp_width,tmp_height,tmp_windowtype,tmp_filter,tmp_fog,tmp_lighting;
double tmp_fov,tmp_far,tmp_near;
float tmp_light0[8],tmp_background[4];
char tmp_title[255],tmp_string[255];
int flag_width,flag_height,flag_title,flag_windowtype,flag_filter,flag_fov,flag_near,flag_far,flag_fog,flag_lighting,flag_light0,flag_background;
fp=raydium_file_fopen(filename,"rt");
flag_width=flag_height=flag_title=flag_windowtype=flag_filter=flag_fov=flag_near=flag_far=flag_fog=flag_lighting=flag_light0=flag_background=0;
while( (ret=raydium_parser_read(var,val_s,val_f,&size,fp))!=RAYDIUM_PARSER_TYPE_EOF)
{
if(!strcasecmp(var,"foobar_variable"))
{
if(ret!=RAYDIUM_PARSER_TYPE_FLOAT || size!=2)
{
raydium_log("error: foobar_variable is not float array");
continue;
}
}
raydium_log("*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***");
raydium_log("La variable %s, tiene el valor %s",var,val_s);
if(strcmp(var,"width")==0)
{
tmp_width=atoi(val_s);
raydium_log("Hemos configurado la anchura con el valor %d",tmp_width);
flag_width=1;
}
if(strcmp(var,"height")==0)
{
tmp_height=atoi(val_s);
raydium_log("Hemos configurado la altura con el valor %d",tmp_height);
flag_height=1;
}
if(strcmp(var,"windowtype")==0)
{
//ulgy way to clean the string. Is there a better way?
strncpy(tmp_string,val_s,strcspn(val_s,"\t "));
tmp_string[strcspn(val_s,"\t ")]=NULL;
if(strcmp(tmp_string,"fullscreen")==0) tmp_windowtype=RAYDIUM_RENDERING_FULLSCREEN;
else tmp_windowtype=RAYDIUM_RENDERING_WINDOW;
raydium_log("Hemos configurado el tipo de ventana como %s",(tmp_windowtype)?"RAYDIUM_RENDERING_FULLSCREEN":"RAYDIUM_RENDERING_WINDOW");
flag_windowtype=1;
}
if(strcmp(var,"title")==0)
{
//ulgy way to clean the string. Is there a better way?
strncpy(tmp_string,val_s,strcspn(val_s,"\t#"));
tmp_string[strcspn(val_s,"\t#")]=NULL;
strcpy(tmp_title,tmp_string);
raydium_log("Hemos configurado el titulo de ventana como: %s",tmp_title);
flag_title=1;
}
if(strcmp(var,"filter")==0)
{
//ulgy way to clean the string. Is there a better way?
strncpy(tmp_string,val_s,strcspn(val_s,"\t "));
tmp_string[strcspn(val_s,"\t ")]=NULL;
if(strcmp(tmp_string,"trilinear")==0)tmp_filter=2;
if(strcmp(tmp_string,"bilinear")==0)tmp_filter=1;
if(strcmp(tmp_string,"none")==0)tmp_filter=0;
if(strcmp(tmp_string,"aniso")==0)tmp_filter=3;
flag_filter=1;
}
if(strcmp(var,"fov")==0)
{
tmp_fov=atof(val_s);
raydium_log("Hemos configurado el angulo de visión como: %f",tmp_fov);
flag_fov=1;
}
if(strcmp(var,"near")==0)
{
tmp_near=atof(val_s);
raydium_log("Hemos configurado el limite cercano a: %f",tmp_near);
flag_near=1;
}
if(strcmp(var,"far")==0)
{
tmp_far=atof(val_s);
raydium_log("Hemos configurado el limite lejano a: %f",tmp_far);
flag_far=1;
}
if(strcmp(var,"fog")==0)
{
//ulgy way to clean the string. Is there a better way?
strncpy(tmp_string,val_s,strcspn(val_s,"\t "));
tmp_string[strcspn(val_s,"\t ")]=NULL;
tmp_fog=((strcmp(tmp_string,"on")==0) || (strcmp(tmp_string,"enable")==0))?1:0;
raydium_log("Hemos configurado la niebla como: %f",tmp_fog);
flag_fog=1;
}
if(strcmp(var,"lighting")==0)
{
//ulgy way to clean the string. Is there a better way?
strncpy(tmp_string,val_s,strcspn(val_s,"\t "));
tmp_string[strcspn(val_s,"\t ")]=NULL;
tmp_lighting=((strcmp(tmp_string,"on")==0) || (strcmp(tmp_string,"enable")==0))?1:0;
raydium_log("Hemos configurado la iluminación como: %f",tmp_lighting);
flag_lighting=1;
}
if(strcmp(var,"light0")==0)
{
//ulgy way to clean the string. Is there a better way?
strncpy(tmp_string,val_s,strcspn(val_s,"\t "));
tmp_string[strcspn(val_s,"\t ")]=NULL;
sscanf( tmp_string, "%f,%f,%f,%f,%f,%f,%f,%f", &tmp_light0[0], &tmp_light0[1],&tmp_light0[2],&tmp_light0[3],&tmp_light0[4],&tmp_light0[5],&tmp_light0[6],&tmp_light0[7]);
raydium_log("Hemos configurado la luz 0 con los valores: %f,%f,%f,%f,%f,%f,%f,%f",tmp_light0[0],tmp_light0[1],tmp_light0[2],tmp_light0[3],tmp_light0[4],tmp_light0[5],tmp_light0[6],tmp_light0[7]);
flag_light0=1;
}
if(strcmp(var,"background")==0)
{
//ulgy way to clean the string. Is there a better way?
strncpy(tmp_string,val_s,strcspn(val_s,"\t "));
tmp_string[strcspn(val_s,"\t ")]=NULL;
sscanf( tmp_string, "%f,%f,%f,%f", &tmp_background[0], &tmp_background[1],&tmp_background[2],&tmp_background[3]);
raydium_log("Hemos configurado la luz 0 con los valores: %f,%f,%f,%f,%f,%f,%f,%f",tmp_background[0], tmp_background[1],tmp_background[2],tmp_background[3]);
flag_background=1;
}
}
//Here, we process all the data achieved and make the raydium calls
if(flag_width && flag_height && flag_windowtype && flag_title)
{
raydium_log("vamos a crear la ventana con esta funcion: raydium_window_create(%d,%d,%d,\"%s\"); ",tmp_width,tmp_height,tmp_windowtype,tmp_title);
raydium_window_create(tmp_width,tmp_height,tmp_windowtype,tmp_title);
}
if(flag_filter)
{
raydium_log("configurando filtrado de textura en modo %d",tmp_filter);
raydium_texture_filter_change(tmp_filter);
}
if (flag_fov && flag_near && flag_far)
{
raydium_log("configurando el fov a %d, y los planos de recorte en %f y %f.",tmp_fov,tmp_near,tmp_far);
raydium_window_view_perspective(tmp_fov,tmp_near,tmp_far); // fov 60 + near and far planes
}
if(flag_fog)
{
raydium_log("configurando la niebla como %d",tmp_fog);
if(tmp_fog)raydium_fog_enable(); else raydium_fog_disable();
}
if(flag_lighting)
{
raydium_log("configurando la iluminación como %d",tmp_lighting);
if(tmp_fog)raydium_light_enable(); else raydium_light_disable();
}
if(flag_light0 && flag_lighting && tmp_lighting)
{
raydium_log("configurando la luz 0 con los valores : %f,%f,%f,%f,%f,%f,%f,%f",tmp_light0[0],tmp_light0[1],tmp_light0[2],tmp_light0[3],tmp_light0[4],tmp_light0[5],tmp_light0[6],tmp_light0[7]);
raydium_light_on(0);
raydium_light_conf_7f(tmp_light0[0],tmp_light0[1],tmp_light0[2],tmp_light0[3],tmp_light0[4],tmp_light0[5],tmp_light0[6],tmp_light0[7]); // id, pos, intensity and color (RGB)
}
if(flag_background)
{
raydium_log("configurando el color de fondo con los valores : %f,%f,%f,%f",tmp_background[0], tmp_background[1],tmp_background[2],tmp_background[3]);
raydium_background_color_change(tmp_background[0], tmp_background[1],tmp_background[2],tmp_background[3]);
}
//This should be configurable???
raydium_sky_box_cache();
}
Comments?