#include "opengl.h"

//bool ignoreRepeats = FALSE;

float board_x;
float board_y;
float win_x;
float win_y;
float win_aspect;

int mouse_x, mouse_y;
struct movement{
	struct grid source;
	struct grid destination;
	int valid_option;
}move;

//[abc] [123] .x1 .y1 = left bottom  x2 y2 = right top
struct vertex{
	float x1;
	float y1;
	float x2;
	float y2;
}bounds[8][8];

const char alphabet[] = "ABCDEFGH";
const char numbers[]  = "12345678";

//fix king/queen
//0-7     8/15   9/14     10/13    11       12
int type_conv[] = {0, 0 ,0 ,0 ,0 ,0 ,0 ,0,
		   1, 2, 3, 4, 5, 3, 2, 1};

//void glutMouseFunc(  void (*func)(int button, int state, int x, int y)  ){

void processMouse(int button, int state, int x, int y){
	int xc, yc;
	if(button == GLUT_LEFT_BUTTON){
		//printf("x: %d y: %d\n", x, y);
		//printf("bottom left bx: %f by: %f\n", bounds[0][0].x1, bounds[0][0].y1);
		//printf("top right   bx: %f by: %f\n", bounds[0][0].x2, bounds[0][0].y2);
		mouse_x = x;
		mouse_y = y;
		for(xc=0; xc<8; xc++){
			for(yc=0; yc<8; yc++){
				if(x>bounds[xc][yc].x1 && x<bounds[xc][yc].x2){
					if(y<bounds[xc][yc].y1 && y>bounds[xc][yc].y2){
						if(state == GLUT_DOWN){
							//button pressed   the movement source
							//move.source = xc*8 + yc;
							move.source.x = xc;
							move.source.y = yc;
							
							//check if valid/moveable/your-turn
							//set move.valid_option 0/1
							Valid_Source();
							printf("source valid: %d\n", move.valid_option);
						}
						if(state == GLUT_UP && move.valid_option){
							//button released  the movement destination
							//move.destination = xc*8 + yc;
							move.destination.x = xc;
							move.destination.y = yc;
							//check if valid/not-occupied/your-turn
							//check if dest == source
							Valid_Destination();
							//set move.valid_option 0/1
							//make move if TRUE
							printf("dest valid: %d\n", move.valid_option);
							//printf("moving to %c %c\n", alphabet[xc], numbers[yc]);
							//my_turn = 0;
							//Send_OK = 1;
							if(move.valid_option){
								my_turn = 0;
								Send_OK = 1;
								printf("moving to %c %c\n", alphabet[xc], numbers[yc]);
							}
						}
						//printf("you clicked position: %c %c\n", alphabet[xc], numbers[yc]);
						return;
					}
				}
			}
		}
		//printf("you did not click in a board position!\n");
		return;
	}
}

void Valid_Source(void){
	//check if valid/moveable/your-turn
	int pcount;
	move.valid_option = 0;	


	//check if it's my turn
	if(!my_turn)
		return;

	//check if one of my piece is on the spot clicked
	for(pcount=0; pcount<16; pcount++){
		//only check alive pieces
		if(my_pieces[pcount].alive){
			if(my_pieces[pcount].position.x == move.source.x && my_pieces[pcount].position.y == move.source.y){
				move.valid_option = 1;
				break;
			}
		}
	}
}

