/***************************************************************************
 *   Copyright (C) 2004-2005 by                                            *
 *     Paolo Sacconier   <axa1981@tin.it>                                  *
 *     Francesco Tamagni <minchiahead@hacari.org>                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "img.h"

namespace gillo {

Img::Img(float x1, float y1, float x2, float y2, const char * texture, float alpha)
: texdata(NULL), externalTexture(false)
{
    setCoords(x1, y1, x2, y2);
	setTexture(texture);
    setAlpha(alpha);
}

Img::~Img() {
    if (this->texdata && !this->externalTexture)
        delete this->texdata;
}

void Img::setTexture(const char * texture) {
    if (texture != NULL) {
		if (this->texdata != NULL && !this->externalTexture)
			delete this->texdata;
		this->externalTexture = false;
		this->texdata = new ssgTexture(texture, 0, 0, 0);
		this->texhandle = this->texdata->getHandle();
	}
}

void Img::setExternalTexture(ssgTexture* texPtr) {
	if (!this->externalTexture)
		delete this->texdata;
	this->externalTexture = true;
	this->texdata = texPtr;
	this->texhandle = this->texdata->getHandle();
}

void Img::draw() {
    sgVec2 rectuv[4]={
            {0 ,0},
            {1 ,0},
            {1 ,1},
            {0 ,1}
    };

    glColor4f(1,1,1,this->alpha);
        
    GLint old_env_mode;
    
    if (this->texdata != NULL){
		glEnable(GL_TEXTURE_2D);
		glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &old_env_mode);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
		glBindTexture(GL_TEXTURE_2D, this->texhandle);
    }
    
    glBegin(GL_POLYGON);
    for(int i=0; i<4; i++){
        glTexCoord2fv(rectuv[i]);
        glVertex2fv(this->coords[i]);
    }
    glEnd();
    
    if (this->texdata != NULL)
    	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, old_env_mode);
}

}
