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

class SurfaceApp : public CL_ClanApplication
{
public:
	virtual int main(int argc, char **argv)
	{
		// Create a console window for text-output if not available
		CL_ConsoleWindow console("Console");
		console.redirect_stdio();

		try
		{
			std::string file1("images/triangle.png");
			std::string file2("images/weird.png");

			if( argc == 3 )
			{
				file1 = argv[1];
				file2 = argv[2];
			}

			CL_SetupCore::init();
			CL_SetupDisplay::init();
			CL_SetupGL::init();
			
			CL_DisplayWindow window("Surface outline", 800, 600);
			
			int x_pos = 0;
			int y_pos = 0;
			
			draw_limit = 0;

			////////////////////////////////////////////
			// load resources:

			CL_Surface surface(file1);
			CL_Surface surface2(file2);

			surface.set_rotation_hotspot(origin_center);
			surface.set_alignment(origin_center);

			surface2.set_rotation_hotspot(origin_center);
			surface2.set_alignment(origin_center);
			
			////////////////////////////////////////////
			// Collision code
			
			CL_CollisionOutline co1(file1);
			CL_CollisionOutline co2(file2);
			
			co1.set_alignment(origin_center);
			co1.set_rotation_hotspot(origin_center);
			co2.set_alignment(origin_center);
			co2.set_rotation_hotspot(origin_center);

			co1.set_inside_test(true);

			// Loop until the user hits escape:
			while (CL_Keyboard::get_keycode(CL_KEY_ESCAPE) == false)
			{
				CL_Display::clear(CL_Color::ghostwhite);

				if( CL_Keyboard::get_keycode(CL_KEY_RIGHT) )
					x_pos+=2;
				if( CL_Keyboard::get_keycode(CL_KEY_LEFT) )
					x_pos-=2;

				if( CL_Keyboard::get_keycode(CL_KEY_UP) )
					y_pos-=2;
				if( CL_Keyboard::get_keycode(CL_KEY_DOWN) )
					y_pos+=2;

				// -----------------------------------
				// co1

				surface.rotate(0.1f);

				co1.set_translation((float)x_pos, (float)y_pos);
				co1.rotate(0.1f);
				draw_outline(co1, CL_Color::limegreen);

				// -----------------------------------
				// co2 

//				surface2.set_scale(1.5f, 1.5f);
				surface2.rotate(0.1f);

				co2.set_translation(CL_Display::get_width() / 2.0f, CL_Display::get_height() / 2.0f);
//				co2.set_scale(1.5f, 1.5f);
				co2.rotate(0.1f);
				draw_outline(co2, CL_Color::red );

				// -----------------------------------
				// collision testing
				if( co1.collide(co2) )
				{
					CL_Display::fill_rect(CL_Rect(0,0,800,600), CL_Color(55,40,250,20));
				}

				surface.draw(x_pos,y_pos);
				surface2.draw(CL_Display::get_width()/2, CL_Display::get_height()/2);

				// Flip front and backbuffer. This makes the changes visible:
				CL_Display::flip();

				// Update keyboard input and handle system events:
				CL_System::keep_alive(10);
			}

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

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

		return 0;
	}

	void draw_outline( CL_CollisionOutline &outline, const CL_Color &color )
	{
		unsigned int num_points = 0;

		std::vector<CL_Contour>::const_iterator it;
		for( it = outline.get_contours().begin(); it != outline.get_contours().end(); ++it )
		{
			const std::vector<CL_Pointf> &points = (*it).points;
			
			for( unsigned int i = 0; i < points.size()-1; i++ )
			{
				// uncomment to show one line at a time, stepping using space
//				if( i != draw_limit ) continue;

				const CL_Pointf &p1 = points[i];
				const CL_Pointf &p2 = points[i+1];
				CL_Display::draw_line( int(p1.x+0.5f), int(p1.y+0.5f), int(p2.x+0.5f), int(p2.y+0.5f), color);
			}
			
			const CL_Pointf &p1 = points.front(); // [0];
			const CL_Pointf &p2 = points.back(); // [points.size()-1];
			
			CL_Display::draw_line( int(p1.x+0.5f), int(p1.y+0.5f), int(p2.x+0.5f), int(p2.y+0.5f), color );

			num_points += points.size();
		}
		
		if( CL_Keyboard::get_keycode(CL_KEY_F1) )
			std::cout << "num points: " << num_points << std::endl;

		if( CL_Keyboard::get_keycode(CL_KEY_SPACE) )
		{
			draw_limit++;
			while( CL_Keyboard::get_keycode(CL_KEY_SPACE) )
				CL_System::keep_alive(10);
		}

		if( draw_limit >= (num_points -1) ) draw_limit = 0;
	}

	unsigned int draw_limit;

} app;
