#include "include.h"

void dibujarTexto(char * texto, double x, double y, double size, bool derecha) {
	glDisable(GL_DEPTH_TEST);
	glPushMatrix();
	glTranslated(x, y, 0);
	glScaled(size, size, size);
	if (derecha) {
		Text::getInstance()->drawRight(texto);
	} else {
		Text::getInstance()->draw(texto);
	}
	glPopMatrix();
	glEnable(GL_DEPTH_TEST);
}

GLuint loadTexture(const char * filename) {
	static GLuint idTextura = 0;
	idTextura++;

	glGenTextures(1, &idTextura);

	FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(filename);
	FIBITMAP * bitmap = FreeImage_Load(fif, filename);
	bitmap = FreeImage_ConvertTo32Bits(bitmap);

	int w = FreeImage_GetWidth(bitmap);
	int h = FreeImage_GetHeight(bitmap);
      
	void * data = FreeImage_GetBits(bitmap);
	
	char * d = (char*)data;
	char aux;
	for (int i=0; i < w*h*4; i+=4) {
        aux = d[i];
        d[i] = d[i+2];
        d[i+2] = aux;
    }

	glBindTexture(GL_TEXTURE_2D, idTextura);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
    
	FreeImage_Unload(bitmap);
	return idTextura;
}

double distancia(double x1, double y1, double z1, double x2, double y2, double z2) {
	return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2));
}

double val(double v) {
	return v * Config::msPorFrame * Config::velocidadJuego / 30;
}
