4.2.1.1 簡単なヘッダファイル


簡単なヘッダファイル

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
{
private:
 csRef<iEngine> engine;
 csRef<iLoader> loader;
 csRef<iGraphics3D> g3d;
 csRef<iKeyboardDriver> kbd;
 csRef<iVirtualClock> vc;

 void ProcessFrame ();
 void FinishFrame ();

public:
 Simple ();
 ~Simple ();

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

 bool Application ();

 CS_EVENTHANDLER_NAMES("application.simple1")
 CS_EVENTHANDLER_NIL_CONSTRAINTS
};

#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.

私達は、いくつかの参照で、スマートポインタ(csRef<>)を使用することに注意して下さい。これは、参照回数を管理することを容易にします。私たちはスマートポインタに、参照回数の管理をさせます。

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).

イベントハンドラマクロを使います。なぜなら、チュートリアルアプリケーションは、イベントハンドラも必要だからです(イベントハンドラが必要なためcsBaseEventHandler?を継承します。)。2つのマクロは、イベントハンドラの名前と、希望する優先度メカニズムを表します。この場合、イベントが到着した順番を気にしないという意味の、「CS_EVENTHANDLER_NIL_CONSTRAINTS」を使用します(CSの他モジュールに関係します)。

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

ソースファイル`simple.cpp`は、次のようです。

#include "simple.h"

CS_IMPLEMENT_APPLICATION

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

Simple::~Simple ()
{
}

void Simple::ProcessFrame ()
{
}

void Simple::FinishFrame ()
{
}

bool Simple::OnInitialize(int argc, char* argv[])
{
 if (!csInitializer::RequestPlugins(GetObjectRegistry(),
   CS_REQUEST_VFS,
   CS_REQUEST_OPENGL3D,
   CS_REQUEST_ENGINE,
   CS_REQUEST_FONTSERVER,
   CS_REQUEST_IMAGELOADER,
   CS_REQUEST_LEVELLOADER,
   CS_REQUEST_REPORTER,
   CS_REQUEST_REPORTERLISTENER,
   CS_REQUEST_END))
   return ReportError("Failed to initialize plugins!");

 csBaseEventHandler::Initialize(GetObjectRegistry());
 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!");

 Run();

 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.

これが、一番シンプルで動作するアプリケーションです。これは完全に役に立ちません。また、実行しているアプリケーションをkillすることのできないOSで実行しないで下さい。なぜなら、これは一度実行したら、停止する方法のないアプリケーションだからです。

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.

「csInitializer::RequestPlugins?()」は、プラグインをロードする場所を見つけるために環境設定ファイル(チュートリアルでは使用いていません)、コマンドライン、リクエストプラグインを使用します。優先度は以下の順です。

  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).

「Simple::Application()」では、「csInitializer::OpenApplication?()」機能を呼び、ウィンドウを開いています。これは、イベントキューを聞いているすべてのコンポーネントに、'cscmdSystemOpen?'同報メッセージを送信します。このメッセージを聞きとるプラグインの1つに、3Dレンダーがあります。3Dレンダーは、'cscmdSystemOpen?'同報メッセージを受け取ると、ウィンドウを開きます(または、非ウインドーオペレーティングシステムでグラフィックスを可能にしてください)。

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().

最後に、「Run()」を呼び、デフォルトメインループを開始します。

最新の20件

2007-02-18 2007-02-12 2007-01-31 2007-02-12 2007-01-14 2007-01-21 2007-02-12 2007-02-11 2007-01-15 2007-01-14 2007-02-12 2007-02-11 2007-02-01 2007-01-21 2007-01-19 2007-02-12 2007-01-19
  • 4.2.1.1 簡単なヘッダファイル
2009-08-27 2007-01-10

今日の13件

  • counter: 86
  • today: 1
  • yesterday: 0
  • online: 1