#include <cstdlib>

#include <GL/gl.h>
#include <GL/glut.h>

int Width = 500;
int Height = 500;
int CubeSize = 200;

void Display()
{
	int left = (Width - CubeSize) / 2;
	int right = left + CubeSize;

	int bottom = (Height - CubeSize) / 2;
	int top = bottom + CubeSize;

	glClearColor(0, 0, 0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);

	glBegin(GL_QUADS);
		glColor3ub(255, 0, 0);
		glVertex2f(left, bottom);

		glColor3ub(0, 255, 0);
		glVertex2f(left, top);

		glColor3ub(0, 0, 255);
		glVertex2f(right, top);

		glColor3ub(255, 255, 255);
		glVertex2f(right, bottom);
	glEnd();

	glFinish();
}

void Reshape(int w, int h)
{
	Width = w;
	Height = h;

	glViewport(0, 0, w, h);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, w, 0, h, -1.0, 1.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void Keyboard(unsigned char key, int x, int y)
{
	const char ESCAPE = '\033';
	if (key == ESCAPE)
		exit(0);
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB);
	glutInitWindowSize(Width, Height);
	glutCreateWindow("Just Another Square Example");

	glutDisplayFunc(Display);
	glutReshapeFunc(Reshape);
	glutKeyboardFunc(Keyboard);

	glutMainLoop();
	return 0;
}
