#include "MiniMap.h"

#include "GroundMap.h"

#ifdef WIN32
#include <windows.h>
#endif
#include <GL/GL.h>

using namespace RenderSystem;
using namespace ground;

void beginOrtho()
{
	glDisable(GL_DEPTH_TEST);
	// on passe en mode projection
	glMatrixMode(GL_PROJECTION);
	// on sauvegarde la matrice de projection
	glPushMatrix();
	// on nettoie la matrice de projection
	glLoadIdentity();
	// on passe en mode ortho de la taille de l'ecran
	glOrtho(0,640,0,480,-1,1);	
	// on passe en mode modelview
	glMatrixMode(GL_MODELVIEW);
	// on sauvegarde la matrice de modelview
	glPushMatrix();
	// on nettoie la matrice de projection
	glLoadIdentity();

}
void endOrtho()
{
	// on selectionne la matrice de projection
	glMatrixMode(GL_PROJECTION);
	// et on recupere la matrice de projection sauvegardé precedement
	glPopMatrix();	
	// on selectionne la matrice de projection modelview
	glMatrixMode(GL_MODELVIEW);
	// et on recupere la matrice de modelview sauvegardé precedement
	glPopMatrix();
	// puis on reactive le depth test
	glEnable(GL_DEPTH_TEST);


}

MiniMap::MiniMap(void)
{
	mapTexId=0;
}

MiniMap::~MiniMap(void)
{
	glDeleteTextures(1,&mapTexId);
	mapTexId=0;
	
}

void MiniMap::mapChanged(){

	unsigned char* temp;
	ground::Case *** map = ground::GroundMap::getInstance().getCases();
	int width = ground::GroundMap::getInstance().getWidth();
	int height = ground::GroundMap::getInstance().getHeight();

	if(mapTexId!=0) {
		glDeleteTextures(1,&mapTexId);
		mapTexId=0;
	}
	glGenTextures(1,&mapTexId);

	temp = new unsigned char[width*height*3];

	printf("bonjour0");
	//on rempli la texture
		for (int j = 0; j < height; j++){
	for(int i = 0; i < width; i++){
			switch(map[i][j]->getGroundType()){
				case ground::MEADOW:
					temp[i*3+j*width*3 +0] = 0;
					temp[i*3+j*width*3 +1] = 255;
					temp[i*3+j*width*3 +2] = 0;
					break;
				case ground::OREE:
				case ground::FOREST:
					temp[i*3+j*width*3 +0] = 0;
					temp[i*3+j*width*3 +1] = 128;
					temp[i*3+j*width*3 +2] = 0;
					break;
				case ground::ROAD:
					temp[i*3+j*width*3 +0] = 0;
					temp[i*3+j*width*3 +1] = 128;
					temp[i*3+j*width*3 +2] = 128;
					break;
				case ground::WATER:
					temp[i*3+j*width*3 +0] = 0;
					temp[i*3+j*width*3 +1] = 0;
					temp[i*3+j*width*3 +2] = 255;
					break;
			}
		}
	}
	
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D,mapTexId);
	//option de répetition
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
	//filtrage a appliquer en cas d'étirement et de ressérage de la texture
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	//on charge la texture en mémoire vidéo
	printf("bonjour1");
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,temp);
	printf("bonjour2");
	glDisable(GL_TEXTURE_2D);

	delete [] temp;
}

void MiniMap::draw(){
	const float offset = 10.0f;
	const float size = 64.0f;
	float rapport = (float)GroundMap::getInstance().getHeight()/(float)GroundMap::getInstance().getWidth();

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D,this->mapTexId);
	glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
	glColor4f(1.0f,1.0f,1.0f,0.4f);
	beginOrtho();
	glBegin(GL_QUADS);
		glTexCoord2f(1.0f,0.0f);
		glVertex2f(offset,offset);
		glTexCoord2f(0.0f,0.0f);
		glVertex2f(offset+size,offset);
		glTexCoord2f(0.0f,1.0f);
		glVertex2f(offset+size,offset+rapport*size);
		glTexCoord2f(1.0f,1.0f);
		glVertex2f(offset,offset+rapport*size);
	glEnd();
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	endOrtho();
}
