CLANLIB SCENE MANAGER (Click Here To Download Source Code)
Those who have used the GIMP or Photoshop, or other such products, will likely know how their useful layer systems work. Based on the idea of clear perspex layers stacked atop one another, the layer system allows images to be composited on transparent layers. For various reasons I needed to code a programmable layer system, such as described, that used the ClanLib Game SDK (http://www.clanlib.org).
The layer system consists of three classes:
CL_SceneObject - This is derived from CL_Sprite and can be any image or animated sprite loaded from a file, or wherever.
CL_SceneLayer – This is derived from CL_Surface and represents a single, transparent layer in a scene. A Layer can contain potentially any number of CL_SceneObjects. At render time, the CL_SceneObjects objects are composited together onto the layer's surface.
CL_SceneManager – This is derived from CL_Surface and represents a scene. This is a collection of Layers composited together at render time. This class can be manipulated manually to build up a system of layers and objects, and can also be loaded from and saved to an XML File.
Building Layers Manually
To build a simple scene consisting of two Layers- one layer containing two sprites, and the other just one- the following process can be used.
|
//Create Scene Manager and Associate with a Resource Manager CL_ResourceManager* m_Manager = new CL_ResourceManager("resources.xml"); CL_PixelBuffer Buffer = CL_PixelBuffer(512,512,512*4,CL_PixelFormat::rgba8888, NULL); CL_SceneManager* m_SceneManager = new CL_SceneManager(Buffer, m_Manager); |
Then create a layer
|
//createLayer(Width, height, PixelFormat, Pitch, Name) CL_SceneLayer *Layer1 = m_SceneManager->createLayer(256, 256, CL_PixelFormat::rgba8888, 256*4, "layer1"); |
Create two SceneObject sprites to add to this layer. As with sprites, the names are based on the sprite definition in the resource file...
|
CL_SceneObject *Sprite1 = m_SceneManager->createSceneObject("Game/Sprite01", m_Manager); CL_SceneObject *Sprite2 = m_SceneManager->createSceneObject("Game/Sprite02", m_Manager); Layer1->attachObject(Sprite1); Layer1->attachObject(Sprite2); |
Add layer to scene
|
m_SceneManager->addLayer(Layer1); |
And so on for however many layers are required
Building Layers From an XML File
The same simple scene could be defined in an XML file rather than built manually. The XML file would appear as follows:
|
<scene> <layers> <layer name="layer0" visible="true" posx="500" posy="900" width="512" height="512"> <sprite name="Game/Sprite01" posx="0" posy="0" visible="true" /> <sprite name="Game/Sprite02" posx="100" posy="50" visible="true" /> </layer> <layer name="layer1" visible="true" posx="500" posy="900" width="512" height="512"> <sprite name="Game/Sprite03" posx="0" posy="100" visible="true" /> </layer> </layers> </scene>
|
Then some code to create and load a scene manager from XML
|
//Create Scene Manager and Associate with a Resource Manager CL_ResourceManager* m_Manager = new CL_ResourceManager("resources.xml"); CL_PixelBuffer Buffer = CL_PixelBuffer(512,512,512*4,CL_PixelFormat::rgba8888, NULL); CL_SceneManager* m_SceneManager = new CL_SceneManager(Buffer, m_Manager); //Load From XML m_SceneManager->loadFromXMLFile("scene.xml"); |
Painting The Scene Manager
The Scene Manager handles all the painting of each of its layers and objects. Like the Layers, the Scene Manager is derived from CL_Surface and can be drawn like any other surface. The whole scene is composited to the surface. Consider the following
|
m_SceneManager->draw(30,0); |
Other Features
To save to XML
|
m_SceneManager->saveToXMLFile("test.xml"); |
To retrieve a pointer to an existing layer
|
CL_SceneLayer *Layer = m_SceneManager->findLayer("layer1"); |
To delete a layer
|
m_SceneManager->removeLayer("layer1"); |
To exchange the Z-Order of two layers
|
m_SceneManager->swapLayers("layer1", "layer2"); |
License
This code is released under the following license
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. |
By Alan Thorn (http://www.alanthorn.net) Please report any bugs