//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------

#include "giofx.h"

//#define LORES_MODE

static float TRANSITION_SPEED       =	0.20;
static float TRANSITION_TEXSIZE     =	512;

//--------------------------------------------------------------------
//
//--------------------------------------------------------------------
cTransition::cTransition(cScene* tParent,TRANSITION_TYPE kindOfTransition)
{

	parent				= tParent;
	transition_kind		= kindOfTransition;
	tex_transition[0]	= 0;
	tex_transition[1]	= 0;
	tex_transition1		= NULL;
	tex_transition2		= NULL;
	transition_step		= 0;
	transition_active	= false;

#ifndef LORES_MODE
    TRANSITION_TEXSIZE = 2048;
    
    if (parent->resolutionX <= 1024 && parent->resolutionY <= 1024)
        TRANSITION_TEXSIZE =  1024;
    
    if (parent->resolutionX <= 512 && parent->resolutionY <= 512)
        TRANSITION_TEXSIZE =  512;
#endif

    
	//crea le textures vuote
	unsigned char* txdata = (unsigned char*)malloc((int)(TRANSITION_TEXSIZE * TRANSITION_TEXSIZE * 3));
    memset(txdata,0,TRANSITION_TEXSIZE * TRANSITION_TEXSIZE * 3);
	for (int i=0;i<2;i++)
	{
		glGenTextures(1,&tex_transition[i]);
		glBindTexture(GL_TEXTURE_2D, tex_transition[i]);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TRANSITION_TEXSIZE, TRANSITION_TEXSIZE, 0, GL_RGB, GL_UNSIGNED_BYTE, txdata);	
	}
	free(txdata);
}

//--------------------------------------------------------------------
//
//--------------------------------------------------------------------
void cTransition::Copy(int whatTex)
{
#ifdef LORES_MODE
    int oResX = parent->resolutionX;
	int oResY = parent->resolutionY;
	parent->Render(TRANSITION_TEXSIZE,TRANSITION_TEXSIZE);
#else
    parent->Render();
#endif

	parent->renderActive = false;
    glDisable(GL_BLEND);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, tex_transition[whatTex]);
	glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,parent->resolutionX,parent->resolutionY);
    glEnable(GL_BLEND);
    
#ifdef LORES_MODE
    parent->resolutionX = oResX;
	parent->resolutionY = oResY;
	parent->SetFrustumFromFOV();
