// r_misc.c

#include "gl_local.h"

/*
==================
R_InitParticleTexture
==================
*/
byte	dottexture[8][8] =
{
	{0,0,0,0,0,0,0,0},
	{0,0,1,1,0,0,0,0},
	{0,1,1,1,1,0,0,0},
	{0,1,1,1,1,0,0,0},
	{0,0,1,1,0,0,0,0},
	{0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0},
};

void R_InitParticleTexture (void)
{
	int		x,y;
	byte	data[8][8][4];
//	int		result;

	//
	// particle texture
	//
	for (x=0 ; x<8 ; x++)
	{
		for (y=0 ; y<8 ; y++)
		{
			data[y][x][0] = 255;
			data[y][x][1] = 255;
			data[y][x][2] = 255;
			data[y][x][3] = dottexture[x][y]*255;
		}
	}
	r_particletexture [PARTICLE_SIMPLE] = GL_LoadPic ("***particle***", (byte *)data, 8, 8, it_sprite, 32, NULL);

	//	Nelno:	load complex particle textures
	r_particletexture [PARTICLE_SNOW] = GL_FindImage ("pics/particles/flake.pcx", it_sprite, RESOURCE_GLOBAL);
	r_particletexture [PARTICLE_RAIN] = GL_FindImage ("pics/particles/rain2.pcx", it_sprite, RESOURCE_GLOBAL);
	r_particletexture [PARTICLE_BLOOD] = GL_FindImage ("pics/particles/blud2.tga", it_sprite, RESOURCE_GLOBAL);
	r_particletexture [PARTICLE_BUBBLE] = GL_FindImage ("pics/particles/bubble.tga", it_sprite, RESOURCE_GLOBAL);
	r_particletexture [PARTICLE_SMOKE] = GL_FindImage ("pics/particles/bigsmoke.tga", it_sprite, RESOURCE_GLOBAL);
	r_particletexture [PARTICLE_BIG_SPARKS] = r_particletexture [PARTICLE_SPARKS] = GL_FindImage ("pics/particles/spark.tga", it_sprite, RESOURCE_GLOBAL);
	r_particletexture [PARTICLE_POISON] = GL_FindImage ("pics/particles/poison.tga", it_sprite, RESOURCE_GLOBAL);
	r_particletexture [PARTICLE_BLUE_SPARKS] = GL_FindImage ("pics/particles/bluespark.tga", it_sprite, RESOURCE_GLOBAL);

	//
	// also use this for bad textures, but without alpha
	//
	for (x=0 ; x<8 ; x++)
	{
		for (y=0 ; y<8 ; y++)
		{
			data[y][x][0] = dottexture[x&3][y&3]*255;
			data[y][x][1] = 0; // dottexture[x&3][y&3]*255;
			data[y][x][2] = 0; //dottexture[x&3][y&3]*255;
			data[y][x][3] = 255;
		}
	}
	r_notexture = GL_LoadPic ("***r_notexture***", (byte *)data, 8, 8, it_wall, 32, NULL);
}

void	R_InitMiscTextures (void)
{
	r_lasertexture = GL_FindImage ("pics/misc/laser.tga", it_sprite, RESOURCE_GLOBAL);
}

/* 
============================================================================== 
 
						SCREEN SHOTS 
 
============================================================================== 
*/ 

typedef struct _TargaHeader {
	unsigned char 	id_length, colormap_type, image_type;
	unsigned short	colormap_index, colormap_length;
	unsigned char	colormap_size;
	unsigned short	x_origin, y_origin, width, height;
	unsigned char	pixel_size, attributes;
} TargaHeader;


/* 
================== 
GL_ScreenShot_f
================== 
*/  
void GL_ScreenShot_f (void) 
{
	byte		*buffer;
	char		picname[80]; 
	char		checkname[MAX_OSPATH];
	int			i, c, temp;
	FILE		*f;

	// create the scrnshots directory if it doesn't exist
	Com_sprintf (checkname, sizeof(checkname), "%s/scrnshot", ri.FS_Gamedir());
	Sys_Mkdir (checkname);

// 
// find a file name to save it to 
// 
	strcpy(picname,"daikatana00.tga");

	for (i=0 ; i<=99 ; i++) 
	{ 
		picname[9] = i/10 + '0'; 
		picname[10] = i%10 + '0'; 
		Com_sprintf (checkname, sizeof(checkname), "%s/scrnshot/%s", ri.FS_Gamedir(), picname);
		f = fopen (checkname, "rb");
		if (!f)
			break;	// file doesn't exist
		fclose (f);
	} 
	if (i==100) 
	{
		ri.Con_Printf (PRINT_ALL, "SCR_ScreenShot_f: Couldn't create a file\n"); 
		return;
 	}


	buffer = malloc(vid.width*vid.height*3 + 18);
	memset (buffer, 0, 18);
	buffer[2] = 2;		// uncompressed type
	buffer[12] = vid.width&255;
	buffer[13] = vid.width>>8;
	buffer[14] = vid.height&255;
	buffer[15] = vid.height>>8;
	buffer[16] = 24;	// pixel size

#ifndef PROFILE_GL
	qglReadPixels (0, 0, vid.width, vid.height, GL_RGB, GL_UNSIGNED_BYTE, buffer+18 ); 
#else
	qglReadPixels (0, 0, vid.width, vid.height, GL_RGB, GL_UNSIGNED_BYTE, buffer+18, "GL_ScreenShot_f" ); 
#endif

	// swap rgb to bgr
	c = 18+vid.width*vid.height*3;
	for (i=18 ; i<c ; i+=3)
	{
		temp = buffer[i];
		buffer[i] = buffer[i+2];
		buffer[i+2] = temp;
	}

	f = fopen (checkname, "wb");
	fwrite (buffer, 1, c, f);
	fclose (f);

	free (buffer);
	ri.Con_Printf (PRINT_ALL, "Wrote %s\n", picname);
} 

