1.9 Loading from a config file

By Ryan Murphy, ramurphy100@hotmail.com.

Reading in drivers and setting from a config file can make your program much easier and more modular. In Crystal Space this can be done easily. The first part is to create a config file, which is placed in the '/data/config/' directory.

Your config file might look something like this:
NOTE: Lines beginning with ';' are comments, and the config manager will skip over these.

---------------------------------------------------------
; First of all, the Virtual File System plug-in
System.Plugins.iVFS = crystalspace.kernel.vfs

; Video driver
;System.Plugins.iGraphics3D = crystalspace.graphics3d.software
System.Plugins.iGraphics3D = crystalspace.graphics3d.opengl

; The console
;System.Plugins.iConsoleInput = crystalspace.console.input.standard
;System.Plugins.iConsoleOutput = crystalspace.console.output.simple
;System.Plugins.iConsoleOutput = crystalspace.console.output.standard
System.Plugins.iConsoleOutput = crystalspace.console.output.fancy

; Sound renderer
;System.Plugins.iSoundRender = crystalspace.sound.render.software
;System.Plugins.iSoundRender = crystalspace.sound.render.ds3d

; Image Loader
System.Plugins.iImageIO = crystalspace.graphic.image.io.multiplex

; Motion manager-- must go HERE BEFORE level loader...
;System.Plugins.iMotionManager = crystalspace.motion.manager.default

; Level Loader
System.Plugins.iLoader = crystalspace.level.loader

; Sound Loader
;System.Plugins.iSoundLoader = crystalspace.sound.loader.multiplexer

; Network Driver
;System.Plugins.iNetworkDriver = crystalspace.network.driver.sockets

; Now the Engine (csWorld) plug-in
System.Plugins.iEngine = crystalspace.engine.3d

; Additional plugins come here
;System.Plugins.iScript = crystalspace.script.python

;System.Plugins.iModelConverter = crystalspace.modelconverter.multiplexer
;System.Plugins.iCrossBuilder = crystalspace.mesh.crossbuilder

;System.Plugins.iPerfStats = crystalspace.utilities.perfstat
;System.Plugins.iBugPlug = crystalspace.utilities.bugplug

System.Plugins.iFontServer = crystalspace.font.server.default

; Application ID string
System.ApplicationID = myapp

VFS.Config = vfs.cfg

myapp.CollDet.JumpSpeed = 0.08
myapp.CollDet.WalkAccelerate = 0.01
myapp.CollDet.WalkMaxSpeed = 0.05
myapp.CollDet.WalkBrake = 0.02
myapp.CollDet.RotateAccelerate = 0.005
myapp.CollDet.RotateMaxSpeed = 0.02
myapp.CollDet.RotateBrake = 0.01
myapp.CollDet.LookAccelerate = 0.02
----------------------------------------------------------

This config file begins by loading the VFS plugin. It then continues with the rest of the plugins that are needed. Note that for example under Video Driver there are two entries, one of which is commented. This allows simple changing of the plugin, in this case making it easy to switch from OpenGL rendering to Software Rendering.

To read in this config file, you also need some code. In your application class, you should have a function called myapp::Initialize. Modify the parameters of this function to:

bool myapp::Initialize (int argc, const char* const argv[], const char *iConfigName)

Now in the Initialize function, add the following code to setup the config manager and use it to get the plugins (only the Virtual clock is shown in this example):

---------------------------------------------------------
// Setup the config manager
if (!csInitializer::SetupConfigManager (object_reg, iConfigName))
{   csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    "crystalspace.application.unsf", "Failed to initialize config!");
  return false;
}

// Get the extra plugins (ie Reporter)
if (!csInitializer::RequestPlugins
    (object_reg,CS_REQUEST_REPORTER,CS_REQUEST_REPORTERLISTENER,CS_REQUEST_END))
{
  csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    "crystalspace.application.unsf", "Can't initialize reporter!");
  return false;
}

// Setup the event handler
if (!csInitializer::SetupEventHandler (object_reg, SimpleEventHandler))
{
  csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    "crystalspace.application.unsf", "Can't initialize event handler!");
  return false;
}

// Initialize the plugin & config managers
plugin_mgr = CS_QUERY_REGISTRY (object_reg, iPluginManager);
config = CS_QUERY_REGISTRY (object_reg, iConfigManager);

// Check for commandline help.
if (csCommandLineHelper::CheckHelp (object_reg))
{
  csCommandLineHelper::Help (object_reg);
  return false;
}

// The virtual clock.
vc = CS_QUERY_REGISTRY (object_reg, iVirtualClock);
if (!vc)
{
  csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    "crystalspace.application.unsf", "Can't find the virtual clock!");
  return false;
}
----------------------------------------------------------

...MORE PLUGINS ETC...

In your main function from where this function is called, add the config file's name, eg:

---------------------------------------------------------
MyApp->Initialize (argc, argv, "/config/myapp.cfg");
----------------------------------------------------------

To get the keys (such as myapp.CollDet.JumpSpeed = 0.08), the code is:

const char *s = config->GetStr("key", "default");

So for the example above and with a default value of 0.10, the code is:

const char *s = config->GetStr("myapp.Colldet.JumpSpeed","0.10");