# 17.4 Layers

## 17.4 Layers

In photo applications and many 2D games, graphics are presented to the screen using a layer system. A layer can be visualized as a sheet of clear plastic upon which images can be drawn. Layers can be stacked atop one another, and where the layer is transparent it will show the contents of the layer immediately beneath, and so on. Furthermore, since each image (scene object) is a child of the layer that contains it, as a layer is moved so too will all the images drawn upon that layer.

Figure 17.2

A scene then is composed of a collection of layers and, in turn, the layers are composed of a collection of images, or scene objects. The scene represents one single coordinate space with an origin in the top-left corner (0,0). Every layer has an (x, y) coordinate relative to this origin. In turn, the layer has its own relative (local) coordinate space whose origin is at the layer's top-left corner (0,0), and when an image is positioned inside a layer, its position can be described as a relative (x, y) coordinate offset from the top-left corner of the layer. So, each image on a layer has two positions: an absolute position and a relative position. The absolute position refers to an image's complete (x, y) location in the scene coordinate space, which is the total sum of its position and its layer's position within the scene. The image's relative position is its (x, y) offset from the layer's origin.

So, as the scene is transformed in coordinate space to a specific (x, y) position, so too will the layers transform, maintaining their same relative distance to the origin of the coordinate space. And as the layers move, so also will the images drawn upon them, while still maintaining the same relative distance from the origin of the layer and therefore to any other images also drawn on that layer.

 Note In the context of the magic carpet example, the carpet, the boy, and the lantern would be part of one layer. The position of that layer would represent the position of the carpet, and as the carpet moved, so would all the other images drawn on that layer.

### 17.4.1 Implementing Layers

A scene manager manages an entire scene, a scene layer is a collection of images or sprites, and a scene object can be any image or animated sprite. This concept then, is the idea behind three specific classes: CL_SceneManager, CL_SceneObject (as discussed), and now, CL_SceneLayer.

So, the scene manager holds a collection of CL_SceneLayer objects, and each layer object in turn holds a collection of sprite objects. When the time comes to paint the sprite objects, the layer will do so by drawing all the visible scene objects onto its canvas, and the scene manager in turn will draw all of the layer surfaces onto its canvas as a final image of the scene.

Like scene objects, the layer can have a name, a position, and a visible status. Its position in turn will affect all scene objects that belong to it, and its visible status will also reflect the visibility of contained scene objects. Consider the following class declaration for CL_SceneLayer. The next section examines how to maintain a list of scene objects.

`      class CL_SceneLayer : public CL_Surface      {      private:         bool m_bVisible;         std::string m_Name;         CL_Vector2 m_Position;      protected:      public:         CL_SceneLayer();         ~CL_SceneLayer();         void draw(float x = 0, float y = 0, CL_GraphicContext* context = 0);         bool getVisible() {return m_bVisible;}         void setVisible(bool Visible) {m_bVisible = Visible;}         std::string getName() {return m_Name;}         void setName(std::string Name) {m_Name = Name;}         CL_Vector2* getPosition() {return &m_Position;}         float getX() {return m_Position.x;}         float getY() {return m_Position.y;}         void setPosition(CL_Vector2 Position) {m_Position = Position;}         void setPosition(float x, float y) {m_Position.x = x, m_Position.y = y;}      }; `

#### 17.4.1.1 Layer Scene Objects

Each layer maintains a collection of sprite objects that are members of the layer. The layer is a transparent surface and the sprites are drawn on the layer as each frame is painted to the display. One of the first features to incorporate into the layer class is the ability to hold a collection of CL_SceneObject classes. To do this, an array or a linked list could be used. For this example, I have chosen to use a class called std::vector, which can be considered a linked list since it may grow and shrink as items are added or removed. Subsequent sections demonstrate how this class can be used. The class declaration for CL_SceneLayer can be refined from the previous section to include a list of scene objects using the std:vector class.

`      class CL_SceneLayer : public CL_Surface      {      private:         std::vector<CL_SceneObject*> m_Objects;         CL_Canvas *m_Canvas; //Canvas to hold the composited pixel data         bool m_bVisible;         std::string m_Name;         CL_Vector2 m_Position;      protected:      public:         CL_SceneLayer();         ~CL_SceneLayer();         bool getVisible() {return m_bVisible;}         void setVisible(bool Visible) {m_bVisible = Visible;}         std::string getName() {return m_Name;}         void setName(std::string Name) {m_Name = Name;}         CL_Vector2* getPosition() {return &m_Position;}         float getX() {return m_Position.x;}         float getY() {return m_Position.y;}         void setPosition(CL_Vector2 Position) {m_Position = Position;}         void setPosition(float x, float y) {m_Position.x = x, m_Position.y = y;}         std::vector<CL_SceneObject*> *getObjects() {return &m_Objects;}      }; `

As discussed, scene objects are encapsulated in the CL_SceneObject class, and these can be added to the CL_SceneLayer class using the attachObject method. This method accepts as its argument a valid scene object to attach to the layer. The attachObject method in turn calls upon the std::vector class to add the scene object to the layer's list of objects. To add an item to a std::vector, the method push_back is called. Consider the following code:

`      void CL_SceneLayer::attachObject(CL_SceneObject* Object)      {         m_Objects.push_back(Object);      } `

#### 17.4.1.3 Cycling through Objects

As more and more objects are added to the scene object list, it will often be useful for the layer class to cycle through its list of objects to access each one. The std::vector class allows its elements to be accessed like a normal array. Consider the following:

`      for(unsigned int i = 0; i < m_Objects.size(); i++)      {         CL_SceneObject* Object = m_Objects[i];      } `

#### 17.4.1.4 Deleting Scene Objects

To clear the list of scene objects, the std::vector method clear can be called. This will erase each item in the list. Take a look at the following code:

`      m_Objects.clear(); `

Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

Similar book on Amazon