/*
** GL_Strings_f
*/
void GL_Strings_f( void )
{
	ri.Con_Printf (PRINT_ALL, "GL_VENDOR: %s\n", gl_config.vendor_string );
	ri.Con_Printf (PRINT_ALL, "GL_RENDERER: %s\n", gl_config.renderer_string );
	ri.Con_Printf (PRINT_ALL, "GL_VERSION: %s\n", gl_config.version_string );
	ri.Con_Printf (PRINT_ALL, "GL_EXTENSIONS: %s\n", gl_config.extensions_string );
}

/*
** GL_SetDefaultState
*/
void GL_SetDefaultState( void )
{
#ifndef PROFILE_GL
	qglClearColor (1,0, 0.5 , 0.5);
	qglCullFace(GL_FRONT);
	qglEnable(GL_TEXTURE_2D);

	qglEnable(GL_ALPHA_TEST);
	qglAlphaFunc(GL_GREATER, 0.666);

	qglDisable (GL_DEPTH_TEST);
	qglDisable (GL_CULL_FACE);
	qglDisable (GL_BLEND);

	qglColor4f (1,1,1,1);

	qglPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
	qglShadeModel (GL_FLAT);
#else
	qglClearColor (1,0, 0.5 , 0.5, "GL_SetDefaultState" );
	qglCullFace(GL_FRONT, "GL_SetDefaultState" );
	qglEnable(GL_TEXTURE_2D, "GL_SetDefaultState" );

	qglEnable(GL_ALPHA_TEST, "GL_SetDefaultState" );
	qglAlphaFunc(GL_GREATER, 0.666, "GL_SetDefaultState" );

	qglDisable (GL_DEPTH_TEST, "GL_SetDefaultState" );
	qglDisable (GL_CULL_FACE, "GL_SetDefaultState" );
	qglDisable (GL_BLEND, "GL_SetDefaultState" );

	qglColor4f (1,1,1,1, "GL_SetDefaultState" );

	qglPolygonMode (GL_FRONT_AND_BACK, GL_FILL, "GL_SetDefaultState" );
	qglShadeModel (GL_FLAT, "GL_SetDefaultState" );

#endif
	GL_TextureMode( gl_texturemode->string );
#ifndef PROFILE_GL

	qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
	qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);

	qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

#else
	qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min, "GL_SetDefaultState" );
	qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max, "GL_SetDefaultState" );

	qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT, "GL_SetDefaultState" );
	qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT, "GL_SetDefaultState" );

	qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, "GL_SetDefaultState" );
#endif
	GL_TexEnv( GL_REPLACE );

	if ( qglPointParameterfEXT )
	{
		float attenuations[3] =
		{
			gl_particle_att_a->value,
			gl_particle_att_b->value,
			gl_particle_att_c->value
		};

		qglEnable( GL_POINT_SMOOTH );
		qglPointParameterfEXT( GL_POINT_SIZE_MIN_EXT, gl_particle_min_size->value );
		qglPointParameterfEXT( GL_POINT_SIZE_MAX_EXT, gl_particle_max_size->value );
		qglPointParameterfvEXT( GL_DISTANCE_ATTENUATION_EXT, attenuations );
	}
	
	if ( qglColorTableEXT && gl_ext_palettedtexture->value )
	{
#ifndef PROFILE_GL
		qglEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
#else
		qglEnable( GL_SHARED_TEXTURE_PALETTE_EXT, "GL_SetDefaultState" );
#endif
		GL_SetTexturePalette( d_8to24table );
	}

	GL_UpdateSwapInterval();
}

void GL_UpdateSwapInterval( void )
{
	if ( gl_swapinterval->modified )
	{
		gl_swapinterval->modified = false;

		if ( !gl_state.stereo_enabled ) 
		{
#ifdef _WIN32
			if ( qwglSwapIntervalEXT )
				qwglSwapIntervalEXT( gl_swapinterval->value );
#endif
		}
	}
}