#include "constant.h"

my_boolean sound=MY_TRUE;
my_boolean time_race=MY_FALSE;
my_boolean two_players=MY_FALSE;
my_boolean textures=MY_TRUE;
my_boolean first_display=MY_TRUE;
char alert_message[100];
char score_string[100];
char score[100];
snake a,b;
int total_matches=0;
int size_max_var=SIZE_MAX_2;
int maze=0;
int x_max=400;
int y_max=400;
int fps=FPS_3;
int size=2;
int score_achieved_a=0;
int winner_id;
status game_status;
my_block mat[1000][1000];
int n_my_blocks_x=SIZE_MAX_2;
int n_my_blocks_y=SIZE_MAX_2;
my_block *p_MY_OUT;
float dim_my_block_x=x_max/n_my_blocks_x;
float dim_my_block_y=y_max/n_my_blocks_y;
int token_x, token_y;
// main()
int main(int argc, char** argv)
{
	if (sound==MY_TRUE) PlaySound(START_SOUND,NULL,SND_ASYNC | SND_NODEFAULT);
	initialization();
// Maze Off
	for (int i=0;i<n_my_blocks_y;i++)
		for (int j=0;j<n_my_blocks_x;j++)
		mat[j][i]=MY_FREE;
//Glut inizialization
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize ((x_max/y_max)*RESHAPE_2*x_max, x_max*(x_max/y_max)*RESHAPE_2*(1+Y_HEADER/y_max));
	glutCreateWindow("Black Mamba");
	glutPositionWindow(2,30); 
	glutReshapeFunc(myReshape);
	glutKeyboardFunc(keyboard);
	glutSpecialFunc(special_keyboard);
	glutDisplayFunc(display);
    glutTimerFunc(1000/fps,myTimer,1);
	glClearColor(0,0,0,0);
    createGLUTMenus();
// Texture Initialization
	glEnable (GL_TEXTURE_2D);
	loadTGA (GRASS_TEXTURE, 1);
	loadTGA (STONE_TEXTURE, 2);
	loadTGA (APPLE_TEXTURE, 3);
	loadTGA (GREEN_SNAKE_TEXTURE, 4);
	loadTGA (RED_SNAKE_TEXTURE, 5);
	glutMainLoop();
	return 0;
}

// Other functions
void put_my_block(int x, int y, colors color)
{
	rgb_color chosen;
	find_color(color, &chosen);
    glColor3f(chosen.r,chosen.g,chosen.b);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
    glBegin(GL_POLYGON);
		glVertex2f(x*dim_my_block_x, y*dim_my_block_y);
		glVertex2f((x+1)*dim_my_block_x, y*dim_my_block_y);
		glVertex2f((x+1)*dim_my_block_x, (y+1)*dim_my_block_y);
		glVertex2f(x*dim_my_block_x, (y+1)*dim_my_block_y);
	glEnd();
}

void find_color(colors color, rgb_color *rgb_value)
{
	switch (color) 
	{
		case BLACK:
			(*rgb_value).r=0;
			(*rgb_value).g=0;
			(*rgb_value).b=0;
			break;
		case RED:
			(*rgb_value).r=1;
			(*rgb_value).g=0;
			(*rgb_value).b=0;
			break;
		case GREEN:
			(*rgb_value).r=0;
			(*rgb_value).g=1;
			(*rgb_value).b=0;
			break;
		case BLUE:
			(*rgb_value).r=0;
			(*rgb_value).g=0;
			(*rgb_value).b=1;
			break;
		case WHITE:
			(*rgb_value).r=1;
			(*rgb_value).g=1;
			(*rgb_value).b=1;
			break;
		case GRAY:
			(*rgb_value).r=0.9;
			(*rgb_value).g=0.9;
			(*rgb_value).b=0.9;
			break;
		case YELLOW:
			(*rgb_value).r=0.7;
			(*rgb_value).g=0.7;
			(*rgb_value).b=0;
			break;

	}

}

void touch(int x, int y, int snake_id)
{
switch (mat[x][y]) {
	case TOKEN:
		if (sound==MY_TRUE) PlaySound(APPLE_SOUND,NULL,SND_ASYNC | SND_NODEFAULT);
		if (time_race==MY_TRUE) fps++;
		if (snake_id==0) {
			a.wanted_length+=size;
			a.score=a.score+(int)(fps*(((float)maze/3)+1)*(1+((float)size_max_var/n_my_blocks_x)/2.5));
		}	else 
		{
			b.wanted_length+=size;
			b.score=b.score+(int)(fps*(((float)maze/3)+1)*(1+((float)size_max_var/n_my_blocks_x)/2.5));
		}
		free_casual_block(&token_x,&token_y);
		mat[token_x][token_y]=TOKEN;
		break;
	case SNAKE0:
		if (sound==MY_TRUE) PlaySound(LOSE_SOUND,NULL,SND_ASYNC | SND_NODEFAULT); 
		game_status=MESSAGE;
		if (snake_id==0) {// A touch A
			strcpy(alert_message,RED_WIN_TEXT);
		    winner_id=1;
		}
		else // B touch A
		{
			if (((*(a.p_head)).x==x)&&((*(a.p_head)).y==y)) // B touch A's head
			{
				winner_id=2;
				strcpy(alert_message,DRAW_TEXT);
			}
			else // B touch A's body
			{
				strcpy(alert_message,GREEN_WIN_TEXT);
				winner_id=0;
			}
		}
		glutAttachMenu(GLUT_RIGHT_BUTTON);
  		break;
	case SNAKE1:
		if (sound==MY_TRUE) PlaySound(LOSE_SOUND,NULL,SND_ASYNC | SND_NODEFAULT); 
		game_status=MESSAGE;
		if (snake_id==1) {// B touch B
			winner_id=0;
			strcpy(alert_message,GREEN_WIN_TEXT);
		}
		else // A touch B
		{
			if (((*(b.p_head)).x==x)&&((*(b.p_head)).y==y)) {// A touch B's head
				strcpy(alert_message,DRAW_TEXT);
				winner_id=2;
			}
			else {// A touch B's body
				strcpy(alert_message,RED_WIN_TEXT);
				winner_id=1;	
			}
		}
		glutAttachMenu(GLUT_RIGHT_BUTTON);
  		break;
	case MAZE:
		total_matches++;
		if (sound==MY_TRUE) PlaySound(LOSE_SOUND,NULL,SND_ASYNC | SND_NODEFAULT); 
		game_status=MESSAGE;
		if (snake_id==0)
		{
			winner_id=1;
		}
		else
		{
			winner_id=0;
		}
		glutAttachMenu(GLUT_RIGHT_BUTTON);
  		break;
}
}

