Crystal Space 2.0.0: 4.1.1 Plugins and Modules

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.1.1 Plugins and Modules

Crystal Space is a package of components and libraries which can all be useful for creating computer games. Although some of the packages are intended more for 3D gaming this is not as such a restriction of the package. Components such as the sound driver function just as well in a 2D gaming environment, and the Virtual File System plugin (see section Virtual File System (VFS)) can be used in non-graphical applications. This highlights one of the important characteristics of Crystal Space: the components and libraries are more or less independent of each other. If you do not want “real” physics, then just don't use the physics plugin. If you don't want scripting then don't include that. All packages are designed to be functional on their own or with a minimal number of other packages. There are some dependencies of course. For example, the 3D engine requires a 3D renderer to display its output, and a renderer requires a canvas onto which to draw. On the other hand, you could very well use the 3D renderer without the 3D engine.

Although there is a high level of independence, there is also a high level of integration. The components and libraries were also designed to be used together and as such offer a flexible scheme of integration.

If you are interested in developing a program using Crystal Space, then it is important to understand that Crystal Space is not a single monolithic library, but instead consists of several libraries and plug-in modules. This manual uses the term module for both libraries and plug-ins when the distinction between the two is not important.

A plug-in module is similar to a library, but has some advantages over a library. All plug-ins can be accessed in a common fashion. They have a pure interface, and they can provide this interface even if they are extracted to a dynamic load library. So, they are the better solution as long as speed is not the dictator of all decisions as in the math library (access to plug-in functions uses virtual methods). The main difference between the interface of a library and a plug-in is that a library may use SCF, the Shared Class Facility (see section Shared Class Facility (SCF)), and C++ classes; plug-ins may only use SCF.

- What is SCF?

  The main SCF object is the interface. An interface is the solution to strictly decouple the public methods of an object from their implementation. You only get a pointer to an abstract class with only virtual methods, called the interface, so your program doesn't know about the actual object behind the pointer. This object is called an implementation of the interface. You as the user of Crystal Space will call functions that create the actual object, but only return the interface. After that you can use the interface like a C++ object. When you don't need it anymore, don't `‘delete’` it, but call `DecRef()`. When you pass a pointer to the interface to anywhere, call `IncRef()` from there, and `DecRef()` when you don't need the interface there anymore.

  Starting with Crystal Space version 0.95 we also have smart pointers. A smart pointer is an instance of the class `csRef`. This class takes care of `IncRef()` and `DecRef()` for you (see section Correctly Using Smart Pointers). It is very important to use smart pointers now. They are making life a lot easier and in the future they may become required usage (i.e. no longer optional).

  As the user you'll only have to include a header that defines the interface, but not the implementation. Despite the obvious advantage of having a very clear structure in your code, there is the advantage that linking the interface to the implementation can be done at run-time, but more about this later.

- What are the characteristics of a library?

  A library is just a normal C++ library as you know them. A library can optionally provide SCF interfaces. In the case of a library this is just a way to define a clear structure. But as their main interface, libraries provide C++ classes.

- What are the characteristics of a plug-in?

  A plug-in, on the other hand, will only provide SCF interfaces, no normal C++ classes. The plug-in itself will also appear as an SCF interface. This is part of the definition of a plug-in. A plug-in can be organized as static library or DLL; this only makes a small difference in how you use it in your program.

  As the user of Crystal Space, you have to do the following steps to use a plug-in:

