2010-07-22 09:39:15 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __SPRITE_CCSPRITE_FRAME_H__
|
|
|
|
#define __SPRITE_CCSPRITE_FRAME_H__
|
|
|
|
|
|
|
|
#include "CCNode.h"
|
|
|
|
#include "CCProtocols.h"
|
2010-08-04 16:45:00 +08:00
|
|
|
#include "NSMutableArray.h"
|
|
|
|
#include "NSObject.h"
|
2010-07-22 09:39:15 +08:00
|
|
|
|
2010-08-03 10:25:38 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-08-04 15:46:12 +08:00
|
|
|
namespace cocos2d {
|
2010-07-22 09:39:15 +08:00
|
|
|
class CGRect;
|
|
|
|
class CGPoint;
|
|
|
|
class CGSize;
|
|
|
|
class CCTexture2D;
|
2010-07-22 15:54:02 +08:00
|
|
|
class NSZone;
|
2010-07-22 09:39:15 +08:00
|
|
|
|
2010-09-28 18:27:33 +08:00
|
|
|
/** @brief A CCSpriteFrame has:
|
2010-07-22 09:39:15 +08:00
|
|
|
- texture: A CCTexture2D that will be used by the CCSprite
|
|
|
|
- rectangle: A rectangle of the texture
|
|
|
|
|
|
|
|
|
|
|
|
You can modify the frame of a CCSprite by doing:
|
|
|
|
|
2010-09-29 17:39:45 +08:00
|
|
|
CCSpriteFrame *frame = CCSpriteFrame::frameWithTexture(texture, rect, offset);
|
|
|
|
sprite->setDisplayFrame(frame);
|
2010-07-22 09:39:15 +08:00
|
|
|
*/
|
2010-09-09 10:17:38 +08:00
|
|
|
class CCX_DLL CCSpriteFrame : public NSObject
|
2010-07-22 09:39:15 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// attributes
|
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** get rect of the frame */
|
2010-07-22 09:39:15 +08:00
|
|
|
inline CGRect getRect(void) { return m_obRect; }
|
2010-09-28 16:18:05 +08:00
|
|
|
/** set rect of the frame */
|
2010-07-22 09:39:15 +08:00
|
|
|
inline void setRect(CGRect rect) { m_obRect = rect; }
|
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** get offset of the frame */
|
2010-07-22 09:39:15 +08:00
|
|
|
inline CGPoint getOffset(void) { return m_obOffset; }
|
2010-09-28 16:18:05 +08:00
|
|
|
/** set offset of the frame */
|
2010-07-22 09:39:15 +08:00
|
|
|
inline void setOffset(CGPoint offset) { m_obOffset = offset; }
|
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** get original size of the trimmed image */
|
2010-07-22 09:39:15 +08:00
|
|
|
inline CGSize getOriginalSize(void) { return m_obOriginalSize; }
|
2010-09-28 16:18:05 +08:00
|
|
|
/** set original size of the trimmed image */
|
2010-07-22 09:39:15 +08:00
|
|
|
inline void setOriginalSize(CGSize size) { m_obOriginalSize = size; }
|
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** get texture of the frame */
|
2010-07-22 09:39:15 +08:00
|
|
|
inline CCTexture2D* getTexture(void) { return m_pobTexture; }
|
2010-09-28 16:18:05 +08:00
|
|
|
/** set texture of the frame, the texture is retained */
|
2010-07-22 09:39:15 +08:00
|
|
|
inline void setTexture(CCTexture2D* pobTexture)
|
|
|
|
{
|
2010-09-27 18:19:10 +08:00
|
|
|
CCX_SAFE_RETAIN(pobTexture);
|
2010-09-06 11:58:14 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pobTexture);
|
2010-07-22 09:39:15 +08:00
|
|
|
m_pobTexture = pobTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2010-07-22 15:54:02 +08:00
|
|
|
~CCSpriteFrame(void);
|
|
|
|
virtual NSObject* copyWithZone(NSZone *pZone);
|
|
|
|
|
2010-07-22 09:39:15 +08:00
|
|
|
/** Create a CCSpriteFrame with a texture, rect and offset.
|
|
|
|
It is assumed that the frame was not trimmed.
|
|
|
|
*/
|
2010-07-22 15:54:02 +08:00
|
|
|
static CCSpriteFrame* frameWithTexture(CCTexture2D* pobTexture, CGRect rect, CGPoint offset);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
|
|
|
/** Create a CCSpriteFrame with a texture, rect, offset and originalSize.
|
|
|
|
The originalSize is the size in pixels of the frame before being trimmed.
|
|
|
|
*/
|
2010-07-22 15:54:02 +08:00
|
|
|
static CCSpriteFrame* frameWithTexture(CCTexture2D* pobTexture, CGRect rect, CGPoint offset, CGSize originalSize);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
/** Initializes a CCSpriteFrame with a texture, rect and offset.
|
|
|
|
It is assumed that the frame was not trimmed.
|
|
|
|
*/
|
2010-09-04 12:02:52 +08:00
|
|
|
bool initWithTexture(CCTexture2D* pobTexture, CGRect rect, CGPoint offset);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
|
|
|
/** Initializes a CCSpriteFrame with a texture, rect, offset and originalSize.
|
|
|
|
The originalSize is the size in pixels of the frame before being trimmed.
|
|
|
|
*/
|
2010-09-04 12:02:52 +08:00
|
|
|
bool initWithTexture(CCTexture2D* pobTexture, CGRect rect, CGPoint offset, CGSize originalSize);
|
2010-07-22 09:39:15 +08:00
|
|
|
protected:
|
|
|
|
CGRect m_obRect;
|
|
|
|
CGPoint m_obOffset;
|
|
|
|
CGSize m_obOriginalSize;
|
|
|
|
CCTexture2D *m_pobTexture;
|
|
|
|
};
|
|
|
|
|
2010-09-29 09:44:57 +08:00
|
|
|
//! @brief an Animation object used within Sprites to perform animations
|
2010-08-11 15:24:15 +08:00
|
|
|
class CCX_DLL CCAnimation : public NSObject
|
2010-07-22 09:39:15 +08:00
|
|
|
{
|
2010-07-22 15:54:02 +08:00
|
|
|
protected:
|
2010-09-06 10:57:21 +08:00
|
|
|
std::string m_nameStr;
|
2010-07-22 15:54:02 +08:00
|
|
|
float m_fDelay;
|
|
|
|
NSMutableArray<CCSpriteFrame*> *m_pobFrames;
|
|
|
|
|
2010-07-22 09:39:15 +08:00
|
|
|
public:
|
|
|
|
// attributes
|
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** get name of the animation */
|
2010-09-06 10:57:21 +08:00
|
|
|
inline const char* getName(void) { return m_nameStr.c_str(); }
|
2010-09-28 16:18:05 +08:00
|
|
|
/** set name of the animation */
|
2010-09-06 10:57:21 +08:00
|
|
|
inline void setName(const char *pszName){ m_nameStr = pszName; }
|
2010-07-22 09:39:15 +08:00
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** get delay between frames in seconds */
|
2010-07-22 09:39:15 +08:00
|
|
|
inline float getDelay(void) { return m_fDelay; }
|
2010-09-28 16:18:05 +08:00
|
|
|
/** set delay between frames in seconds */
|
2010-07-22 09:39:15 +08:00
|
|
|
inline void setDelay(float fDelay) { m_fDelay = fDelay; }
|
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** get array of frames */
|
2010-07-22 15:08:30 +08:00
|
|
|
inline NSMutableArray<CCSpriteFrame*>* getFrames(void) { return m_pobFrames; }
|
2010-09-28 16:18:05 +08:00
|
|
|
/** set array of frames, the Frames is retained */
|
2010-07-22 15:54:02 +08:00
|
|
|
inline void setFrames(NSMutableArray<CCSpriteFrame*> *pFrames)
|
2010-07-22 09:39:15 +08:00
|
|
|
{
|
2010-09-27 18:19:10 +08:00
|
|
|
CCX_SAFE_RETAIN(pFrames);
|
2010-09-06 11:55:56 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pobFrames);
|
2010-07-22 15:54:02 +08:00
|
|
|
m_pobFrames = pFrames;
|
2010-07-22 09:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2010-07-22 15:54:02 +08:00
|
|
|
~CCAnimation(void);
|
|
|
|
|
2010-07-22 09:39:15 +08:00
|
|
|
/** Initializes a CCAnimation with a name
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2010-09-04 12:02:52 +08:00
|
|
|
bool initWithName(const char *pszName);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
|
|
|
/** Initializes a CCAnimation with a name and frames
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2010-09-04 12:02:52 +08:00
|
|
|
bool initWithName(const char *pszName, NSArray<CCSpriteFrame*> *pFrames);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** Initializes a CCAnimation with a name and delay between frames. */
|
2010-09-04 12:02:52 +08:00
|
|
|
bool initWithName(const char *pszName, float fDelay);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** Initializes a CCAnimation with a name, delay and an array of CCSpriteFrames. */
|
2010-09-04 12:02:52 +08:00
|
|
|
bool initWithName(const char *pszName, float fDelay, NSArray<CCSpriteFrame*> *pFrames);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** adds a frame to a CCAnimation */
|
2010-07-22 15:54:02 +08:00
|
|
|
void addFrame(CCSpriteFrame *pFrame);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
|
|
|
/** Adds a frame with an image filename. Internally it will create a CCSpriteFrame and it will add it.
|
|
|
|
Added to facilitate the migration from v0.8 to v0.9.
|
|
|
|
*/
|
2010-07-23 14:06:19 +08:00
|
|
|
void addFrameWithFileName(const char *pszFileName);
|
2010-07-22 09:39:15 +08:00
|
|
|
|
|
|
|
/** Adds a frame with a texture and a rect. Internally it will create a CCSpriteFrame and it will add it.
|
|
|
|
Added to facilitate the migration from v0.8 to v0.9.
|
|
|
|
*/
|
|
|
|
void addFrameWithTexture(CCTexture2D* pobTexture, CGRect rect);
|
|
|
|
|
2010-07-22 10:16:53 +08:00
|
|
|
public:
|
|
|
|
/** Creates a CCAnimation with a name
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2010-07-22 15:08:30 +08:00
|
|
|
static CCAnimation* animationWithName(const char *pszName);
|
2010-07-22 10:16:53 +08:00
|
|
|
|
|
|
|
/** Creates a CCAnimation with a name and frames
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2010-07-22 15:54:02 +08:00
|
|
|
static CCAnimation* animationWithName(const char *pszName, NSArray<CCSpriteFrame*> *pFrames);
|
2010-07-22 10:16:53 +08:00
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** Creates a CCAnimation with a name and delay between frames. */
|
2010-07-22 15:08:30 +08:00
|
|
|
static CCAnimation* animationWithName(const char *pszName, float fDelay);
|
2010-07-22 10:16:53 +08:00
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** Creates a CCAnimation with a name, delay and an array of CCSpriteFrames. */
|
2010-07-23 14:24:23 +08:00
|
|
|
static CCAnimation* animationWithName(const char *pszName, float fDelay, NSArray<CCSpriteFrame*> *pFrames);
|
2010-07-22 09:39:15 +08:00
|
|
|
};
|
2010-08-04 15:46:12 +08:00
|
|
|
}//namespace cocos2d
|
2010-07-22 09:39:15 +08:00
|
|
|
|
|
|
|
#endif //__SPRITE_CCSPRITE_FRAME_H__
|