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

4.3.3.2 Loading The Map

Here we add the code to load a map. In this example we will load the `flarge' map which is included with Crystal Space.

In the second tutorial we already mentioned VFS (see section Virtual File System (VFS)). This is important in this case too since we are going to load the map from the virtual filesystem. To do this we first add the new LoadMap() routine right before the Initialize() function:

 
bool Simple::LoadMap ()
{
  // Set VFS current directory to the level we want to load.
  csRef<iVFS> VFS (CS_QUERY_REGISTRY (object_reg, iVFS));
  VFS->ChDir ("/lev/flarge");
  // Load the level file which is called 'world'.
  if (!loader->LoadMapFile ("world"))
    ReportError("Error couldn't load level!");

  engine->Prepare ();

  return true;
}

This code first uses iVFS::ChDir() to set the current directory in the virtual file system to `/lev/flarge'. In the case of `flarge' this mount point exists already in the configuration file `vfs.cfg'. If this is not the case for your own levels, you can either modify `vfs.cfg', provide your own `vfs.cfg', or else call iVFS::Mount() to map a physical file path (can be a ZIP archive file as well) to a virtual directory.

The call to iLoader::LoadMapFile() will take the given filename (in this case `world') and open it from the current VFS directory. Then, it will parse that file and create the geometry which is specified there.

If loading is successful, then you must call iEngine::Prepare() to make sure that all lightmaps are correctly loaded from the cache and other necessary setup work is done (i.e. textures are registered and so on).

Additionally, change the last part of Application() to this:

 
bool Simple::Application ()
{
  ...

  view.AttachNew(new csView (engine, g3d));
  iGraphics2D* g2d = g3d->GetDriver2D ();
  view->SetRectangle (0, 0, g2d->GetWidth (), g2d->GetHeight ());

  if (!LoadMap ()) return false;

  ...
}

So, first we create our view but haven't set the current sector. Instead, we will have LoadMap() do that based upon the loaded map, as shown below.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated using texi2html 1.76.