- How can I register the plug-in library?

  Registering means to tell SCF the name of the plug-in that was given to it by its author, and a class or a dynamic library to associate it with. Plugin libraries can contain multiple named SCF classes. For example, to use the null graphics renderer SCF must know that the SCF class `‘crystalspace.graphics3d.null’` can be found in `‘null3d.dll’` (Windows) or `‘null3d.so’` (Unix). SCF determines dynamically which plugin libraries contain which SCF classes by consulting meta-information associated with each plugin. Depending upon the platform, the associated meta-information may be bundled into the plugin itself, or it might exist in a separate file with a `‘.csplugin’` extension alongside the plugin module.

  How you register a library depends on whether it is a static library or a dynamic library (`‘.dll’` or `‘.so’`). For a static library, that is, one which is linked directly into the application rather than loaded dynamically at run-time, put the following macro invocation at top-level in one of your C++ files once for each SCF class contained in your library:

      SCF_REGISTER_STATIC_CLASS(
        cxx-class,
        scf-name,
        description,
        dependencies)

  Here, cxx-class is the name of the C++ class which implements the factory for this particular SCF class. cxx-class is the same name given to the `SCF_IMPLEMENT_FACTORY()` macro. scf-name is the SCF class name corrsponding to cxx-class, description is a human-readable string describing the purpose of the class, and dependencies is a comma-separated list of other SCF class upon which this class depends.

  For a dynamic library, SCF will discover the plugin and register the contained classes automatically by consulting the associated meta-information.

- How can I load the plug-in?

  To load a plug-in, you must tell the plug-in manager the name of the plug-in as it was registered in the previous step. In the most common case you will probably use the plug-in loader to load the plug-in. This is a convenience class that is able to load plug-in as specified in a config file, from the commandline, or as requested by the application programmer. In addition to loading the plug-in (the plug-in loader will use the plug-in manager to do that), the plug-in loader will optionally also register the plug-in with the Object Registry.

  The object registry is a central location in the Crystal Space framework where any module can register SCF objects. The object registry is not only for plug-ins. It can contain any SCF object. Objects in the object registry also have a tag name associated with them. By convention the default object for a given SCF interface will be added to the object registry with the tag name equal to the interface name. For example, a 3D renderer is a plug-in module that implements (among others) the `‘iGraphics3D’` interface. At the same time there can be multiple 3D renderers in memory (for example, for procedural textures). But the default 3D renderer will be registered with the tag name `‘iGraphics3D’`.

  Note that the decision which graphics driver you use (e.g. Null or OpenGL) is done at the time you load the plug-in by passing the name of that driver. At the time you ask for the plug-in interface and use it in your program, this does not make a difference anymore. This makes it possible to exchange the driver simply by loading another driver, but without changing your main program.

  The Crystal Space modules themselves will use the standard plug-ins with the default tag name as explained above. For example, the 3d engine looks for the graphics driver by looking in the object registry for an object with the tag `‘iGraphics3D’`.

  Now how can you actually load the plug-in? You can either load them manually in the code using the `csLoadPlugin` function or else you can use the plugin loader. As explained above, the plugin loader can load plugins specified on the commandline, a config file, or else explicitly requested in the code. This is done in a specific way: The commandline has highest priority. i.e. if the user specified the OpenGL video driver on the commandline then this will be the plugin that is loaded on the `‘iGraphics3D’` tag. The config file and plugins requested from the code are ignored then (for that tag at least). The plugins requested from the code have lowest priority and only serve as a default if neither the commandline nor the config file specified a plug-in for the given tag.

  There is a class called `csInitializer()` which contains various convenience routines to help initialize a Crystal Space application. This class also contains a routine (`RequestPlugins()`) which will use the plugin loader automatically.

  There are several advantages to using the plugin loader as opposed to manually loading plug-ins using `csLoadPlugin`:

- How can I query the plug-in interface?

  This is the last step before you can use the plug-in. It means that inside your program you ask the object registry for an object registered with the desired tag. The easiest way to do this is as follows: `csQueryRegistry<iInterface>(object_reg)`. This macro will search the object registry for the default object implementing the given interface (i.e. the object registered with the tag equal to the interface name). Alternatively you can also use `csQueryRegistryTag` to get an object with a specific tag.

- Conclusion

  To sum it up, SCF is mainly used to provide common interfaces for DLLs, but it can also be used by statically linked libraries. If you want to know how to write a plug-in yourself, you should read the complete SCF documentation. See section Shared Class Facility (SCF).

For further information about modules and plug-in drivers, see the sections on using Crystal Space (see section Using Crystal Space).

Now that you have learned some basics about the Crystal Space environment, you can try writing your first program.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated using texi2html 1.76.