ClanLib SDK

Display "Hello World" Example

A very simple example using clanCore, clanApplication, clanDisplay and clanGL:

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

class DisplayProgram
{
public:
	static int main(const std::vector<CL_String> &args);
};

// Create global application object:
// You MUST include this line or the application start-up will fail to
// locate your application object.
CL_ClanApplication app(&DisplayProgram::main);

int DisplayProgram::main(const std::vector<CL_String> &args)
{
	// Setup clanlib modules:
	CL_SetupCore setup_core;
	CL_SetupDisplay setup_display;
	CL_SetupGL setup_gl;
	
	try
	{
		// Create a window:
		CL_DisplayWindow window("Hello World", 640, 480);
	
		// Retrieve some commonly used objects:
		CL_GraphicContext gc = window.get_gc();
		CL_InputDevice keyboard = window.get_ic().get_keyboard();
		CL_Font font(gc, "Tahoma", 30);
	
		// Loop until user hits escape:
		while (!keyboard.get_keycode(CL_KEY_ESCAPE))
		{
			// Draw some text and lines:
			gc.clear(CL_Colorf::cadetblue);

			CL_Draw::line(gc, 0, 110, 640, 110, CL_Colorf::yellow);
			font.draw_text(gc, 100, 100, "Hello World!", CL_Colorf::lightseagreen);
		
			// Make the stuff visible:
			window.flip();
	
			// Read messages from the windowing system message queue,
			// if any are available:
			CL_KeepAlive::process();
		
			// Avoid using 100% CPU in the loop:
			CL_System::sleep(10);
		}
	}
	catch(CL_Exception &exception)
	{
		// Create a console window for text-output if not available
		CL_ConsoleWindow console("Console", 80, 160);
		CL_Console::write_line("Exception caught: " + exception.get_message_and_stack_trace());
		console.display_close_message();

		return -1;
	}
	
	return 0;
}

To build this example with Visual C++:

  1. Create a new Win32 project.
  2. Add the source file to the project.
  3. Change the threading model to Multithreaded Debug (or just Multithreaded for release builds) in the project settings' C/C++ Code Generation section.
  4. Change the character set to Multi-Byte Character Set in the project settings General section, if you did not build the Unicode version of ClanLib.