/******************************************************************/
/* A simple program that loads multiple textures from file, then  */
/* allows the user to choose which one to display using a FLTK    */
/* slider widget inside its own Widget Window.                    */
/*                                                                */
/* Chris Wyman (02/28/2012)                                       */
/******************************************************************/

#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;

// Number of textures we have to display
#define NUM_TEXTURES 6

// A list of our IGLU-managed textures
IGLUTexture2D::Ptr      textures[NUM_TEXTURES];

// An IGLUInt, which acts like an int (i.e., you can use it as an int
//    in an equation) but also allows the interfaces to update it.
IGLUInt                 curTex ( 0, IGLURange<int>(0,NUM_TEXTURES-1), 1, "Current texture:" );

// A list of image files to load.
char fNames[NUM_TEXTURES][128] = {
	"../../CommonSampleFiles/images/dragonCaustic.ppm",
	"../../CommonSampleFiles/images/lake.ppm",
	"../../CommonSampleFiles/images/newZealand_512.ppm",
	"../../CommonSampleFiles/images/niceSunset.ppm",
	"../../CommonSampleFiles/images/ruinedRomanBath.ppm",
	"../../CommonSampleFiles/images/lena512x512.ppm"
};


// The display routine
void display ( void )
{
	// Draw the current texture on screen
	IGLUDraw::Fullscreen( textures[curTex], IGLU_DRAW_FLIP_Y );

	// Print instructions on how to use the IGLUWindow/IGLUWidgetWindow combination
	IGLUDraw::DrawText( IGLU_FONT_VARIABLE, 5, 490, 
		"Click the (+) button to display the widget window!", 0.8 );
}

// Is called upon initialization when the IGLUWindow is ready to allow OpenGL calls
void OpenGLInitialization( void )
{
	for (int i=0; i<NUM_TEXTURES; i++)
		textures[i] = new IGLUTexture2D( fNames[i] );
}

int main(int argc, char** argv)
{
	// Put together our main window
	IGLUWindow::Ptr myWin = new IGLUWindow( 512, 512, "Simple IGLU Example:  Texture Array Generator" );
	myWin->SetWindowProperties( IGLU_WINDOW_NO_RESIZE |	
								IGLU_WINDOW_DOUBLE |
								IGLU_WINDOW_BLEND |
								IGLU_WINDOW_REDRAW_ON_IDLE ); 
	myWin->SetDisplayCallback( display );  
	myWin->SetIdleCallback( IGLUWindow::NullIdle );
	myWin->SetPreprocessOnGLInit( OpenGLInitialization );
	myWin->CreateWindow( argc, argv );     

	// Create a widget window to allow us to interact/change our IGLUVariables
	IGLUWidgetWindow::Ptr uiWin = new IGLUWidgetWindow( 300, 100, "UI Widget Window" );
	uiWin->AddWidget( &curTex );

	// Tell our main window about the widget window... (This allows it to be opened
	//    using the (+) button and perhaps be tied together in other ways.)
	myWin->SetWidgetWindow( uiWin );

	// Start interaction.
	IGLUWindow::Run();
	return 0;
}
