NOGDUS

Articles, Tutorials, and other things. => ClanLib Game Programming => Topic started by: Richard Marks on March 12, 2009, 11:54:55 PM



Title: Getting started with ClanLib - Part I
Post by: Richard Marks on March 12, 2009, 11:54:55 PM
Getting started with ClanLib - Part I

I just got the random urge to try out ClanLib (http://www.clanlib.org/), and following the existing tutorials proved to be tricky.
I like to use SConS as we all know, and well there isn't any good info anywhere on how to build a CL app with it (that I could find)

I'm going to go over just creating a window and having it wait for the ESC key to be pressed.
A simple task, or so it would seem.

First, lets have a look at the SConstruct that we need to build...
Create and open the file for editing in gedit:
Code:
$ touch SConstruct
$ gedit SConstruct &

And we add this to the file:
Code:
#
# SConstruct
# this scons build script produces the executable for the project
################################################################################
# a little preparation for building a ClanLib project
buildEnv = Environment(CCFLAGS = '-g -Wall')
clanLibEnv = Environment()
clanLibEnv.ParseConfig('pkg-config --cflags --libs clanCore-0.8 clanDisplay-0.8 clanApp-0.8 clanGL-0.8')
projectConfig = {}
################################################################################
# this is the file name of the executable file to output
projectConfig['executable'] = 'TestExe'
################################################################################
# if you need to look in special folders for include files add them here
projectConfig['include path'] = Split("""
.
""") + clanLibEnv['CPPPATH']
################################################################################
# if your libs are in special locations set their paths here
projectConfig['library path'] = Split("""
""") + clanLibEnv['LIBPATH']
################################################################################
# if you need to link against other libs, add them here
projectConfig['libraries'] = Split("""

""") + clanLibEnv['LIBS']
################################################################################
# add your sources for your project here
projectConfig['sources'] = Glob('./*.cpp')
################################################################################
buildEnv.Program('release/'+projectConfig['executable'], projectConfig['sources'],
LIBS = projectConfig['libraries'],
LIBPATH = projectConfig['library path'],
CPPPATH = projectConfig['include path'],
LINKFLAGS = clanLibEnv['LINKFLAGS'],
CXXFLAGS = clanLibEnv['CXXFLAGS'] + clanLibEnv['CCFLAGS'])
################################################################################

And now we write the code.
For once I'm not going to dump a ton of code and then say, "here there you go!". ;D

Create and open the main.cpp file:
Code:
$ touch main.cpp
$ gedit main.cpp &

We start with the basic CODESTYLE V2.0 framework:
Code:

// CODESTYLE: v2.0

// main.cpp
// Project: ClanLib Test Project #1 (CLTEST1)
// Author: Richard Marks

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

namespace CLTEST1
{

} // end namespace


Unlike every other program you have written, you will not create a main() function!
Well...not like you think you will..
Every ClanLib application starts with a single instance of a special class called the Application.
The Application class needs to inherit from the ClanLib CL_ClanApplication class.

You need to declare main() as a virtual public method that takes the int and char-pointer-pointer vars that are so common.

And now inside the namespace's curly braces, we start our code:

Code:
class CLTest1Application : public CL_ClanApplication
{
public:
virtual int main(int argc, char** argv);
}; // end class

That is too easy. :D

Just underneath that code, we will write the implementation.

Code:
int CLTest1Application::main(int argc, char** argv)
{
// setup std i/o redirecting to a console window
CL_ConsoleWindow console("Debug Console");
console.redirect_stdio();

try
{
// setup our modules we are using
CL_SetupCore setupCore;
CL_SetupDisplay setupDisplay;
CL_SetupGL setupGL;

// create the main window
CL_DisplayWindow mainWindow("ClanLib Test Project #1", 800, 600);

// enter the main loop that exits on ESC
while(!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
{
// clear to a nice shade of blue
CL_Display::clear(CL_Color(69, 127, 230));

// flip double buffer
CL_Display::flip();

// rest the CPU ten milliseconds and do OS house-keeping
CL_System::keep_alive(10);
}
}
catch(CL_Error error)
{
std::cout << error.message.c_str() << std::endl;
}

// wait for a keypress to close the console
console.display_close_message();

// return success
return 0;
}

Okay, its commented so you should understand it...If not, ask me or read the ClanLib documentation. :D

last thing we need is our instance

Code:
// create the application instance
CLTEST1::CLTest1Application globalApplicationInstance;

Save.

build with scons:
Code:
$ scons

and run:
Code:
$ ./TestExe

Thanks for reading! 8)