void Valid_Destination(void){
	//check if valid/not-occupied/your-turn/equals-src
	move.valid_option = 0;
	
	if(!my_turn)
		return;
	int xdiff = abs(move.destination.x - move.source.x);
	int ydiff = abs(move.destination.y - move.source.y);

	//make sure we aren't moving to same destination as source
	if(xdiff == 0 && ydiff == 0)
		return;
	int pcount;
	int capturing = 0;
	int piece_to_capture;
	//make sure one of my peices isn't already on the destination
	for(pcount=0; pcount<16; pcount++){
		//only check alive pieces
		if(my_pieces[pcount].alive){
			if(my_pieces[pcount].position.x == move.destination.x && my_pieces[pcount].position.y == move.destination.y)
				return;
		}
		if(oponent_pieces[pcount].alive){
			if(oponent_pieces[pcount].position.x == move.destination.x && oponent_pieces[pcount].position.y == move.destination.y){
				capturing=1;
				piece_to_capture = pcount;
			}
		}
	}
	/*check type limitations for move
	  -pawns move 1 square forward(to empty destinations) or forward angle(if oponent can be captured)
	  -rook move right/left/up/down to empty destination or oponent occupied long as not blocked by team
	  -knight move in L shape to empty or oponent occupied
	  -bishop move at angles to empty or oponent occupied long as not blocked by team
          -queen move right/left/up/down/angle long as not blocked by team
	  -king move 1 square right/left/up/down/angle long as not blocked by team
	  ---remember to implement castling maneuver
	*/
	//check what type I am
	int my_type = 6;
	int my_pcount;
	//int xdiff, ydiff;
	for(pcount=0; pcount<16; pcount++){
		if(my_pieces[pcount].alive){
			if(my_pieces[pcount].position.x == move.source.x && my_pieces[pcount].position.y == move.source.y){
				my_type = my_pieces[pcount].type;
				my_pcount = pcount;
				break;
			}
		}
	}
	xdiff = abs(move.destination.x - move.source.x);
	ydiff = abs(move.destination.y - move.source.y);
	switch(my_type){
		case 0: //pawn
			//check that move is no more than 1 space in correct direction (cept first turn)  
			// remember En Passant


			//has 4 posible moves from any position

			//check not moving sideways
			if(ydiff == 0)
				return;

			//check not moving backwards
			if(my_team_color){ //black top
				if(move.destination.y > move.source.y)
					return;
			}else{ //white bottom
				if(move.destination.y < move.source.y)
					return;
			}
			
			//check that x movement is no greater than +1
			if(xdiff != 0){
				if(xdiff > 1)
					return;
			}

printf("ft: %d\n", my_pieces[my_pcount].first_turn);
			//check that y is no greater than +1 unless first turn ... 
			if(ydiff > 1){
				if(ydiff != 2 || my_pieces[my_pcount].first_turn != 1)
					return;
				//check that not moving thru an occupied space (can only happen on first turn)
				for(pcount=0; pcount<16; pcount++){
					if(oponent_pieces[pcount].alive && oponent_pieces[pcount].position.x == move.destination.x){
						if(my_team_color){ //if black
							if(oponent_pieces[pcount].position.y == move.destination.y+1)
								return;
						}else{ //white
							if(oponent_pieces[pcount].position.y == move.destination.y-1)
								return;
						}
					}
					if(pcount == my_pcount)
						continue;
					if(my_pieces[pcount].alive && my_pieces[pcount].position.x == move.destination.x){
						if(my_team_color){ //if black
							if(my_pieces[pcount].position.y == move.destination.y+1)
								return;
						}else{ //white
							if(my_pieces[pcount].position.y == move.destination.y-1)
								return;
						}
					}
				}
			}

			//check that enemy is not on space if moving forward
			if(capturing){
				if(xdiff == 0)
					return;
			}
			//check that enemy is on space if moving diag
			if(xdiff == ydiff){
				if(!capturing)
					return;
				//if(oponent_pieces[piece_to_capture].position.y != 
			}

			break;
		case 1: //rook
			
			//implement castling later

			//check not moving diagonal
			if(xdiff > 0 && ydiff > 0)
				return;
			
			//check not moving thru a piece
			if(xdiff > 0 && ydiff == 0){
				//moving sideways
				for(pcount=0; pcount<16; pcount++){
					if(oponent_pieces[pcount].alive){
						if(oponent_pieces[pcount].position.y == move.destination.y){
							if(oponent_pieces[pcount].position.x < move.destination.x && oponent_pieces[pcount].position.x > move.source.x)
								return;
							if(oponent_pieces[pcount].position.x > move.destination.x && oponent_pieces[pcount].position.x < move.source.x)
								return;
						}
					}
					if(my_pieces[pcount].alive){
						if(my_pieces[pcount].position.y == move.destination.y){
							if(my_pieces[pcount].position.x < move.destination.x && my_pieces[pcount].position.x > move.source.x)
								return;
							if(my_pieces[pcount].position.x > move.destination.x && my_pieces[pcount].position.x < move.source.x)
								return;
						}
					}
				}
			}
			if(ydiff > 0 && xdiff == 0){
				//moving up/down
				for(pcount=0; pcount<16; pcount++){
					if(oponent_pieces[pcount].alive){
						if(oponent_pieces[pcount].position.x == move.destination.x){
							if(oponent_pieces[pcount].position.y < move.destination.y && oponent_pieces[pcount].position.y > move.source.y)
								return;
							if(oponent_pieces[pcount].position.y > move.destination.y && oponent_pieces[pcount].position.y < move.source.y)
								return;
						}
					}
					if(my_pieces[pcount].alive){
						if(oponent_pieces[pcount].position.x == move.destination.x){
							if(my_pieces[pcount].position.y < move.destination.y && my_pieces[pcount].position.y > move.source.y)
								return;
							if(my_pieces[pcount].position.y > move.destination.y && my_pieces[pcount].position.y < move.source.y)
								return;
						}
					}
				}
			}

			break;
		case 2: //knight
			
			//check that L shaped move is happening
			if(xdiff != 2 && ydiff != 2)
				return;
			if(xdiff == 2){
				if(ydiff != 1)
					return;
			}
			if(ydiff == 2){
				if(xdiff != 1)
					return;
			}

			break;
		case 3: //bishop

			//check that move is diagonal
			if(xdiff != ydiff)
				return;
			//check that not moving thru a piece
			for(pcount=0; pcount<16; pcount++){
				if(capturing){
					if(pcount == piece_to_capture){
						continue;
					}
				}
				if(!oponent_pieces[pcount].alive)
					continue;
	//	if( move.destination.x == oponent_pieces[pcount].position.x && move.destination.y == oponent_pieces[pcount].position.y){
	//				return;
	//			}
	//	if(oponent_pieces[pcount].position.y == oponent_pieces[pcount].position.x + move.destination.y - move.destination.x){
	//(x2-x1)*(y3-y1) = (x3-x1)*(y2-y1)
		if( (move.destination.x-move.source.x)*(oponent_pieces[pcount].position.y-move.source.y) == 
		    (oponent_pieces[pcount].position.x-move.source.x)*(move.destination.y-move.source.y) ){
					if(oponent_pieces[pcount].position.x > move.source.x && oponent_pieces[pcount].position.x < move.destination.x){
						return;
					}
					if(oponent_pieces[pcount].position.x < move.source.x && oponent_pieces[pcount].position.x > move.destination.x){
						return;
					}
				}
			}
			for(pcount=0; pcount<16; pcount++){
				if(!my_pieces[pcount].alive)
					continue;
				if(pcount == my_pcount)
					continue;
				//if( move.destination.x == my_pieces[pcount].position.x && move.destination.y == my_pieces[pcount].position.y){
				//	return;
				//}
				//y = mx + b
                if( (move.destination.x-move.source.x)*(my_pieces[pcount].position.y-move.source.y) ==
                    (my_pieces[pcount].position.x-move.source.x)*(move.destination.y-move.source.y) ){

			//if(my_pieces[pcount].position.y == my_pieces[pcount].position.x + move.destination.y - move.destination.x){
					if(my_pieces[pcount].position.x > move.source.x && my_pieces[pcount].position.x < move.destination.x){
						return;
					}
					if(my_pieces[pcount].position.x < move.source.x && my_pieces[pcount].position.x > move.destination.x){
						return;
					}
				}


			}

			break;
		case 4: //queen

			//check that move is either: diagonal, up/down, or right/left
			if(xdiff != ydiff){ //if not diagonal
				if(xdiff != 0){ //if x moved then y can't
					if(ydiff != 0)
						return;
					//moving sideways
					for(pcount=0; pcount<16; pcount++){
						if(oponent_pieces[pcount].alive){
							if(oponent_pieces[pcount].position.y == move.destination.y){
								if(oponent_pieces[pcount].position.x < move.destination.x && oponent_pieces[pcount].position.x > move.source.x)
									return;
								if(oponent_pieces[pcount].position.x > move.destination.x && oponent_pieces[pcount].position.x < move.source.x)
									return;
							}
						}
						if(my_pieces[pcount].alive){
							if(my_pieces[pcount].position.y == move.destination.y){
								if(my_pieces[pcount].position.x < move.destination.x && my_pieces[pcount].position.x > move.source.x)
									return;
								if(my_pieces[pcount].position.x > move.destination.x && my_pieces[pcount].position.x < move.source.x)
									return;
							}
						}
					}
				}
				if(ydiff != 0){ //if y moved then x can't
					if(xdiff != 0)
						return;
					//moving up/down
					for(pcount=0; pcount<16; pcount++){
						if(oponent_pieces[pcount].alive){
							if(oponent_pieces[pcount].position.x == move.destination.x){
								if(oponent_pieces[pcount].position.y < move.destination.y && oponent_pieces[pcount].position.y > move.source.y)
									return;
								if(oponent_pieces[pcount].position.y > move.destination.y && oponent_pieces[pcount].position.y < move.source.y)
									return;
							}
						}
						if(my_pieces[pcount].alive){
							if(oponent_pieces[pcount].position.x == move.destination.x){
								if(my_pieces[pcount].position.y < move.destination.y && my_pieces[pcount].position.y > move.source.y)
									return;
								if(my_pieces[pcount].position.y > move.destination.y && my_pieces[pcount].position.y < move.source.y)
									return;
							}
						}
					}

				}
			}

                        //check that not moving thru a piece
                        for(pcount=0; pcount<16; pcount++){
                                if(oponent_pieces[pcount].alive){
                                        //slope of 1
                                        if( move.source.y-oponent_pieces[pcount].position.y / move.source.x-oponent_pieces[pcount].position.x ){
                                                //check if between source/dest x values
                                                if(oponent_pieces[pcount].position.x > move.source.x && oponent_pieces[pcount].position.x < move.destination.x){
                                                        //check if shares the same y intercept
                                                        if(move.source.y-move.source.x == oponent_pieces[pcount].position.y-oponent_pieces[pcount].position.x){
                                                                //don't count piece we are capturing
                                                                if(pcount != piece_to_capture)
                                                                        return;
                                                        }
                                                }
                                                if(oponent_pieces[pcount].position.x < move.source.x && oponent_pieces[pcount].position.x > move.destination.x){
                                                        if(move.source.y-move.source.x == oponent_pieces[pcount].position.y-oponent_pieces[pcount].position.x){
                                                                if(pcount != piece_to_capture)
                                                                        return;
                                                        }
                                                }
                                        }
                                }
                                if(my_pieces[pcount].alive){
                                        if( move.source.y-my_pieces[pcount].position.y / move.source.x-my_pieces[pcount].position.x ){
                                                if(my_pieces[pcount].position.x > move.source.x && my_pieces[pcount].position.x < move.destination.x){
                                                        if(move.source.y-move.source.x == my_pieces[pcount].position.y-my_pieces[pcount].position.x){
                                                                //don't count self
                                                                if(pcount != my_pcount)
                                                                        return;
                                                        }
                                                }
                                                if(my_pieces[pcount].position.x < move.source.x && my_pieces[pcount].position.x > move.destination.x){
                                                        if(move.source.y-move.source.x == my_pieces[pcount].position.y-my_pieces[pcount].position.x){
                                                                if(pcount != my_pcount)
                                                                        return;
                                                        }
                                                }
                                        }
                                }
                        }		

			break;
		case 5: //king

			//8 possible moves

			//check not moving more than 1 space
			if(xdiff > 1 || ydiff > 1)
				return;
			//check not moving to spot occupied by own piece
			for(pcount=0; pcount<16; pcount++){
				if(my_pieces[pcount].alive){
					if(my_pieces[pcount].position.x == move.destination.x && my_pieces[pcount].position.y == move.destination.y)
						return;
				}
			}


			break;
		default:
			printf("ERROR: Incorrect Valid_Destination() usage! %d is not a chess piece\n", my_type);
			exit(1);
			break;
	}

	//if you've made it this far then move is valid
	move.valid_option = 1;

	my_pieces[my_pcount].position.x = move.destination.x;
	my_pieces[my_pcount].position.y = move.destination.y;
	my_pieces[my_pcount].first_turn = 0;
	if(capturing){
		oponent_pieces[piece_to_capture].alive = 0;
	}
}

