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

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;
  csRef<iRenderManager> rm;
  
  csRef<FramePrinter> printer;

public:
  Simple ();
  ~Simple ();

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

  bool Application ();
  
  void Frame ();
  
  bool SetupModules ();

  CS_EVENTHANDLER_PHASE_LOGIC("application.simple1")
};

#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 macro is used because our tutorial application also needs to be an event handler (it inherits from csBaseEventHandler for that). The macro indicates the name of this event handler and specifies that the application wants to handle the frame event in the first (or 'logic') phase.

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::Frame ()
{
}

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());
  csEventID events[] = {
    csevFrame (GetObjectRegistry()),
    csevKeyboardEvent (GetObjectRegistry()),
    CS_EVENTLIST_END
  };
  if (!RegisterQueue(GetObjectRegistry(), events))
    return ReportError("Failed to set up event handler!");

  return true;
}

void Simple::OnExit()
{
}

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

  if (SetupModules())
  {
    Run();
  }

  return true;
}

bool Simple::SetupModules ()
{
  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:

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 call Simple::SetupModules() 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.