#include "stdafx.h"
#include "Texture.h"

#ifdef GUI_GTK

namespace gui {

	GLTexture::GLTexture(GLSurface &surface, Texture *texture) : surface(surface) {
		surface.makeCurrent();

		glGenTextures(1, &handle);

		// TODO: Can we configure the texture without binding it?
		glBindTexture(GL_TEXTURE_2D, handle);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

		update(texture);
	}

	void GLTexture::update(Texture *texture) {
		Image* image = texture->image();
		glBindTexture(GL_TEXTURE_2D, handle);
		glTexImage2D(GL_TEXTURE_2D,
					0,                /* level */
					GL_RGBA,          /* internalformat */
					image->width(),   /* width */
					image->height(),  /* height */
					0,                /* border */
					GL_RGBA,          /* format */
					GL_UNSIGNED_BYTE, /* type */
					image->buffer()   /* data */
			);
	}

	GLTexture::~GLTexture() {
		surface.makeCurrent();
		glDeleteTextures(1, &handle);
	}

}

#endif
