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

4.2.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 just before the OnInitialize() function:

 
bool Simple::LoadMap ()
{
  // Set VFS current directory to the level we want to load.
  csRef<iVFS> VFS (csQueryRegistry<iVFS> (object_reg));
  VFS->ChDir ("/lev/flarge");
  // Load the level file which is called 'world'.
  if (!loader->LoadMapFile ("world"))
    ReportError("Error couldn't load level!");
  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). This is demonstrated in the code below, which you add to the Application() method, following the SetRectangle() invocation. We also add code to initialize collision detection for geometry in the map which was just loaded.

 
bool Simple::Application ()
{
  ...
  view->SetRectangle (0, 0, g2d->GetWidth (), g2d->GetHeight ());

  // Here we load our world from a map file.
  if (!LoadMap ()) return false;

  // Initialize collision objects for all loaded objects.
  csColliderHelper::InitializeCollisionWrappers (cdsys, engine);

  // Let the engine prepare all lightmaps for use and also free all
  // images that were loaded for the texture manager.
  engine->Prepare ();

  Run();
  ...
}

So, we now have geometry loaded into the world, but we haven't set the current sector or the camera's position in the world. Often, maps contain information specifying where the camera should be placed when the map is first loaded, so we will use this information if present. This is the topic of the next section.


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

This document was generated using texi2html 1.76.