void add_fifo(elem **p_p_head, elem **p_p_tail, int x, int y)
{
	elem *p_new_elem = (elem *)malloc(sizeof(elem));
	(*p_new_elem).x = x;
	(*p_new_elem).y = y;
	(*p_new_elem).p_next = (elem *) NULL;
	if (*(p_p_head)==NULL) {
		*(p_p_tail)=p_new_elem;
		*(p_p_head)=p_new_elem;
	} else
	{
	(*(*(p_p_head))).p_next = p_new_elem;
	*(p_p_head)=p_new_elem;
	}
}
void remove_fifo(elem **p_p_tail)
{
	elem *p_to_be_removed=(*(p_p_tail));
	(*(p_p_tail))=(*(*(p_p_tail))).p_next;
	free(p_to_be_removed);
}

void myTimer(int value){
if (game_status==PLAY)
	{
	switch (a.prev_direction) { //Update the matrix managing user's input
		case LEFT:
			switch (a.direction) {
			case LEFT:
				if (mat[(a.x-1+n_my_blocks_x)%n_my_blocks_x][a.y]!=MY_FREE) touch((a.x-1+n_my_blocks_x)%n_my_blocks_x,a.y,0);
				if (game_status==PLAY) a.x=(a.x-1+n_my_blocks_x)%n_my_blocks_x;
					  break;
			case RIGHT: //impossible: go to left
				if (mat[(a.x-1+n_my_blocks_x)%n_my_blocks_x][a.y]!=MY_FREE) touch((a.x-1+n_my_blocks_x)%n_my_blocks_x,a.y,0);
				if (game_status==PLAY) a.x=(a.x-1+n_my_blocks_x)%n_my_blocks_x;
					  break;
			case UP:
                if (mat[a.x][(a.y+1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(a.x,(a.y+1+n_my_blocks_y)%n_my_blocks_y,0);
				if (game_status==PLAY) {
					a.y=(a.y+1+n_my_blocks_y)%n_my_blocks_y;
					a.prev_direction=UP;
				}
					  break;
			case DOWN:
				if (mat[a.x][(a.y-1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(a.x,(a.y-1+n_my_blocks_y)%n_my_blocks_y,0);
				if (game_status==PLAY) {
					a.y=(a.y-1+n_my_blocks_y)%n_my_blocks_y;
					a.prev_direction=DOWN;
				}
					  break;
			}
			break;
		case RIGHT:
			switch (a.direction) {
			case LEFT: // impossible: go to right;
				if (mat[(a.x+1+n_my_blocks_x)%n_my_blocks_x][a.y]!=MY_FREE) touch((a.x+1+n_my_blocks_x)%n_my_blocks_x,a.y,0);
				if (game_status==PLAY) a.x=(a.x+1+n_my_blocks_x)%n_my_blocks_x; 
					 break;
			case RIGHT:
				if (mat[(a.x+1+n_my_blocks_x)%n_my_blocks_x][a.y]!=MY_FREE) touch((a.x+1+n_my_blocks_x)%n_my_blocks_x,a.y,0);
				if (game_status==PLAY) a.x=(a.x+1+n_my_blocks_x)%n_my_blocks_x;
					break;
			case UP:
                if (mat[a.x][(a.y+1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(a.x,(a.y+1+n_my_blocks_y)%n_my_blocks_y,0);
				if (game_status==PLAY) {
					a.y=(a.y+1+n_my_blocks_y)%n_my_blocks_y;
					a.prev_direction=UP;
				}
					  break;
			case DOWN:
				if (mat[a.x][(a.y-1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(a.x,(a.y-1+n_my_blocks_y)%n_my_blocks_y,0);
				if (game_status==PLAY) {
					a.y=(a.y-1+n_my_blocks_y)%n_my_blocks_y; 
					a.prev_direction=DOWN;
				}
					  break;
			}
			break;
		case UP:
			switch (a.direction) {
			case LEFT:
				if (mat[(a.x-1+n_my_blocks_x)%n_my_blocks_x][a.y]!=MY_FREE) touch((a.x-1+n_my_blocks_x)%n_my_blocks_x,a.y,0);
				if (game_status==PLAY) {
					a.x=(a.x-1+n_my_blocks_x)%n_my_blocks_x; 
					a.prev_direction=LEFT;
				}
					  break;
			case RIGHT: 
				if (mat[(a.x+1+n_my_blocks_x)%n_my_blocks_x][a.y]!=MY_FREE) touch((a.x+1+n_my_blocks_x)%n_my_blocks_x,a.y,0);
				if (game_status==PLAY) {
					a.x=(a.x+1+n_my_blocks_x)%n_my_blocks_x;
					a.prev_direction=RIGHT;
				}
					  break;
			case UP:
				if (mat[a.x][(a.y+1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(a.x,(a.y+1+n_my_blocks_y)%n_my_blocks_y,0);
				if (game_status==PLAY) a.y=(a.y+1+n_my_blocks_y)%n_my_blocks_y;
					  break;
			case DOWN: //impossible: go up;
				if (mat[a.x][(a.y+1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(a.x,(a.y+1+n_my_blocks_y)%n_my_blocks_y,0);
				if (game_status==PLAY) a.y=(a.y+1+n_my_blocks_y)%n_my_blocks_y;
                      break;
			}
			break;
		case DOWN:
			switch (a.direction) {
			case LEFT:
				if (mat[(a.x-1+n_my_blocks_x)%n_my_blocks_x][a.y]!=MY_FREE) touch((a.x-1+n_my_blocks_x)%n_my_blocks_x,a.y,0);
				if (game_status==PLAY) {
					a.x=(a.x-1+n_my_blocks_x)%n_my_blocks_x;
					a.prev_direction=LEFT;
				}
					  break;
			case RIGHT: 
				if (mat[(a.x+1+n_my_blocks_x)%n_my_blocks_x][a.y]!=MY_FREE) touch((a.x+1+n_my_blocks_x)%n_my_blocks_x,a.y,0);
				if (game_status==PLAY) {
					a.x=(a.x+1+n_my_blocks_x)%n_my_blocks_x;
					a.prev_direction=RIGHT;
				}
					  break;
			case UP: //impossible: go down;
				if (mat[a.x][(a.y-1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(a.x,(a.y-1+n_my_blocks_y)%n_my_blocks_y,0);
				if (game_status==PLAY) a.y=(a.y-1+n_my_blocks_y)%n_my_blocks_y;
				      break;
			case DOWN: 
				if (mat[a.x][(a.y-1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(a.x,(a.y-1+n_my_blocks_y)%n_my_blocks_y,0);
				if (game_status==PLAY) a.y=(a.y-1+n_my_blocks_y)%n_my_blocks_y;
                      break;
			}
			break;
		} // End select;
	if (game_status==PLAY)
	{
		add_fifo(&(a.p_head),&(a.p_tail), a.x, a.y);
		mat[a.x][a.y]=SNAKE0;
	}
	a.length++;
	if (a.length==a.wanted_length) // Tail needs to be removed
	{
		mat[(*(a.p_tail)).x][(*(a.p_tail)).y]=MY_FREE;
		remove_fifo(&(a.p_tail));
		a.length--;
	}
//Snake 2
	if ((two_players==TRUE)&&(game_status==PLAY))	{

	switch (b.prev_direction) { //Update the matrix managing user's input
		case LEFT:
			switch (b.direction) {
			case LEFT:
				if (mat[(b.x-1+n_my_blocks_x)%n_my_blocks_x][b.y]!=MY_FREE) touch((b.x-1+n_my_blocks_x)%n_my_blocks_x,b.y,1);
				b.x=(b.x-1+n_my_blocks_x)%n_my_blocks_x;
					  break;
			case RIGHT: //impossible: go to left
				if (mat[(b.x-1+n_my_blocks_x)%n_my_blocks_x][b.y]!=MY_FREE) touch((b.x-1+n_my_blocks_x)%n_my_blocks_x,b.y,1);
				b.x=(b.x-1+n_my_blocks_x)%n_my_blocks_x;
					  break;
			case UP:
                if (mat[b.x][(b.y+1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(b.x,(b.y+1+n_my_blocks_y)%n_my_blocks_y,1);
				b.y=(b.y+1+n_my_blocks_y)%n_my_blocks_y;
				b.prev_direction=UP;
					  break;
			case DOWN:
				if (mat[b.x][(b.y-1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(b.x,(b.y-1+n_my_blocks_y)%n_my_blocks_y,1);
				b.y=(b.y-1+n_my_blocks_y)%n_my_blocks_y;
				b.prev_direction=DOWN;
					  break;
			}
			break;
		case RIGHT:
			switch (b.direction) {
			case LEFT: // impossible: go to right;
				if (mat[(b.x+1+n_my_blocks_x)%n_my_blocks_x][b.y]!=MY_FREE) touch((b.x+1+n_my_blocks_x)%n_my_blocks_x,b.y,1);
				b.x=(b.x+1+n_my_blocks_x)%n_my_blocks_x; 
					 break;
			case RIGHT:
				if (mat[(b.x+1+n_my_blocks_x)%n_my_blocks_x][b.y]!=MY_FREE) touch((b.x+1+n_my_blocks_x)%n_my_blocks_x,b.y,1);
				b.x=(b.x+1+n_my_blocks_x)%n_my_blocks_x;
					break;
			case UP:
                if (mat[b.x][(b.y+1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(b.x,(b.y+1+n_my_blocks_y)%n_my_blocks_y,1);
				b.y=(b.y+1+n_my_blocks_y)%n_my_blocks_y;
				b.prev_direction=UP;
					  break;
			case DOWN:
				if (mat[b.x][(b.y-1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(b.x,(b.y-1+n_my_blocks_y)%n_my_blocks_y,1);
				b.y=(b.y-1+n_my_blocks_y)%n_my_blocks_y; 
				b.prev_direction=DOWN;
					  break;
			}
			break;
		case UP:
			switch (b.direction) {
			case LEFT:
				if (mat[(b.x-1+n_my_blocks_x)%n_my_blocks_x][b.y]!=MY_FREE) touch((b.x-1+n_my_blocks_x)%n_my_blocks_x,b.y,1);
				b.x=(b.x-1+n_my_blocks_x)%n_my_blocks_x; 
				b.prev_direction=LEFT;
					  break;
			case RIGHT: 
				if (mat[(b.x+1+n_my_blocks_x)%n_my_blocks_x][b.y]!=MY_FREE) touch((b.x+1+n_my_blocks_x)%n_my_blocks_x,b.y,1);
				b.x=(b.x+1+n_my_blocks_x)%n_my_blocks_x;
				b.prev_direction=RIGHT;
					  break;
			case UP:
				if (mat[b.x][(b.y+1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(b.x,(b.y+1+n_my_blocks_y)%n_my_blocks_y,1);
				b.y=(b.y+1+n_my_blocks_y)%n_my_blocks_y;
					  break;
			case DOWN: //impossible: go up;
				if (mat[b.x][(b.y+1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(b.x,(b.y+1+n_my_blocks_y)%n_my_blocks_y,1);
				b.y=(b.y+1+n_my_blocks_y)%n_my_blocks_y;
                      break;
			}
			break;
		case DOWN:
			switch (b.direction) {
			case LEFT:
				if (mat[(b.x-1+n_my_blocks_x)%n_my_blocks_x][b.y]!=MY_FREE) touch((b.x-1+n_my_blocks_x)%n_my_blocks_x,b.y,1);
				b.x=(b.x-1+n_my_blocks_x)%n_my_blocks_x;
				b.prev_direction=LEFT;
					  break;
			case RIGHT: 
				if (mat[(b.x+1+n_my_blocks_x)%n_my_blocks_x][b.y]!=MY_FREE) touch((b.x+1+n_my_blocks_x)%n_my_blocks_x,b.y,1);
				b.x=(b.x+1+n_my_blocks_x)%n_my_blocks_x;
				b.prev_direction=RIGHT;
					  break;
			case UP: //impossible: go down;
				if (mat[b.x][(b.y-1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(b.x,(b.y-1+n_my_blocks_y)%n_my_blocks_y,1);
				b.y=(b.y-1+n_my_blocks_y)%n_my_blocks_y;
				      break;
			case DOWN: 
				if (mat[b.x][(b.y-1+n_my_blocks_y)%n_my_blocks_y]!=MY_FREE) touch(b.x,(b.y-1+n_my_blocks_y)%n_my_blocks_y,1);
				b.y=(b.y-1+n_my_blocks_y)%n_my_blocks_y;
                      break;
			}
			break;
		} // End select;
	
	if (game_status==PLAY) {
		add_fifo(&(b.p_head),&(b.p_tail), b.x, b.y);
		mat[b.x][b.y]=SNAKE1;
	}
	b.length++;
	if (b.length==b.wanted_length) // Tail needs to be removed
	{
		mat[(*(b.p_tail)).x][(*(b.p_tail)).y]=MY_FREE;
		remove_fifo(&(b.p_tail));
		b.length--;
	}

	}


	//End Snake 2


glutPostRedisplay();
}

glutTimerFunc(1000/fps, myTimer, 1);
}

void free_casual_block(int *x, int *y){
boolean found=MY_FALSE;
while (!found){
    *x=rand(); 
	*x=floor((double)*x/RAND_MAX*n_my_blocks_x);
	*y=rand();
	*y=floor((double)*y/RAND_MAX*n_my_blocks_y);
	if (mat[*x][*y]==MY_FREE)
		found=MY_TRUE;
}
}


void renderBitmapString(float x, float y, float z, void *font, char *string) {  
  char *c;
  glRasterPos3f(x, y, z);
  for (c=string; *c != '\0'; c++) {
    glutBitmapCharacter(font, *c);
  }
}
void display(void)

{  
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1,0,0);
	
	for (int x=0;x<n_my_blocks_x;x++) {
		for (int y=0;y<n_my_blocks_y;y++) {
			switch (mat[x][y]) {
				case MY_FREE:
					if (textures!=MY_TRUE) put_my_block(x,y,BLACK);
					else draw_texture(x,y,1);
					break;
				case SNAKE0:
					if (textures!=MY_TRUE) put_my_block(x,y,a.color);
                    else draw_texture(x,y,4);
					if ((x==(*(a.p_head)).x)&&(y==(*(a.p_head)).y)) {
						draw_eyes(x,y,a.prev_direction,BLACK);
						if ((a.score>b.score)&&(two_players==MY_TRUE))
							draw_eyes(x,y,a.prev_direction,YELLOW);
					}
                	break;
				case SNAKE1:
					if (textures!=MY_TRUE) put_my_block(x,y,b.color);
					else draw_texture(x,y,5);
					if ((x==(*(b.p_head)).x)&&(y==(*(b.p_head)).y)) 
						if (b.score>a.score)
                            draw_eyes(x,y,b.prev_direction,YELLOW);
						else 
                            draw_eyes(x,y,b.prev_direction,BLACK);
					break;
				case TOKEN:
					if (textures!=MY_TRUE) put_my_block(x,y,BLUE);
					else draw_texture(x,y,3);
					break;
				case MAZE:
					if (textures!=MY_TRUE) put_my_block(x,y,GRAY);
					else draw_texture(x,y,2);
					break;
			}
		}
	}
	if (game_status==MESSAGE)
	{
		if (two_players==MY_FALSE)
		{
			glColor3f(1,0,0);
			strcpy(alert_message,"You lose!");
			renderBitmapString(1,y_max+(Y_HEADER)/2,0,FONT,alert_message);
		} else {
			if (first_display==MY_TRUE) total_matches++;
			switch (winner_id) {
				case 0:
					glColor3f(0,1,0);
					strcpy(alert_message,"Green mamba wins!");
					renderBitmapString(1,y_max+(Y_HEADER)/2,0,FONT,alert_message);
					if (first_display==MY_TRUE) {a.total_score+=a.score; a.matches_win++;}
					break;
				case 1:
					glColor3f(1,0,0);
					strcpy(alert_message,"Red mamba wins!");
					renderBitmapString(1,y_max+(Y_HEADER)/2,0,FONT,alert_message);
					if (first_display==MY_TRUE) {b.total_score+=b.score; b.matches_win++;}
					break;
				case 2:
					if (a.score>b.score)
					{
						glColor3f(0,1,0);
						strcpy(alert_message,"Green mamba wins!");
						renderBitmapString(1,y_max+(Y_HEADER)/2,0,FONT,alert_message);
						if (first_display==MY_TRUE) {a.total_score+=a.score; a.matches_win++;}
					} else if (a.score<b.score)
					{
						glColor3f(1,0,0);
						strcpy(alert_message,"Red mamba wins!");
						renderBitmapString(1,y_max+(Y_HEADER)/2,0,FONT,alert_message);
						if (first_display==MY_TRUE) {b.total_score+=b.score; b.matches_win++;}
					} else {
						glColor3f(1,1,1);
						strcpy(alert_message,"Round draw.");
						renderBitmapString(1,y_max+(Y_HEADER)/2,0,FONT,alert_message);
					}
					break;
			}
		writeTotalScore();
		first_display=MY_FALSE;
		}
   	}
	if (game_status==READY)
	{
        glColor3f(1,1,0);
		renderBitmapString(1,y_max+(Y_HEADER)/2,0,FONT,READY_TEXT);
	}
	if (game_status==PAUSE)
	{
        glColor3f(1,1,0);
		renderBitmapString(1,y_max+(Y_HEADER)/2,0,FONT,PAUSE_TEXT);
	}
	if ((game_status==PLAY)&&(time_race==MY_TRUE)) //Time race text visualization
	{
        glColor3f(1,1,0);
		strcpy(score_string,TIME_RACE_TEXT);
		itoa(fps,score,10);
		strcat(score_string,score);
		renderBitmapString(1,y_max+(Y_HEADER)/2,0,FONT,score_string);
	}
	if (game_status==START)
	{
        glColor3f(0,1,1);
		renderBitmapString(1,y_max+(Y_HEADER)/2,0,FONT,START_TEXT);
		writeInstructions();
	}

	// Write the name of the author
	glColor3f(0,0,0);
	renderBitmapString(1.5*dim_my_block_x,1.5*dim_my_block_y,0,GLUT_BITMAP_8_BY_13,AUTHOR_TEXT);
	
	// Write the score
	if (two_players==TRUE)
	{
		glColor3f(0,1,0);
		strcpy(score_string,SCORE_TEXT_A);
		itoa(a.score,score,10);
		strcat(score_string,score);
		renderBitmapString(x_max*SCORE_POSITION,y_max+(Y_HEADER)/2+SCORE_DELTA,0,FONT,score_string);
		glColor3f(1,0,0);
		strcpy(score_string,SCORE_TEXT_B);
		itoa(b.score,score,10);
		strcat(score_string,score);
		renderBitmapString(x_max*SCORE_POSITION,y_max+(Y_HEADER)/2-SCORE_DELTA,0,FONT,score_string);
	} else {
		glColor3f(1,1,1);
		strcpy(score_string,SCORE_TEXT);
		itoa(a.score,score,10);
		strcat(score_string,score);
	    renderBitmapString(x_max*SCORE_POSITION,y_max+(Y_HEADER)/2,0,FONT,score_string);
	}
	// Draw a Line
	glColor3f(1,1,1);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
    glBegin(GL_POLYGON);
		glVertex2f(0,y_max+LINE_THICKNESS);	
		glVertex2f(0, y_max);
		glVertex2f(x_max,y_max);
		glVertex2f(x_max,y_max+LINE_THICKNESS);
	glEnd();

	glutSwapBuffers();
}

void myReshape(int w, int h)
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glViewport(0, 0, w, h);
	gluOrtho2D(0, x_max, 0, y_max+Y_HEADER);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
void initialization (void){
	
	//Maze initialization
	switch(maze){
		case 0:
			for (int i=0;i<n_my_blocks_y;i++)
				for (int j=0;j<n_my_blocks_x;j++)
					mat[j][i]=MY_FREE;
			break;
		case 1:
			for (int i=0;i<n_my_blocks_y;i++)
				for (int j=0;j<n_my_blocks_x;j++)
					mat[j][i]=MY_FREE;
            for (int i=0;i<(int)(n_my_blocks_x/3);i++){
				mat[i][n_my_blocks_y-1]=MAZE;
				mat[n_my_blocks_x-1-i][n_my_blocks_y-1]=MAZE;
				mat[i][0]=MAZE;
				mat[n_my_blocks_x-1-i][0]=MAZE;
			}
			for (int j=0;j<(int)(n_my_blocks_y/3);j++){
				mat[0][n_my_blocks_y-1-j]=MAZE;
				mat[n_my_blocks_x-1][n_my_blocks_y-1-j]=MAZE;
				mat[0][j]=MAZE;
				mat[n_my_blocks_x-1][j]=MAZE;
			}
			break;
		case 2:
			for (int i=0;i<n_my_blocks_y;i++)
				for (int j=0;j<n_my_blocks_x;j++)
					mat[j][i]=MY_FREE;
			for (i=0;i<n_my_blocks_y;i++){
				mat[0][i]=MAZE;
				mat[n_my_blocks_x-1][i]=MAZE;
			}
			for (j=0;j<n_my_blocks_x;j++){
				mat[j][0]=MAZE;
				mat[j][n_my_blocks_y-1]=MAZE;
			}

			break;
	}
		
	
	//Variables Initialization
	if (time_race==TRUE) fps=FPS_3;
	//Snake A
	a.score=0;
	a.length=0; // actual length of the snake in my_blocks;
	a.wanted_length=START_LENGTH; // length of the snake, to be reached;
	a.prev_direction=LEFT; // previous direction of the snake;
	a.direction=LEFT; // actual direction of the snake;
	a.x=(int)(n_my_blocks_x/2); // x coordinate of the head my_block;
	a.y=(int)(n_my_blocks_y/2); // y coordinate of the head my_block;
	a.p_head=a.p_tail=NULL;
	a.color=GREEN;
	//Snake B
	b.score=0;
	b.length=0; // actual length of the snake in my_blocks;
	b.wanted_length=START_LENGTH; // length of the snake, to be reached;
	b.prev_direction=RIGHT; // previous direction of the snake;
	b.direction=RIGHT; // actual direction of the snake;
	b.x=(int)(n_my_blocks_x/2+1); // x coordinate of the head my_block;
	b.y=(int)(n_my_blocks_y/2); // y coordinate of the head my_block;
	b.p_head=b.p_tail=NULL;
	b.color=RED;
	
	srand((unsigned)time( NULL ) );
	free_casual_block(&token_x,&token_y);	 // Create first token
	mat[token_x][token_y]=TOKEN;
	game_status=START; // Set the initial status
}

void keyboard (unsigned char key, int x, int y)
{
	if (game_status==READY) {
		game_status=PLAY;
		first_display=MY_TRUE;
	    if (sound==MY_TRUE) PlaySound(START_NORMAL_SOUND,NULL,SND_ASYNC | SND_NODEFAULT); 
	}
	switch(key)
	{
	case 'w':
	case 'W':
		b.direction=UP;
		break;
	case 'a':
	case 'A':
		b.direction=LEFT;
		break;
	case 's':
	case 'S':
		b.direction=DOWN;
		break;	
	case 'd':
	case 'D':
		b.direction=RIGHT;
		break;
   case 'p':
   case 'P':
	    if (game_status==PAUSE) game_status=PLAY;
		else if (game_status==PLAY) game_status=PAUSE;
		glutPostRedisplay();
	    break;
   case 27:
	   if (game_status==START||game_status==MESSAGE)
			exit(1);
	   else
			{
				initialization();
				game_status=START;
				glutPostRedisplay();
				glutAttachMenu(GLUT_RIGHT_BUTTON);
			}
	   break;
	}

}

void special_keyboard (int key, int x, int y)
{
	if (game_status==READY) {
		game_status=PLAY;
		first_display=MY_TRUE;
	    if (sound==MY_TRUE) PlaySound(START_NORMAL_SOUND,NULL,SND_ASYNC | SND_NODEFAULT); 
	}
	switch(key)
	{
	case GLUT_KEY_UP:
		a.direction=UP;
		break;
	case GLUT_KEY_LEFT:
		a.direction=LEFT;
		break;
	case GLUT_KEY_DOWN:
		a.direction=DOWN;
		break;	
	case GLUT_KEY_RIGHT:
		a.direction=RIGHT;
		break;
    case GLUT_KEY_F5:
		glutReshapeWindow((x_max/y_max)*RESHAPE_1*x_max,x_max*(x_max/y_max)*RESHAPE_1*(1+Y_HEADER/y_max));
		glutPositionWindow(2,30); 
		break;
	case GLUT_KEY_F6:
		glutReshapeWindow((x_max/y_max)*RESHAPE_2*x_max,x_max*(x_max/y_max)*RESHAPE_2*(1+Y_HEADER/y_max));
		glutPositionWindow(2,30);
		break;
	case GLUT_KEY_F7:
		glutFullScreen();
		break;
	case GLUT_KEY_F1:
		if (game_status==START||game_status==MESSAGE) 
		{
			glutDetachMenu(GLUT_RIGHT_BUTTON);
			initialization();
			glutPostRedisplay();
			game_status=READY;
		}
		break;
	}

}

void createGLUTMenus() {
	int popup,level_submenu,maze_submenu,view_submenu,size_submenu,sound_submenu,players_submenu,textures_submenu;

	level_submenu = glutCreateMenu(processMenuEvents);
	glutAddMenuEntry(LEVEL_1,1);
	glutAddMenuEntry(LEVEL_2,2);
	glutAddMenuEntry(LEVEL_3_C,3);
    glutAddMenuEntry(LEVEL_4,4);
	glutAddMenuEntry(LEVEL_5,5);
	glutAddMenuEntry(LEVEL_6,6);

    maze_submenu = glutCreateMenu(processMenuEvents);
	glutAddMenuEntry(MAZE_1_C,10);
	glutAddMenuEntry(MAZE_2,11);
	glutAddMenuEntry(MAZE_3,12);

	view_submenu = glutCreateMenu(processMenuEvents);
	glutAddMenuEntry(VIEW_1,20);
	glutAddMenuEntry(VIEW_2,21);
	glutAddMenuEntry(VIEW_3,22);

	size_submenu = glutCreateMenu(processMenuEvents);
	glutAddMenuEntry(SIZE_1,30);
	glutAddMenuEntry(SIZE_2_C,31);
	glutAddMenuEntry(SIZE_3,32);

	sound_submenu = glutCreateMenu(processMenuEvents);
	glutAddMenuEntry(ON_C,40);
	glutAddMenuEntry(OFF,41);

	players_submenu = glutCreateMenu(processMenuEvents);
	glutAddMenuEntry(PLAYERS_1_C,50);
	glutAddMenuEntry(PLAYERS_2,51);

	textures_submenu = glutCreateMenu(processMenuEvents);
	glutAddMenuEntry(ON_C,60);
	glutAddMenuEntry(OFF,61);
	
	popup = glutCreateMenu(processMenuEvents);
	glutAddMenuEntry("Start \tF1",0);
	glutAddSubMenu("Players",players_submenu);
	glutAddSubMenu("Level",level_submenu);
	glutAddSubMenu("Maze",maze_submenu);
	glutAddSubMenu("Size",size_submenu);
	glutAddSubMenu("View",view_submenu);
	glutAddSubMenu("Textures",textures_submenu);
	glutAddSubMenu("Sound",sound_submenu);
	glutAddMenuEntry("Reset scores",70);
	glutAddMenuEntry("Exit \tESC",100);
	glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void processMenuEvents(int option) {

	switch (option) {
		case 0:
			glutDetachMenu(GLUT_RIGHT_BUTTON);
			initialization();
			glutPostRedisplay();
			game_status=READY;
			break;
		case 1 :  
			fps=FPS_1;
			time_race=MY_FALSE;
			glutChangeToMenuEntry(1,LEVEL_1_C,1);
			glutChangeToMenuEntry(2,LEVEL_2,2);
			glutChangeToMenuEntry(3,LEVEL_3,3);
			glutChangeToMenuEntry(4,LEVEL_4,4);
			glutChangeToMenuEntry(5,LEVEL_5,5);
			glutChangeToMenuEntry(6,LEVEL_6,6);
			break;
		case 2 : 
			fps=FPS_2;
			time_race=MY_FALSE;
			glutChangeToMenuEntry(1,LEVEL_1,1);
			glutChangeToMenuEntry(2,LEVEL_2_C,2);
			glutChangeToMenuEntry(3,LEVEL_3,3);
			glutChangeToMenuEntry(4,LEVEL_4,4);
			glutChangeToMenuEntry(5,LEVEL_5,5);
			glutChangeToMenuEntry(6,LEVEL_6,6);
			break;
		case 3 :
			fps=FPS_3;
			time_race=MY_FALSE;
			glutChangeToMenuEntry(1,LEVEL_1,1);
			glutChangeToMenuEntry(2,LEVEL_2,2);
			glutChangeToMenuEntry(3,LEVEL_3_C,3);
			glutChangeToMenuEntry(4,LEVEL_4,4);
			glutChangeToMenuEntry(5,LEVEL_5,5);
			glutChangeToMenuEntry(6,LEVEL_6,6);
			break;
		case 4 :
			fps=FPS_4;
			time_race=MY_FALSE;
			glutChangeToMenuEntry(1,LEVEL_1,1);
			glutChangeToMenuEntry(2,LEVEL_2,2);
			glutChangeToMenuEntry(3,LEVEL_3,3);
			glutChangeToMenuEntry(4,LEVEL_4_C,4);
			glutChangeToMenuEntry(5,LEVEL_5,5);
			glutChangeToMenuEntry(6,LEVEL_6,6);
			break;
		case 5 :
			fps=FPS_5;
			glutChangeToMenuEntry(1,LEVEL_1,1);
			glutChangeToMenuEntry(2,LEVEL_2,2);
			glutChangeToMenuEntry(3,LEVEL_3,3);
			glutChangeToMenuEntry(4,LEVEL_4,4);
			glutChangeToMenuEntry(5,LEVEL_5_C,5);
			glutChangeToMenuEntry(6,LEVEL_6,6);
			break;
		case 6 :
			fps=FPS_3;
			time_race=MY_TRUE;
			glutChangeToMenuEntry(1,LEVEL_1,1);
			glutChangeToMenuEntry(2,LEVEL_2,2);
			glutChangeToMenuEntry(3,LEVEL_3,3);
			glutChangeToMenuEntry(4,LEVEL_4,4);
			glutChangeToMenuEntry(5,LEVEL_5,5);
			glutChangeToMenuEntry(6,LEVEL_6_C,6);
			break;
        case 100 :
			exit(1);
		case 10 :
			maze=0;
			initialization();
            glutChangeToMenuEntry(1,MAZE_1_C,10);
			glutChangeToMenuEntry(2,MAZE_2,11);
			glutChangeToMenuEntry(3,MAZE_3,12);
			glutPostRedisplay();
			break;
		case 11:
			maze=1;
			initialization();
            glutChangeToMenuEntry(1,MAZE_1,10);
			glutChangeToMenuEntry(2,MAZE_2_C,11);
			glutChangeToMenuEntry(3,MAZE_3,12); 
			glutPostRedisplay();
			break;
		case 12:
			maze=2;
			initialization();
            glutChangeToMenuEntry(1,MAZE_1,10);
			glutChangeToMenuEntry(2,MAZE_2,11);
			glutChangeToMenuEntry(3,MAZE_3_C,12);
			glutPostRedisplay();
			break;
		case 20:
			glutReshapeWindow((x_max/y_max)*RESHAPE_1*x_max,x_max*(x_max/y_max)*RESHAPE_1*(1+Y_HEADER/y_max));
			glutPositionWindow(2,30); 
			break;
		case 21:
			glutReshapeWindow((x_max/y_max)*RESHAPE_2*x_max,x_max*(x_max/y_max)*RESHAPE_2*(1+Y_HEADER/y_max));
			glutPositionWindow(2,30); 
			break;
		case 22:
			glutFullScreen();
			break;
		case 30:
			n_my_blocks_x=n_my_blocks_y=SIZE_MAX_1;
			size=1;
			dim_my_block_x=x_max/n_my_blocks_x;
			dim_my_block_y=y_max/n_my_blocks_y;
			initialization();
            glutChangeToMenuEntry(1,SIZE_1_C,30);
			glutChangeToMenuEntry(2,SIZE_2,31);
			glutChangeToMenuEntry(3,SIZE_3,32);
			glutPostRedisplay();
			break;
		case 31:
			size=2;
			n_my_blocks_x=n_my_blocks_y=SIZE_MAX_2;
			dim_my_block_x=x_max/n_my_blocks_x;
			dim_my_block_y=y_max/n_my_blocks_y;
			initialization();
            glutChangeToMenuEntry(1,SIZE_1,30);
			glutChangeToMenuEntry(2,SIZE_2_C,31);
			glutChangeToMenuEntry(3,SIZE_3,32);
			glutPostRedisplay();
			break;
		case 32:
			size=4;
			n_my_blocks_x=n_my_blocks_y=SIZE_MAX_3;
			dim_my_block_x=x_max/n_my_blocks_x;
			dim_my_block_y=y_max/n_my_blocks_y;
			initialization();
            glutChangeToMenuEntry(1,SIZE_1,30);
			glutChangeToMenuEntry(2,SIZE_2,31);
			glutChangeToMenuEntry(3,SIZE_3_C,32);
			glutPostRedisplay();
			break;
		case 40:
			sound=MY_TRUE;
			glutChangeToMenuEntry(1,ON_C,40);
			glutChangeToMenuEntry(2,OFF,41);
			break;
		case 41:
			sound=MY_FALSE;
			PlaySound(NULL,NULL,NULL); //Stop sounds
			glutChangeToMenuEntry(1,ON,40);
			glutChangeToMenuEntry(2,OFF_C,41);
			break;
		case 50:
			two_players=MY_FALSE;
			glutChangeToMenuEntry(1,PLAYERS_1_C,50);
			glutChangeToMenuEntry(2,PLAYERS_2,51);
			break;
		case 51:
			two_players=MY_TRUE;
			glutChangeToMenuEntry(1,PLAYERS_1,50);
			glutChangeToMenuEntry(2,PLAYERS_2_C,51);
			first_display=MY_FALSE;
			a.total_score=0;
			b.total_score=0;
			a.matches_win=0;
			b.matches_win=0;
			total_matches=0;
			glutPostRedisplay();
			break;
		case 60:
			textures=MY_TRUE;
			glutChangeToMenuEntry(1,ON_C,60);
			glutChangeToMenuEntry(2,OFF,61);
			glutPostRedisplay();
			break;
		case 61:
			textures=MY_FALSE;
			glutChangeToMenuEntry(1,ON,60);
			glutChangeToMenuEntry(2,OFF_C,61);
			glutPostRedisplay();
			break;
		case 70:
			a.total_score=0;
			b.total_score=0;
			a.matches_win=0;
			b.matches_win=0;
			total_matches=0;
			glutPostRedisplay();
			break;
	}
}
void writeInstructions(void) {
	glColor3f(1,1,1);
	int delta=y_max-Y_HEADER;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_1);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_2);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_3);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_4);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_5);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_6);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_7);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_8);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_9);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_10);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_11);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_12);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_13);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_14);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,INSTRUCTION_TEXT_15);
}

void writeTotalScore(void) {
	glColor3f(0,1,0);
	int delta=y_max-Y_HEADER;
	renderBitmapString(DELTA_X,delta,0,FONT,SCORE_TEXT_1);
	delta-=DELTA_Y;
	strcpy(score_string,SCORE_TEXT_2);
	itoa(a.matches_win,score,10);
	strcat(score_string,score);
	renderBitmapString(DELTA_X,delta,0,FONT,score_string);
	delta-=DELTA_Y;
	strcpy(score_string,SCORE_TEXT_3);
	itoa(a.total_score,score,10);
	strcat(score_string,score);
	renderBitmapString(DELTA_X,delta,0,FONT,score_string);
    glColor3f(1,0,0);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,SCORE_TEXT_4);
	delta-=DELTA_Y;
	strcpy(score_string,SCORE_TEXT_5);
	itoa(b.matches_win,score,10);
	strcat(score_string,score);
	renderBitmapString(DELTA_X,delta,0,FONT,score_string);
	delta-=DELTA_Y;
	strcpy(score_string,SCORE_TEXT_6);
	itoa(b.total_score,score,10);
	strcat(score_string,score);
	renderBitmapString(DELTA_X,delta,0,FONT,score_string);
	glColor3f(1,1,1);
	delta-=DELTA_Y;
	renderBitmapString(DELTA_X,delta,0,FONT,SCORE_TEXT_7);
	delta-=DELTA_Y;
	strcpy(score_string,SCORE_TEXT_8);
	itoa(total_matches,score,10);
	strcat(score_string,score);
	strcpy(score_string,score_string);
	renderBitmapString(DELTA_X,delta,0,FONT,score_string);


}
void draw_texture(int x,int y,int texture_id)
{

glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL); 
glEnable (GL_TEXTURE_2D); 
glBindTexture(GL_TEXTURE_2D, texture_id);
glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex3f(x*dim_my_block_x, y*dim_my_block_y,0); 
	glTexCoord2f(1, 0); glVertex3f((x+1)*dim_my_block_x, y*dim_my_block_y,0);
	glTexCoord2f(1, 1); glVertex3f((x+1)*dim_my_block_x, (y+1)*dim_my_block_y,0); 
	glTexCoord2f(0, 1); glVertex3f(x*dim_my_block_x, (y+1)*dim_my_block_y,0); 
glEnd();
glDisable (GL_TEXTURE_2D);
}

void draw_eyes(int x, int y, directions direction, colors color){
    rgb_color chosen;
	find_color(color, &chosen);
    glColor3f(chosen.r,chosen.g,chosen.b);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	switch(direction) {
		case LEFT:
	    glBegin(GL_POLYGON);
			glVertex2f(dim_my_block_x/5*(5*x+1), (5*y+1)*dim_my_block_y/5);
			glVertex2f((5*x+2)*dim_my_block_x/5, (5*y+1)*dim_my_block_y/5);
			glVertex2f((5*x+2)*dim_my_block_x/5, (5*y+2)*dim_my_block_y/5);
			glVertex2f((5*x+1)*dim_my_block_x/5, (5*y+2)*dim_my_block_y/5);
		glEnd();
		glBegin(GL_POLYGON);
			glVertex2f(dim_my_block_x/5*(5*x+1), (5*y+3)*dim_my_block_y/5);
			glVertex2f((5*x+2)*dim_my_block_x/5, (5*y+3)*dim_my_block_y/5);
			glVertex2f((5*x+2)*dim_my_block_x/5, (5*y+4)*dim_my_block_y/5);
			glVertex2f((5*x+1)*dim_my_block_x/5, (5*y+4)*dim_my_block_y/5);
		glEnd();
		break;
		case RIGHT:
	    glBegin(GL_POLYGON);
			glVertex2f(dim_my_block_x/5*(5*x+3), (5*y+1)*dim_my_block_y/5);
			glVertex2f((5*x+4)*dim_my_block_x/5, (5*y+1)*dim_my_block_y/5);
			glVertex2f((5*x+4)*dim_my_block_x/5, (5*y+2)*dim_my_block_y/5);
			glVertex2f((5*x+3)*dim_my_block_x/5, (5*y+2)*dim_my_block_y/5);
		glEnd();
		glBegin(GL_POLYGON);
			glVertex2f(dim_my_block_x/5*(5*x+3), (5*y+3)*dim_my_block_y/5);
			glVertex2f((5*x+4)*dim_my_block_x/5, (5*y+3)*dim_my_block_y/5);
			glVertex2f((5*x+4)*dim_my_block_x/5, (5*y+4)*dim_my_block_y/5);
			glVertex2f((5*x+3)*dim_my_block_x/5, (5*y+4)*dim_my_block_y/5);
		glEnd();
		break;
		case UP:
	    glBegin(GL_POLYGON);
			glVertex2f(dim_my_block_x/5*(5*x+1), (5*y+3)*dim_my_block_y/5);
			glVertex2f((5*x+2)*dim_my_block_x/5, (5*y+3)*dim_my_block_y/5);
			glVertex2f((5*x+2)*dim_my_block_x/5, (5*y+4)*dim_my_block_y/5);
			glVertex2f((5*x+1)*dim_my_block_x/5, (5*y+4)*dim_my_block_y/5);
		glEnd();
		glBegin(GL_POLYGON);
			glVertex2f(dim_my_block_x/5*(5*x+3), (5*y+3)*dim_my_block_y/5);
			glVertex2f((5*x+4)*dim_my_block_x/5, (5*y+3)*dim_my_block_y/5);
			glVertex2f((5*x+4)*dim_my_block_x/5, (5*y+4)*dim_my_block_y/5);
			glVertex2f((5*x+3)*dim_my_block_x/5, (5*y+4)*dim_my_block_y/5);
		glEnd();
		break;
		case DOWN:
	    glBegin(GL_POLYGON);
			glVertex2f(dim_my_block_x/5*(5*x+1), (5*y+1)*dim_my_block_y/5);
			glVertex2f((5*x+2)*dim_my_block_x/5, (5*y+1)*dim_my_block_y/5);
			glVertex2f((5*x+2)*dim_my_block_x/5, (5*y+2)*dim_my_block_y/5);
			glVertex2f((5*x+1)*dim_my_block_x/5, (5*y+2)*dim_my_block_y/5);
		glEnd();
		glBegin(GL_POLYGON);
			glVertex2f(dim_my_block_x/5*(5*x+3), (5*y+1)*dim_my_block_y/5);
			glVertex2f((5*x+4)*dim_my_block_x/5, (5*y+1)*dim_my_block_y/5);
			glVertex2f((5*x+4)*dim_my_block_x/5, (5*y+2)*dim_my_block_y/5);
			glVertex2f((5*x+3)*dim_my_block_x/5, (5*y+2)*dim_my_block_y/5);
		glEnd();
		break;
	}
}