
#include "stdafx.h"
#include "2DObject.h"

#include <gl\gl.h>			
#include <gl\glu.h>	

C_2DOBJECT_0::C_2DOBJECT_0()
{
	_fColor = RGBf_WHITE;
	_texture_id = 0;
	_begin_tag = GL_QUADS;
	_blend = 0;
	_neAktBlend=0;
}

C_2DOBJECT_0::C_2DOBJECT_0(TRect rect)	
{
	_pos.x = rect.x;
	_pos.y = rect.y;
	_width = rect.width;
	_height = rect.height;

	_fColor = RGBf_WHITE;
	_texture_id = 0;
	_begin_tag = GL_QUADS;
	_alpha = 1;
	_blend = 0;
	_neAktBlend=0;
}

C_2DOBJECT_0::C_2DOBJECT_0(uint texture_id, TRect rect)
: _pos(rect.x, rect.y), _width(rect.width), _height(rect.height)
{
	_fColor = RGBf_WHITE;
	_texture_id = texture_id;
	_begin_tag = GL_QUADS;
	_alpha = 1;
	_blend = 0;
	_neAktBlend=0;
}

C_2DOBJECT_0::C_2DOBJECT_0(const float x,const float y,const float width,const float height)
: _pos(x,y), _width(width), _height(height)
{
	_fColor = RGBf_WHITE;
	_texture_id = 0;
	_begin_tag = GL_QUADS;
	_alpha = 1;
	_blend =0;
}

C_2DOBJECT_0::C_2DOBJECT_0(uint texture_id,const float x,const float y,const float width,const float height)
: _pos(x,y), _width(width), _height(height)
{
	_fColor = RGBf_WHITE;
	_texture_id = texture_id;
	_begin_tag = GL_QUADS;
	_alpha = 1;
	_blend = 0;
	_neAktBlend=0;
}

void C_2DOBJECT_0::_doOrtho()
{
	W_Ortho();
}

void C_2DOBJECT_0::_backOrtho()
{
	W_Proj();
}

void C_2DOBJECT_0::Render()
{
	glPushMatrix();
	this->_doOrtho();
	glLoadIdentity();
	//glColor3f(_fColor.r,_fColor.g,_fColor.b);
	(_texture_id == 0) ? glDisable(GL_TEXTURE_2D) :	glEnable(GL_TEXTURE_2D);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _texture_id);
	(_alpha == 0) ? glDisable(GL_ALPHA_TEST) :	glEnable(GL_ALPHA_TEST);
	bool bLight = (bool)glIsEnabled(GL_LIGHTING);
	if(bLight) glDisable(GL_LIGHTING);
	//glDisable(GL_BLEND);
	if( _blend )
	{
		glEnable(GL_BLEND);
		if(!_neAktBlend) glBlendFunc(GL_SRC_ALPHA, GL_ONE); 
		//glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE);
		//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		//glDisable(GL_ALPHA_TEST);
	}
	glBegin(_begin_tag);
		glTexCoord2i(0,0);		glVertex2f(_pos.x,			_pos.y+_height);
		glTexCoord2i(1,0);		glVertex2f(_pos.x+_width,	_pos.y+_height);
		glTexCoord2i(1,1);		glVertex2f(_pos.x+_width,	_pos.y);
		glTexCoord2i(0,1);		glVertex2f(_pos.x,			_pos.y);
	glEnd();
	if( _blend ) glDisable(GL_BLEND);
	glDisable(GL_ALPHA_TEST);
	glDisable(GL_TEXTURE_2D);
	if(bLight) glEnable(GL_LIGHTING);
	this->_backOrtho();
	glPopMatrix();
}


