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

5.2.1 Simple Header File

It is good practice to always put defines and declares in header files as opposed to source files. In some cases it is even needed. 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 <stdarg.h>

struct iEngine;
struct iLoader;
struct iGraphics3D;
struct iKeyboardDriver;
struct iSector;
struct iView;
struct iVirtualClock;
struct iObjectRegistry;
struct iEvent;

class Simple
{
private:
  iObjectRegistry* object_reg;
  csRef<iEngine> engine;
  csRef<iLoader> loader;
  csRef<iGraphics3D> g3d;
  csRef<iKeyboardDriver> kbd;
  csRef<iVirtualClock> vc;

  float rotX, rotY;
 
public:
  Simple (iObjectRegistry* object_reg);
  ~Simple ();

  bool Initialize ();
  void Start ();
};

#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, and finally a Start() function to start the event handler.

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. For `iObjectRegistry' we don't do this because for technical reasons it doesn't make sense to keep references to that.

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

 
#include "cssysdef.h"
#include "csutil/sysfunc.h"
#include "iutil/vfs.h"
#include "csutil/cscolor.h"
#include "cstool/csview.h"
#include "cstool/initapp.h"
#include "simple.h"
#include "iutil/eventq.h"
#include "iutil/event.h"
#include "iutil/objreg.h"
#include "iutil/csinput.h"
#include "iutil/virtclk.h"
#include "iengine/sector.h"
#include "iengine/engine.h"
#include "iengine/camera.h"
#include "iengine/light.h"
#include "iengine/statlght.h"
#include "iengine/texture.h"
#include "iengine/mesh.h"
#include "iengine/movable.h"
#include "iengine/material.h"
#include "imesh/thing/polygon.h"
#include "imesh/thing/thing.h"
#include "imesh/object.h"
#include "ivideo/graph3d.h"
#include "ivideo/graph2d.h"
#include "ivideo/txtmgr.h"
#include "ivideo/texture.h"
#include "ivideo/material.h"
#include "ivideo/fontserv.h"
#include "igraphic/imageio.h"
#include "imap/parser.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "csutil/cmdhelp.h"
#include "csutil/event.h"

CS_IMPLEMENT_APPLICATION

// The global pointer to simple
Simple *simple;

Simple::Simple (iObjectRegistry* object_reg)
{
  Simple::object_reg = object_reg;
}

Simple::~Simple ()
{
}

bool Simple::Initialize ()
{
  if (!csInitializer::RequestPlugins (object_reg,
        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))
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
        "crystalspace.application.simple",
        "Can't initialize plugins!");
    return false;
  }

  // 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.simple",
        "Can't find the virtual clock!");
    return false;
  }

  // Find the pointer to engine plugin
  engine = CS_QUERY_REGISTRY (object_reg, iEngine);
  if (!engine)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
        "crystalspace.application.simple",
        "No iEngine plugin!");
    return false;
  }

  loader = CS_QUERY_REGISTRY (object_reg, iLoader);
  if (!loader)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
        "crystalspace.application.simple",
        "No iLoader plugin!");
    return false;
  }

  g3d = CS_QUERY_REGISTRY (object_reg, iGraphics3D);
  if (!g3d)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
        "crystalspace.application.simple",
        "No iGraphics3D plugin!");
    return false;
  }

  kbd = CS_QUERY_REGISTRY (object_reg, iKeyboardDriver);
  if (!kbd)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
        "crystalspace.application.simple",
        "No iKeyboardDriver plugin!");
    return false;
  }

  // Open the main system. This will open all the previously
  // loaded plug-ins.
  if (!csInitializer::OpenApplication (object_reg))
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
        "crystalspace.application.simple",
        "Error opening system!");
    return false;
  }

  csReport (object_reg, CS_REPORTER_SEVERITY_NOTIFY,
        "crystalspace.application.simple",
        "Simple Crystal Space Application version 0.1.");

  return true;
}

void Simple::Start ()
{
  csDefaultRunLoop (object_reg);
}

/*---------------*
 * Main function
 *---------------*/
int main (int argc, char* argv[])
{
  iObjectRegistry* object_reg =
    csInitializer::CreateEnvironment (argc, argv);
  if (!object_reg) return -1;

  simple = new Simple (object_reg);

  if (simple->Initialize ())
    simple->Start ();

  delete simple;
  csInitializer::DestroyApplication (object_reg);

  return 0;
}

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.

The main routine first calls csInitializer::CreateEnvironment() to make sure SCF is set up, the object registry is created, commandline reading is set up, and several other useful utilities are created (plugin manager, event queue, ...). Then our main routine creates an instance of our `Simple' class. We put this instance into a global variable to make access to it easier. The next step is application initialization which is done in the Initialize() function.

Note the usage of the csReport() function. This is a convenience function to send a message (usually an error or notification) to the reporter plugin. It works a lot like printf() except that you additionally need to give the severity level and an identifier which can give someone listening to the reporter an idea of the origin of the message.

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

The csCommandLineHelper::CheckHelp() function will check if the `-help' commandline option is given and if so show the help for all loaded plugins (every plugin that is loaded in memory is capable of extending the commandline options).

After that we will query the object registry to find out all the common objects that we're going to need later and store a reference in our main class. Because we use csRef<> or smart pointers we don't have to worry about DecRef().

Finally, when all is done the window is opened with a call to the function csInitializer::OpenApplication(). This sends the `cscmdSystemOpen' message to all components that are listening to the event queue. One of the plugins that does this is the 3D renderer which will then open its window (or enable graphics on a non-windowing operating system).

This concludes the initialization pass.

In Simple::Start() we will start the default main loop by calling csDefaultRunLoop(). This function will only return when the application exits (which this example cannot yet do). Basically this function will start the loop to handle events.


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

This document was generated using texi2html