void Setup_Pieces(void){
	int pcount;
	//0 pawn 1 rook 2 knight 3 bishop 4 queen 5 king
	//0-7     8/15   9/14     10/13    11       12	
	while(my_turn == 0){
		sleep(1);
	}

	//my_turn = 1;
	//my_team_color = 0;
	int pcount2 = 0;

	//void *pieceptr[2];
	int row;

	int black_pawn_row = 6;
	int white_pawn_row = 1;

/*
	//for the pawn's row
	if(my_team_color){
		//black top
		row = 6;
	}else{
		//white bottom
		row = 1;
	}
*/

	for(pcount=0; pcount<16; pcount++){
		my_pieces[pcount].alive         = 1;
		my_pieces[pcount].type          = type_conv[pcount];
		my_pieces[pcount].first_turn    = 1;
		pcount2 = (pcount > 7) ? pcount - 8 : pcount;

		my_pieces[pcount].position.x    = pcount2;
		if(my_team_color) //i'm the black team
			my_pieces[pcount].position.y = (pcount<8) ? black_pawn_row : black_pawn_row+1;
			//my_pieces[pcount].position      = (pcount<8) ? pcount*8+row : pcount*8+row-1;
		else
			my_pieces[pcount].position.y = (pcount<8) ? white_pawn_row : white_pawn_row-1;
			//my_pieces[pcount].position      = (pcount<8) ? pcount*8+row : pcount*8+row+1;
	}

	/*
	//for the pawn's row
	if(my_team_color)
		row = 1; //white bottom
	else
	*/	row = 6; //black top

	for(pcount=0; pcount<16; pcount++){
		oponent_pieces[pcount].alive         = 1;
		oponent_pieces[pcount].first_turn    = 1;
		oponent_pieces[pcount].type          = type_conv[pcount];
		pcount2 = (pcount > 7) ? pcount - 8 : pcount;
		oponent_pieces[pcount].position.x    = pcount2;
		if(my_team_color) //i'm the black team
			oponent_pieces[pcount].position.y = (pcount<8) ? white_pawn_row : white_pawn_row-1;
			//openent_pieces[pcount].position = (pcount<8) ? pcount*8+row : pcount*8+row+1;
		else
			oponent_pieces[pcount].position.y = (pcount<8) ? black_pawn_row : black_pawn_row+1;
			//openent_pieces[pcount].position = (pcount<8) ? pcount*8+row : pcount*8+row-1;
	}

}

