//***********************************************************//
// Demo:    Lens Flare Editor
// Author:  terror
// Spec:    demo for gamedev.ru
//***********************************************************//
#define  WIN32_LEAN_AND_MEAN 
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <stdio.h>
#include "dlgref.h"
#include "render.h"
#include "texture.h"
#include "utils.h"
#include "Lens.h"


// current lens project
extern Lens_t proj;

extern window_t GLWindow;
extern Point_t  MousePos;
vector4 color;

extern int FoundTex;
uint texLens[999];
uint texBackground=0;
bool ShowLensTex = false;
bool ShowBackTex = false;
int  CurrentLensTex = 0;


void Begin2D ( void )
{
	glDisable ( GL_DEPTH_TEST );
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0, GLWindow.Width, 0, GLWindow.Height);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
}

void End2D ( void )
{
	glPopMatrix();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glEnable ( GL_DEPTH_TEST );
}

void DrawQuad ( float x, float y, float w, float h, unsigned int texture )
{
	glBindTexture ( GL_TEXTURE_2D, texture );
	y = (float)GLWindow.Height - y - h;
	glBegin ( GL_TRIANGLE_STRIP );
		glTexCoord2f ( 0.0f, 0.0f );		glVertex2f ( x,     y );
		glTexCoord2f ( 1.0f, 0.0f );		glVertex2f ( x + w, y );
		glTexCoord2f ( 0.0f, 1.0f );		glVertex2f ( x,     y + h );
		glTexCoord2f ( 1.0f, 1.0f );		glVertex2f ( x + w, y + h );
	glEnd();
}

void FindLensTextures ( void )
{
	char p[128] = "";
	for ( int i = 0; i < 999; i++ )
	{
		strcpy ( p, "" );
		sprintf ( p, "Data/lens%i.jpg", i+1 );
		if ( CheckFileWOMsg ( p ))
		{
			FoundTex++;
		}
	}
}


void LoadLensTextures ( void )
{
	int i = 0;
	char buf[256] = "";

	for ( i = 0; i < 999; i++ )
		DeleteTexture ( texLens[i] );

	for ( i = 0; i < FoundTex; i++ )
	{
		strcpy ( buf, "" );
		sprintf ( buf, "Data/lens%i.jpg", i+1 );
		UploadImage ( buf, texLens[i] );
	}

}


void InitRender ( void )
{
	glBlendFunc ( GL_SRC_ALPHA, GL_ONE );
	UploadImage ( "Data/back.jpg", texBackground );

	for ( int i = 0; i < 999; i++ )
		texLens[i] = 0;

	VECTOR4_SET ( color, 1 );
}


void RenderLens ( float lx, float ly, Lens_t &l )
{
	int i = 0;
	float cx = (float)(GLWindow.Width  / 2);
	float cy = (float)(GLWindow.Height / 2);
	float vx = cx - lx;
	float vy = cy - ly;

	l.Lens[0].Pos.x = lx;
	l.Lens[0].Pos.y = ly;
	l.Lens[1].Pos.x = lx + vx / 4.0f;
	l.Lens[1].Pos.y = ly + vy / 4.0f;
	l.Lens[2].Pos.x = lx + vx / 2.0f;
	l.Lens[2].Pos.y = ly + vy / 2.0f;
	l.Lens[3].Pos.x = lx + vx / 1.5f;
	l.Lens[3].Pos.y = ly + vy / 1.5f;
	l.Lens[4].Pos.x = lx + vx / 1.1f;
	l.Lens[4].Pos.y = ly + vy / 1.1f;

	l.Lens[5].Pos.x = lx + vx * 1.1f;
	l.Lens[5].Pos.y = ly + vy * 1.1f;
	l.Lens[6].Pos.x = lx + vx * 1.5f;
	l.Lens[6].Pos.y = ly + vy * 1.5f;
	l.Lens[7].Pos.x = lx + vx * 2.5f;
	l.Lens[7].Pos.y = ly + vy * 2.5f;
	l.Lens[8].Pos.x = lx + vx * 3.0f;
	l.Lens[8].Pos.y = ly + vy * 3.0f;


	for ( i = 0; i < l.Count; i++ )
	{
		l.Lens[i].Pos.x -= l.Lens[i].Size / 2;
		l.Lens[i].Pos.y -= l.Lens[i].Size / 2;
	}

	glColor4fv ( color );
	glEnable ( GL_TEXTURE_2D );
	glEnable ( GL_BLEND );
	glBlendFunc ( GL_SRC_ALPHA, GL_ONE );
	Begin2D();
		for ( i = 0; i < l.Count; i++ )
		{
			glColor4fv ( l.Lens[i].Color );
			DrawQuad   ( (float)l.Lens[i].Pos.x, (float)l.Lens[i].Pos.y, (float)l.Lens[i].Size, (float)l.Lens[i].Size, l.Lens[i].TexID  );
		}
	End2D();
	glDisable ( GL_BLEND );
	glDisable ( GL_TEXTURE_2D );
}


void Render ( void )
{
	glClearColor ( 0, 0, 0, 0 );
	glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	if ( ShowBackTex )
	{
		glColor4f ( 1.0f, 1.0f, 1.0f, 1.0f );
		glEnable ( GL_TEXTURE_2D );
		glDisable ( GL_BLEND );
		Begin2D();
			DrawQuad ( 0.0f, 0.0f, (float)(GLWindow.Width), (float)(GLWindow.Height), texBackground );
		End2D();
	}

	RenderLens ( (float)MousePos.x, (float)MousePos.y, proj );
	glEnable ( GL_TEXTURE_2D );
	glEnable ( GL_BLEND );
	glColor4f ( 1.0f, 1.0f, 1.0f, 1.0f );

	if ( ShowLensTex )
	{
		Begin2D();
		DrawQuad ( (float)(GLWindow.Width - 128), 0.0f, 128.0f, 128.0f, texLens[CurrentLensTex] );
		End2D();
	}

	glDisable ( GL_TEXTURE_2D );
	glDisable ( GL_BLEND );
	SwapBuffers ( GLWindow.hDC );
}


void ShutDownGL ( void )
{
	DeleteTexture  ( texBackground );
	DeleteTextures ( FoundTex, texLens );
}

