Resources are a main part of any game and ClanLib has a system that makes resources easy to use.  Let’s go back to the code from the first tutorial and modify it so that it uses resources.

 

First I will create a file “resources.scr” which contains the following

 


section Surfaces
{
                        my_surface = clanlib.tga (type=surface);
}

 


The Resource Overview provides a good explanation of how to create a resource file but I will cover some basics here

 

section Surfaces

 

You can separate your resources into different sections for easier organization.  Sections may also have subsections.

 

my_surface = clanlib.tga (type=surface);

 

Creates a resource called my_surface.


 

//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 defining a CL_ResourceManager who’s main job is to load resources and make them available to objects such as a CL_Surface

 


 

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

               

                //Create a CL_Surface that we will load from file.

                CL_Surface *Image = new CL_Surface(“Surfaces\my_Surfaces”, 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();

 

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

                delete myResourceManager;

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

                CL_Display::end_2d();

                CL_SetupCore::deinit();

                CL_SetupApplication::deinit();

                CL_SetupDisplay::deinit();

                CL_SetupGL::deinit();

                 return 0;

 

}

 

SimpleApp app;


                 //Create and setup our resource manager to look at a file named “resources.scr

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

 

This creates a CL_ResourceManager that will load and parse the file “resources.scr”.  This file is not a compressed file which is what the false parameter is.

Compressed files hold the resources and usually have the extension .dat files. Non-compressed files commonly have .scr extensions.

 

                //Create a CL_Surface that we will load from file.

                CL_Surface *Image = new CL_Surface(“Surfaces\my_Surfaces”, myResourceManager );

 

We load our surface now from the CL_ResourceManager instead of directly from disk.  This specifies that we want to load the my_Surfaces CL_Surface from the Surfaces section.

 

Next we will do Sprites…