void Adjust_Bounds(void){
	int xc, yc;
	float lx, ty, rx, by;
	float eighth_x, eighth_y;

	lx = win_x-board_x-(win_x-board_x)/2;
	ty = win_y-board_y-(win_y-board_y)/2;
	by = ty + board_y;
	rx = lx + board_x;

	eighth_x = board_x/8;
	eighth_y = board_y/8;
	
	for(xc=0; xc<8; xc++){
		for(yc=0; yc<8; yc++){
			//.x1 .y1 = left bottom  x2 y2 = right top
			bounds[xc][yc].x1 = lx + xc*eighth_x;
			bounds[xc][yc].y1 = by - yc*eighth_y;
			bounds[xc][yc].x2 = lx + eighth_x + xc*eighth_x;
			bounds[xc][yc].y2 = by - eighth_y - yc*eighth_y;
		}
	}
}

void createGLUTMenus(){
	int menu;
	menu = glutCreateMenu(processMenuEvents);
	glutAddMenuEntry("Restart",RESTART);
	glutAddMenuEntry("Pause",PAUSED);
	glutAddMenuEntry("Exit",EXIT);
	glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void processMenuEvents(int option){
	switch (option){
		case RESTART :
			break;
		case PAUSED :
			break;
		case EXIT :
			break;
	}
}

void special (int key, int x, int y){
	switch(key){
		case GLUT_KEY_UP:
			board_y++;
			//x/y = aspect
			board_x = win_aspect*board_y;
			break;
		case GLUT_KEY_DOWN:
			board_y--;
			board_x = win_aspect*board_y;
			break;
		case GLUT_KEY_RIGHT:
			break;
		case GLUT_KEY_LEFT:
			break;
	}
	Adjust_Bounds();
}

void keyboard(unsigned char key, int x, int y){
	if(key==27) //esc key exits
		exit(0);
}

void *Create_OpenGL(void *data){
	win_x = 800;
	win_y = 600;
	board_x = .65*win_x;
	board_y = board_x*win_y/win_x;
	Adjust_Bounds();

	if(!IS_Server){
		Setup_Pieces();
	}else{
		while(my_turn == 0)
			sleep(1);
	}		
	glutInit(&My_argc,My_argv);
	if(my_team_color){
		printf("YOU ARE THE %s team!\n", "black");
	}else{
		printf("YOU ARE THE %s team!\n", "white");
	}
//	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
	glutInitWindowPosition( 0, 0 );
	glutInitWindowSize( win_x, win_y );
	glutCreateWindow( "societychess test version" );
	OpenGLInit();
	glutReshapeFunc( ResizeWindow );
	glutDisplayFunc( Animate);
	glutIgnoreKeyRepeat(0);
	glutSpecialFunc(special);
	glutKeyboardFunc (keyboard);
	createGLUTMenus();
	glutMouseFunc(processMouse);


	glutMainLoop(  );
	//return 0;
	pthread_exit(NULL);
}

void Animate(void){
	sleep(1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//int x, y;
	//float x_trans, y_trans;
	//float x_fourth, y_fourth;
	//float p[6];


	board_x = win_aspect*board_y;
        int xc, yc;
        float lx, ty, rx, by;
        float eighth_x, eighth_y;

        lx = win_x-board_x-(win_x-board_x)/2;
        ty = win_y-board_y-(win_y-board_y)/2;
        by = ty + board_y;
        rx = lx + board_x;

        eighth_x = board_x/8;
        eighth_y = board_y/8;

	glLoadIdentity();
	glColor3f( 0.55, 0.30, 0.30 );
	glRectf(bounds[0][0].x1-eighth_x, bounds[0][0].y1+eighth_y, bounds[7][7].x2+eighth_x, bounds[7][7].y2-eighth_y);

        for(xc=0; xc<8; xc++){
		glLoadIdentity();
		glColor3f(1.0, 0.0, 0.0);
		glRasterPos2f(bounds[xc][0].x1 + eighth_x/2, bounds[0][0].y1 + eighth_y/2);
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, alphabet[xc]);
                for(yc=0; yc<8; yc++){
                        //.x1 .y1 = left bottom  x2 y2 = right top
                        //bounds[xc][yc].x1 = lx + xc*eighth_x;
                        //bounds[xc][yc].y1 = by - yc*eighth_y;
                        //bounds[xc][yc].x2 = lx + eighth_x + xc*eighth_x;
                        //bounds[xc][yc].y2 = by - eighth_y - yc*eighth_y;
			if(xc==0){
				glLoadIdentity();
				glColor3f(1.0, 0.0, 0.0);
				glRasterPos2f(bounds[0][0].x1 - eighth_x/2, bounds[0][yc].y1 - eighth_y/2);
				glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, numbers[yc]);
			}
			glLoadIdentity();
			if( ((yc+xc)%2) == 0){
				glColor3f( 0.45, 0.45, 0.45 );
			}else{
				glColor3f( 0.6, 0.6, 0.6 );
			}
			glBegin(GL_QUADS);
			glVertex3f(bounds[xc][yc].x1, bounds[xc][yc].y2, 0.0f); //top left
			glVertex3f(bounds[xc][yc].x2, bounds[xc][yc].y2, 0.0f); //top right
			glVertex3f(bounds[xc][yc].x2, bounds[xc][yc].y1, 0.0f); //bottom right
			glVertex3f(bounds[xc][yc].x1, bounds[xc][yc].y1, 0.0f); //bottom left
			glEnd();

                }
        }


	//draw the pieces
	for(xc=0; xc<16; xc++){
		if(my_pieces[xc].alive){
			glLoadIdentity();
			if(my_team_color)
				glColor3f(0.0, 0.0, 0.0);
			else
				glColor3f(1.0, 1.0, 1.0);

			glRasterPos2f(bounds[0][0].x1 + eighth_x*my_pieces[xc].position.x + (eighth_x/2), 
			              bounds[0][0].y1 - eighth_y*my_pieces[xc].position.y - (eighth_y/2) );
			switch(my_pieces[xc].type){
				case 0:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'P');
					break;
				case 1:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'R');
					break;
				case 2:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'K');
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'n');
					break;
				case 3:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'B');
					break;
				case 4:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Q');
					break;
				case 5:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'K');
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'i');
					break;
				default:
					break;
			}
		}
		if(oponent_pieces[xc].alive){
			glLoadIdentity();
			if(my_team_color)
				glColor3f(1.0, 1.0, 1.0);
			else
				glColor3f(0.0, 0.0, 0.0);
			glRasterPos2f(bounds[0][0].x1 + eighth_x*oponent_pieces[xc].position.x + (eighth_x/2),
		        	      bounds[0][0].y1 - eighth_y*oponent_pieces[xc].position.y - (eighth_y/2) );
			switch(oponent_pieces[xc].type){
				case 0:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'P');
					break;
				case 1:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'R');
					break;
				case 2:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'K');
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'n');
					break;
				case 3:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'B');
					break;
				case 4:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'Q');
					break;
				case 5:
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'K');
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'i');
					break;
				default:
					break;
			}
		}

	}


