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

#### 4.2.2.1 Loading a Material in Memory

To make this example a little more interesting the material is going to be loaded after the other set-up of the texture manager has been performed. This is not really needed but it illustrates how a game might load materials dynamically after the application is already running. So, first we edit ``simple.h'`:

    ...
    class Simple
    {
    private:
      ...
      void CreateSprites ();
      ...

Then, we edit ``simple.cpp'` and add the following in `Simple::Application()` and `Simple::CreateSprites()` (before ``return true'`):

    bool Simple::Application()
    {
      ...
      CreateSprites();
      ...
      return true;
    }

    ...

    void Simple::CreateSprites()
    {
      // Load a texture for our sprite.
      iTextureWrapper* txt = loader->LoadTexture ("spark",
            "/lib/std/spark.png");
      if (txt == 0)
        ReportError("Error loading texture!");
    }

This code first loads a texture 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 loading succeeds this function will register our texture with the texture manager so it can be immediatelly used.

Further note that when you load a texture like this, there will also be an associated material with the same name. The engine itself works with materials and doesn't directly use textures. The material in this case is simply a wrapper on top of the texture. But you could also add detail textures.

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

This document was generated using texi2html 1.76.