#include "VTexture.h"
#include "corona.h"

#include <GL/glew.h>
#include <GL/glut.h>

#include <string>
#include <iostream>

using namespace corona;
using namespace std;

VTexture::VTexture(string _filename)
{
	this->filename = "data/textures/"+_filename;
	Image* image = OpenImage(this->filename.c_str(),PF_R8G8B8A8 );
	if (!image) {
		cout << "Couldn't open image " << this->filename << endl;
	} else {
		this->width = image->getWidth();
		this->height = image->getHeight();
		this->pixels = (GLbyte*)image->getPixels();
		glGenTextures(1,&(this->tex));
		glBindTexture(GL_TEXTURE_2D, this->tex);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, this->pixels);
	}
	delete image;
	image = 0;
}
void VTexture::bind() {
	glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glBindTexture(GL_TEXTURE_2D, this->tex);
}

VTexture::~VTexture(void){
	delete[] this->pixels;
}