//.x1 .y1 = left bottom  x2 y2 = right top

/*
	p[0] = (board_x / win_x)*20*-1;
	p[1] = (board_y / win_y)*20;
	p[2] = (board_x / win_x)*20;
	p[3] = (board_y / win_y)*20*-1;


	glLoadIdentity();
	glColor3f( 0.55, 0.30, 0.30 );
	glRectf(-300, -200, 300, 200); 



        glLoadIdentity();
        glTranslatef ( 0, 0, -24 );
        glColor3f( 0.55, 0.30, 0.30 );
        //glRectf(-0.50f,2.00f, 0.50f, -2.00f);
	glRectf( p[0]-p[2]*.15, p[1]+p[1]*.15, p[2]+p[2]*.15, p[3]-p[1]*.15);


	y_fourth = p[1]/4;
	x_fourth = p[2]/4;
	//sizes for 1 rect
	//p[0] /= 4;
	//p[1] /= 4;

	//backup
	//p[4] = p[2];
	//p[5] = p[3];

	p[2] = p[0]+x_fourth;
	//p[3] /= 4;
	p[1] = p[3]+y_fourth;

        //backup
        p[4] = p[1];
        p[5] = p[3];


	//x_trans = 4*p[0]; 
	//y_trans = 4*p[1];
	
	for(x=0; x<8; x++){
		p[1] = p[4];
		p[3] = p[5];
		//printf chess alphabet notation
		glLoadIdentity();
		glTranslatef ( 0.0, 0.0, -25 );
		glColor3f(1.0, 0.0, 0.0);
		glRasterPos2f(p[0] + x_fourth/2, (board_y / win_y )*20*-1 - y_fourth/2);
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, alphabet[x]);

		for(y=0; y<8; y++){
			if(x==0){
				glLoadIdentity();
				glTranslatef ( 0.0, 0.0, -25 );
				glColor3f(1.0, 0.0, 0.0);
				glRasterPos2f((board_x / win_x )*20*-1 - x_fourth/2,  p[1] - y_fourth/2); 
				glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, numbers[y]);
			}
			glLoadIdentity();
			//glTranslatef ( 0, 0, -24 );
			if( ((y+x)%2) == 0){
				glColor3f( 0.25, 0.25, 0.25 );
			}else{
				glColor3f( 0.9, 0.9, 0.9 );
			}
			//glRectf( p[0], p[3], p[2], p[1]); 

			//bounds[xc][yc].y1

			glBegin(GL_QUADS);
			glVertex3f(p[0], p[3], -24.0f); //top left
			glVertex3f(p[2], p[3], -24.0f); //top right
			glVertex3f(p[2], p[1], -24.0f); //bottom right
			glVertex3f(p[0], p[1], -24.0f); //bottom left
			glEnd();

			//printf("x1: %f y1: %f x2: %f y2: %f\n", p[0], p[3], p[2], p[1]);
			p[1] += y_fourth;
			p[3] += y_fourth;
		}
		p[0] += x_fourth;
		p[2] += x_fourth;
	}




        glLoadIdentity();
        glColor3f( 0.55, 0.30, 0.30 );
        glRectf(-30, -20, 30, 20);
*/


	glFlush();
	glutSwapBuffers();
	glutPostRedisplay();
