It is good practice to always put definitions and declarations in header files as opposed to source files. In some cases it is even required. Here we will show the header file for a simple Crystal Space application. Although this is not strictly required, we use a class to encapsulate the application logic. Our `simple.h' header looks as follows:

ソースファイルに対してより、ヘッダファイルに定義と宣言をいつも置くことは、良い習慣です。いくつかの場合、それが必要でさえあります。ここでは、簡単なCrystal Spaceアプリケーションのためのヘッダファイルをお見せします。厳密には必要でないけれど、私たちはアプリケーションのロジックをカプセル化するためにクラスを使用します。`simple.h`ヘッダは次のようなものです。

#ifndef __SIMPLE_H__
#define __SIMPLE_H__

#include <crystalspace.h>

class Simple : public csApplicationFramework, public csBaseEventHandler
 csRef<iEngine> engine;
 csRef<iLoader> loader;
 csRef<iGraphics3D> g3d;
 csRef<iKeyboardDriver> kbd;
 csRef<iVirtualClock> vc;

 void ProcessFrame ();
 void FinishFrame ();

 Simple ();
 ~Simple ();

 void OnExit ();
 bool OnInitialize (int argc, char* argv[]);

 bool Application ();


#endif // __SIMPLE1_H__

In the `Simple' class we keep a number of references to important objects that we are going to need a lot. That way we don't have to get them every time when we need them. Other than that we have a constructor which will do the initialization of these variables, a destructor which will clean up the application, an initialization function which will be responsible for the full set up of Crystal Space and our application.

`Simple`クラスの中で、私達は、とても必要となる重要なオブジェクトのために、多くの参照を保持します。そうすると、それを必要とするときに、毎回それらを取得する必要はないです。それ以外では、これらの変数の初期化をおこなうコンストラクタ、アプリケーションをクリーンアップするデストラクタ、Crystal Spaceと私達のアプリケーションのフルセットアップの責任のある初期化関数があります。

Note that we use smart pointers (csRef<>) for several of those references. That makes it easier to manage reference counting. We let the smart pointer take care of this for us.


The event handler macros are used because our tutorial application also needs to be an event handler (it inherits from csBaseEventHandler? for that). The two macros indicate the name of this event handler and also the desired priority mechanism. In this case we use CS_EVENTHANDLER_NIL_CONSTRAINTS which means that we don't care about the order in which our events arrive (relative to other modules in CS).


In the source file `simple.cpp' we place the following:


#include "simple.h"


Simple::Simple ()
 SetApplicationName ("CrystalSpace.Simple1");

Simple::~Simple ()

void Simple::ProcessFrame ()

void Simple::FinishFrame ()

bool Simple::OnInitialize(int argc, char* argv[])
 if (!csInitializer::RequestPlugins(GetObjectRegistry(),
   return ReportError("Failed to initialize plugins!");

 if (!RegisterQueue(GetObjectRegistry(), csevAllEvents(GetObjectRegistry())))
   return ReportError("Failed to set up event handler!");

 return true;

void Simple::OnExit()

bool Simple::Application()
 if (!OpenApplication(GetObjectRegistry()))
   return ReportError("Error opening system!");

 g3d = csQueryRegistry<iGraphics3D> (GetObjectRegistry());
 if (!g3d) return ReportError("Failed to locate 3D renderer!");

 engine = csQueryRegistry<iEngine> (GetObjectRegistry());
 if (!engine) return ReportError("Failed to locate 3D engine!");

 vc = csQueryRegistry<iVirtualClock> (GetObjectRegistry());
 if (!vc) return ReportError("Failed to locate Virtual Clock!");

 kbd = csQueryRegistry<iKeyboardDriver> (GetObjectRegistry());
 if (!kbd) return ReportError("Failed to locate Keyboard Driver!");

 loader = csQueryRegistry<iLoader> (GetObjectRegistry());
 if (!loader) return ReportError("Failed to locate Loader!");


 return true;

 * Main function
int main (int argc, char* argv[])
 return csApplicationRunner<Simple>::Run (argc, argv);

This is almost the simplest possible application and it is absolutely useless. Also don't run it on an operating system where you can't kill a running application because there is no way to stop the application once it has started running.


Even though this application is useless it already has a lot of features that are going to be very useful later. Here is a short summary of all the things and features it already has:


  • It will open a window.
  • You can control the size of the window and the video driver used for that window with command-line options (`-video' and `-mode' command-line options).
  • It will give command-line help when you use the `-help' command-line option.
  • It has the following plugins initialized and ready to use: engine, 3D renderer, canvas, reporter, reporter listener, font server, image loader, map loader, and VFS.
  • ウィンドウを開きます。
  • ウィンドウのサイズを変更することができます。コマンドラインオプションで、ウィンドウに使用するビデオドライバを変更することができます。
  • '-help'コマンドラインを使用したとき、コマンドラインヘルプが表示されます。
  • 次のプラグインが初期化され、使用することができる状態です。
    • エンジン
    • 3Dレンダー
    • キャンバス
    • レポーター
    • レポーターリスナ
    • フォントサーバ
    • イメージローダ
    • マップローダ
    • VFS

Before we start making this application more useful lets have a look at what actually happens here.


Before doing anything at all, after including the necessary header files, we first need to use a few macros. The CS_IMPLEMENT_APPLICATION macro is essential for every application using Crystal Space. It makes sure that the main() routine is correctly linked and called on every platform.

すべてをする前に、必要はヘッダファイルをインクルードした後は、まずいくつかのマクロを使用する必要があります。「CS_IMPLEMENT_APPLICATION」マクロは、Crystal Spaceを使用するすべてのアプリケーションで必要です。「main()」ルーチンはすべてのプラットホームでリンクされ呼ばれることを理解して下さい。

csInitializer::RequestPlugins?() will use the configuration file (which we are not using in this tutorial), the command-line and the requested plugins to find out which plugins to load. The command-line has highest priority, followed by the configuration file and lastly the requested plugins.


  1. コマンドライン
  2. 環境設定ファイル
  3. リクエストプラグイン

This concludes the initialization pass.


In Simple::Application() we open the window with a call to the function csInitializer::OpenApplication?(). This sends the `cscmdSystemOpen?' broadcast message to all components that are listening to the event queue. One of the plugins that listens for this is the 3D renderer which will then open its window (or enable graphics on a non-windowing operating system).


After that, we query the object registry to locate all the common objects that we will need later, and store references to them in our main class. Because we use csRef<> or smart pointers, we don't have to worry about invoking IncRef?() and DecRef?() manually.

この後、後に必要なすべての共通オブジェクト設置するために、オブジェクトレジストリを問い合わせます。そして、それらの参照をメインクラスに保持します。私たちはcsRef<> またはスマートポインタを使用するため、「IncRef?()」と「DecRef?()」の手動での実行を心配する必要はありません。

Finally we start the default main loop by calling Run().