#include <ClanLib/display.h>
#include <ClanLib/sound.h>
#include <ClanLib/gl.h>

#include "app.h"
#include "world.h"

Application app;

int Application::main(int argc, char** argv)
{
	#ifdef _DEBUG
	// Create a console window for text-output if not available
	CL_ConsoleWindow console("ClanLib RTS Demo", 80, 1000); // 1000 allows a y-scrollbar to be present
	console.redirect_stdio();
	#endif
	
	try
	{
		// Init stuff
		CL_SetupCore::init();
		CL_SetupDisplay::init();
		CL_SetupSound::init();
		CL_SetupGL::init();
		
		// Create a window
//		#ifdef _DEBUG
		CL_DisplayWindow window("ClanLib RTS Demo", 1024, 768, false);
//		#else
//		CL_DisplayWindow window("ClanLib RTS Demo", 1024, 768, true);
//		#endif

		CL_SoundOutput output(44100);

		// Create world
		World world;

		// Run the main loop
		world.run();

		// Deinit stuff
		CL_SetupGL::deinit();
		CL_SetupDisplay::deinit();
		CL_SetupSound::deinit();
		CL_SetupCore::deinit();
	}
	catch (CL_Error e)
	{
		std::cout << e.message.c_str() << std::endl;

		#ifdef _DEBUG
		console.display_close_message();
		#endif
	}

	return 0;
}