//exit(1);
}

void OpenGLInit(void)
{
    glShadeModel( GL_SMOOTH );
    glClearColor( 0.0, 0.0, 0.0, 0.0 );
    glDisable( GL_DEPTH_TEST );

//    glDepthFunc(GL_ALWAYS);
//    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//    glEnable( GL_DEPTH_TEST );
//    glClearDepth( 1.0 );
    glClear(GL_COLOR_BUFFER_BIT);
}

void ResizeWindow(int w, int h)
{
	//win_x = w;
	//win_y = h;
    float aspectRatio;
        h = (h == 0) ? 1 : h;
        w = (w == 0) ? 1 : w;
        glViewport( 0, 0, w, h );       // View port uses whole window
        aspectRatio = (float)w/(float)h;
        win_x = w;
        win_y = h;
	//board_x = aspect*win_y;
	//board_y = 
	//board_x = 
	//board_y = 
	
	//board_y = board_x*win_y/win_x;

	//printf("x: %f y: %f\n", win_x, win_y);
	win_aspect = aspectRatio;
	Adjust_Bounds();

    // Set up the projection view matrix (not very well!)
    glMatrixMode( GL_PROJECTION );

    glLoadIdentity();
//    gluPerspective( 70.0, aspectRatio, 1.0, 40.0 );

    // Select the Modelview matrix
glOrtho (0, w, h, 0, 0, 1);
    glMatrixMode( GL_MODELVIEW );
}