#endif

    parent->renderActive = true;
}
//--------------------------------------------------------------------
//
//--------------------------------------------------------------------
void cTransition::Start()
{
    transition_step   = 0;
    transition_active = true;
}
//--------------------------------------------------------------------
//
//--------------------------------------------------------------------
void cTransition::Stop()
{
    transition_step   = 0;
	transition_active = false;
}
//--------------------------------------------------------------------
//
//--------------------------------------------------------------------
void cTransition::SetTransition(TRANSITION_TYPE kindOfTransition)
{
	transition_kind		= kindOfTransition;
}
//--------------------------------------------------------------------
//
//--------------------------------------------------------------------
void cTransition::Render()
{
	if (!transition_active)
		return;

    glLoadIdentity();

	glDisable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
	glColor4f(1,1,1,1);
    cVertex dim = parent->GetExtents();
    float _vertCoord[]={-dim.x,+dim.y,0, +dim.x,+dim.y,0, +dim.x,-dim.y,0,  -dim.x,+dim.y,0,  +dim.x,-dim.y,0,  -dim.x,-dim.y,0};
    
#ifdef LORES_MODE
    float texSize = 0.874;
    float tBottom = 1.0 - texSize;
    float tTop    = yBottom + (texSize - tBottom);
    float _texCoord[]={0,tTop,  1,tTop,  1,tBottom,  0,tTop,  1,tBottom,  0,tBottom};
#else
    float tx = (float)parent->resolutionX / (float)TRANSITION_TEXSIZE; //normalizza le coordinate in 0..1;
    float ty = (float)parent->resolutionY / (float)TRANSITION_TEXSIZE;
    float _texCoord[]={0,ty,  tx,ty,  tx,0,  0,ty,  tx,0,  0,0};
#endif

    //disegna la texture ferma
	glBindTexture(GL_TEXTURE_2D, tex_transition[0]);
    glTexCoordPointer(2, GL_FLOAT, 0, _texCoord);
    glVertexPointer(3, GL_FLOAT, 0, _vertCoord);
    glDrawArrays(GL_TRIANGLES,0,6);
	
	//disegna la texture in movimento
    glBindTexture(GL_TEXTURE_2D, tex_transition[1]);
    
	//----------------------------------------------------------
	if (transition_kind == TRANSITION_SLIDE_DOWN)
	{
        glTranslatef(0,(dim.y*2)-transition_step,0);
	}
    else if (transition_kind == TRANSITION_SLIDE_UP) 
	{
        glTranslatef(0,+transition_step,0);
	}
    else if (transition_kind == TRANSITION_SLIDE_RIGHT) 
	{
        glTranslatef(-transition_step,0,0);
	}
    else if (transition_kind == TRANSITION_SLIDE_LEFT)
	{
        glTranslatef(-(dim.x*2) + transition_step,0,0);
	}
    //----------------------------------------------------------
    else if (transition_kind == TRANSITION_CIACK_UP)
	{
        glTranslatef(0,+transition_step,0);
        glRotatef(+transition_step*25,0,0,1);
	}
    else if (transition_kind == TRANSITION_CIACK_DOWN) 
	{
        glTranslatef(0,+(dim.y*2)-transition_step,0);
        glRotatef(+90-transition_step*25,0,0,1);
	}
    else if (transition_kind == TRANSITION_CIACK_RIGHT) 
	{
        glTranslatef(-transition_step,0,0);
        glRotatef(+transition_step*25,0,0,1);
        
	}
    else if (transition_kind == TRANSITION_CIACK_LEFT)
	{
        glTranslatef(-(dim.x*2)+transition_step,0,0);
        glRotatef(+90-transition_step*25,0,0,1);
	}
    //----------------------------------------------------------
    else if (transition_kind == TRANSITION_COMPRESS_RIGHT)
	{
        _vertCoord[3]  = +dim.x - transition_step;
        _vertCoord[6]  = _vertCoord[3];
        _vertCoord[12] = _vertCoord[3];
        
	}
    else if (transition_kind == TRANSITION_COMPRESS_LEFT)
	{
        _vertCoord[3]  = +transition_step - dim.x;
        _vertCoord[6]  = _vertCoord[3];
        _vertCoord[12] = _vertCoord[3];
	}
    else if (transition_kind == TRANSITION_COMPRESS_UP) 
	{
        _vertCoord[7]  = transition_step - dim.y;
        _vertCoord[13] = _vertCoord[7];
        _vertCoord[16] = _vertCoord[7];
        
	}
    else if (transition_kind == TRANSITION_COMPRESS_DOWN)
	{
        _vertCoord[7]  = +dim.y - transition_step;
        _vertCoord[13] = _vertCoord[7];
        _vertCoord[16] = _vertCoord[7];
	}
    //----------------------------------------------------------
    
    glDrawArrays(GL_TRIANGLES,0,6);
    
    
    //avanzamento
    if (transition_kind == TRANSITION_SLIDE_UP      || transition_kind == TRANSITION_SLIDE_DOWN ||
        transition_kind == TRANSITION_CIACK_UP      || transition_kind == TRANSITION_CIACK_DOWN ||
        transition_kind == TRANSITION_COMPRESS_UP   || transition_kind == TRANSITION_COMPRESS_DOWN)
        {
            if ((transition_step+=TRANSITION_SPEED) >= dim.y * 2)
                Stop();
        }
    else if (transition_kind == TRANSITION_SLIDE_RIGHT      || transition_kind == TRANSITION_SLIDE_LEFT ||
             transition_kind == TRANSITION_CIACK_RIGHT      || transition_kind == TRANSITION_CIACK_LEFT ||
             transition_kind == TRANSITION_COMPRESS_RIGHT   || transition_kind == TRANSITION_COMPRESS_LEFT)
        {
            if ((transition_step+=TRANSITION_SPEED) >= dim.x * 2)
                Stop();
        }


    glEnable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);
	
}

//--------------------------------------------------------------------
//
//--------------------------------------------------------------------
bool cTransition::HasEnded()
{
	return (transition_active == false);
}
//--------------------------------------------------------------------
//
//--------------------------------------------------------------------
cTransition::~cTransition()
{
	if (tex_transition[0])
		glDeleteTextures(1,&tex_transition[0]);
	
	if (tex_transition[1])
		glDeleteTextures(1,&tex_transition[1]);
	
	tex_transition[0]	= 0;
	tex_transition[1]	= 0;
	tex_transition1		= NULL;
	tex_transition2		= NULL;
	transition_step		= 1;
	transition_active	= false;
}
//--------------------------------------------------------------------
//
//--------------------------------------------------------------------