#include "quakedef.h"

void CheckOldVideoLimits (void)
{
	// 3dfx can only handle 256 wide textures
	if (is_3dfx || is_PowerVR)
	{
		if (is_3dfx)
			Cvar_Set ("gl_ignore16bpp", "0");
		if (gl_aniso_level.value>1)
			Cvar_Set ("gl_aniso_level", "1");
		if (gl_max_size.value>256)
			Cvar_Set ("gl_max_size", "256");
		if (gl_multitexture.value>0)
			Cvar_Set ("gl_multitexture", "0");
		if (gl_bloom.value>0)
			Cvar_Set ("gl_bloom", "0");
	}
}

void Set_Vsync (boolean enable)
{
  PFNWGLSWAPINTERVALEXTPROC wglSwapInterval = NULL;

  wglSwapInterval = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
  if ( wglSwapInterval ) wglSwapInterval(enable ? 1 : 0);
}

void Apply_Startup_Settings (void)
{
	VID_ChangeMenuSize();
	CheckOldVideoLimits();
	GL_SetGamma();
	Set_Vsync ((boolean)gl_vsync.value);
	Check_ARB_nonpow2_Extension();
}

void Load_ScrEffect_Textures (void)
{
	int		i;
	char LoadPath[MAX_QPATH];
    int tex;


	detailtexture = loadtextureimage("TextEff/detail", false, true, false);
    chrometexture = loadtextureimage("TextEff/shiny", false, true, false);
    chrometexture2 = loadtextureimage("TextEff/shiny2", false, true, false);
	chrometexture3 = loadtextureimage("TextEff/shiny3", false, true, false);
	rustmetaltexture = loadtextureimage("TextEff/rustmetal", false, true, false);
	rustmetaltexture2 = loadtextureimage("TextEff/rustmetal2", false, true, false);
	crackstonetexture = loadtextureimage("TextEff/crackstone", false, true, false);
	crackstonetexture2 = loadtextureimage("TextEff/crackstone2", false, true, false);
	crackstonetexture3 = loadtextureimage("TextEff/crackstone3", false, true, false);
	scratchglasstexture = loadtextureimage("TextEff/scratchglass", false, true, false);
	scratchglasstexture2 = loadtextureimage("TextEff/scratchglass2", false, true, false);
	modelchrometexture = loadtextureimage("TextEff/shinymodel", false, true, false);
	modelchrometexture2 = loadtextureimage("TextEff/shinymodel2", false, true, false);
	waterreflecttexture = loadtextureimage("TextEff/shinywater", false, true, false);
	//glowtexture  = loadtextureimage("glow", false, true, false);

	// Load bloodscreen textures
	num_bloodscreens = 0;
	for(i=0; i < BLOODSCREENS; i++)
	{
		sprintf(LoadPath, "TextEff/blood%i", i);
		tex = loadtextureimage(LoadPath, false, false, false);
        
        if (tex == 0)
            {
            sprintf(LoadPath, "TextEff/blood%02i", i);
		    tex = loadtextureimage(LoadPath, false, false, false);
            }

        if (tex != 0)
            bloodscreen[num_bloodscreens++] = tex; 
	}
	//jeank bloodscreens

	// Load the crosshair image files
	num_crosshairs = 0;
	for(i=0; i < CROSSHAIRS; i++)
	{
		sprintf(LoadPath, "TextEff/cross%i", i);
		tex = loadtextureimage(LoadPath, false, false, false);
        
        if (tex == 0)
            {
            sprintf(LoadPath, "TextEff/cross%02i", i);
		    tex = loadtextureimage(LoadPath, false, false, false);
            }

        if (tex != 0)
            crosshairtexture[num_crosshairs++] = tex; 
	}
    // Load caustic image files
    num_caustics = 0;
	for(i=0; i < MAX_CAUSTICS; i++)
	{
		sprintf(LoadPath, "TextEff/caust%i", i);
		tex = loadtextureimage(LoadPath, false, true, false);
        
        if (tex == 0)
            {
            sprintf(LoadPath, "TextEff/caust%02i", i);
		    tex = loadtextureimage(LoadPath, false, true, false);
            }

        if (tex != 0)
            caustictexture[num_caustics++] = tex; 
	}


}

int screenchoice;

