/*
	The minimum requirements needed by every ClanLib application.

	Note that the try/catch is not required but should be added 
	to every ClanLib application.
*/

#include <ClanLib/core.h>
#include <ClanLib/png.h>
#include <ClanLib/display.h>
#include <ClanLib/application.h>

#include "rect.h"
#include "collision.h"

class MinimumApp : public CL_ClanApplication
{
public:
	virtual char *get_title() { return "Minimum application"; }

	virtual int main(int, char **)
	{
		// Create a console window for text-output if not available
		CL_ConsoleWindow console("Console");
		console.redirect_stdio();
		
		try
		{		      
		  CL_SetupCore::init();
		  CL_SetupDisplay::init();
		  CL_SetupPNG::init();
		  
		  CL_Display::set_videomode(640,480,32,false);
		  
		  CL_Surface* sur1 = new CL_Surface(new CL_PNGProvider ("sur1.png"));
		  CL_Surface* sur2 = new CL_Surface(new CL_PNGProvider ("sur2.png"));

		  bool collision = false;
		  CL_System::keep_alive ();
		  while (!CL_Keyboard::get_keycode (CL_KEY_ESCAPE))
		    {
		      if (collision)
			CL_Display::clear_display(1.0, 0, 0);
		      else
			CL_Display::clear_display(0, 1.0, 0);

		      sur1->put_screen (100, 100);
		      sur2->put_screen (CL_Mouse::get_x (), CL_Mouse::get_y ());

		      unsigned int time = CL_System::get_time ();
		      for (int i = 0; i < 100; ++i)
		      collision = RaceCollision::check (sur1, sur2, 100, 100,
							CL_Mouse::get_x (), CL_Mouse::get_y ());

		      /* 256x256 - 128x128 = 0.5msec worst case
			 1000Mhz
		       */
		      std::cout << "Time: " << CL_System::get_time () - time << std::endl;

		      CL_Display::flip_display();
		      CL_System::keep_alive ();
		    }

		  CL_SetupPNG::deinit();
		  CL_SetupDisplay::deinit();
		  CL_SetupCore::deinit();
		}
		catch (CL_Error err)
		{
			std::cout << "Exception caught: " << err.message.c_str() << std::endl;

			// Display console close message and wait for a key
			console.display_close_message();
		}

		return 0;
	}
} app;
