By Charles Quarra, charsquarra@hotmail.com.
Before loading a sprite, you should load any textures that the sprite will require. Remember the g3d (3d graphics plugin) you loaded? Now it's time to use it:
iTextureManager* txtmgr = g3d->GetTextureManager ();
txtmgr->SetPalette ();
Now load your texture with the following:
iTextureWrapper* txt = loader->LoadTexture ("spark", "/lib/std/spark.png", CS_TEXTURE_3D, txtmgr, true);
So lets see quickly what these options mean:
"spark": "spark" it the handle name you want for your texture so you can found it anytime with the call iMaterialWrapper* tm = engine->GetMaterialList ()->FindByName ("spark");
"/lib/std/spark.png": In our example, "/lib/std/spark.png" is the location of the texture file in the Crystal Space Virtual Filesystem: Check vfs.cfg in your CS/ directory to know the real directory it points to.
CS_TEXTURE_3D: A flag. I'm not aware of other flags, so keep this one.
txtmgr: The pointer to the texture manager (someone [or something] has to keep track of all those textures, right?)
bool reg=true/false: This tells to object registry whether or not to register this material. If you later call engine->Prepare(); then it's not needed, but if you already did this call, and doesnt plan to call it again anywhere
near, its safe to keep this true.
Ok so we have loaded the texture, we have a reference thru the interface to the texture with the *txt pointer, so lets use it for loading our sprite:
iMeshFactoryWrapper* imeshfact = loader->LoadMeshObjectFactory ("/lib/std/sprite1");
This loads the sprite definition from vfs:/lib/std/ . This points to the sprite "factory". I guess the name its due to the fact that we can instantiate all the sprites we want from the sprite1 definition. Now you're going to instantiate a concrete sprite from the sprite1 abstract definition. For this, you need an iMeshWrapper* pointer to keep track of this sprite, so probably you would like to declare it as a member of the object which this sprite graphically represents.
iMeshWrapper* sprite = engine->CreateMeshWrapper (imeshfact, ...
There are a lot of ways of instantiate the sprite: iEngine::CreateMeshWrapper represents not one function, but a set of overloaded functions. Depending if you are creating the sprite from a "factory" or from an existing iMeshObject (which this tut doesn't cover) you call the corresponding version. For our example we call it with:
sprite = engine->CreateMeshWrapper (imeshfact, "body", room , csVector3(x,y,z));
imeshfact: Have you forgotten what it is? It's the pointer to the "sprite1" mesh factory
"body": A handle name you can use it just like we did for the texture "spark"
room: Points to the iSector* where the sprite belongs
csVector3: The sector coordinates where the sprite start. Note that x, y, and z should be replaced with small integer values (I suggest something between 0 and 5).
Now, if you already know how to do sprite, you know that you could program "actions" for your sprite, which are esentially different animations (sequence of frames) for the sprite. You can set the sprite action with:
iSprite3DState* spstate = SCF_QUERY_INTERFACE (sprite->GetMeshObject(),iSprite3DState);
spstate->SetAction ("default");
spstate->DecRef ();
Now lets look at those lines.
Why the sprite->GetMeshObject()? Remember! the iMeshWrapper is just an interface to the MeshObject which CS actually handles. With this you tell the actual object.
With the SetAction() you explicitly call which action its going to be activated. The definition of "default" is cointained in the "sprite1" definition.
Every time you call a SCF_QUERY_INTERFACE, it's safe to do the DecRef() when you have finished using the resulting pointer. The DecRef() tells the garbage collector (the system which tells CS when to delete a memory location) that this particular reference its not longer needed.
Now with this code you set light properties for the sprite so you can see it:
sprite->DeferUpdateLighting (CS_NLIGHT_STATIC|CS_NLIGHT_DYNAMIC, 10);
When you are finished initializing the sprite, remember to call sprite->DecRef(); Now you have loaded and initialized your sprite settings. Keep reading to learn other things to do with your sprite.