#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>     // openGL header
#include <GL/glu.h>   // glut header
#include <GL/glut.h>   // glut header



bool fullscreen = false;        // Keeps track of window mode
int drawshape = 0;              // Which shape to draw -> both when 0, triangle when 1, quad when 2


//----------------------------------------------//
// Display                                      //
//  - clears the screen                         //
//  - draws the current scene and swaps buffers //
//----------------------------------------------//
void display() {
	glClear(GL_COLOR_BUFFER_BIT);							// Clear Screen

	if (drawshape==0 || drawshape==1) {
		glBegin(GL_TRIANGLES);                              // Draw triangle
			glVertex3f(-0.5f, 0.5f, 0.0f);                   // Top
			glVertex3f(-1.0f,-0.5f, 0.0f);                   // Bottom left
			glVertex3f( 0.0f,-0.5f, 0.0f);                   // Bottom right
		glEnd();                                            // End triangle
	}
	
	if (drawshape==0 || drawshape==2) {
		glBegin(GL_QUADS);                                  // Draw quad
			glVertex3f( 0.25f, 0.5f, 0.0f);                  // Top left
			glVertex3f( 0.75f, 0.5f, 0.0f);                  // Top right
			glVertex3f( 0.75f,-0.5f, 0.0f);                  // Bottom right
			glVertex3f( 0.25f,-0.5f, 0.0f);                  // Bottom left
		glEnd();                                            // End quad
	}
	
	glFlush();
}


//------------------------------------------------------//
// Normal_Keys                                          //
//  - handles all "normal" keypresses (with ascii code) //
//------------------------------------------------------//
void normal_keys(unsigned char key, int x, int y) {
	if (key==27) exit(0);			// Escape exits the program
}


//-----------------------------------------------------//
// Special_Keys                                        //
//  - handles all "special" keypresses (no ascii code) //
//-----------------------------------------------------//
void special_keys(int keys, int x, int y) {
	switch(keys) {
		case GLUT_KEY_F1:       // F1 toggles fullscreen mode
			fullscreen = !fullscreen;
			if (fullscreen) glutFullScreen();
			else glutReshapeWindow(500,500);
			break;
		default:
			break;
	}
}


//----------------------------------------------------//
// Mouse_Button                                       //
//  - a mouse button was pressed or released          //
//  - cycle the drawshape flag if left button pressed //
//----------------------------------------------------//
void mouse_button(int button, int state, int x, int y) {
	if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) {
		drawshape = (drawshape+1)%3;
		display();
	}
}


//--------------------------------------//
// Main                                 //
//  - inits glut and creates the window //
//  - assigns functions to glut         //
//  - starts the main rendering loop    //
//--------------------------------------//
main(int argc, char** argv) {
	glutInit(&argc,argv);                           // Initialize glut
	glutInitDisplayMode(GLUT_RGB);					// Set the display mode
	glutInitWindowSize(500,500);                    // Set the window size
	glutCreateWindow("Homework 1");                 // Create the window with given title
	glutDisplayFunc(display);                       // Set the display function
	glutKeyboardFunc(normal_keys);                  // Set the normal keyboard function
	glutSpecialFunc(special_keys);                  // Set the special keyboard function
	glutMouseFunc(mouse_button);                    // Set the mouse button function
	glutMainLoop();                                 // Initialize main loop
}
