Home - Forums - Documentation - Gallery - Bugs

Step Two : Basic configuration handling

First, from that point we will use more and more cim-specific files, like pictures, map files, music, sound etc. It this tutorial we load the first version of cim’s configuration file.

The configuration handling is Crystal Space is very easy to use. Each plugin has its own configuration file, you can override the values with your own configuration file. You can add or override many options from command line. The manual describes this mechanism in depth, please read this section for details.

You can configure many things from your configuration file. We can load plugins from the configuration file, this is a very nice feature. You can dynamically change plugins without recompiling your application. For example, the cim.cfg (config file) has this line somewhere:

System.Plugins.iConsoleOutput = crystalspace.console.output.standard

That means, Crystal Space will use the standard output plugin. When you change the line a little bit:

System.Plugins.iConsoleOutput = crystalspace.console.output.fancy

In that case the fancy console output awaits you, when you run the application (Note, we will change the console source a little bit., now it’s s load the standard plugin statically. When you edit the walktest.cfg file, in the /data/config directory , you can see the effect in walktest.).

You can override any settings of the official CS plugins. For example:

Video.ScreenWidth = 800
Video.ScreenHeight = 600
Video.ScreenDepth = 32
Video.FullScreen = yes

When your configuration file has this lines, your app will start at 800*600 resolution, in fullscreen. You can change any system configuration key, by adding the name and the value in the config file.

Note that the vfs.cfg is a standard configuration file but has it's own configuration space. So if you want to mount directories from your application configuration file you will need to write some code to process the VFS.Mount entries.

So you have to put an vfs.cfg file in the same directory, when your exe exists, with the following context:

VFS.Mount.cim = $^$/

will mount your real cim.exe/ directory to /cim/ the virtual path. This solution prevents having to rewrite the original vfs.cfg.

Since vfs.cfg is just a normal configuration file you can add your own configuration keys. Cim will use some of its own keys, for example:

Cim.Settings.StartLevel = terrain
Cim.Settings.EnableConsole = true;

We have to handle these config keys from code, of course.

The whole cim.cfg will look like this:

;mounting cim working folder
;VFS.Mount.lev/cim   = $@data$/cim$/

;cs plugins
System.Plugins.iGraphics3D = crystalspace.graphics3d.opengl
System.Plugins.iConsoleInput = crystalspace.console.input.standard
System.Plugins.iConsoleOutput = crystalspace.console.output.standard
System.Plugins.iImageIO = crystalspace.graphic.image.io.multiplexer
System.Plugins.iLoader = crystalspace.level.loader
System.Plugins.iDocumentSystem = crystalspace.documentsystem.xmlread
System.Plugins.iEngine = crystalspace.engine.3d
System.ApplicationID = Tutorial.Cim

;video settings
Video.ScreenWidth = 1024
Video.ScreenHeight = 768
Video.ScreenDepth = 32
Video.FullScreen = no

;mouse settings
MouseDriver.DoubleClickTime = 300
MouseDriver.DoubleClickDist = 2

;game specific settings
Cim.Settings.StartLevel = /lev/terrainf 
Cim.Settings.EnableConsole = false

This file you have to put into your CS/config/ directory.

To load a config file, first we need a pointer to the config manager. Add the following private member to cim class in cim.h:

    /// A pointer to the configuration manager.
    csRef<iConfigManager> confman;

