#include "quakedef.h"

//jeank
cvar_t gl_screen_width = {"gl_screen_width","0", true};
cvar_t gl_screen_height = {"gl_screen_height","0", true};
cvar_t gl_screen_bpp = {"gl_screen_bpp","32", true};
cvar_t gl_ignore16bpp = {"gl_ignore16bpp","1", true};
cvar_t gl_ignorelowres = {"gl_ignorelowres","1", true};

cvar_t	gl_vsync	= {"gl_vsync","0", true};
cvar_t	gl_fpslimit = {"gl_fpslimit","0", true};
cvar_t	gl_ztrick	= {"gl_ztrick","1",true};

cvar_t	gl_fbo_pbo	= {"gl_fbo_pbo","2", true};

cvar_t  gl_bumpmap	= {"gl_bumpmap","0", true};
cvar_t  gl_specular = {"gl_specular","0", true};

// mouse variables
cvar_t	m_filter = {"m_filter","0",true};
cvar_t	m_accel = {"m_accel","0",true};
cvar_t	m_accelscale = {"m_accelscale","1",true};

cvar_t r_menusize = {"r_menusize","6",true};
cvar_t s_samplerate = {"s_samplerate","44100",true};
cvar_t gl_aniso_level = {"gl_aniso_level","1",true};
cvar_t gl_menu_linear = {"gl_menu_linear","1",true};
cvar_t gl_hud_linear = {"gl_hud_linear","0",true};

cvar_t gl_nonpow2textures = {"gl_nonpow2textures","1",true};

cvar_t gl_contrast_level = {"gl_contrast_level","1",true};
cvar_t gl_overbright_level = {"gl_overbright_level","10",true};
cvar_t gl_fogautocolor = {"gl_fogautocolor","1",true};
cvar_t r_transwater = {"r_transwater", "1", true};
cvar_t r_translava = {"r_translava", "1", true};
cvar_t gl_waterfogred = {"gl_waterfogred","0.22",true}; 
cvar_t gl_waterfoggreen = {"gl_waterfoggreen","0.45",true}; 
cvar_t gl_waterfogblue = {"gl_waterfogblue","0.52",true}; 
cvar_t gl_waterfogalpha = {"gl_waterfogalpha", "0.5",true};
cvar_t gl_waterfogdistance = {"gl_waterfogdistance", "1400",true};

cvar_t gl_lavafogred = {"gl_lavafogred","1.0",true}; 
cvar_t gl_lavafoggreen = {"gl_lavafoggreen","0.3",true}; 
cvar_t gl_lavafogblue = {"gl_lavafogblue","0",true}; 
cvar_t gl_lavafogalpha = {"gl_lavafogalpha", "1",true};
cvar_t gl_lavafogdistance = {"gl_lavafogdistance", "400",true};

cvar_t gl_slimefogred = {"gl_slimefogred","0.1",true}; 
cvar_t gl_slimefoggreen = {"gl_slimefoggreen","0.4",true}; 
cvar_t gl_slimefogblue = {"gl_slimefogblue","0.15",true}; 
cvar_t gl_slimefogalpha = {"gl_slimefogalpha", "0.8",true};
cvar_t gl_slimefogdistance = {"gl_slimefogdistance", "500",true};

cvar_t r_interpolate_alias = {"r_interpolate_alias", "1", true};
cvar_t r_interpolate_time = {"r_interpolate_time", "0.05", true};
//jeank

cvar_t	r_texture_external = {"r_texture_external","1",true};
cvar_t	gl_max_size = {"gl_max_size", "1024",true};
cvar_t	gl_round_down = {"gl_round_down", "0", true};

cvar_t	gl_lerpimages = {"gl_lerpimages","1",true};
cvar_t	r_spritefix = {"r_spritefix","1",true};
cvar_t	gl_spritealpha = {"gl_spritealpha","90",true};
cvar_t	gl_hudalpha = {"gl_hudalpha","0.75",true};

