{"id":354,"date":"2011-10-14T20:50:07","date_gmt":"2011-10-14T20:50:07","guid":{"rendered":"http:\/\/www.drmop.com\/?p=354"},"modified":"2011-10-18T22:28:22","modified_gmt":"2011-10-18T22:28:22","slug":"marmalade-sdk-ciwgamesprite-creating-a-robust-sprite-class","status":"publish","type":"post","link":"http:\/\/www.drmop.com\/index.php\/2011\/10\/14\/marmalade-sdk-ciwgamesprite-creating-a-robust-sprite-class\/","title":{"rendered":"Marmalade SDK Tutorial &#8211; CIwGameSprite &#8211; Creating a Robust Sprite Class"},"content":{"rendered":"<p>This tutorial is part of the Marmalade SDK tutorials collection. To see the tutorials index <a title=\"Marmalade SDK Tutorials Index\" href=\"http:\/\/www.drmop.com\/index.php\/marmalade-sdk-tutorials\/\" target=\"_self\">click here<\/a><\/p>\n<p>Well, another busy day and as usual I have too much to do and there are only 24 hours in a day (I could do with coding and blogging on a ship travelling faster then the speed of light, so I can finish before I even started!). As usual if you just want the code then <a title=\"Sprite Marmalade Tutorial SDK Source Code\" href=\"http:\/\/www.drmop.com\/wp-content\/uploads\/2011\/10\/Sprite.zip\" target=\"_self\">grab it from here<\/a>. if you want the details then please read on<\/p>\n<p>I\u2019m getting quite excited about the tutorial series as we are finally building up to something much more useful than a simple bunch of loosely connected tutorials. That something is a \u201c2D game engine\u201d that will allow you to create cool fast games using the Marmalade SDK that you can deploy to a bucket load of platforms simultaneously and hopefully earn an even bigger bucket load of cash from!<\/p>\n<p>Today we are going to begin our game engine by implementing one of the most basic components of a 2D game engine, The Sprite.<\/p>\n<h2>The Sprite<\/h2>\n<p>From our game engines point of view, we are going to define a sprite as a visual component that can move and animate on the users screen. I wont go as far as defining a sprite as a bitmap image because our sprites are special, they can be anything we define them to be. They can be anything from a simple point, line  or bitmap to something as extravagant as a vector based image. We basically want to leave our options open and offer as much extensibility to our game engine as possible.<\/p>\n<p>One very important point to make at this point. A sprite is only a visual component and should not be made to deal with game logic, collision detection, playing audio etc. It should only be concerned with drawing itself. Some people like this separation of concerns style programming but others (like me), whilst others do not.<\/p>\n<p>Ok, so what do we want our basic sprite to do? Lets make a list:<\/p>\n<ul>\n<li>Move around the screen<\/li>\n<li>Scale<\/li>\n<li>Rotate<\/li>\n<li>Change colour \/ transparency (allows flashing, fades etc..)<\/li>\n<li>Draw itself<\/li>\n<li>Managed by some controller class (so we don\u2019t have to deal with allocation \/ deletion)<\/li>\n<li>Ability to be pooled to reduce memory fragmentation<\/li>\n<\/ul>\n<p>In rides CIwGameSprite our new sprite class<\/p>\n<h2>CIwGameSprite \u2013 The mother of all sprite classes<\/h2>\n<p>Erm, put down that Marmalade SDK documentation, don\u2019t let the name fool you, you wont find this class in there. I have named the class as such so that it feels more like its part of the Marmalade SDK.<\/p>\n<p>CIwGameSprite is the name of our basic sprite class that we are going to base our 2D game engine around. CIwGameSprite is not actually a usable class in the sense that you can create one and do something with it. CIwGameSprite acts as a base class for other types of sprite classes and defines some basic sprite information that is going to be common to all types of sprites. Lets take a quick look at CIwGameSprite (defined in the IwGameSprite.h header file) to see what it does.<\/p>\n<pre>\r\n<blockquote>\r\n\r\nclass <span style=\"color: #0000ff;\">CIwGameSprite<\/span>\r\n{\r\n    <span style=\"color: #008000;\">\/\/\/ Properties<\/span>\r\nprotected:\r\n    CIwGameSpriteManager* <span style=\"color: #808000;\">Parent<\/span>;       <span style=\"color: #008000;\">\/\/ Parent sprite manager<\/span>\r\n    CIwSVec2    <span style=\"color: #808000;\">Position<\/span>;               <span style=\"color: #008000;\">\/\/ Position of the sprite<\/span>\r\n    iwangle     <span style=\"color: #808000;\">Angle<\/span>;                  <span style=\"color: #008000;\">\/\/ Rotation of sprite (IW_ANGLE_2PI = 360 degrees)<\/span>\r\n    iwfixed     <span style=\"color: #808000;\">Scale<\/span>;                  <span style=\"color: #008000;\">\/\/ Scale of sprite (IW_GEOM_ONE = 1.0)<\/span>\r\n    CIwColour   <span style=\"color: #808000;\">Colour<\/span>;                 <span style=\"color: #008000;\">\/\/ Colour of sprite<\/span>\r\n    bool        <span style=\"color: #808000;\">Visible<\/span>;                <span style=\"color: #008000;\">\/\/ Sprites visible state<\/span>\r\n    bool        <span style=\"color: #808000;\">Pooled<\/span>;                 <span style=\"color: #008000;\">\/\/ Tells system if we belong to a sprite pool or not<\/span>\r\n    bool        <span style=\"color: #808000;\">InUse<\/span>;                  <span style=\"color: #008000;\">\/\/ Used in a memory pooling system to mark this sprite as in use<\/span>\r\npublic:\r\n    void        setParent(CIwGameSpriteManager* parent) { Parent = parent; }\r\n    void        setPosAngScale(int x, int y, iwangle angle, iwfixed scale)\r\n    {\r\n        Position.x = x;\r\n        Position.y = y;\r\n        Angle = angle;\r\n        Scale = scale;\r\n        TransformDirty = true;\r\n    }\r\n    void        setPosition(int x, int y)\r\n    {\r\n        Position.x = x;\r\n        Position.y = y;\r\n        TransformDirty = true;\r\n    }\r\n    CIwSVec2    getPosition() const         { return Position; }\r\n    void        setAngle(iwangle angle)\r\n    {\r\n        Angle = angle;\r\n        TransformDirty = true;\r\n    }\r\n    iwangle     getAngle() const            { return Angle; }\r\n    void        setScale(iwfixed scale)\r\n    {\r\n        Scale = scale;\r\n        TransformDirty = true;\r\n    }\r\n    iwfixed     getScale() const            { return Scale; }\r\n    void        setColour(CIwColour colour) { Colour = colour; }\r\n    CIwColour   getColour() const           { return Colour; }\r\n    void        setVisible(bool show)       { Visible = show; }\r\n    bool        isVisible() const           { return Visible; }\r\n    void        forceTransformDirty()       { TransformDirty = true; }\r\n    void        setPooled(bool pooled)      { Pooled = pooled; }\r\n    bool        isPooled() const            { return Pooled; }\r\n    void        setInUse(bool in_use)       { InUse = in_use; }\r\n    bool        isUsed() const              { return InUse; }\r\n\r\n    <span style=\"color: #008000;\">\/\/\/ Properties End<\/span>\r\nprotected:\r\n    CIwMat2D    Transform;                 <span style=\"color: #008000;\">\/\/ Transform<\/span>\r\n    bool        TransformDirty;            <span style=\"color: #008000;\">\/\/ Dirty when transform change<\/span>\r\n\r\n    void        RebuildTransform();        <span style=\"color: #008000;\">\/\/ Rebuilds the display transform<\/span>\r\n\r\npublic:\r\n    CIwGameSprite() : Pooled(false) { Init(); }\r\n    virtual ~CIwGameSprite() {}\r\n\r\n    virtual void    Init();                <span style=\"color: #008000;\">\/\/ Called to initialise the sprite, used after construction or to reset the sprite in a pooled sprite system<\/span>\r\n    <span style=\"color: #008080;\">virtual void    Draw() = 0<\/span>;            <span style=\"color: #008000;\">\/\/ Pure virtual, need to implement in derived classes<\/span>\r\n};<\/blockquote>\r\n<\/pre>\n<p>Ok, we see that we have a bunch of properties for our sprite:<\/p>\n<ul>\n<li>Parent \u2013 Our sprites are managed by a sprite manager, the parent of our sprites will be the sprite manager that is looking after them (more on the sprite manager later)<\/li>\n<li>Position \u2013 The 2D position of our sprite in 2D space<\/li>\n<li>Angle \u2013 The orientation of our sprite in 2D space (Marmalade SDK angles range from 0 to IW_ANGLE_2PI)<\/li>\n<li>Scale \u2013 The scale of our sprite in 2D space (Marmalade SDK used IW_GEOM_ONE as the value of 1.0f)<\/li>\n<li>Colour \u2013 The colour and transparency of our sprite<\/li>\n<li>Visible \u2013 The visible state of our sprite (can the user see it?)<\/li>\n<li>Pooled \u2013 Should our sprites data be deleted when it is removed from the sprite manager (more on sprite pooling later)<\/li>\n<li>InUse \u2013 Used to mark a sprite as being used in a pooled sprite system<\/li>\n<\/ul>\n<p>We also have a few private member variables in there that deal with the transform for our sprite. We covered the Iw2D transform in our Iw2D sprite example code in a previous tutorial. Note that building transforms can be expensive in terms of time when we are rebuilding lots of them every frame, so in our sprite class we only going to rebuild the sprites transform when the sprites position, scale or angle changes (no sense doing all that work if nothings changed).<\/p>\n<p>Note that our Draw() method is a pure virtual method which makes the class abstract. This means that you cannot and are not supposed to create instances of this class. Instead it serves as an interface to defining classed based upon that class. Now thats said lets take a look at a class that we have derived from CIwGameSprite called CIwGameBitmapSprite.<\/p>\n<h2>CIwGameBitmapSprite \u2013 In Comes the Meat<\/h2>\n<p>Here is the class definition for CIwGameBitmapSprite:<\/p>\n<pre>\r\n<blockquote>\r\n\r\nclass <span style=\"color: #ff6600;\">CIwGameBitmapSprite <\/span>: public <span style=\"color: #0000ff;\">CIwGameSprite<\/span>\r\n{\r\n    <span style=\"color: #008000;\">\/\/ Properties<\/span>\r\nprotected:\r\n    CIw2DImage*    Image;                    <span style=\"color: #008000;\">\/\/ Bitmapped image that represents this sprite<\/span>\r\n    int            Width, Height;            <span style=\"color: #008000;\">\/\/ Destination width and height<\/span>\r\n    int            SrcX, SrcY;              <span style=\"color: #008000;\"> \/\/ Top left position in source texture<\/span>\r\n    int            SrcWidth, SrcHeight;      <span style=\"color: #008000;\">\/\/ Width and height of sprite in source texture<\/span>\r\npublic:\r\n    void        setImage(CIw2DImage* image)\r\n    {\r\n        Image = image;\r\n    }\r\n    void        setDestSize(int width, int height)\r\n    {\r\n        Width = width;\r\n        Height = height;\r\n    }\r\n    void        setSrcRect(int x, int y, int width, int height)\r\n    {\r\n        SrcX = x;\r\n        SrcY = y;\r\n        SrcWidth = width;\r\n        SrcHeight = height;\r\n    }\r\n    \/\/ Properties End\r\npublic:\r\n\r\n    CIwGameBitmapSprite() : CIwGameSprite(), Image(NULL)    {}\r\n    virtual ~CIwGameBitmapSprite() {}\r\n\r\n    void    Draw();\r\n};<\/blockquote>\r\n<\/pre>\n<p>As you can see we have derived CIwGameBitmapSprite from CIwGameSprite, basically borrowing all of its functionality and then adding on some more. This class now represents a visual component that is represented by a bitmap, or in this case a CIw2DImage.<\/p>\n<p>You can see that we have added a few additional properties:<\/p>\n<ul>\n<li>Image \u2013 Our Iw2D bitmap image that we have previously loaded somewhere<\/li>\n<li>Width \u2013 The width that the sprite will appear on screen at a scale of 1.0<\/li>\n<li>Height \u2013 The height that the sprite will appear on screen at a scale of 1.0<\/li>\n<li>SrcX, SrcY, SrcWidth and SrcHeight \u2013 These 4 variables are all inter-related. They mark a rectangular area within our source Iw2D image, which allows us to render just a portion of a large image instead of the whole thing, allowing us to use sprite sheets. If you haven\u2019t heard of sprite sheets (or sprite atlases) then you can think of them as a collection of images arranged onto one large image. For example, you may have arranged a whole bunch of animation frames of one of your characters onto one large bitmap. This system will allow us to pick out the smaller images and render them without worrying about the rest.<\/li>\n<\/ul>\n<p>Ok, we now have a fully functional bitmapped sprite class that will allow us to draw bitmapped sprites to the screen. We can also spin, scale, move, hide and even change the colour or transparency of these sprites<\/p>\n<h2>CIwGameSpriteManager \u2013 Managing our Little Sprite Children<\/h2>\n<p>I like to compare instantiated classes to children. if you allow them, they will misbehave and be difficult to manage. To help prevent your unruly sprites from misbehaving and crashing your awesome game, you need something to manage them, a common place to examine them, draw them and delete them when no longer needed (ok the comparison with children stops at that one!)<\/p>\n<p>So to manage our sprites we create a sprite manager (CIwGameSpriteManager). Lets take a quick look at the CIwGameSpriteManager class:<\/p>\n<pre>\r\n<blockquote>\r\n\r\nclass CIwGameSpriteManager\r\n{\r\npublic:\r\n    <span style=\"color: #008000;\">\/\/ Provide public access to iteration of the sprite list<\/span>\r\n<span style=\"color: #666699;\">    typedef CIwList::iterator    Iterator;\r\n    Iterator        begin()     { return Sprites.begin(); }\r\n    Iterator        end()       { return Sprites.end(); }<\/span>\r\n\r\n    <span style=\"color: #008000;\">\/\/ Properties<\/span>\r\nprotected:\r\n    CIwMat2D        Transform;          <span style=\"color: #008000;\">\/\/ Transform<\/span>\r\n    CIwList         Sprites;            <span style=\"color: #008000;\">\/\/ Our list of sprites<\/span>\r\npublic:\r\n    void            addSprite(CIwGameSprite* sprite);\r\n    void            removeSprite(CIwGameSprite* sprite, bool delete_sprites = true);\r\n    void            setTransform(const CIwMat2D&amp; transform)    { Transform = transform; DirtyChildTransforms(); }\r\n    const CIwMat2D&amp; getTransform() const                       { return Transform; }\r\n\r\n    \/\/ Properties End\r\n\r\nprotected:\r\n    void            DirtyChildTransforms();\r\n\r\npublic:\r\n    CIwGameSpriteManager()\r\n    {\r\n        <span style=\"color: #008000;\">\/\/ Set up default rotation, scaling and translation<\/span>\r\n        Transform.SetIdentity();\r\n        Transform.m[0][0] = IW_GEOM_ONE;\r\n        Transform.m[1][1] = IW_GEOM_ONE;\r\n    }\r\n    ~CIwGameSpriteManager() { Release(); }\r\n\r\n    void            Draw();\r\n    void            Release(bool delete_sprites = true);\r\n};<\/blockquote>\r\n<\/pre>\n<p>Ok, if you aren&#8217;t fond on collections \/ iterators and the likes then you probably won\u2019t like this class very much. I will admit, i am quite obsessed with them. I find them \u201ctoo\u201d useful.<\/p>\n<p>I chose to use Marmalades CIwList class, which is basically a templated linked list class that lets you define a list of objects \/ data that is somehow related. In our case we have a list of sprites (A linked list is basically a list of objects where each object points to another and then that object points to another and so on).<\/p>\n<p>Ok, so this class provides some basic functionality:<\/p>\n<ul>\n<li>We can add sprites to our manager using addSprite() and not worry about having to delete them when we are done with them. Simply deleting the sprite manager will delete all of the sprites for us<\/li>\n<li>We can remove individual sprites from our manager using removeSprite()<\/li>\n<li>We can draw all of our sprites in one go<\/li>\n<li>We can set a base transform that all of our sprites that are managed by this sprite manager are transformed by. This allows us to rotate, move and scale all of the sprites in one go by the same amount. This can be great for applying effects to all of your sprites and \/ or simply ensuring that all sprites are scaled and translated to fit on any sized screen.<\/li>\n<\/ul>\n<h2>What\u2019s changed in the example code<\/h2>\n<p>If you build and run the Sprite project you will see 100 sprites spinning around their centres with the whole scene of sprites spinning and scaling around the centre of the screen:<\/p>\n<figure id=\"attachment_358\" aria-describedby=\"caption-attachment-358\" style=\"width: 330px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/www.drmop.com\/wp-content\/uploads\/2011\/10\/Sprite_Example.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-358\" title=\"Sprite Marmalade SDK Example Screenshot\" src=\"http:\/\/www.drmop.com\/wp-content\/uploads\/2011\/10\/Sprite_Example.png\" alt=\"\" width=\"330\" height=\"553\" srcset=\"http:\/\/www.drmop.com\/wp-content\/uploads\/2011\/10\/Sprite_Example.png 330w, http:\/\/www.drmop.com\/wp-content\/uploads\/2011\/10\/Sprite_Example-179x300.png 179w\" sizes=\"auto, (max-width: 330px) 100vw, 330px\" \/><\/a><figcaption id=\"caption-attachment-358\" class=\"wp-caption-text\">Sprite Marmalade SDK Example Screenshot<\/figcaption><\/figure>\n<p>We have re-used the code from our previous Audio tutorial but ripped a few bits out of the main loop to clarify what\u2019s going on.<\/p>\n<p>Our first change involves creating a sprite manager and then adding some sprites:<\/p>\n<pre>\r\n<blockquote>\r\n<span style=\"color: #008000;\">\/\/ Create a sprite manager and a bunch of sprites<\/span>\r\nCIwGameSpriteManager* sprite_manager = new CIwGameSpriteManager();\r\nfor (int t = 0; t &lt; 100; t++)\r\n{\r\n    <span style=\"color: #008000;\">\/\/ Create sprite<\/span>\r\n    CIwGameBitmapSprite* sprite = new CIwGameBitmapSprite();\r\n    <span style=\"color: #008000;\">\/\/ Set sprite position, angle and scale<\/span>\r\n    sprite-&gt;setPosAngScale(IwRandMinMax(-max_size, max_size), IwRandMinMax(-max_size, max_size), 0, IW_GEOM_ONE);\r\n    <span style=\"color: #008000;\">\/\/ Give sprite a random colour<\/span>\r\n    CIwColour colour;\r\n    colour.r = 55 + IwRandMinMax(0, 200);\r\n    colour.g = 55 + IwRandMinMax(0, 200);\r\n    colour.b = 55 + IwRandMinMax(0, 200);\r\n    colour.a = 255;\r\n    sprite-&gt;setColour(colour);\r\n    <span style=\"color: #008000;\">\/\/ Set sprite image<\/span>\r\n    if ((t &amp; 1) == 0)\r\n        sprite-&gt;setImage(image1);\r\n    else\r\n        sprite-&gt;setImage(image2);\r\n    <span style=\"color: #008000;\">\/\/ Add sprite to sprite manager<\/span>\r\n    sprite_manager-&gt;addSprite(sprite);\r\n}<\/blockquote>\r\n<\/pre>\n<p>Note that the code is a lot simpler than it looks. We are basically create 100 sprites giving each one a random position, random colour and alternative between two different bitmaps.<\/p>\n<p>I want to point out here that using two separate images is bad practice as the underlying Iw2D render will need to switch states continually to switch between both images. To improve performance we would combine both images into a sprite sheet and then change the rectangular area within the image for each sprite.<\/p>\n<p>Next we define a world transform matrix and world angle that we can modify on a per frame basis:<\/p>\n<pre>\r\n<blockquote>\r\n<span style=\"color: #008000;\">\/\/ Dynamic variables<\/span>\r\nCIwMat2D    WorldTransform;\r\niwangle     WorldAngle = 0;<\/blockquote>\r\n<\/pre>\n<p>Next we spin our sprites by walking the sprites list adjusting the angle of each sprite at a different rate (it would be boring if all of our sprites rotated at the same rate)<\/p>\n<pre>\r\n<blockquote>\r\n<span style=\"color: #008000;\">\/\/ Update all sprite rotations<\/span>\r\nint speed = 0;\r\nfor (CIwGameSpriteManager::Iterator it = sprite_manager-&gt;begin(); it != sprite_manager-&gt;end(); ++it, speed++)\r\n{\r\n    (*it)-&gt;setAngle((*it)-&gt;getAngle() + speed);\r\n}<\/blockquote>\r\n<\/pre>\n<p>Now we do a bit of matrix math jiggery pokery to spin and scale our sprite manager:<\/p>\n<pre>\r\n<blockquote>\r\n<span style=\"color: #008000;\">\/\/ Spin and scale the sprite manager<\/span>\r\nWorldTransform.SetIdentity();\r\nWorldTransform.SetRot(WorldAngle);\r\nWorldTransform.ScaleRot((IW_GEOM_ONE \/ 2) + (IW_GEOM_COS(WorldAngle) \/ 3));\r\nWorldTransform.SetTrans(CIwSVec2(surface_width \/ 2, surface_height \/ 2));\r\nsprite_manager-&gt;setTransform(WorldTransform);\r\nWorldAngle += 20;<\/blockquote>\r\n<\/pre>\n<p>We firstly reset our world transform using SetIdentity(), think of this as setting a variable to its default value. We then set the rotation, scale and translation that all sprites within the sprite manager will transform by. Note that we set the translation position to the middle of the screen because when we created our sprites earlier we defined their positions based around the origin being at 0,0 (top-left hand corner of screen). The world translation will move them back to the middle of the screen.<\/p>\n<p>We now send the world transform to the sprite manager and adjust our world angle, so everything spins.<\/p>\n<p>lastly, we tell our sprite manager to go away and draw its child sprites:<\/p>\n<pre>\r\n<blockquote>\r\n<span style=\"color: #008000;\">\/\/ Draw our sprite manager<\/span>\r\nsprite_manager-&gt;Draw();<\/blockquote>\r\n<\/pre>\n<p>And finally before we exit the game, we delete the sprite manager which in turn deletes all of our sprites.<\/p>\n<pre>\r\n<blockquote>\r\n<span style=\"color: #008000;\">\/\/ Clean up sprite manager<\/span>\r\ndelete sprite_manager;<\/blockquote>\r\n<\/pre>\n<h2>I Love Concatenating Matrix Transforms<\/h2>\n<p>If you are wondering how we manage to spin a sprite individually as well as transform it by the worlds scale and rotation then open up IwGameSprite.cpp and take a look at CIwGameSprite::RebuildTransform()<\/p>\n<pre>\r\n<blockquote>\r\nvoid CIwGameSprite::RebuildTransform()\r\n{\r\n    <span style=\"color: #008000;\">\/\/ Build the transform<\/span>\r\n    <span style=\"color: #008000;\">\/\/ Set the rotation transform<\/span>\r\n    Transform.SetRot(Angle);\r\n    <span style=\"color: #008000;\">\/\/ Scale the transform<\/span>\r\n    Transform.ScaleRot(Scale);\r\n    <span style=\"color: #008000;\">\/\/ Translate the transform<\/span>\r\n    Transform.SetTrans(Position);\r\n    Transform.PostMult(Parent-&gt;getTransform());\r\n    TransformDirty = false;\r\n}<\/blockquote>\r\n<\/pre>\n<p>We use a matrix math track called matrix concatenation to multiple two matrices together, effectively combing both transforms into the one single transform. This following line multiplies out two matrices together:<\/p>\n<pre>\r\n<blockquote>\r\nTransform.PostMult(Parent-&gt;getTransform());<\/blockquote>\r\n<\/pre>\n<h2>Object Pooling and Sprites<\/h2>\n<p>As your projects get larger and more complex you will find that you are constantly creating and deleting many objects. The constant process of allocating and deleting objects can take its toll on the memory management system, which causes something called fragmentation. Fragmentation is when your available memory pool consists of many small chunks instead of a few large chunks. Depending on the memory management system in use, this can increase the overhead of allocating future objects and risk the chance of running out of memory even though the system is reporting plenty of free memory (this happens because there isn\u2019t a large enough chunk of contiguous memory available to allocate).<\/p>\n<p>To help alleviate this problem we can pre-allocate a large number of objects in one go instead of lots of little ones at random times during the game. This is usually called an object pool.<\/p>\n<p>The problem with C++ is that it doesn\u2019t work very well with object pools because:<\/p>\n<ul>\n<li>The objects in the pool usually all need to be of the same type<\/li>\n<li>Constructors are usually called to construct and set up your object<\/li>\n<li>Destructor&#8217;s\u00a0are usually called to destroy and clean-up your object<\/li>\n<\/ul>\n<p>We can get around the last two problems quite easily by emptying out our constructors and destructors and putting the code into Init() and Release() methods instead. This will allow us to setup and tear down objects without every having to recreate or delete them. Also, for certain types of objects (those that need to be reset to a default state) I like to add a Reset() method which allows me to set the object back to its original state.<\/p>\n<p>The first problem is a lot more difficult to deal with, but the easiest solution is to simply create object pools for each type of object that you want to pool.<\/p>\n<p>Pooling objects is as easy as allocating a bunch of them during game boot and then using some kind of marking system to mark them as in use or not in use. Instead of creating a new object you would simply search the object pool for a object that is not in use then reset it and use it. The only thing to do is to delete the object pool before you exit your game.<\/p>\n<p>Well that\u2019s it for this tutorial. Our next tutorial will handle adding frame based animations to our bitmapped sprite system which will allow us to truly walk the path of making a cool game.<\/p>\n<p>You can download the code that accompanies this article <a title=\"Sprite Marmalade Tutorial SDK Source Code\" href=\"http:\/\/www.drmop.com\/wp-content\/uploads\/2011\/10\/Sprite.zip\" target=\"_self\">from here<\/a>.<\/p>\n<p>Hope you all find this blog useful and until next time, don\u2019t forget, dont read my blog at the wheel!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial is part of the Marmalade SDK tutorials collection. To see the tutorials index click here Well, another busy day and as usual I have too much to do and there are only 24 hours in a day (I could do with coding and blogging on a ship travelling faster then the speed of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13,29,43,118,44,42,28,3,45,1],"tags":[131,130,132,718,134,133],"class_list":["post-354","post","type-post","status-publish","format-standard","hentry","category-airplay-sdk","category-android-app-development","category-blackberry-playbook","category-blackberry-playbook-app-development","category-c-programming","category-game-and-app-development","category-ios-app-development","category-marmalade-sdk","category-programming","category-samsung-bada-development","category-uncategorized","tag-ciwgamebitmapsprite","tag-ciwgamesprite","tag-ciwgamespritemanager","tag-marmalade-sdk","tag-object-pooling","tag-sprite-engine"],"_links":{"self":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/comments?post=354"}],"version-history":[{"count":11,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/354\/revisions"}],"predecessor-version":[{"id":374,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/354\/revisions\/374"}],"wp:attachment":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/media?parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/categories?post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/tags?post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}