This is meant as a basic tutorial that will teach you how to
do some of the more basic things in ClanLib. This document refers currently to the 0.8
series. We will start off by explaining
the main include statements in ClanLib.
<ClanLib/core.h>
- This must be included when using any part of
clanlib
<ClanLib/application.h>
-
<ClanLib/display.h>
- Needed for any graphics use in ClanLib
<ClanLib/GL.h>
- Used for OpenGL, currently the only hardware
support (DX7 and DX8 are planned)
I’m going to approach this as if
we are going to have 2 files, main.h and main.cpp. Let’s first set up our header file with
our main class
//main.h – has the definition for our main clanapp class
#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/display.h>
#include <ClanLib/gl.h>
class SimpleApp : public
CL_ClanApplication
{
virtual int
main(int, char**);
};
Above we created a class named
SimpleApp that inherits from CL_ClanApplication. Within that class we have a function main(int, char**) which will be the starting function
for your application. Ok so let’s go
on to main.cpp
//main.cpp
#include “main.h”
int SimpleApp::main(int
argc, char **argv)
{
//we need to set up all the
different pieces
CL_SetupCore::init();
CL_SetupApplication::init();
CL_SetupDisplay::init();
CL_SetupGL::init();
//We need to create a window so
people can see what we’re doing
//Tell Clanlib that we will be
drawing in 2D
//Create a CL_Surface
that we will load from file.
CL_Surface
*Image = new CL_Surface("clanlib.tga");
//let’s start our while loop and
draw on the screen
//If escape was hit, we should quit
while(!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
{
//clear the
screen to black
CL_Display::clear();
//Draw our image
on the screen
Image->draw(150, 150);
//flip our screen
so it draws it to the front buffer
CL_Display::flip();
//call keep_alive, the most important call
in any loop
CL_System::keep_alive();
}
delete
Image;
//Tell Clanlib that we are done
drawing in 2D
CL_SetupCore::deinit();
CL_SetupApplication::deinit();
CL_SetupDisplay::deinit();
CL_SetupGL::deinit();
return 0;
}
SimpleApp app;
So let’s go over the separate ClanLib related pieces of app
//we need to set up all the different pieces
CL_SetupCore::init();
CL_SetupApplication::init();
CL_SetupDisplay::init();
CL_SetupGL::init();
First thing we need to do is initialize the different ClanLib subsystems we will use. Basic rule of thumb is for every ClanLib include you will need to do a CL_Setup(subsystem)::init().
//We need to create a window so people can see what we’re doing
CL_DisplayWindow
window("A Simple ClanLib App",
640, 480, false, true);
CL_DisplayWindow creates a window named “A Simple ClanLib App” of specified size (640 x 480 in our case) which is not fullscreen and is resizable. CL_DisplayWindow will be the main window of your app.
Tell Clanlib that we will be drawing in 2D at this point
//Create a CL_Surface that we will load from
file.
CL_Surface *Image = new CL_Surface("clanlib.tga");
CL_Surface is the main class used for loading graphics file and displaying them on screen. CL_Surfaces are created either from resources or a CL_Provider class. The following Providers are available at this time:
CL_PCXProvider
CL_PNGProvider
CL_TGAProvider
CL_JPEGProvider
Feel free to create more.
//clear the screen to black
CL_Display::clear();
Clears the backbuffer to black…
//Draw our image on the screen
Image->draw(150, 150);
Draws our tga file to x:150, y:150 starting from the upper left corner…
//flip our screen so it draws it to the front buffer
CL_Display::.flip();
Draws the image on our screen by moving it from the backbuffer to the front buffer…
//call keep_alive, the most important call in
any loop
CL_System::keep_alive();
Does housecleaning and makes sure our app doesn’t die…
//Tell Clanlib that we are done
drawing in 2D
Tell Clanlib that we are done drawing in 2D at this point
CL_SetupCore::deinit();
CL_SetupApplication::deinit();
CL_SetupDisplay::deinit();
CL_SetupGL::deinit();
And shutdown your subsystems.
SimpleApp app;
This call makes an instance of our class.
That’s it. Next is resources…