cvar_t	m_fullangles = {"m_fullangles","1",true};

int		texture_mode = GL_LINEAR;
int		texture_extension_number = 1;

int		crosshairtexture[CROSSHAIRS];
int		num_crosshairs;
int		bloodscreen[BLOODSCREENS];
int		num_bloodscreens;

cvar_t	crosshair = {"crosshair", "1", true};
cvar_t	crosshairtype = {"crosshairtype", "0",true};
cvar_t	crosshairsize = {"crosshairsize", "48", true};
cvar_t	crosshairalpha = {"crosshairalpha", "0.4",true};

double bloodtimetodie = 0;
cvar_t	bloodscr = {"bloodscr", "1", true};
cvar_t	bloodscrtime = {"bloodscrtime", "4", true};
cvar_t	bloodscralpha = {"bloodscralpha", "0.9", true};

cvar_t	waterscreendeform = {"waterscreendeform", "2", true};
cvar_t  language = {"language", "0", true};

cvar_t	gl_detailtexture ={"gl_detailtexture", "1",true};
cvar_t	gl_detailscale ={"gl_detailscale", "8",true};
cvar_t	gl_detailalpha ={"gl_detailalpha", "1.0",true};
cvar_t	gl_caustictexture ={"gl_caustictexture", "1",true};
cvar_t	gl_causticalpha ={"gl_causticalpha", "0.5",true};
cvar_t	gl_shinytexture ={"gl_shinytexture", "1",true};
cvar_t	gl_shinynormalize ={"gl_shinynormalize", "1",true};
cvar_t	gl_shinydistancefactor ={"gl_shinydistancefactor", "0.5",true};
cvar_t	gl_shinyalpha ={"gl_shinyalpha", "0.6",true};
cvar_t  gl_shinycrackedalpha ={"gl_shinycrackedalpha", "0.5",true};
cvar_t	gl_shinymetalalpha ={"gl_shinymetalalpha", "0.9",true};
cvar_t  gl_shinymetalrustedalpha ={"gl_shinymetalrustedalpha", "0.6",true};
cvar_t	gl_shinyglassalpha ={"gl_shinyglassalpha", "0.6",true};

cvar_t  gl_shinyscale ={"gl_shinyscale", "2.0",true};
cvar_t  gl_shinycrackedscale ={"gl_shinycrackedscale", "1.0",true};
cvar_t  gl_shinymetalscale ={"gl_shinymetalscale", "6.0",true};
cvar_t  gl_shinymetalrustedscale ={"gl_shinymetalrustedscale", "4.0",true};
cvar_t  gl_shinyglassscale ={"gl_shinyglassscale", "8.0",true};

cvar_t	gl_rustytexture ={"gl_rustytexture", "1",true};
cvar_t	gl_rustyscale ={"gl_rustyscale", "0.5",true};
cvar_t	gl_rustyglassscale ={"gl_rustyglassscale", "0.5",true};
cvar_t	gl_rustymetalscale ={"gl_rustymetalscale", "1",true};

cvar_t  gl_alias_chrome = {"gl_alias_chrome","1",true};
cvar_t  gl_power_chrome = {"gl_power_chrome","0.8",true};
cvar_t  gl_weapon_chrome = {"gl_weapon_chrome","0.8",true};
cvar_t  gl_bonus_chrome = {"gl_bonus_chrome","0.8",true};
cvar_t  gl_monster_chrome = {"gl_monster_chrome","0.8",true};
cvar_t  gl_player_chrome = {"gl_player_chrome","0.8",true};
cvar_t  gl_interior_chrome = {"gl_interior_chrome","0.8",true};