And in the cim.cpp, on the OnInitialize function before you call RequestPlugins:

    if (!csInitializer::SetupConfigManager (GetObjectRegistry (),
        return ReportError ("Error reading config file 'cim.cfg'!");

Your config file will be loaded and your settings will be applied. Put cim.cfg in same directory, that your cim binary, recompile the application and run it. Try different Video.ScreenWidth Video.ScreenHeight, etc values (i.e 800*600, 1024*768), and you can see, Crystal Space respects your changes. This the real power of Crystal Space configuration handling.

Our config file contents the following Cim-specific keys:

Cim.Settings.StartLevel = terrain;
Cim.Settings.EnableConsole = true;

The StartLevel defines the name of the starting map, the EnableConsole decides, the app creates a console or not.

Let’s now write the code. We make many changes. First, we introduce a variable to check whether we use console or not. This will a private member of the cimGame class, called use_console. When this flag is false, we don’t instance and use the console class. The event handlers first check, we have console or not, to avoid crashes :

bool cimGame::OnKeyboard(iEvent& ev)
    // We got a keyboard event.
    csKeyEventType eventtype = csKeyEventHelper::GetEventType(&ev);
    if (eventtype == csKeyEventTypeDown)
        // The user pressed a key (as opposed to releasing it).
        utf32_char code = csKeyEventHelper::GetCookedCode(&ev);
	//The user pressed tab to toggle console, when we have any console
	if (use_console)
	if (code == CSKEY_TAB)
	    return false;
    //more code of course 

(This is a very simple solution,. Cs provides us a better way to event handling, we will deal with this problem later.)

On the cimGame::Setup, we do the following changes:

We have to change the ProcessFrame function from same reason:

void cimGame::ProcessFrame ()
    // First get elapsed time from the virtual clock.
    csTicks elapsed_time = vc->GetElapsedTicks ();

    csVector3 obj_move (0);
    csVector3 obj_rotate (0);

    if (use_console)
        if(console->IsVisible() )
    // more code

We change the our console class a little bit. First, we change the plugin loading method, we don’t use concrete plugin name, since we give the used plugins from the configuration file:

bool cimConsole::Initialize(iObjectRegistry* obj_reg)
    object_reg = obj_reg;

    conout = CS_QUERY_REGISTRY(obj_reg, iConsoleOutput);
    if (!conout)
        csReport (object_reg,
	    	CS_REPORTER_SEVERITY_ERROR, "cim.console",
		"Can't load the output console!");
        return false;

    conin = CS_QUERY_REGISTRY(obj_reg,iConsoleInput);
    if (!conin)
        csReport (object_reg,
	    	CS_REPORTER_SEVERITY_ERROR, "cim.console",
		"Can't load the input console!");
        return false;

    //more code

In the cimGame class, we introduce a new private function, called LoadConfig. This function parse the application’s special konfiguration keys, and makes the desired settings.

The configuration manager has some GetSomeType function to get the key values, these are GetInt. GetStr, GetBool, GetFloat. These functions look up in the configuration file, and return with the given key’s value (first parameter). You can give a default value, when the key was not found, in the second parameter.

bool cimGame::LoadConfig()
    csRef<iConfigManager> confman (CS_QUERY_REGISTRY (GetObjectRegistry(), iConfigManager));
    use_console = confman->GetBool("Cim.Settings.EnableConsole ", true);
    startmap = confman->GetStr("Cim.Settings.StartLevel","terrain");

Finally, we learn the basics of the command line handling in CS. Cystal Space heavily uses command lines and provides an easy way to process it by a CS based application. The iCommandLineParser helps to do it. So we extend the cimGame::Setup function:

    csRef<iCommandLineParser> cmdline(CS_QUERY_REGISTRY (object_reg,
    if (!cmdline) 
        return ReportError("Failed to set up command line parser!");

Then we look up the option enable-python-console. When the user runs the application with this option, we set up the console anyway:

    if( cmdline->GetOption("enable-python-console")) use_console=true;

Much more is possible with the command line parser, but probably this function you will use most often.

And finally, we organize the map loading and collosion detection init code to a new LoadMap (const char* mapname) function. This function can load any CS map file by name:

bool cimGame::LoadMap (const char* mapname)
    //Delete the engine's content.

    // Set VFS current directory to the level we want to load.
    csRef<iVFS> VFS (CS_QUERY_REGISTRY (GetObjectRegistry (), iVFS));
    VFS->ChDir (mapname);
    // Load the level file which is called 'world'.
    if (!loader->LoadMapFile ("world"))
        ReportError("Error couldn't load level!");

    // Initialize collision objects for all loaded objects.
    csColliderHelper::InitializeCollisionWrappers (cdsys, engine);
    // Let the engine prepare all lightmaps for use and also free all images 
    // that were loaded for the texture manager.
    engine->Prepare ();

    // Find the starting position in this level.
    csVector3 pos (0);
    if (engine->GetCameraPositions ()->GetCount () > 0)
        // There is a valid starting position defined in the level file.
        iCameraPosition* campos = engine->GetCameraPositions ()->Get (0);
        room = engine->GetSectors ()->FindByName (campos->GetSector ());
        pos = campos->GetPosition ();
        // We didn't find a valid starting position. So we default
        // to going to room called 'room' at position (0,0,0).
        room = engine->GetSectors ()->FindByName ("room");
        pos = csVector3 (0, 0, 0);
    if (!room)
        ReportError("Can't find a valid starting position!");

    // Now we need to position the camera in our world.
    view->GetCamera ()->SetSector (room);
    view->GetCamera ()->GetTransform ().SetOrigin (pos);

    // Initialize our collider actor.
    collider_actor.SetCollideSystem (cdsys);
    collider_actor.SetEngine (engine);
    csVector3 legs (.2f, .3f, .2f);
    csVector3 body (.2f, 1.2f, .2f);
    csVector3 shift (0, -1, 0);
    collider_actor.InitializeColliders (view->GetCamera (),
  	legs, body, shift);

    return true;

Look at the source for all changes.

Thats it. We can configure CS engine and we can add more cim-specific option later. In the next step we will create the basement of our window handling solution.

Get the sources from here

Tutorial Home

Next: Step3 - Event handling in CS

Retrieved from "/main/Cimstep2"
| Article | Discussion | View source | History |