#include <allegro5/allegro.h>
#include <GL/glu.h>
#include <iostream>
#include <stdio.h>
#define displayHeight 400
#define displayWidth 600
const float FPS = 60.0;
float angle = 0.0;

char* vertexSource =
	"#version 150 core\n "
	"in vec2 position; "
	"in vec3 ColorIn; "
	"out vec3 ColorFragment; "
	"void main()"
	"{"
		"ColorFragment = ColorIn;"
		"gl_Position = vec4(position, 0.0, 1.0);"
	"}";

void DrawCube(float size)
{
	glLoadIdentity();
	// Reset The Current Modelview Matrix
	glTranslatef(1.5f,0.0f,-13.0f);
	// Move Right 1.5 Units And Into The Screen 7.0
	glRotatef(angle,1.0f,1.0f,1.0f);
	// Rotate The Quad On The X axis ( NEW )
	glBegin(GL_QUADS); // Draw A Quad

	glColor3f(1.0f,1.0f,1.0f);
	// Set The Color To Green

	glVertex3f( 1.0f, 1.0f,-1.0f);
	// Top Right Of The Quad (Top)
	glVertex3f(-1.0f, 1.0f,-1.0f);
	// Top Left Of The Quad (Top)
	glVertex3f(-1.0f, 1.0f, 1.0f);
	// Bottom Left Of The Quad (Top)
	glVertex3f( 1.0f, 1.0f, 1.0f);
	// Bottom Right Of The Quad (Top)
	glColor3f(1.0f,0.5f,0.0f);
	// Set The Color To Orange
	glVertex3f( 1.0f,-1.0f, 1.0f);
	// Top Right Of The Quad (Bottom)
	glVertex3f(-1.0f,-1.0f, 1.0f);
	// Top Left Of The Quad (Bottom)
	glVertex3f(-1.0f,-1.0f,-1.0f);
	// Bottom Left Of The Quad (Bottom)
	glVertex3f( 1.0f,-1.0f,-1.0f);
	// Bottom Right Of The Quad (Bottom)
	glColor3f(1.0f,0.0f,0.0f);
	// Set The Color To Red

	glVertex3f( 1.0f, 1.0f, 1.0f);
	// Top Right Of The Quad (Front)
	glVertex3f(-1.0f, 1.0f, 1.0f);
	// Top Left Of The Quad (Front)
	glVertex3f(-1.0f,-1.0f, 1.0f);
	// Bottom Left Of The Quad (Front)
	glVertex3f( 1.0f,-1.0f, 1.0f);
	// Bottom Right Of The Quad (Front)
	glColor3f(255.0f,255.0f,0.0f);
	// Set The Color To Yellow

	glVertex3f( 1.0f,-1.0f,-1.0f);
	// Top Right Of The Quad (Back)
	glVertex3f(-1.0f,-1.0f,-1.0f);
	// Top Left Of The Quad (Back)
	glVertex3f(-1.0f, 1.0f,-1.0f);
	// Bottom Left Of The Quad (Back)
	glVertex3f( 1.0f, 1.0f,-1.0f);
	// Bottom Right Of The Quad (Back)
	glColor3f(0.0f,0.0f,1.0f);
	// Set The Color To Blue

	glVertex3f(-1.0f, 1.0f, 1.0f);
	// Top Right Of The Quad (Left)
	glVertex3f(-1.0f, 1.0f,-1.0f);
	// Top Left Of The Quad (Left)
	glVertex3f(-1.0f,-1.0f,-1.0f);
	// Bottom Left Of The Quad (Left)
	glVertex3f(-1.0f,-1.0f, 1.0f);
	// Bottom Right Of The Quad (Left)
	glColor3f(1.0f,0.0f,1.0f);
	// Set The Color To Violet

	glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
	glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
	glVertex3f( 1.0f,-1.0f, 1.0f);	// Bottom Left Of The Quad (Right)
	glVertex3f( 1.0f,-1.0f,-1.0f);	// Bottom Right Of The Quad (Right)


	glEnd(); angle += 0.25;
}
void Init_Opengl() {
	glViewport(0,0,600,400);
	// Reset The Current Viewport
	glMatrixMode(GL_PROJECTION);
	// Select The Projection Matrix
	glLoadIdentity();
	// Reset The Projection Matrix
	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)600/(GLfloat)400,0.1f,100.0f);
	glMatrixMode(GL_MODELVIEW);
	// Select The Modelview Matrix
	glLoadIdentity();
	glShadeModel(GL_SMOOTH);
	// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
	// Black Background
	glClearDepth(1.0f);
	// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);
	// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);
	// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void RenderScene() {
	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(0.0,0.0,-5.0);
	glRotatef(angle,1.0,1.0,1.0); DrawCube(1.0);
}

int main(int argc, char *argv[])
{
	float displayDimensions[] = { 600, 400 };
	float moveSpeed = 1.0;
	al_init();
	al_install_keyboard();
	al_set_new_display_flags(ALLEGRO_OPENGL| ALLEGRO_RESIZABLE);
	ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
	ALLEGRO_DISPLAY *display = al_create_display(600, 400);
	ALLEGRO_KEYBOARD_STATE key_state;
	ALLEGRO_TIMER *baseTimer = al_create_timer(1 / FPS);
	al_register_event_source(event_queue, al_get_display_event_source(display));
	al_register_event_source(event_queue, al_get_keyboard_event_source());
	al_register_event_source(event_queue, al_get_timer_event_source(baseTimer));
	bool done = false, draw = true;
	Init_Opengl();
	al_start_timer(baseTimer);
	while(done != true) {
		ALLEGRO_EVENT events;
		al_wait_for_event(event_queue, &events);
		if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
			done = true;
		if(events.type == ALLEGRO_EVENT_TIMER)
		{
			al_get_keyboard_state(&key_state);
			if(al_key_down(&key_state, ALLEGRO_KEY_ESCAPE))
				done = true; draw = true;

		}
		if(draw == true) {
			std::cout << "drawing" << std::endl;
			RenderScene(); al_flip_display();

		}

	}
	al_destroy_display(display);
	al_destroy_event_queue(event_queue);
	al_destroy_timer(baseTimer);
	printf("%s", vertexSource);
	return 0;
}