cvar_t  gl_powerup_chromescale = {"gl_powerup_chromescale","1.4",true};
cvar_t  gl_weapon_chromescale = {"gl_weapon_chromescale","8",true};
cvar_t  gl_bonus_chromescale = {"gl_bonus_chromescale","0.1",true};
cvar_t  gl_monster_chromescale = {"gl_monster_chromescale","0.2",true};
cvar_t  gl_monstertoxic_chromescale = {"gl_monstertoxic_chromescale","0.1",true};
cvar_t  gl_monstershined_chromescale = {"gl_monstershined_chromescale","0.2",true};
cvar_t  gl_monsterwater_chromescale = {"gl_monsterwater_chromescale","0.1",true};
cvar_t  gl_statue_chromescale = {"gl_statue_chromescale","0.3",true};
cvar_t  gl_arcyellow_chromescale = {"gl_arcyellow_chromescale","0.2",true};
cvar_t  gl_interior_chromescale = {"gl_interior_chromescale","0.1",true};
cvar_t  gl_playerwpn_chromescale = {"gl_playerwpn_chromescale","0.2",true};
cvar_t  gl_pazzles_chromescale = {"gl_pazzles_chromescale","0.5",true};
cvar_t  gl_health_chromescale = {"gl_health_chromescale","0.12",true};
cvar_t  gl_blumana_chromescale = {"gl_blumana_chromescale","1",true};
cvar_t  gl_redmana_chromescale = {"gl_redmana_chromescale","1",true};
cvar_t  gl_grnmana_chromescale = {"gl_grnmana_chromescale","1",true};

int     detailtexture = 0;
int     chrometexture = 0;
int     chrometexture2 = 0;
int     chrometexture3 = 0;
int		rustmetaltexture = 0;
int		rustmetaltexture2 = 0;
int		crackstonetexture = 0;
int		crackstonetexture2 = 0;
int		crackstonetexture3 = 0;
int		scratchglasstexture = 0;
int		scratchglasstexture2 = 0;
int     modelchrometexture = 0;
int     modelchrometexture2 = 0;
int     waterreflecttexture = 0;
int     glowtexture   = 0;
int     caustictexture[MAX_CAUSTICS];
int     num_caustics = 0;

///jeank for initialize width, height, bpp variables
char readname[50];
char readvalue[30];

void ReadConfiguration (char *fname)
{
	FILE	*f;
	f = fopen (va("%s/%s",com_gamedir,fname), "r");
	if (!f)
	{
		Con_Printf ("Couldn't get %s.\n",fname);
		return;
	}
	
	while (!feof (f))
	{
		fscanf (f, "%s \"%s\"\n", &readname, &readvalue);
		if (strcmp (readname,"gl_screen_width")==0)
			Cvar_SetValue ("gl_screen_width", atoi(readvalue));
		else if (strcmp (readname,"gl_screen_height")==0)
			Cvar_SetValue ("gl_screen_height", atoi(readvalue));
		else if (strcmp (readname,"gl_screen_bpp")==0)
			Cvar_SetValue ("gl_screen_bpp", atoi(readvalue));
		else if (strcmp (readname,"gl_ignore16bpp")==0)
			Cvar_SetValue ("gl_ignore16bpp", atoi(readvalue));
		else if (strcmp (readname,"gl_ignorelowres")==0)
			Cvar_SetValue ("gl_ignorelowres", atoi(readvalue));
		else if (strcmp (readname,"gl_menu_linear")==0)
			Cvar_SetValue ("gl_menu_linear", atoi(readvalue));
		else if (strcmp (readname,"gl_hud_linear")==0)
			Cvar_SetValue ("gl_hud_linear", atoi(readvalue));
		else if (strcmp (readname,"developer")==0)
			Cvar_SetValue ("developer", atoi(readvalue));
	}	
	fclose (f);
}

/*
===============
Register vars
===============
*/

void RegisterMouseVars(void)
{
	// mouse variables
	Cvar_RegisterVariable (&m_filter);
	Cvar_RegisterVariable (&m_accel);
	Cvar_RegisterVariable (&m_accelscale);
	Raw_VarsRegister ();
}

