/*********************************************************************
*	Loading screen
*	SOURCE FILE
*	Autor:	Michal Jirouš
*	Datum: 31.7.2008
*	Soubor: image.cpp
*	Popis: Vykresluje jednoduchy obrazek..Lze nastavit vykreslovat 
*			v realne velikosti nebo podle velikosti konponenty
**********************************************************************/

#include "gfg.h"

Image::Image(  float w, float h )
{
	baseInit( w, h );
	m_Picture = 0;
	m_ColorBackGround = GFG_COLOR_IMAGE_COLOR_MODIFICATOR;
	m_iPictureWidth = 0;
	m_iPictureHeight = 0;
	m_iObjectType = GFG_IMAGE;
}


bool Image::loadImage( std::string filename )
{
	if( filename.empty() )
		return false;

	if( m_Picture != 0 )
	{
		textureLibrary.deleteTexture( m_Picture );
		m_Picture = 0;
	}

	TextureElement texture;
	texture.image.setFilename( filename );
	textureLibrary.applyTexture( &texture );

	
	m_Picture = texture.texture_id;
	m_iPictureWidth = texture.image.width;
	m_iPictureHeight = texture.image.height;

	return true;
}

Image::~Image()
{
	if( m_Picture != 0 )
	{
		glDeleteTextures( 1, &m_Picture );
		m_Picture = 0;
	}
}

void Image::getImageSize( int &x, int &y )
{
	if( m_Picture )
	{
		x = m_iPictureWidth;
		y = m_iPictureHeight;
	}
}


void Image::draw( ScissorBox &scissorBox )
{
	if( !m_bVisible )
		return;

	glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_SCISSOR_BIT );
	glEnable( GL_TEXTURE_2D );
	glEnable(GL_BLEND);
	glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
	

	glBindTexture( GL_TEXTURE_2D, m_Picture );
	setColor( m_ColorBackGround );

	if( m_bRealSize )
	{
		glBegin( GL_QUADS );
			glTexCoord2i( 0,0 ); glVertex2f( m_fGlobalXpos, m_fGlobalYpos );
			glTexCoord2i( 1,0 ); glVertex2f( m_fGlobalXpos + m_iPictureWidth, m_fGlobalYpos );
			glTexCoord2i( 1,1 ); glVertex2f( m_fGlobalXpos + m_iPictureWidth, m_fGlobalYpos + m_iPictureHeight );
			glTexCoord2i( 0,1 ); glVertex2f( m_fGlobalXpos, m_fGlobalYpos + m_iPictureHeight );
		glEnd();
	}
	else
	{
		glBegin( GL_QUADS );
			glTexCoord2i( 0,0 ); glVertex2f( m_fGlobalXpos, m_fGlobalYpos );
			glTexCoord2i( 1,0 ); glVertex2f( m_fGlobalXpos + m_fWidth, m_fGlobalYpos );
			glTexCoord2i( 1,1 ); glVertex2f( m_fGlobalXpos + m_fWidth, m_fGlobalYpos + m_fHeight );
			glTexCoord2i( 0,1 ); glVertex2f( m_fGlobalXpos, m_fGlobalYpos + m_fHeight );
		glEnd();
	}

	glPopAttrib();

}
