Sprites are one of the best features of Clanlib.  They are relatively straightforward and make the animating of characters much easier.

 

First let us create a resources file that includes the sprite information.


 

ship_flame =

(

                                type = sprite,

                                image1 = ship_flame_0001.tga,

                                image2 = ship_flame_0002.tga,

                                image3 = ship_flame_0003.tga,

                                image4 = ship_flame_0004.tga,

                                image5 = ship_flame_0005.tga

 
}

 


 

Ok so let’s go over some of the basics

 

      ship_flame =

 

This is the name of the sprite

 

      type = sprite,

 

Tells us this resource is a sprite (note the “,” after)

 

                image1 = ship_flame_0001.tga,

                image2 = ship_flame_0002.tga,

 

imageX is the order in which you want the images displayed.

Let’s go back to our basic app and add a sprite.


//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**);

 

 };

 


 

In main.h we will add a line a line that creates a CL_Sprite called Flaming_Ship.


//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

               CL_DisplayWindow window("A Simple ClanLib App", 640, 480, false, true);

 

                //Tell Clanlib that we will be drawing in 2D

                CL_Display::begin_2d();

 

                CL_ResourceManager *myResourceManager = new CL_ResourceManager(“resources.scr”, false);

               

                //Setup our Flaming_Ship sprite to use the resource file sprite.

 

                CL_Sprite * Flaming_Ship = new CL_Sprite("ship_flame", myResourceManager);

 

                 //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();

 

                                Flaming_Ship->update();

 

                                Flaming_Ship->draw(315, 235)

                                //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();

               

                }

 

                 //Tell Clanlib that we are done drawing in 2D

                delete Flaming_Ship;

                delete MyResourceManager;

                CL_Display::end_2d();

                CL_SetupCore::deinit();

                CL_SetupApplication::deinit();

                CL_SetupDisplay::deinit();

                CL_SetupGL::deinit();

 

                return 0;

 

}

 

SimpleApp app;


  

                CL_Sprite *Flaming_Ship = new CL_Sprite("ship_flame", myResourceManager);

 

This sets up our sprite using the ship_flame resource we defined earlier in our new resource file

 

                                Flaming_Ship->update();

 

This updates our sprite to the current frame based on a 20 frames per second default.

 

                                Flaming_Ship->draw(315, 235);

 

Draws our sprite on the screen.

 

So as you can see, ClanLib offers you a lot of functionality with very little code.