void InitBloodscreen (void)
{
	if (num_bloodscreens == 0)
		return;
	bloodtimetodie = bloodscrtime.value;
	screenchoice = rand()%num_bloodscreens;
}

/*
Bloodscreen
*/
void DrawBloodscreen (void)
{
	float xsize,ysize;
	int cross_x, cross_y, sblines;

	if (bloodtimetodie < 0)
		bloodtimetodie = 0;

	if (bloodtimetodie == 0)
		return;

	if (!bloodscreen[screenchoice])
		return;	

	if(scr_viewsize.value >= 110) // No status bar
		sblines = 0;
	else
		sblines = 36;
	
	xsize = (vid.width - r_refdef.vrect.width)/2;
	ysize = (vid.height - r_refdef.vrect.height-sblines)/2;
	cross_x = (vid.width + r_refdef.vrect.width)/2;
	cross_y = (vid.height + r_refdef.vrect.height-sblines)/2;

					 //min(cl.cshifts[CSHIFT_DAMAGE].percent*0.01,0.5f)
	glColor4f(1, 1, 1, min(bloodtimetodie*0.25f,bloodscralpha.value));

	GL_Bind (bloodscreen[screenchoice]);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glEnable(GL_BLEND);
	glDisable(GL_ALPHA_TEST);
	glBegin (GL_QUADS);
	glTexCoord2f (0, 0);
	glVertex2f (xsize, ysize);
	glTexCoord2f (1, 0);
	glVertex2f (cross_x, ysize);
	glTexCoord2f (1, 1);
	glVertex2f (cross_x, cross_y);
	glTexCoord2f (0, 1);
	glVertex2f (xsize, cross_y);
	glEnd ();
	//restore standard values
	glColor4f(1, 1, 1, 1);
	glEnable (GL_ALPHA_TEST);
	glDisable( GL_BLEND );

	bloodtimetodie -= host_frametime;
}

void Draw_CrosshairImage (int x, int y)
{
	if (y <= -8)
		return; 		// totally off screen

	if (!crosshairtexture[(int)crosshairtype.value])
	{
		Draw_Character (x, y, '+');
		return;	
	}

	if (crosshairsize.value < 16)
		Cvar_SetValue ("crosshairsize", 16);
	else if (crosshairsize.value  > 64)
		Cvar_SetValue ("crosshairsize", 64);

	if (crosshairalpha.value < 0)
		Cvar_SetValue ("crosshairalpha", 0);
	else if (crosshairalpha.value  > 1)
		Cvar_SetValue ("crosshairalpha", 1);

	glColor4f(1, 1, 1, crosshairalpha.value);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	GL_Bind (crosshairtexture[(int)crosshairtype.value]);
	glEnable(GL_BLEND);
	glDisable(GL_ALPHA_TEST);
	glBegin (GL_QUADS);
	glTexCoord2f (0, 0);
	glVertex2f (x, y);
	glTexCoord2f (1, 0);
	glVertex2f (x+crosshairsize.value*sizeX, y);
	glTexCoord2f (1, 1);
	glVertex2f (x+crosshairsize.value*sizeX, y+crosshairsize.value*sizeX);
	glTexCoord2f (0, 1);
	glVertex2f (x, y+crosshairsize.value*sizeX);
	glEnd ();
	glEnable (GL_ALPHA_TEST);
	glDisable( GL_BLEND );

	glColor4f(1, 1, 1, 1);
}

void DrawCrosshair (void)
{
	int cross_x, cross_y;

	cross_x = r_refdef.vrect.x + (r_refdef.vrect.width-crosshairsize.value*sizeX)*0.5f;
	cross_y = r_refdef.vrect.y + (r_refdef.vrect.height-crosshairsize.value*sizeX)*0.5f;
	Draw_CrosshairImage(cross_x, cross_y);
}

void DrawScreenEffects (void)
{
	if (bloodscr.value)
		DrawBloodscreen();
	if (crosshair.value)
		DrawCrosshair();
}

void ResetScreenSettings (void)
{
	v_gamma.value = 1;
	Cvar_SetValue ("gamma", v_gamma.value);
	gl_contrast_level.value = 1;
	Cvar_SetValue ("gl_contrast_level", gl_contrast_level.value);
	gl_overbright_level.value = 10;
	Cvar_SetValue ("gl_overbright_level", gl_overbright_level.value);
	GL_SetGamma();
}

