[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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; iEngine* engine; iLoader* loader; iGraphics3D* g3d; iKeyboardDriver* kbd; iVirtualClock* vc; public: Simple (); ~Simple (); bool Initialize (int argc, const char* const argv[]); 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.
In the source file `simple.cpp' we place the following:
#include "cssysdef.h" #include "cssys/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" CS_IMPLEMENT_APPLICATION // The global pointer to simple Simple *simple; Simple::Simple () { engine = NULL; loader = NULL; g3d = NULL; kbd = NULL; vc = NULL; } Simple::~Simple () { if (vc) vc->DecRef (); if (engine) engine->DecRef (); if (loader) loader->DecRef(); if (g3d) g3d->DecRef (); if (kbd) kbd->DecRef (); csInitializer::DestroyApplication (object_reg); } bool Simple::Initialize (int argc, const char* const argv[]) { object_reg = csInitializer::CreateEnvironment (); if (!object_reg) return false; csInitializer::SetupCommandLineParser (object_reg, argc, argv); if (!csInitializer::RequestPlugins (object_reg, CS_REQUEST_VFS, CS_REQUEST_SOFTWARE3D, 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[]) { simple = new Simple (); if (simple->Initialize (argc, argv)) simple->Start (); delete simple; 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:
-video
and -mode
commandline options).
-help
commandline
option.
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 CS. It makes sure that the main()
routine is correctly linked and called on every platform.
The main routine first 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 initialization. The first thing the
Initialize()
function does is to create the environment with
csInitializer::CreateEnvironment()
. This will initialize SCF,
create the object registry, and then create a number of other useful
entities (plugin manager, event queue, ...).
Note the usage of the csReport()
function. This is a conveniance
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.
Then we call csInitializer::SetupCommandLineParser()
to setup
the commandline parser. This will allow the rest of the initialization
pass to actually read the commandline.
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. At destruction time we must then release these references
with 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] | [ ? ] |