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

5.3.1 Loading a Material in Memory

To make this example a little more interesting the material is going to be loaded after the other setup of the texture manager has been done. This is not really needed but it illustrates how a game might load materials dynamically later when the application is already running. So first we edit `simple.cpp' and add the following in Simple::Initialize() (between txtmgr->SetPalette() and `return true'):

 
bool Simple::Initialize(int argc, const char* const argv[])
{
  ...
  txtmgr->SetPalette ();

  // Load a texture for our sprite.
  iTextureWrapper* txt = loader->LoadTexture ("spark",
  	"/lib/std/spark.png");
  if (txt == NULL)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.simple",
    	"Error loading texture!");
    return false;
  }
  txt->Register (txtmgr);
  txt->GetTextureHandle()->Prepare ();
  iMaterialWrapper* mat =
    engine->GetMaterialList ()->FindByName ("spark");
  mat->Register (txtmgr);
  mat->GetMaterialHandle ()->Prepare ();

  return true;
}

This code first loads a texture in our engine with iLoader::LoadTexture(). The second argument is the file name for our texture (VFS path) and the first argument is how that texture should be named in the engine. In this case we use `spark' for that because that's how the `sprite1' definition wants it.

If this loading succeeds we need to register our texture with the texture manager. This is because we didn't put the loading of our texture before the engine->Prepare() call (which takes care of this registering automatically). The lines txt->Register() and Prepare() can be used at any time while the application is running to dynamically load additional textures. The first call to Register() registers the texture with the texture manager so that it is aware of this texture. The second call makes sure that the texture can really be used for 3D rendering by mapping it to the internal format used by the 3D rasterizer. If you plan to dynamically add a lot of textures in one step then you should call Register() once for every texture and then do txtmgr->PrepareTextures() instead. This will be more efficient.

After the texture has been created we still have to create and register a material. The engine works with materials. The material in this case is simply a wrapper on top of the texture. But you could also add detail textures.


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

This document was generated using texi2html