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

5.4.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 7.2 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"))
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.simple",
    	"Couldn't load level!");
    return false;
  }
  engine->Prepare ();

  return true;
}

This code will first use iVFS::ChDir() to set the current directory in the virtual file system to `/lev/flarge'. In the case of `flarge' this MOUNT POINT is already created in the config file `vfs.cfg'. If this is not the case for your own levels you can either modify `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 this 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).

We additionally change the last part of Initialize() to this:

 
bool Simple::Initialize ()
{
  ...

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

  if (!LoadMap ()) return false;
  return true;
}

So first we create our view but doesn't yet set the current sector. LoadMap() will do that based on the loaded level.


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

This document was generated using texi2html