AppEasy Core SDK  1.5.0
Cross platform mobile and desktop app and game development SDK - The easy way to make apps
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
CzSprite.h
Go to the documentation of this file.
00001 // 
00002 //
00003 // AppEasy SDK - Cross Platform Multi-purpose Game and App Engine
00004 //
00005 // Developed by Matthew Hopwood of Pocketeers Limited - www.pocketeers.co.uk
00006 //
00007 // For updates, tutorials and more details check out www.appeasymobile.com
00008 //
00009 // This code is provided free of charge and without any warranty whatsoever. You must ensure that this whole notice is present in all files and derivatives, so the reader knows its origin.
00010 // If you use this SDK in your product then please ensure that you credit AppEasy's usage appropriately. Please see www.appeasymobile.com for licensing details and support
00011 //
00012 //
00013 
00014 #if !defined(_CCZ_SPRITE_H_)
00015 #define _CCZ_SPRITE_H_
00016 
00017 #include "CzTypes.h"
00018 #include "CzAnim.h"
00019 #include "CzImage.h"
00020 #include "CzFont.h"
00021 #include "CzBrush.h"
00022 #include "CzRender.h"
00023 #include "CzGeometry.h"
00024 
00025 class CzSpriteManager;
00026 class CzActor;
00027 
00028 /**
00029  @class CzSprite
00030 
00031  @brief A sprite is the visual representation of an on screen game object
00032     
00033  Can be thought of as an interface rather than a concrete class as other sprite types are created from this.
00034 
00035  */
00036 
00037 class CzSprite
00038 {
00039 public:
00040     static uint16 SpriteIndices[];
00041 
00042     /**
00043      @enum  eType
00044     
00045      @brief Values that represent the type of sprite to render.
00046      */
00047     enum eType
00048     {
00049         ST_None,    ///< Invalid type
00050         ST_Image,   ///< Renders as an image sprite
00051         ST_Text,    ///< Renders as a text sprite
00052         ST_Dummy,   ///< A dummy sprite has no visible component and is only meant to provide invisible transforms
00053         ST_9Patch,  ///< Renders as a 9-patch image sprite
00054         ST_Poly,    ///< Renders as a poygon image sprite
00055     };
00056     enum eAnchor
00057     {
00058         Centre,     ///< Sprite will be anchored around its centre
00059         TopLeft,    ///< Sprite will be anchored around its top left corner
00060     };
00061 
00062     // Provide public access to iteration of the sprite list
00063     typedef CzList<CzSprite*>::iterator Iterator;
00064     Iterator        begin() { return Children.begin(); }
00065     Iterator        end()   { return Children.end(); }
00066 
00067     // Properties
00068 protected:
00069     CzActor*                Actor;                  ///< The actor that this sprite is attached to
00070     CzGeometry*             Geometry;               ///< Geometry used to display the sprite
00071     CzSprite*               Parent;                 ///< Parent sprite
00072     CzList<CzSprite*>       Children;               ///< List of children sprites
00073     eType                   SpriteType;             ///< Type of sprite
00074     CzSpriteManager*        Manager;                ///< Parent sprite manager
00075     float                   Width, Height;          ///< Destination width and height (used to represent the visible extents of the sprite on screen)
00076     float                   Depth;                  ///< Depth
00077     CzMatrix3               Transform;              ///< Transform
00078     CzVec2                  Position;               ///< Position of the sprite
00079     CzVec2                  Origin;                 ///< Origin of sprite (0, 0 is sprites centre)
00080     CzVec4                  Skew;                   ///< 4 vertex skew offsets (3 bit fixed)
00081     float                   Angle;                  ///< Rotation of sprite (degrees)
00082     float                   ScaleX;                 ///< X axis scale of sprite
00083     float                   ScaleY;                 ///< X axis scale of sprite
00084     CzColour                Colour;                 ///< Colour of sprite
00085     bool                    Visible;                ///< Sprites visible state
00086     bool                    Pooled;                 ///< Tells system if we belong to a sprite pool or not
00087     bool                    InUse;                  ///< Used in a memory pooling system to mark this sprite as in use
00088     bool                    BeforeChildren;         ///< When true th parent sprite will be drawn before all children otherwise after
00089     int                     Layer;                  ///< Depth layer
00090     CzSprite*               LinkedTo;               ///< When a sprite is linked to another it takes on that sprites transform etc..
00091     IzBrush*                Brush;                  ///< Current brush assigned to this sprite
00092     CzVec4                  ClipRect;               ///< Local clipping rect used to clip this sprites children
00093     bool                    Orphan;                 ///< When set to true sprites are orphaned outside the usual parent / child hierarchy and layered as independent objects
00094     bool                    IgnoreCamera;           ///< When set to true sprite ignores camera transform
00095     eAnchor                 Anchor;                 ///< Position the sprite is anchored around
00096 public:
00097     void            setActor(CzActor* owner)                { Actor = owner; }
00098     void            setParent(CzSprite* parent)             { Parent = parent; }
00099     CzSprite*       getParent()                             { return Parent; }
00100     void            addChild(CzSprite* sprite);
00101     void            removeChild(CzSprite* sprite, bool delete_sprites = true);
00102     void            setSpriteType(eType type)               { SpriteType = type; }
00103     eType           getSpriteType() const                   { return SpriteType; }
00104     void            setManager(CzSpriteManager* manager)    { Manager = manager; TransformDirty = true; }
00105     CzSpriteManager* getManager()                           { return Manager; }
00106     void            setDestSize(int width, int height)
00107     {
00108         Width = (float)width;
00109         Height = (float)height;
00110         TransformDirty = true;
00111     }
00112     void            setDepth(float depth)       { if (depth != Depth) TransformDirty = true; Depth = depth; }
00113     float           getDestWidth() const        { return Width; }
00114     float           getDestHeight() const       { return Height; }
00115     CzVec2          getDestSize() const         { return CzVec2(Width, Height); }
00116     float           getDepth() const            { return Depth; }
00117     void            setPosAngScale(float x, float y, float angle, float scale)
00118     {
00119         if (x != Position.x || y != Position.y || angle != Angle || scale != ScaleX || scale != ScaleY)
00120         {
00121             Position.x = x;
00122             Position.y = y;
00123             Angle = angle;
00124             ScaleX = ScaleY = scale;
00125             TransformDirty = true;
00126         }
00127     }
00128     void            setPosAngScale(float x, float y, float angle, float scale_x, float scale_y)
00129     {
00130         if (x != Position.x || y != Position.y || angle != Angle || scale_x != ScaleX || scale_y != ScaleY)
00131         {
00132             Position.x = x;
00133             Position.y = y;
00134             Angle = angle;
00135             ScaleX = scale_x;
00136             ScaleY = scale_y;
00137             TransformDirty = true;
00138         }
00139     }
00140     void            setPosition(float x, float y)
00141     {
00142         if (x != Position.x || y != Position.y)
00143         {
00144             Position.x = x;
00145             Position.y = y;
00146             TransformDirty = true;
00147         }
00148     }
00149     CzVec2          getPosition() const         { return Position; }
00150     void            setOrigin(float x, float y)
00151     {
00152         if (x != Origin.x || y != Origin.y)
00153         {
00154             Origin.x = x;
00155             Origin.y = y;
00156             TransformDirty = true;
00157         }
00158     }
00159     CzVec2          getOrigin() const           { return Origin; }
00160     void            setSkew(float x1, float x2, float y1, float y2)
00161     {
00162         if (Skew.x != x1 || Skew.y != x2 || Skew.z != y1 || Skew.w != y2)
00163         {
00164             Skew.x = x1;
00165             Skew.y = x2;
00166             Skew.z = y1;
00167             Skew.w = y2;
00168             TransformDirty = true;
00169         }
00170     }
00171     CzVec4          getSkew() const         { return Skew; }
00172     void            setAngle(float angle)
00173     {
00174         if (angle != Angle)
00175         {
00176             Angle = angle;
00177             TransformDirty = true;
00178         }
00179     }
00180     float           getAngle() const            { return Angle; }       
00181     void            setScale(float scale)
00182     {
00183         if (scale != ScaleX || scale != ScaleY)
00184         {
00185             ScaleX = scale;
00186             ScaleY = scale;
00187             TransformDirty = true;
00188         }
00189     }
00190     void            setScale(float scale_x, float scale_y)
00191     {
00192         if (scale_x != ScaleX || scale_y != ScaleY)
00193         {
00194             ScaleX = scale_x;
00195             ScaleY = scale_y;
00196             TransformDirty = true;
00197         }
00198     }
00199     float           getScale() const            { return ScaleX; }
00200     float           getScaleX() const           { return ScaleX; }
00201     float           getScaleY() const           { return ScaleY; }
00202     virtual void    setColour(const CzColour& colour, bool use_parent = false)
00203     {
00204         Colour = colour;
00205         if (use_parent && LinkedTo != NULL)
00206             Colour.a = (uint8)(((int)colour.a * LinkedTo->getOpacity()) / 255);
00207         if (Colour.a == 0 && colour.a != 0)
00208             TransformDirty = true;
00209     }
00210     CzColour        getColour() const           { return Colour; }
00211     int             getOpacity() const          { return Colour.a; }
00212     void            setVisible(bool show)       { Visible = show; }
00213     bool            isVisible() const           { return Visible; }
00214     void            setFilter(bool enable)      { Material->Filter = enable; }
00215     bool            isFilter() const            { return Material->Filter; }
00216     bool            isVisibleWithParents() const;
00217     void            forceTransformDirty()       { TransformDirty = true; }
00218     void            setPooled(bool pooled)      { Pooled = pooled; }
00219     bool            isPooled() const            { return Pooled; }
00220     void            setInUse(bool in_use)       { InUse = in_use; }
00221     bool            isInUse() const             { return InUse; }
00222     void            setLayer(int layer)         { Layer = layer; }
00223     int             getLayer() const            { return Layer; }
00224     CzMatrix3&      getTransform()              { if (TransformDirty) RebuildTransform(); return Transform; }
00225     void            setLinkedTo(CzSprite* sprite);
00226     CzSprite*       getLinkedTo()               { return LinkedTo; }
00227     float           getAccumDepth() const       { return AccumDepth; }
00228     bool            isTransformDirty() const    { return TransformDirty; }
00229     IzBrush*        getBrush()                  { return Brush; }
00230     int             getVertexCount() const      { return Prim->VertCount; }
00231     void            setClipRect(CzVec4& rc);
00232     CzVec4          getClipRect() const         { return ClipRect; }
00233     CzVec4          getScreenClipRect() const   { return ScreenClipRect; }
00234     void            setBeforeChildren(bool before) { BeforeChildren = before; }
00235     bool            getBeforeChildren() const   { return BeforeChildren; }
00236     CzMatrix3&      getFinalTransform()         { return FinalTransform; }
00237     bool            affectsClip() const         { return ClipRect.w >= 0; }
00238     CzVec4          FindFirstClipRect();
00239     CzVec4          FindFirstScreenClipRect();
00240     CzVec4          FindFirstScreenClipRect2();
00241     virtual bool    isClipped();
00242     CzVec2*         getScreenV()                { return Prim->Verts; }
00243     void            setAlphaMode(eCzAlphaMode mode)     { Material->AlphaMode = mode; }
00244     eCzAlphaMode    getAlphaMode() const                    { return Material->AlphaMode; }
00245     void            setOrphan(bool enable)              { Orphan = enable; }
00246     bool            getOrphan() const                   { return Orphan; }
00247     void            setIgnoreCamera(bool enable)        { IgnoreCamera = enable; }
00248     bool            getIgnoreCamera() const             { return IgnoreCamera; }
00249     void            setTiled(bool tiled)                { Material->Tiled = tiled; }
00250     bool            isTiled() const                     { return Material->Tiled; }
00251     CzRenderPrim*   getPrim()                           { return Prim; }
00252     CzRenderMaterial* getMaterial()                     { return Material; }
00253     eAnchor         getAnchor() const                   { return Anchor; }
00254     void            setAnchor(eAnchor anchor)           { if (Anchor != anchor) TransformDirty = true; Anchor = anchor; }
00255     CzGeometry*     getGemoetry()                       { return Geometry; }
00256     virtual void    setGeometry(CzGeometry* geom);
00257 
00258     // Properties End
00259 protected:
00260     bool            ChildChangeClip;            ///< Set to true if child changes clip rect
00261     CzMatrix3       FinalTransform;             ///< Final transform
00262     bool            TransformDirty;             ///< Dirty when transform changed
00263     CzRenderPrim*   Prim;                       ///< 2D renderer primitive used to render this sprite
00264     CzRenderMaterial* Material;                 ///< Material used to render this sprite
00265     float           AccumDepth;                 ///< Accumulated depth
00266     CzVec4          ScreenClipRect;             ///< Transformed screen clipping rect for this sprite
00267 
00268     void            TransformClipRect();        ///< Transforms the local clipping rect to screen coords
00269     virtual void    RebuildTransform();         ///< Rebuilds the display transform
00270     virtual void    BuildFinalTransform();      ///< Buiolds the final transform
00271     virtual void    TransformVertices();        ///< Builds a list of transformed vertices
00272     void            UpdateClipping();
00273 
00274 public:
00275     CzSprite() : Pooled(false), Prim(NULL), Material(NULL), Actor(NULL), Geometry(NULL)
00276     {
00277         setSpriteType(ST_None);
00278 #if defined(CZ_ENABLE_METRICS)
00279         CzMetrics::TotalSpritesCreated++;
00280 #endif
00281     }
00282     virtual ~CzSprite();
00283     virtual void    Init(int vertex_count = 4);         // Called to initialise the sprite, used after construction or to reset the sprite in a pooled sprite system
00284     virtual bool    Update();                           // Updates the sprite
00285     virtual void    Draw() = 0;                         // Pure virtual, need to implement in derived classes
00286     virtual void    DrawChildren();                     // Draws the sprites children
00287     virtual bool    HitTest(float x, float y);          // Check to see if point is within area covered by transformed sprite
00288     virtual bool    HitTestNoClip(float x, float y);    // Check to see if point is within area covered by transformed sprite
00289     virtual bool    isOutsideFocusRange(float x, float y, float scale = 1.0f);
00290     virtual CzVec2  TransformPoint(float x, float y);   // Transform point by sprites local angle / scale transform
00291     virtual CzVec2  TransformPointToScreen(float x, float y);   // Transform point by sprites final transform
00292     void            BringToFront();
00293 
00294     virtual void    RebuildTransformNow();              // Rebuilds the display transform immediately
00295 
00296     bool            isClippedByManager(uint16 *indices = NULL, int count = 4);
00297     virtual bool    SimpleTestOverlap(CzSprite* other);
00298     virtual bool    TestOverlap(CzSprite* other, int i1, int i2, int i3);
00299     virtual bool    TestOverlap(CzSprite* other);
00300 };
00301 
00302 
00303 
00304 
00305 
00306 #endif // _CCZ_SPRITE_H_