
#include <GL/glew.h>
#include <stdio.h>

// All headers are automatically included from "iglu.h"
#include "iglu.h"

// IGLU classes and constants are all inside the iglu namespace.
using namespace iglu;

// Variable containing the current background color
vec4              bgColor = vec4( 0.0f, 0.0f, 0.0f, 0.0f );
char              stringBuf[2] = { " " };
char              idString[8];
bool              useHex = false;

void display ( void )
{
	// Why isn't there a verctor version of glClearColor()??
	glClearColor( bgColor.X(), bgColor.Y(), bgColor.Z(), bgColor.W() );
	glClear( GL_COLOR_BUFFER_BIT );

	for (int i=0; i<16; i++)
		for (int j=0; j<16; j++)
		{
			// Figure out what character is in this sapace...
			int charID = (15-j)*16 + i;

			// Draw that character
			stringBuf[0] = charID;
			IGLUDraw::DrawText( IGLU_FONT_FIXED, 32*i, 37*j, stringBuf );

			// Print out the numeral identifying that character.
			sprintf( idString, useHex ? "0x%x" : "%d", charID );
			IGLUDraw::DrawColorText( IGLU_FONT_VARIABLE, 32*i, 37*j+25, vec4( 0.6, 0.6, 0.6, 1.0 ), idString, 0.5 );
		}
}

// Allows the user to change background color & toggle between hex/decimal identifiers
void keys(unsigned char key, int x, int y)
{
	switch( key )
	{
	case ' ':  bgColor = color::Black;       break;
	case 'r':  bgColor = color::Red;         break;
	case 'w':  bgColor = color::White;       break;
	case 'b':  bgColor = color::Blue;        break;
	case 'g':  bgColor = color::Green;       break;
	case 'o':  bgColor = color::Orange;      break; 
	case 'y':  bgColor = color::Yellow;      break;
	case 'p':  bgColor = color::Purple;      break;
	case 'c':  bgColor = color::Cyan;        break;
	case 'a':  bgColor = color::Aquamarine;  break;
	case 's':  bgColor = color::Sienna;      break;
	case 't':  bgColor = color::Tan;         break;
	case 'n':  bgColor = color::NavyBlue;    break;
	case 'm':  bgColor = color::Maroon;      break;
	case 'l':  bgColor = color::LimeGreen;   break;
	case 'k':  bgColor = color::Khaki;       break;
	case 'f':  bgColor = color::ForestGreen; break;
	case 'd':  bgColor = color::DarkKhaki;   break;

	case 'h':
	case 'H':
		useHex = !useHex;
		break;

	case 27:
	case 'q':
	case 'Q':
		exit(0);
	}
}

void idle ( void )
{
}

int main(int argc, char** argv)
{
	IGLUWindow *myWin = new IGLUWindow( 512, 592, 
		                                "Simple IGLU Example:  Built-In 'IGLUDraw' Font Display" );
	myWin->SetWindowProperties( IGLU_WINDOW_NO_RESIZE |	
								IGLU_WINDOW_DOUBLE |
								IGLU_WINDOW_BLEND |
								IGLU_WINDOW_REDRAW_ON_IDLE );
	myWin->SetDisplayCallback( display );
	myWin->SetIdleCallback( idle );
	myWin->SetKeyboardCallback( keys );
	myWin->CreateWindow( argc, argv );

	// Start the FLTK interaction loop
	IGLUWindow::Run();
	return 0;
}