void RegisterVars(void)
{

	Cvar_RegisterVariable (&gl_max_size);
	Cvar_RegisterVariable (&gl_round_down);
	Cvar_RegisterVariable (&gl_lerpimages);
	Cvar_RegisterVariable (&r_spritefix);
	Cvar_RegisterVariable (&gl_spritealpha);
	Cvar_RegisterVariable (&gl_hudalpha);

	Cvar_RegisterVariable (&r_texture_external);

	Cvar_RegisterVariable (&r_menusize);
	Cvar_RegisterVariable (&s_samplerate);
	Cvar_RegisterVariable (&gl_aniso_level);
	Cvar_RegisterVariable (&gl_menu_linear);
	Cvar_RegisterVariable (&gl_hud_linear);

	Cvar_RegisterVariable (&gl_nonpow2textures);

	Cvar_RegisterVariable (&gl_contrast_level);
	Cvar_RegisterVariable (&gl_overbright_level);
	Cvar_RegisterVariable (&gl_fogautocolor);
	Cvar_RegisterVariable (&r_transwater);
	Cvar_RegisterVariable (&r_translava);

	//Fog
	Cvar_RegisterVariable (&gl_waterfogred);
	Cvar_RegisterVariable (&gl_waterfoggreen);
	Cvar_RegisterVariable (&gl_waterfogblue);
	Cvar_RegisterVariable (&gl_waterfogalpha);
	Cvar_RegisterVariable (&gl_waterfogdistance);


	Cvar_RegisterVariable (&gl_lavafogred);
	Cvar_RegisterVariable (&gl_lavafoggreen);
	Cvar_RegisterVariable (&gl_lavafogblue);
	Cvar_RegisterVariable (&gl_lavafogalpha);
	Cvar_RegisterVariable (&gl_lavafogdistance);

	Cvar_RegisterVariable (&gl_slimefogred);
	Cvar_RegisterVariable (&gl_slimefoggreen);
	Cvar_RegisterVariable (&gl_slimefogblue);
	Cvar_RegisterVariable (&gl_slimefogalpha);
	Cvar_RegisterVariable (&gl_slimefogdistance);
	//Fog


    Cvar_RegisterVariable (&gl_detailtexture);
    Cvar_RegisterVariable (&gl_detailalpha);
    Cvar_RegisterVariable (&gl_detailscale);
    Cvar_RegisterVariable (&gl_caustictexture);
	Cvar_RegisterVariable (&gl_causticalpha);
    
	//Shiny
	Cvar_RegisterVariable (&gl_shinytexture);
	Cvar_RegisterVariable (&gl_shinynormalize);
	Cvar_RegisterVariable (&gl_shinydistancefactor);
    Cvar_RegisterVariable (&gl_shinyalpha);
	Cvar_RegisterVariable (&gl_shinycrackedalpha);
	Cvar_RegisterVariable (&gl_shinymetalalpha);
	Cvar_RegisterVariable (&gl_shinymetalrustedalpha);
	Cvar_RegisterVariable (&gl_shinyglassalpha);

	//Shinyscale
	Cvar_RegisterVariable (&gl_shinyscale);
	Cvar_RegisterVariable (&gl_shinycrackedscale);
	Cvar_RegisterVariable (&gl_shinymetalscale);
	Cvar_RegisterVariable (&gl_shinymetalrustedscale);
	Cvar_RegisterVariable (&gl_shinyglassscale);

	Cvar_RegisterVariable (&gl_alias_chrome);
	Cvar_RegisterVariable (&gl_power_chrome);
	Cvar_RegisterVariable (&gl_weapon_chrome);
	Cvar_RegisterVariable (&gl_bonus_chrome);
	Cvar_RegisterVariable (&gl_monster_chrome);
	Cvar_RegisterVariable (&gl_player_chrome);
	Cvar_RegisterVariable (&gl_interior_chrome);
	
	Cvar_RegisterVariable (&gl_rustytexture);
	Cvar_RegisterVariable (&gl_rustyscale);
	Cvar_RegisterVariable (&gl_rustyglassscale);
	Cvar_RegisterVariable (&gl_rustymetalscale);

	Cvar_RegisterVariable (&m_fullangles);

	//crosshairs
	Cvar_RegisterVariable (&crosshair);
	Cvar_RegisterVariable (&crosshairtype);
	Cvar_RegisterVariable (&crosshairsize);
	Cvar_RegisterVariable (&crosshairalpha);

	Cvar_RegisterVariable (&bloodscr);
	Cvar_RegisterVariable (&bloodscrtime);
	Cvar_RegisterVariable (&bloodscralpha);

	Cvar_RegisterVariable (&waterscreendeform);
	Cvar_RegisterVariable (&language);

	//Alias chrome scale vars
	Cvar_RegisterVariable (&gl_powerup_chromescale);
	Cvar_RegisterVariable (&gl_weapon_chromescale);
	Cvar_RegisterVariable (&gl_bonus_chromescale);
	Cvar_RegisterVariable (&gl_monster_chromescale);
	Cvar_RegisterVariable (&gl_monstertoxic_chromescale);
	Cvar_RegisterVariable (&gl_monstershined_chromescale);
	Cvar_RegisterVariable (&gl_monsterwater_chromescale);
	Cvar_RegisterVariable (&gl_statue_chromescale);
	Cvar_RegisterVariable (&gl_arcyellow_chromescale);
	Cvar_RegisterVariable (&gl_interior_chromescale);
	Cvar_RegisterVariable (&gl_playerwpn_chromescale);
	Cvar_RegisterVariable (&gl_pazzles_chromescale);
	Cvar_RegisterVariable (&gl_health_chromescale);
	Cvar_RegisterVariable (&gl_blumana_chromescale);
	Cvar_RegisterVariable (&gl_redmana_chromescale);
	Cvar_RegisterVariable (&gl_grnmana_chromescale);

	Cvar_RegisterVariable(&gl_screen_width);
	Cvar_RegisterVariable(&gl_screen_height);
	Cvar_RegisterVariable(&gl_screen_bpp);
	Cvar_RegisterVariable(&gl_ignore16bpp);
	Cvar_RegisterVariable(&gl_ignorelowres);

	Cvar_RegisterVariable(&gl_vsync);
	Cvar_RegisterVariable(&gl_fpslimit);
	Cvar_RegisterVariable (&gl_ztrick);

	Cvar_RegisterVariable (&gl_fbo_pbo);

	Cvar_RegisterVariable (&gl_bumpmap);
	Cvar_RegisterVariable (&gl_specular);

	//Cvar_RegisterVariable (&r_nolerp_list, R_NoLerpList_f);
	
	R_BloomRegister ();
	RegisterMouseVars ();

	ReadConfiguration ("config.cfg");
}


/*
=================
Mod_SetExtraFlags - set up extra flags that aren't in the mdl
=================
*/
/*
void Mod_SetExtraFlags (model_t *mod)
{
        extern cvar_t r_nolerp_list;
        char *s;
        int i;

        if (!mod || !mod->name || mod->type != mod_alias)
                return;

        mod->flags &= 0xFF; //only preserve first byte

        // nolerp flag
        for (s=r_nolerp_list.string; *s; s += i+1, i=0)
        {
                //search forwards to the next comma or end of string
                for (i=0; s[i] != ',' && s[i] != 0; i++) ;

                //compare it to the model name
                if (!strncmp(mod->name, s, i))
                {
                        mod->flags |= MOD_NOLERP;
                        break;
                }
        }
}
*/
/*
===============
R_NoLerpList_f - called when r_nolerp_list cvar changes
===============
*/
/*
void R_NoLerpList_f (void)
{
        int i;

        for (i=0; i < MAX_MODELS; i++)
                Mod_SetExtraFlags (cl.model_precache[i]);
}
*/