/*
	Init display example.
*/

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

class InitDisplayApp : public CL_ClanApplication
{
public:
	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_SetupGL::init();

			CL_DisplayWindow display("Init Display example", 640, 480);

			// Loop until the user hits escape:
			while (CL_Keyboard::get_keycode(CL_KEY_ESCAPE) == false)
			{
				// Clear display with random colors:
				CL_Display::clear(CL_Color(
					rand() % 255,  // red component
					rand() % 255,  // green component
					rand() % 255)); // blue component

				CL_Display::draw_line(
					rand() % 2000 - 1000,
					rand() % 2000 - 1000,
					rand() % 2000 - 1000,
					rand() % 2000 - 1000, CL_Color::black);

				// Flip front and backbuffer. This makes the changes visible:
				CL_Display::flip();
			
				// Update keyboard input and handle system events:
				CL_System::keep_alive();
			}

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

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

		return 0;
	}
} app;
