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

4.2.1.1 Simple Header File

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:

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

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"

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.

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:

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.

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.

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.

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


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

This document was generated using texi2html 1.76.