Merge pull request #844 from dumganhar/gles20_optimize_data

issue #1166: Improve CCString and CCArray, optimize CCDictionary.
This commit is contained in:
James Chen 2012-04-16 00:01:18 -07:00
commit 381b764da5
39 changed files with 611 additions and 450 deletions

View File

@ -1 +1 @@
2d9494d763406ba9945e40f011820005d14b92eb
ba02d3744870cd69d88ef40bfe303f99d85313a6

View File

@ -32,6 +32,7 @@ cocoa/CCDictionary.cpp \
cocoa/CCNS.cpp \
cocoa/CCObject.cpp \
cocoa/CCSet.cpp \
cocoa/CCString.cpp \
cocoa/CCZone.cpp \
cocos2d.cpp \
CCDirector.cpp \

View File

@ -24,7 +24,7 @@ THE SOFTWARE.
****************************************************************************/
#include "CCCamera.h"
//#include "CCDirector.h"
#include "CCString.h"
#include "CCGL.h"
#include "CCDrawingPrimitives.h"
@ -32,7 +32,8 @@ THE SOFTWARE.
#include "kazmath/GL/matrix.h"
using namespace std;
namespace cocos2d {
NS_CC_BEGIN
CCCamera::CCCamera(void)
{
@ -43,11 +44,9 @@ CCCamera::~CCCamera(void)
{
}
char * CCCamera::description(void)
const char* CCCamera::description(void)
{
char *ret = new char[100];
sprintf(ret, "<CCCamera | center = (%.2f,%.2f,%.2f)>", m_fCenterX, m_fCenterY, m_fCenterZ);
return ret;
return CCString::stringWithFormat("<CCCamera | center = (%.2f,%.2f,%.2f)>", m_fCenterX, m_fCenterY, m_fCenterZ)->getCString();
}
void CCCamera::init(void)
@ -140,4 +139,6 @@ void CCCamera::getUpXYZ(float *pUpX, float *pUpY, float *pUpZ)
*pUpY = m_fUpY;
*pUpZ = m_fUpZ;
}
}//namespace cocos2d
NS_CC_END

View File

@ -53,12 +53,11 @@ CCAction * CCAction::action()
return pRet;
}
char * CCAction::description()
const char* CCAction::description()
{
char *ret = new char[100] ;
sprintf(ret,"<CCAction | Tag = %d>", m_nTag);
return ret;
return CCString::stringWithFormat("<CCAction | Tag = %d>", m_nTag)->getCString();
}
CCObject* CCAction::copyWithZone(CCZone *pZone)
{
CCZone *pNewZone = NULL;

View File

@ -24,7 +24,7 @@ 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.
****************************************************************************/
#include "CCString.h"
#include "CCNode.h"
#include "CCPointExtension.h"
#include "support/TransformUtils.h"
@ -39,6 +39,7 @@ THE SOFTWARE.
// externals
#include "kazmath/GL/matrix.h"
#if CC_NODE_RENDER_SUBPIXEL
#define RENDER_IN_SUBPIXEL
#else
@ -452,11 +453,9 @@ void CCNode::cleanup()
}
char * CCNode::description()
const char* CCNode::description()
{
char *ret = new char[100] ;
sprintf(ret, "<CCNode | Tag = %d>", m_nTag);
return ret;
return CCString::stringWithFormat("<CCNode | Tag = %d>", m_nTag)->getCString();
}
// lazy allocs

View File

@ -88,14 +88,14 @@ CCArray* CCDictionary::allKeysForObject(CCObject* object)
return pArray;
}
CCObject* CCDictionary::objectForKey(const string& key)
CCObject* CCDictionary::objectForKey(const CCString& key)
{
if (m_eDictType == kCCDictUnknown && m_eDictType == kCCDictUnknown) return NULL;
CCAssert(m_eDictType == kCCDictStr, "this dictionary does not use string as key.");
CCObject* pRetObject = NULL;
CCDictElement *pElement = NULL;
HASH_FIND_STR(m_pElements,key.c_str(), pElement);
HASH_FIND_STR(m_pElements, key.getCString(), pElement);
if (pElement != NULL)
{
pRetObject = pElement->m_pObject;
@ -118,7 +118,27 @@ CCObject* CCDictionary::objectForKey(int key)
return pRetObject;
}
bool CCDictionary::setObject(CCObject* pObject, const string& key)
const CCString* CCDictionary::valueForKey(const CCString& key)
{
CCString* pStr = (CCString*)objectForKey(key);
if (pStr == NULL)
{
pStr = CCString::stringWithCString("");
}
return pStr;
}
const CCString* CCDictionary::valueForKey(int key)
{
CCString* pStr = (CCString*)objectForKey(key);
if (pStr == NULL)
{
pStr = CCString::stringWithCString("");
}
return pStr;
}
void CCDictionary::setObject(CCObject* pObject, const CCString& key)
{
CCAssert(key.length() > 0 && pObject != NULL, "Invalid Argument!");
if (m_eOldDictType == kCCDictUnknown)
@ -128,29 +148,23 @@ bool CCDictionary::setObject(CCObject* pObject, const string& key)
m_eDictType = kCCDictStr;
CCAssert(m_eDictType == m_eOldDictType, "this dictionary does not use string as key.");
bool bRet = false;
CCDictElement *pElement = NULL;
HASH_FIND_STR(m_pElements, key.c_str(), pElement);
HASH_FIND_STR(m_pElements, key.getCString(), pElement);
if (pElement == NULL)
{
pObject->retain();
pElement = new CCDictElement(key.c_str(), pObject);
HASH_ADD_STR(m_pElements, m_szKey, pElement);
bRet = true;
setObjectUnSafe(pObject, key);
}
else
else if (pElement->m_pObject != pObject)
{
CCObject* pTmpObj = pElement->m_pObject;
pTmpObj->retain();
removeObjectForKey(key);
setObject(pObject, key);
removeObjectForElememt(pElement);
setObjectUnSafe(pObject, key);
pTmpObj->release();
bRet = true;
}
return bRet;
}
bool CCDictionary::setObject(CCObject* pObject, int key)
void CCDictionary::setObject(CCObject* pObject, int key)
{
CCAssert(pObject != NULL, "Invalid Argument!");
if (m_eOldDictType == kCCDictUnknown)
@ -160,40 +174,30 @@ bool CCDictionary::setObject(CCObject* pObject, int key)
m_eDictType = kCCDictInt;
CCAssert(m_eDictType == m_eOldDictType, "this dictionary does not use integer as key.");
bool bRet = false;
CCDictElement *pElement = NULL;
HASH_FIND_INT(m_pElements, &key, pElement);
if (pElement == NULL)
{
pObject->retain();
pElement = new CCDictElement(key, pObject);
HASH_ADD_INT(m_pElements, m_iKey, pElement);
bRet = true;
setObjectUnSafe(pObject, key);
}
else
else if (pElement->m_pObject != pObject)
{
CCObject* pTmpObj = pElement->m_pObject;
pTmpObj->retain();
removeObjectForKey(key);
setObject(pObject, key);
removeObjectForElememt(pElement);
setObjectUnSafe(pObject, key);
pTmpObj->release();
bRet = true;
}
return bRet;
}
void CCDictionary::removeObjectForKey(const string& key)
void CCDictionary::removeObjectForKey(const CCString& key)
{
CCAssert(m_eDictType == kCCDictStr, "this dictionary does not use string as its key");
CCAssert(key.length() > 0, "Invalid Argument!");
CCDictElement *pElement = NULL;
HASH_FIND_STR(m_pElements, key.c_str(), pElement);
if (pElement)
{
HASH_DEL(m_pElements, pElement);
pElement->m_pObject->release();
CC_SAFE_DELETE(pElement);
}
HASH_FIND_STR(m_pElements, key.getCString(), pElement);
removeObjectForElememt(pElement);
}
void CCDictionary::removeObjectForKey(int key)
@ -201,6 +205,35 @@ void CCDictionary::removeObjectForKey(int key)
CCAssert(m_eDictType == kCCDictInt, "this dictionary does not use integer as its key");
CCDictElement *pElement = NULL;
HASH_FIND_INT(m_pElements, &key, pElement);
removeObjectForElememt(pElement);
}
void CCDictionary::setObjectUnSafe(CCObject* pObject, const CCString& key)
{
pObject->retain();
CCDictElement* pElement = new CCDictElement(key.getCString(), pObject);
HASH_ADD_STR(m_pElements, m_szKey, pElement);
}
void CCDictionary::setObjectUnSafe(CCObject* pObject, const int key)
{
pObject->retain();
CCDictElement* pElement = new CCDictElement(key, pObject);
HASH_ADD_INT(m_pElements, m_iKey, pElement);
}
void CCDictionary::removeObjectsForKeys(CCArray* pKeyArray)
{
CCObject* pObj = NULL;
CCARRAY_FOREACH(pKeyArray, pObj)
{
CCString* pStr = (CCString*)pObj;
removeObjectForKey(*pStr);
}
}
void CCDictionary::removeObjectForElememt(CCDictElement* pElement)
{
if (pElement != NULL)
{
HASH_DEL(m_pElements, pElement);
@ -244,13 +277,25 @@ CCObject* CCDictionary::copyWithZone(CCZone* pZone)
return pNewDict;
}
/* need deep copy ?*/
CCDictionary* CCDictionary::dictionaryWithDictionary(CCDictionary* srcDict)
{
CCDictionary* pNewDict = (CCDictionary*)srcDict->copyWithZone(NULL);
CCDictionary* pNewDict = (CCDictionary*)srcDict->copy();
pNewDict->autorelease();
return pNewDict;
}
extern CCDictionary* ccFileUtils_dictionaryWithContentsOfFileThreadSafe(const char *pFileName);
CCDictionary* CCDictionary::dictionaryWithContentsOfFileThreadSafe(const char *pFileName)
{
return ccFileUtils_dictionaryWithContentsOfFileThreadSafe(pFileName);
}
CCDictionary* CCDictionary::dictionaryWithContentsOfFile(const char *pFileName)
{
CCDictionary* pRet = dictionaryWithContentsOfFileThreadSafe(pFileName);
pRet->autorelease();
return pRet;
}
NS_CC_END

194
cocos2dx/cocoa/CCString.cpp Normal file
View File

@ -0,0 +1,194 @@
#include "CCString.h"
#include "CCFileUtils.h"
#include "ccMacros.h"
#include <stdlib.h>
#include <stdio.h>
NS_CC_BEGIN
#define kMaxStringLen (1024*100)
CCString::CCString()
:m_sString("")
{}
CCString::CCString(const char * str)
:m_sString(str)
{}
CCString::CCString(const std::string& str)
:m_sString(str)
{}
CCString::CCString(const CCString& str)
:m_sString(str.getCString())
{}
CCString::~CCString()
{
m_sString.clear();
}
CCString& CCString::operator= (const CCString& other)
{
m_sString = other.m_sString;
return *this;
}
bool CCString::initWithFormatAndValist(const char* format, va_list ap)
{
bool bRet = false;
char* pBuf = (char*)malloc(kMaxStringLen);
if (pBuf != NULL)
{
vsnprintf(pBuf, kMaxStringLen, format, ap);
m_sString = pBuf;
free(pBuf);
bRet = true;
}
return bRet;
}
bool CCString::initWithFormat(const char* format, ...)
{
bool bRet = false;
m_sString.clear();
va_list ap;
va_start(ap, format);
bRet = initWithFormatAndValist(format, ap);
va_end(ap);
return bRet;
}
int CCString::intValue() const
{
if (length() == 0)
{
return 0;
}
return atoi(m_sString.c_str());
}
unsigned int CCString::uintValue() const
{
if (length() == 0)
{
return 0;
}
return (unsigned int)atoi(m_sString.c_str());
}
float CCString::floatValue() const
{
if (length() == 0)
{
return 0.0f;
}
return (float)atof(m_sString.c_str());
}
double CCString::doubleValue() const
{
if (length() == 0)
{
return 0.0;
}
return atof(m_sString.c_str());
}
bool CCString::boolValue() const
{
if (length() == 0)
{
return false;
}
if (0 == strcmp(m_sString.c_str(), "0") || 0 == strcmp(m_sString.c_str(), "false"))
{
return false;
}
return true;
}
const char* CCString::getCString() const
{
return m_sString.c_str();
}
unsigned int CCString::length() const
{
return m_sString.length();
}
CCObject* CCString::copyWithZone(CCZone* pZone)
{
CCAssert(pZone == NULL, "CCString should not be inherited.");
CCString* pStr = new CCString(m_sString.c_str());
return pStr;
}
bool CCString::isEqual(const CCObject* pObject)
{
bool bRet = false;
const CCString* pStr = dynamic_cast<const CCString*>(pObject);
if (pStr != NULL)
{
if (0 == m_sString.compare(pStr->m_sString))
{
bRet = true;
}
}
return bRet;
}
CCString* CCString::stringWithCString(const char* pStr)
{
CCString* pRet = new CCString(pStr);
pRet->autorelease();
return pRet;
}
CCString* CCString::stringWithData(unsigned char* pData, unsigned long nLen)
{
CCString* pRet = NULL;
if (pData != NULL && nLen > 0)
{
char* pStr = (char*)malloc(nLen+1);
if (pStr != NULL)
{
pStr[nLen] = '\0';
memcpy(pStr, pData, nLen);
pRet = CCString::stringWithCString(pStr);
free(pStr);
}
}
return pRet;
}
CCString* CCString::stringWithFormat(const char* format, ...)
{
CCString* pRet = CCString::stringWithCString("");
va_list ap;
va_start(ap, format);
pRet->initWithFormatAndValist(format, ap);
va_end(ap);
return pRet;
}
CCString* CCString::stringWithContentsOfFile(const char* pszFileName)
{
unsigned long size = 0;
unsigned char* pData = 0;
CCString* pRet = NULL;
pData = CCFileUtils::getFileData(pszFileName, "rb", &size);
pRet = stringWithData(pData, size);
CC_SAFE_DELETE_ARRAY(pData);
return pRet;
}
NS_CC_END

View File

@ -48,7 +48,7 @@ public:
CCAction(void);
virtual ~CCAction(void);
char * description();
const char* description();
virtual CCObject* copyWithZone(CCZone *pZone);

View File

@ -85,7 +85,6 @@ public:
private:
void parseVersion1(CCDictionary* animations);
void parseVersion2(CCDictionary* animations);
const char * valueForKey(const char *key, CCDictionary* dict);
private:
CCDictionary* m_pAnimations;
static CCAnimationCache* s_pSharedAnimationCache;

View File

@ -79,7 +79,7 @@ public:
void init(void);
char * description(void);
const char* description(void);
/** sets the dirty value */
inline void setDirty(bool bValue) { m_bDirty = bValue; }

View File

@ -28,7 +28,7 @@ THE SOFTWARE.
#include "support/data_support/uthash.h"
#include "CCObject.h"
#include "CCArray.h"
#include <string>
#include "CCString.h"
NS_CC_BEGIN
@ -36,12 +36,23 @@ class CCDictionary;
class CC_DLL CCDictElement
{
#define MAX_KEY_LEN 256
public:
CCDictElement(const char* pszKey, CCObject* pObject)
{
init();
strncpy(m_szKey, pszKey, sizeof(m_szKey));
m_pObject = pObject;
const char* pStart = pszKey;
int len = strlen(pszKey);
if (len > MAX_KEY_LEN )
{
char* pEnd = (char*)&pszKey[len-1];
pStart = pEnd - (MAX_KEY_LEN-1);
}
strcpy(m_szKey, pStart);
}
CCDictElement(int iKey, CCObject* pObject)
@ -78,7 +89,7 @@ private:
}
private:
char m_szKey[256]; /** hash key of string type*/
char m_szKey[MAX_KEY_LEN+1]; /** hash key of string type*/
int m_iKey; /** hash key of integer type */
CCObject* m_pObject;/** hash value */
public:
@ -107,23 +118,45 @@ public:
/** @warning : We use '==' to compare two objects*/
CCArray* allKeysForObject(CCObject* object);
CCObject* objectForKey(const std::string& key);
CCObject* objectForKey(const CCString& key);
CCObject* objectForKey(int key);
const CCString* valueForKey(const CCString& key);
const CCString* valueForKey(int key);
bool setObject(CCObject* pObject, const std::string& key);
bool setObject(CCObject* pObject, int key);
void removeObjectForKey(const std::string& key);
void setObject(CCObject* pObject, const CCString& key);
void setObject(CCObject* pObject, int key);
void removeObjectForKey(const CCString& key);
void removeObjectForKey(int key);
void removeObjectsForKeys(CCArray* pKeyArray);
void removeObjectForElememt(CCDictElement* pElement);
void removeAllObjects();
virtual CCObject* copyWithZone(CCZone* pZone);
static CCDictionary* dictionaryWithDictionary(CCDictionary* srcDict);
static CCDictionary* dictionaryWithDictionary(CCDictionary* srcDict);
/**
@brief Generate a CCDictionary pointer by file
@param pFileName The file name of *.plist file
@return The CCDictionary pointer generated from the file
*/
static CCDictionary* dictionaryWithContentsOfFile(const char *pFileName);
/*
@brief The same meaning as dictionaryWithContentsOfFile(), but it doesn't call autorelease, so the
invoker should call release().
*/
static CCDictionary* dictionaryWithContentsOfFileThreadSafe(const char *pFileName);
private:
void setObjectUnSafe(CCObject* pObject, const CCString& key);
void setObjectUnSafe(CCObject* pObject, const int key);
public:
CCDictElement* m_pElements;
private:
enum CCDictType
{
kCCDictUnknown = 0,

View File

@ -22,53 +22,55 @@ 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 __CCLABEL_H__
#define __CCLABEL_H__
#ifndef __CCLABELTTF_H__
#define __CCLABELTTF_H__
#include "CCSprite.h"
#include "CCTexture2D.h"
namespace cocos2d{
NS_CC_BEGIN
/** @brief CCLabelTTF is a subclass of CCTextureNode that knows how to render text labels
*
* All features from CCTextureNode are valid in CCLabelTTF
*
* CCLabelTTF objects are slow. Consider using CCLabelAtlas or CCLabelBMFont instead.
/** @brief CCLabelTTF is a subclass of CCTextureNode that knows how to render text labels
*
* All features from CCTextureNode are valid in CCLabelTTF
*
* CCLabelTTF objects are slow. Consider using CCLabelAtlas or CCLabelBMFont instead.
*/
class CC_DLL CCLabelTTF : public CCSprite, public CCLabelProtocol
{
public:
CCLabelTTF();
virtual ~CCLabelTTF();
const char* description();
/** creates a CCLabelTTF from a fontname, alignment, dimension and font size */
static CCLabelTTF * labelWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize);
/** creates a CCLabelTTF from a fontname and font size */
static CCLabelTTF * labelWithString(const char *label, const char *fontName, float fontSize);
/** initializes the CCLabelTTF with a font name, alignment, dimension and font size */
bool initWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize);
/** initializes the CCLabelTTF with a font name and font size */
bool initWithString(const char *label, const char *fontName, float fontSize);
/** initializes the CCLabelTTF */
bool init();
/** changes the string to render
* @warning Changing the string is as expensive as creating a new CCLabelTTF. To obtain better performance use CCLabelAtlas
*/
class CC_DLL CCLabelTTF : public CCSprite, public CCLabelProtocol
{
public:
CCLabelTTF();
virtual ~CCLabelTTF();
char * description();
/** creates a CCLabelTTF from a fontname, alignment, dimension and font size */
static CCLabelTTF * labelWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize);
/** creates a CCLabelTTF from a fontname and font size */
static CCLabelTTF * labelWithString(const char *label, const char *fontName, float fontSize);
/** initializes the CCLabelTTF with a font name, alignment, dimension and font size */
bool initWithString(const char *label, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize);
/** initializes the CCLabelTTF with a font name and font size */
bool initWithString(const char *label, const char *fontName, float fontSize);
/** initializes the CCLabelTTF */
bool init();
/** changes the string to render
* @warning Changing the string is as expensive as creating a new CCLabelTTF. To obtain better performance use CCLabelAtlas
*/
virtual void setString(const char *label);
virtual const char* getString(void);
virtual void setString(const char *label);
virtual const char* getString(void);
virtual CCLabelProtocol* convertToLabelProtocol() { return (CCLabelProtocol*)this; }
protected:
/** Dimensions of the label in Points */
CCSize m_tDimensions;
CCTextAlignment m_eAlignment;
/** Font name used in the label */
std::string * m_pFontName;
/** Font size of the label */
float m_fFontSize;
std::string * m_pString;
};
virtual CCLabelProtocol* convertToLabelProtocol() { return (CCLabelProtocol*)this; }
protected:
/** Dimensions of the label in Points */
CCSize m_tDimensions;
CCTextAlignment m_eAlignment;
/** Font name used in the label */
std::string * m_pFontName;
/** Font size of the label */
float m_fFontSize;
std::string * m_pString;
};
NS_CC_END
} //namespace cocos2d
#endif //__CCLABEL_H__

View File

@ -330,7 +330,7 @@ public:
virtual ~CCNode(void);
char * description(void);
const char* description(void);
/** allocates and initializes a node.
The node will be created as "autorelease".

View File

@ -391,19 +391,6 @@ public:
virtual void update(ccTime dt);
virtual void updateWithNoTime(void);
private:
/** Private method, return the string found by key in dict.
@return "" if not found; return the string if found.
*/
inline const char * valueForKey(const char *key, CCDictionary *dict)
{
if (dict)
{
CCString *pString = (CCString*)dict->objectForKey(key);
return pString ? pString->m_sString.c_str() : "";
}
return "";
}
};
NS_CC_END

View File

@ -126,8 +126,6 @@ public:
private:
CCSpriteFrameCache(void) : m_pSpriteFrames(NULL), m_pSpriteFramesAliases(NULL){}
const char * valueForKey(const char* key, CCDictionary* dict);
protected:
CCDictionary* m_pSpriteFrames;
CCDictionary* m_pSpriteFramesAliases;

View File

@ -25,110 +25,84 @@ THE SOFTWARE.
#define __CCSTRING_H__
#include <string>
#include <stdlib.h>
#include "CCObject.h"
#include "CCFileUtils.h"
NS_CC_BEGIN
class CC_DLL CCString : public CCObject
{
public:
std::string m_sString;
public:
CCString()
:m_sString("")
{}
CCString();
CCString(const char* str);
CCString(const std::string& str);
CCString(const CCString& str);
CCString(const char * str)
{
m_sString = str;
}
virtual ~CCString()
{
m_sString.clear();
}
virtual ~CCString();
int toInt()
{
return atoi(m_sString.c_str());
}
/* override assignment operator */
CCString& operator= (const CCString& other);
unsigned int toUInt()
{
return (unsigned int)atoi(m_sString.c_str());
}
/** init a string with format, it's similar with the c function 'sprintf' */
bool initWithFormat(const char* format, ...);
float toFloat()
{
return (float)atof(m_sString.c_str());
}
/** convert to int value */
int intValue() const;
bool toBool()
{
if (0 == strcmp(m_sString.c_str(), "0") || 0 == strcmp(m_sString.c_str(), "false"))
{
return false;
}
return true;
}
/** convert to unsigned int value */
unsigned int uintValue() const;
std::string toStdString()
{
return m_sString;
}
/** convert to float value */
float floatValue() const;
const char* c_str()
{
return m_sString.c_str();
}
/** convert to double value */
double doubleValue() const;
bool isEmpty()
{
return m_sString.empty();
}
/** convert to bool value */
bool boolValue() const;
virtual CCObject* copyWithZone(CCZone* pZone)
{
CCAssert(pZone == NULL, "CCString should not be inherited.");
CCString* pStr = new CCString(m_sString.c_str());
return pStr;
}
/** get the C string */
const char* getCString() const;
virtual bool isEqual(const CCObject* pObject)
{
bool bRet = false;
const CCString* pStr = dynamic_cast<const CCString*>(pObject);
if (pStr != NULL)
{
if (0 == m_sString.compare(pStr->m_sString))
{
bRet = true;
}
}
return bRet;
}
/** get the length of string */
unsigned int length() const;
/** @brief: Get string from a file.
* @return: a pointer which needs to be deleted manually by 'delete[]' .
*/
static char* stringWithContentsOfFile(const char* pszFileName)
{
unsigned long size = 0;
unsigned char* pData = 0;
char* pszRet = 0;
pData = CCFileUtils::getFileData(pszFileName, "rb", &size);
do
{
CC_BREAK_IF(!pData || size <= 0);
pszRet = new char[size+1];
pszRet[size] = '\0';
memcpy(pszRet, pData, size);
CC_SAFE_DELETE_ARRAY(pData);
} while (false);
return pszRet;
}
/* override functions */
virtual CCObject* copyWithZone(CCZone* pZone);
virtual bool isEqual(const CCObject* pObject);
/* static funcitons */
/** create a string with c string
* @return A CCString pointer which is an autorelease object pointer,
* it means that you needn't do a release operation unless you retain it.
*/
static CCString* stringWithCString(const char* pStr);
/** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes,
* if you want to change it, you should modify the kMaxStringLen macro in CCString.cpp file.
* @return A CCString pointer which is an autorelease object pointer,
* it means that you needn't do a release operation unless you retain it.
*/
static CCString* stringWithFormat(const char* format, ...);
/** create a string with binary data
* @return A CCString pointer which is an autorelease object pointer,
* it means that you needn't do a release operation unless you retain it.
*/
static CCString* stringWithData(unsigned char* pData, unsigned long nLen);
/** create a string with a file,
* @return A CCString pointer which is an autorelease object pointer,
* it means that you needn't do a release operation unless you retain it.
*/
static CCString* stringWithContentsOfFile(const char* pszFileName);
private:
/** only for internal use */
bool initWithFormatAndValist(const char* format, va_list ap);
public:
std::string m_sString;
};
NS_CC_END

View File

@ -103,7 +103,7 @@ public:
CCTexture2D();
virtual ~CCTexture2D();
char * description(void);
const char* description(void);
/** These functions are needed to create mutable textures */
void releaseData(void *data);

View File

@ -62,7 +62,7 @@ public:
CCTextureCache();
virtual ~CCTextureCache();
char * description(void);
const char* description(void);
/** Retruns ths shared instance of the cache */
static CCTextureCache * sharedTextureCache();

View File

@ -439,15 +439,13 @@ CCBMFontConfiguration::~CCBMFontConfiguration()
const char* CCBMFontConfiguration::description(void)
{
char* pBuf = new char[100];
sprintf(pBuf, "<CCBMFontConfiguration = %08X | Glphys:%d Kernings:%d | Image = %s>", this,
return CCString::stringWithFormat(
"<CCBMFontConfiguration = %08X | Glphys:%d Kernings:%d | Image = %s>",
this,
HASH_COUNT(m_pFontDefDictionary),
HASH_COUNT(m_pKerningDictionary),
m_sAtlasName.c_str());
CCString* pRet = new CCString(pBuf);
pRet->autorelease();
CC_SAFE_DELETE_ARRAY(pBuf);
return pRet->c_str();
m_sAtlasName.c_str()
)->getCString();
}
void CCBMFontConfiguration::purgeKerningDictionary()

View File

@ -171,11 +171,9 @@ const char* CCLabelTTF::getString(void)
return m_pString->c_str();
}
char * CCLabelTTF::description()
const char* CCLabelTTF::description()
{
char *ret = new char[100] ;
sprintf(ret, "<CCLabelTTF | FontName = %s, FontSize = %.1f>", m_pFontName->c_str(), m_fFontSize);
return ret;
return CCString::stringWithFormat("<CCLabelTTF | FontName = %s, FontSize = %.1f>", m_pFontName->c_str(), m_fFontSize)->getCString();
}
NS_CC_END

View File

@ -150,7 +150,7 @@ bool CCParticleSystem::initWithFile(const char *plistFile)
{
bool bRet = false;
m_sPlistFile = CCFileUtils::fullPathFromRelativePath(plistFile);
CCDictionary *dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(m_sPlistFile.c_str());
CCDictionary *dict = CCDictionary::dictionaryWithContentsOfFileThreadSafe(m_sPlistFile.c_str());
CCAssert( dict != NULL, "Particles: file not found");
bRet = this->initWithDictionary(dict);
@ -167,99 +167,92 @@ bool CCParticleSystem::initWithDictionary(CCDictionary *dictionary)
CCImage *image = NULL;
do
{
int maxParticles = atoi(valueForKey("maxParticles", dictionary));
int maxParticles = dictionary->valueForKey("maxParticles")->intValue();
// self, not super
if(this->initWithTotalParticles(maxParticles))
{
// angle
m_fAngle = (float)atof(valueForKey("angle", dictionary));
m_fAngleVar = (float)atof(valueForKey("angleVariance", dictionary));
m_fAngle = dictionary->valueForKey("angle")->floatValue();
m_fAngleVar = dictionary->valueForKey("angleVariance")->floatValue();
// duration
m_fDuration = (float)atof(valueForKey("duration", dictionary));
m_fDuration = dictionary->valueForKey("duration")->floatValue();
// blend function
m_tBlendFunc.src = atoi(valueForKey("blendFuncSource", dictionary));
m_tBlendFunc.dst = atoi(valueForKey("blendFuncDestination", dictionary));
m_tBlendFunc.src = dictionary->valueForKey("blendFuncSource")->intValue();
m_tBlendFunc.dst = dictionary->valueForKey("blendFuncDestination")->intValue();
// color
m_tStartColor.r = (float)atof(valueForKey("startColorRed", dictionary));
m_tStartColor.g = (float)atof(valueForKey("startColorGreen", dictionary));
m_tStartColor.b = (float)atof(valueForKey("startColorBlue", dictionary));
m_tStartColor.a = (float)atof(valueForKey("startColorAlpha", dictionary));
m_tStartColor.r = dictionary->valueForKey("startColorRed")->floatValue();
m_tStartColor.g = dictionary->valueForKey("startColorGreen")->floatValue();
m_tStartColor.b = dictionary->valueForKey("startColorBlue")->floatValue();
m_tStartColor.a = dictionary->valueForKey("startColorAlpha")->floatValue();
m_tStartColorVar.r = (float)atof(valueForKey("startColorVarianceRed", dictionary));
m_tStartColorVar.g = (float)atof(valueForKey("startColorVarianceGreen", dictionary));
m_tStartColorVar.b = (float)atof(valueForKey("startColorVarianceBlue", dictionary));
m_tStartColorVar.a = (float)atof(valueForKey("startColorVarianceAlpha", dictionary));
m_tStartColorVar.r = dictionary->valueForKey("startColorVarianceRed")->floatValue();
m_tStartColorVar.g = dictionary->valueForKey("startColorVarianceGreen")->floatValue();
m_tStartColorVar.b = dictionary->valueForKey("startColorVarianceBlue")->floatValue();
m_tStartColorVar.a = dictionary->valueForKey("startColorVarianceAlpha")->floatValue();
m_tEndColor.r = (float)atof(valueForKey("finishColorRed", dictionary));
m_tEndColor.g = (float)atof(valueForKey("finishColorGreen", dictionary));
m_tEndColor.b = (float)atof(valueForKey("finishColorBlue", dictionary));
m_tEndColor.a = (float)atof(valueForKey("finishColorAlpha", dictionary));
m_tEndColor.r = dictionary->valueForKey("finishColorRed")->floatValue();
m_tEndColor.g = dictionary->valueForKey("finishColorGreen")->floatValue();
m_tEndColor.b = dictionary->valueForKey("finishColorBlue")->floatValue();
m_tEndColor.a = dictionary->valueForKey("finishColorAlpha")->floatValue();
m_tEndColorVar.r = (float)atof(valueForKey("finishColorVarianceRed", dictionary));
m_tEndColorVar.g = (float)atof(valueForKey("finishColorVarianceGreen", dictionary));
m_tEndColorVar.b = (float)atof(valueForKey("finishColorVarianceBlue", dictionary));
m_tEndColorVar.a = (float)atof(valueForKey("finishColorVarianceAlpha", dictionary));
m_tEndColorVar.r = dictionary->valueForKey("finishColorVarianceRed")->floatValue();
m_tEndColorVar.g = dictionary->valueForKey("finishColorVarianceGreen")->floatValue();
m_tEndColorVar.b = dictionary->valueForKey("finishColorVarianceBlue")->floatValue();
m_tEndColorVar.a = dictionary->valueForKey("finishColorVarianceAlpha")->floatValue();
// particle size
m_fStartSize = (float)atof(valueForKey("startParticleSize", dictionary));
m_fStartSizeVar = (float)atof(valueForKey("startParticleSizeVariance", dictionary));
m_fEndSize = (float)atof(valueForKey("finishParticleSize", dictionary));
m_fEndSizeVar = (float)atof(valueForKey("finishParticleSizeVariance", dictionary));
m_fStartSize = dictionary->valueForKey("startParticleSize")->floatValue();
m_fStartSizeVar = dictionary->valueForKey("startParticleSizeVariance")->floatValue();
m_fEndSize = dictionary->valueForKey("finishParticleSize")->floatValue();
m_fEndSizeVar = dictionary->valueForKey("finishParticleSizeVariance")->floatValue();
// position
float x = (float)atof(valueForKey("sourcePositionx", dictionary));
float y = (float)atof(valueForKey("sourcePositiony", dictionary));
float x = dictionary->valueForKey("sourcePositionx")->floatValue();
float y = dictionary->valueForKey("sourcePositiony")->floatValue();
this->setPosition( ccp(x,y) );
m_tPosVar.x = (float)atof(valueForKey("sourcePositionVariancex", dictionary));
m_tPosVar.y = (float)atof(valueForKey("sourcePositionVariancey", dictionary));
m_tPosVar.x = dictionary->valueForKey("sourcePositionVariancex")->floatValue();
m_tPosVar.y = dictionary->valueForKey("sourcePositionVariancey")->floatValue();
// Spinning
m_fStartSpin = (float)atof(valueForKey("rotationStart", dictionary));
m_fStartSpinVar = (float)atof(valueForKey("rotationStartVariance", dictionary));
m_fEndSpin= (float)atof(valueForKey("rotationEnd", dictionary));
m_fEndSpinVar= (float)atof(valueForKey("rotationEndVariance", dictionary));
m_fStartSpin = dictionary->valueForKey("rotationStart")->floatValue();
m_fStartSpinVar = dictionary->valueForKey("rotationStartVariance")->floatValue();
m_fEndSpin= dictionary->valueForKey("rotationEnd")->floatValue();
m_fEndSpinVar= dictionary->valueForKey("rotationEndVariance")->floatValue();
m_nEmitterMode = atoi(valueForKey("emitterType", dictionary));
m_nEmitterMode = dictionary->valueForKey("emitterType")->intValue();
// Mode A: Gravity + tangential accel + radial accel
if( m_nEmitterMode == kCCParticleModeGravity )
{
// gravity
modeA.gravity.x = (float)atof(valueForKey("gravityx", dictionary));
modeA.gravity.y = (float)atof(valueForKey("gravityy", dictionary));
modeA.gravity.x = dictionary->valueForKey("gravityx")->floatValue();
modeA.gravity.y = dictionary->valueForKey("gravityy")->floatValue();
// speed
modeA.speed = (float)atof(valueForKey("speed", dictionary));
modeA.speedVar = (float)atof(valueForKey("speedVariance", dictionary));
modeA.speed = dictionary->valueForKey("speed")->floatValue();
modeA.speedVar = dictionary->valueForKey("speedVariance")->floatValue();
const char * pszTmp = NULL;
// radial acceleration
pszTmp = valueForKey("radialAcceleration", dictionary);
modeA.radialAccel = (pszTmp) ? (float)atof(pszTmp) : 0;
pszTmp = valueForKey("radialAccelVariance", dictionary);
modeA.radialAccelVar = (pszTmp) ? (float)atof(pszTmp) : 0;
modeA.radialAccel = dictionary->valueForKey("radialAcceleration")->floatValue();
modeA.radialAccelVar = dictionary->valueForKey("radialAccelVariance")->floatValue();
// tangential acceleration
pszTmp = valueForKey("tangentialAcceleration", dictionary);
modeA.tangentialAccel = (pszTmp) ? (float)atof(pszTmp) : 0;
pszTmp = valueForKey("tangentialAccelVariance", dictionary);
modeA.tangentialAccelVar = (pszTmp) ? (float)atof(pszTmp) : 0;
modeA.tangentialAccel = dictionary->valueForKey("tangentialAcceleration")->floatValue();
modeA.tangentialAccelVar = dictionary->valueForKey("tangentialAccelVariance")->floatValue();
}
// or Mode B: radius movement
else if( m_nEmitterMode == kCCParticleModeRadius )
{
modeB.startRadius = (float)atof(valueForKey("maxRadius", dictionary));
modeB.startRadiusVar = (float)atof(valueForKey("maxRadiusVariance", dictionary));
modeB.endRadius = (float)atof(valueForKey("minRadius", dictionary));
modeB.endRadiusVar = 0;
modeB.rotatePerSecond = (float)atof(valueForKey("rotatePerSecond", dictionary));
modeB.rotatePerSecondVar = (float)atof(valueForKey("rotatePerSecondVariance", dictionary));
modeB.startRadius = dictionary->valueForKey("maxRadius")->floatValue();
modeB.startRadiusVar = dictionary->valueForKey("maxRadiusVariance")->floatValue();
modeB.endRadius = dictionary->valueForKey("minRadius")->floatValue();
modeB.endRadiusVar = 0.0f;
modeB.rotatePerSecond = dictionary->valueForKey("rotatePerSecond")->floatValue();
modeB.rotatePerSecondVar = dictionary->valueForKey("rotatePerSecondVariance")->floatValue();
} else {
CCAssert( false, "Invalid emitterType in config file");
@ -267,8 +260,8 @@ bool CCParticleSystem::initWithDictionary(CCDictionary *dictionary)
}
// life span
m_fLife = (float)atof(valueForKey("particleLifespan", dictionary));
m_fLifeVar = (float)atof(valueForKey("particleLifespanVariance", dictionary));
m_fLife = dictionary->valueForKey("particleLifespan")->floatValue();
m_fLifeVar = dictionary->valueForKey("particleLifespanVariance")->floatValue();
// emission Rate
m_fEmissionRate = m_uTotalParticles / m_fLife;
@ -278,7 +271,7 @@ bool CCParticleSystem::initWithDictionary(CCDictionary *dictionary)
{
// texture
// Try to get the texture from the cache
char *textureName = (char *)valueForKey("textureFileName", dictionary);
const char* textureName = dictionary->valueForKey("textureFileName")->getCString();
std::string fullpath = CCFileUtils::fullPathFromRelativeFile(textureName, m_sPlistFile.c_str());
CCTexture2D *tex = NULL;
@ -300,7 +293,7 @@ bool CCParticleSystem::initWithDictionary(CCDictionary *dictionary)
}
else
{
char *textureData = (char*)valueForKey("textureImageData", dictionary);
const char *textureData = dictionary->valueForKey("textureImageData")->getCString();
CCAssert(textureData, "");
int dataLen = strlen(textureData);

View File

@ -24,6 +24,7 @@ THE SOFTWARE.
#include "CCFileUtils.h"
#include "CCDirector.h"
#include "CCDictionary.h"
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
@ -239,13 +240,12 @@ public:
}
CCSAXState curState = m_tStateStack.empty() ? SAX_DICT : m_tStateStack.top();
CCString *pText = new CCString();
pText->m_sString = std::string((char*)ch,0,len);
CCString *pText = new CCString(std::string((char*)ch,0,len));
switch(m_tState)
{
case SAX_KEY:
m_sCurKey = pText->m_sString;
m_sCurKey = pText->getCString();
break;
case SAX_INT:
case SAX_REAL:
@ -291,18 +291,10 @@ std::string& CCFileUtils::removeSuffixFromFile(std::string& path)
return path;
}
CCDictionary* CCFileUtils::dictionaryWithContentsOfFile(const char *pFileName)
{
CCDictionary* ret = dictionaryWithContentsOfFileThreadSafe(pFileName);
ret->autorelease();
return ret;
}
CCDictionary* CCFileUtils::dictionaryWithContentsOfFileThreadSafe(const char *pFileName)
CCDictionary* ccFileUtils_dictionaryWithContentsOfFileThreadSafe(const char *pFileName)
{
CCDictMaker tMaker;
return tMaker.dictionaryWithContentsOfFile(pFileName);
return tMaker.dictionaryWithContentsOfFile(pFileName);
}
unsigned char* CCFileUtils::getFileDataFromZip(const char* pszZipFilePath, const char* pszFileName, unsigned long * pSize)

View File

@ -25,9 +25,10 @@ THE SOFTWARE.
#define __CC_FILEUTILS_PLATFORM_H__
#include <string>
#include "CCDictionary.h"
#include "CCPlatformMacros.h"
#include "ccTypes.h"
NS_CC_BEGIN;
NS_CC_BEGIN
//! @brief Helper class to handle file operations
class CC_DLL CCFileUtils
@ -144,19 +145,6 @@ public:
*/
static void setResourcePath(const char *pszResourcePath);
/**
@brief Generate a CCDictionary pointer by file
@param pFileName The file name of *.plist file
@return The CCDictionary pointer generated from the file
*/
static CCDictionary* dictionaryWithContentsOfFile(const char *pFileName);
/*
@brief The same meaning as dictionaryWithContentsOfFile(), but it doesn't call autorelease, so the
invoker should call release().
*/
static CCDictionary* dictionaryWithContentsOfFileThreadSafe(const char *pFileName);
/**
@brief Get the writeable path
@return The path that can write/read file
@ -210,6 +198,6 @@ public:
CC_SYNTHESIZE_READONLY(unsigned long , m_uSize, Size);
};
NS_CC_END;
NS_CC_END
#endif // end of __CC_EGLVIEW_PLATFORM_H__
#endif // __CC_FILEUTILS_PLATFORM_H__

View File

@ -34,6 +34,7 @@ THE SOFTWARE.
#include "CCFileUtils.h"
#include "CCDirector.h"
#include "CCSAXParser.h"
#include "CCDictionary.h"
#include "support/zip_support/unzip.h"
#define MAX_PATH 260
@ -415,17 +416,9 @@ namespace cocos2d {
return pRet->m_sString.c_str();
}
CCDictionary *CCFileUtils::dictionaryWithContentsOfFile(const char *pFileName)
CCDictionary* ccFileUtils_dictionaryWithContentsOfFileThreadSafe(const char *pFileName)
{
CCDictionary *ret = dictionaryWithContentsOfFileThreadSafe(pFileName);
ret->autorelease();
return ret;
}
CCDictionary *CCFileUtils::dictionaryWithContentsOfFileThreadSafe(const char *pFileName)
{
const char* pszFullPath = fullPathFromRelativePath(pFileName);
const char* pszFullPath = CCFileUtils::fullPathFromRelativePath(pFileName);
NSString* pPath = [NSString stringWithUTF8String:pszFullPath];
NSDictionary* pDict = [NSDictionary dictionaryWithContentsOfFile:pPath];
@ -434,7 +427,7 @@ namespace cocos2d {
id value = [pDict objectForKey:key];
static_addValueToCCDict(key, value, pRet);
}
return pRet;
}

View File

@ -235,6 +235,10 @@
RelativePath="..\cocoa\CCSet.cpp"
>
</File>
<File
RelativePath="..\cocoa\CCString.cpp"
>
</File>
<File
RelativePath="..\cocoa\CCZone.cpp"
>

View File

@ -137,6 +137,7 @@ xcopy /Y /Q "$(SolutionDir)cocos2dx\platform\third_party\win32\libraries\*.*" "$
<ClCompile Include="..\cocoa\CCNS.cpp" />
<ClCompile Include="..\cocoa\CCObject.cpp" />
<ClCompile Include="..\cocoa\CCSet.cpp" />
<ClCompile Include="..\cocoa\CCString.cpp" />
<ClCompile Include="..\cocoa\CCZone.cpp" />
<ClCompile Include="..\effects\CCGrabber.cpp" />
<ClCompile Include="..\effects\CCGrid.cpp" />

View File

@ -402,6 +402,9 @@
<ClCompile Include="..\layers_scenes_transitions_nodes\CCTransitionProgress.cpp">
<Filter>layers_scenes_transitions_nodes</Filter>
</ClCompile>
<ClCompile Include="..\cocoa\CCString.cpp">
<Filter>cocoa</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\cocoa\CCNS.h">

View File

@ -112,19 +112,15 @@ bool CCGLProgram::initWithVertexShaderByteArray(const GLchar* vShaderByteArray,
bool CCGLProgram::initWithVertexShaderFilename(const char* vShaderFilename, const char* fShaderFilename)
{
const GLchar * vertexSource = (GLchar*) CCString::stringWithContentsOfFile(CCFileUtils::fullPathFromRelativePath(vShaderFilename));
const GLchar * fragmentSource = (GLchar*) CCString::stringWithContentsOfFile(CCFileUtils::fullPathFromRelativePath(fShaderFilename));
bool ret = initWithVertexShaderByteArray(vertexSource, fragmentSource);
CC_SAFE_DELETE_ARRAY(vertexSource);
CC_SAFE_DELETE_ARRAY(fragmentSource);
return ret;
const GLchar * vertexSource = (GLchar*) CCString::stringWithContentsOfFile(CCFileUtils::fullPathFromRelativePath(vShaderFilename))->getCString();
const GLchar * fragmentSource = (GLchar*) CCString::stringWithContentsOfFile(CCFileUtils::fullPathFromRelativePath(fShaderFilename))->getCString();
return initWithVertexShaderByteArray(vertexSource, fragmentSource);
}
const char* CCGLProgram::description()
{
static char strDescription[100] = {0};
sprintf(strDescription, "<CCGLProgram = %08X | Program = %i, VertexShader = %i, FragmentShader = %i>", this, m_uProgram, m_uVertShader, m_uFragShader);
return strDescription;
return CCString::stringWithFormat("<CCGLProgram = %08X | Program = %i, VertexShader = %i, FragmentShader = %i>", this, m_uProgram, m_uVertShader, m_uFragShader)->getCString();
}
bool CCGLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source)
@ -216,11 +212,10 @@ const char* CCGLProgram::logForOpenGLObject(GLuint object, GLInfoFunction infoFu
char *logBytes = (char*)malloc(logLength);
logFunc(object, logLength, &charsWritten, logBytes);
CCString* log = new CCString(logBytes);
log->autorelease();
CCString* log = CCString::stringWithCString(logBytes);
free(logBytes);
return log->toStdString().c_str();
return log->getCString();
}
const char* CCGLProgram::vertexShaderLog()

View File

@ -29,6 +29,7 @@ THE SOFTWARE.
#include "CCSpriteFrame.h"
#include "CCSpriteFrameCache.h"
#include "CCString.h"
#include "CCFileUtils.h"
using namespace std;
@ -98,7 +99,7 @@ void CCAnimationCache::parseVersion1(CCDictionary* animations)
{
CCDictionary* animationDict = (CCDictionary*)pElement->getObject();
CCArray* frameNames = (CCArray*)animationDict->objectForKey("frames");
float delay = (float)atof(valueForKey("delay", animationDict));
float delay = animationDict->valueForKey("delay")->floatValue();
CCAnimation* animation = NULL;
if ( frameNames == NULL )
@ -113,7 +114,7 @@ void CCAnimationCache::parseVersion1(CCDictionary* animations)
CCObject* pObj = NULL;
CCARRAY_FOREACH(frameNames, pObj)
{
const char* frameName = ((CCString*)pObj)->c_str();
const char* frameName = ((CCString*)pObj)->getCString();
CCSpriteFrame* spriteFrame = frameCache->spriteFrameByName(frameName);
if ( ! spriteFrame ) {
@ -152,8 +153,8 @@ void CCAnimationCache::parseVersion2(CCDictionary* animations)
const char* name = pElement->getStrKey();
CCDictionary* animationDict = (CCDictionary*)pElement->getObject();
int loops = atoi(valueForKey("loops", animationDict));
bool restoreOriginalFrame = atoi(valueForKey("restoreOriginalFrame", animationDict)) == 0 ? false : true;
int loops = animationDict->valueForKey("loops")->intValue();
bool restoreOriginalFrame = animationDict->valueForKey("restoreOriginalFrame")->boolValue();
CCArray* frameArray = (CCArray*)animationDict->objectForKey("frames");
@ -171,7 +172,7 @@ void CCAnimationCache::parseVersion2(CCDictionary* animations)
{
CCDictionary* entry = (CCDictionary*)(pObj);
const char* spriteFrameName = valueForKey("spriteframe", entry);
const char* spriteFrameName = entry->valueForKey("spriteframe")->getCString();
CCSpriteFrame *spriteFrame = frameCache->spriteFrameByName(spriteFrameName);
if( ! spriteFrame ) {
@ -180,7 +181,7 @@ void CCAnimationCache::parseVersion2(CCDictionary* animations)
continue;
}
float delayUnits = (float)atof(valueForKey("delayUnits", entry));
float delayUnits = entry->valueForKey("delayUnits")->floatValue();
CCDictionary* userInfo = (CCDictionary*)entry->objectForKey("notification");
CCAnimationFrame *animFrame = new CCAnimationFrame();
@ -190,7 +191,7 @@ void CCAnimationCache::parseVersion2(CCDictionary* animations)
animFrame->release();
}
float delayPerUnit = (float)atof(valueForKey("delayPerUnit", animationDict));
float delayPerUnit = animationDict->valueForKey("delayPerUnit")->floatValue();
CCAnimation *animation = new CCAnimation();
animation->initWithAnimationFrames(array, delayPerUnit, loops);
array->release();
@ -215,14 +216,14 @@ void CCAnimationCache::addAnimationsWithDictionary(CCDictionary* dictionary)
CCDictionary* properties = (CCDictionary*)dictionary->objectForKey("properties");
if( properties )
{
version = atoi(valueForKey("format", properties));
version = properties->valueForKey("format")->intValue();
CCArray* spritesheets = (CCArray*)properties->objectForKey("spritesheets");
CCObject* pObj = NULL;
CCARRAY_FOREACH(spritesheets, pObj)
{
CCString* name = (CCString*)(pObj);
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name->c_str());
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name->getCString());
}
}
@ -238,23 +239,13 @@ void CCAnimationCache::addAnimationsWithDictionary(CCDictionary* dictionary)
}
}
const char * CCAnimationCache::valueForKey(const char *key, CCDictionary *dict)
{
if (dict)
{
CCString *pString = (CCString*)dict->objectForKey(key);
return pString ? pString->m_sString.c_str() : "";
}
return "";
}
/** Read an NSDictionary from a plist file and parse it automatically for animations */
void CCAnimationCache::addAnimationsWithFile(const char* plist)
{
CCAssert( plist, "Invalid texture file name");
const char* path = CCFileUtils::fullPathFromRelativePath(plist);
CCDictionary* dict = CCFileUtils::dictionaryWithContentsOfFile(path);
CCDictionary* dict = CCDictionary::dictionaryWithContentsOfFile(path);
CCAssert( dict, "CCAnimationCache: File could not be found");

View File

@ -91,7 +91,7 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(CCDictionary* dictionary,
// get the format
if(metadataDict != NULL)
{
format = atoi(valueForKey("format", metadataDict));
format = metadataDict->valueForKey("format")->intValue();
}
// check the format
@ -109,14 +109,14 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(CCDictionary* dictionary,
if(format == 0)
{
float x = (float)atof(valueForKey("x", frameDict));
float y = (float)atof(valueForKey("y", frameDict));
float w = (float)atof(valueForKey("width", frameDict));
float h = (float)atof(valueForKey("height", frameDict));
float ox = (float)atof(valueForKey("offsetX", frameDict));
float oy = (float)atof(valueForKey("offsetY", frameDict));
int ow = atoi(valueForKey("originalWidth", frameDict));
int oh = atoi(valueForKey("originalHeight", frameDict));
float x = frameDict->valueForKey("x")->floatValue();
float y = frameDict->valueForKey("y")->floatValue();
float w = frameDict->valueForKey("width")->floatValue();
float h = frameDict->valueForKey("height")->floatValue();
float ox = frameDict->valueForKey("offsetX")->floatValue();
float oy = frameDict->valueForKey("offsetY")->floatValue();
int ow = frameDict->valueForKey("originalWidth")->intValue();
int oh = frameDict->valueForKey("originalHeight")->intValue();
// check ow/oh
if(!ow || !oh)
{
@ -136,17 +136,17 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(CCDictionary* dictionary,
}
else if(format == 1 || format == 2)
{
CCRect frame = CCRectFromString(valueForKey("frame", frameDict));
CCRect frame = CCRectFromString(frameDict->valueForKey("frame")->getCString());
bool rotated = false;
// rotation
if (format == 2)
{
rotated = atoi(valueForKey("rotated", frameDict)) == 0 ? false : true;
rotated = frameDict->valueForKey("rotated")->boolValue();
}
CCPoint offset = CCPointFromString(valueForKey("offset", frameDict));
CCSize sourceSize = CCSizeFromString(valueForKey("sourceSize", frameDict));
CCPoint offset = CCPointFromString(frameDict->valueForKey("offset")->getCString());
CCSize sourceSize = CCSizeFromString(frameDict->valueForKey("sourceSize")->getCString());
// create frame
spriteFrame = new CCSpriteFrame();
@ -156,15 +156,15 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(CCDictionary* dictionary,
offset,
sourceSize
);
} else
if (format == 3)
}
else if (format == 3)
{
// get values
CCSize spriteSize = CCSizeFromString(valueForKey("spriteSize", frameDict));
CCPoint spriteOffset = CCPointFromString(valueForKey("spriteOffset", frameDict));
CCSize spriteSourceSize = CCSizeFromString(valueForKey("spriteSourceSize", frameDict));
CCRect textureRect = CCRectFromString(valueForKey("textureRect", frameDict));
bool textureRotated = atoi(valueForKey("textureRotated", frameDict)) == 0 ? false : true;
CCSize spriteSize = CCSizeFromString(frameDict->valueForKey("spriteSize")->getCString());
CCPoint spriteOffset = CCPointFromString(frameDict->valueForKey("spriteOffset")->getCString());
CCSize spriteSourceSize = CCSizeFromString(frameDict->valueForKey("spriteSourceSize")->getCString());
CCRect textureRect = CCRectFromString(frameDict->valueForKey("textureRect")->getCString());
bool textureRotated = frameDict->valueForKey("textureRotated")->boolValue();
// get aliases
CCArray* aliases = (CCArray*) (frameDict->objectForKey("aliases"));
@ -173,7 +173,7 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(CCDictionary* dictionary,
CCObject* pObj = NULL;
CCARRAY_FOREACH(aliases, pObj)
{
std::string oneAlias = ((CCString*)pObj)->m_sString;
std::string oneAlias = ((CCString*)pObj)->getCString();
if (m_pSpriteFramesAliases->objectForKey(oneAlias.c_str()))
{
CCLOG("cocos2d: WARNING: an alias with name %s already exists", oneAlias.c_str());
@ -200,7 +200,7 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(CCDictionary* dictionary,
void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist, CCTexture2D *pobTexture)
{
const char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist);
CCDictionary *dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(pszPath);
CCDictionary *dict = CCDictionary::dictionaryWithContentsOfFileThreadSafe(pszPath);
addSpriteFramesWithDictionary(dict, pobTexture);
@ -225,7 +225,7 @@ void CCSpriteFrameCache::addSpriteFramesWithFile(const char* plist, const char*
void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist)
{
const char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist);
CCDictionary *dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(pszPath);
CCDictionary *dict = CCDictionary::dictionaryWithContentsOfFileThreadSafe(pszPath);
string texturePath("");
@ -233,7 +233,7 @@ void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist)
if (metadataDict)
{
// try to read texture file name from meta data
texturePath = string(valueForKey("textureFileName", metadataDict));
texturePath = metadataDict->valueForKey("textureFileName")->getCString();
}
if (! texturePath.empty())
@ -290,7 +290,7 @@ void CCSpriteFrameCache::removeUnusedSpriteFrames(void)
if( spriteFrame->retainCount() == 1 )
{
CCLOG("cocos2d: CCSpriteFrameCache: removing unused frame: %s", pElement->getStrKey());
m_pSpriteFrames->removeObjectForKey(pElement->getStrKey());
m_pSpriteFrames->removeObjectForElememt(pElement);
}
}
}
@ -305,12 +305,12 @@ void CCSpriteFrameCache::removeSpriteFrameByName(const char *pszName)
}
// Is this an alias ?
CCString *key = (CCString*)m_pSpriteFramesAliases->objectForKey(pszName);
CCString* key = (CCString*)m_pSpriteFramesAliases->objectForKey(pszName);
if (key)
{
m_pSpriteFrames->removeObjectForKey(key->c_str());
m_pSpriteFramesAliases->removeObjectForKey(key->c_str());
m_pSpriteFrames->removeObjectForKey(*key);
m_pSpriteFramesAliases->removeObjectForKey(*key);
}
else
{
@ -321,7 +321,7 @@ void CCSpriteFrameCache::removeSpriteFrameByName(const char *pszName)
void CCSpriteFrameCache::removeSpriteFramesFromFile(const char* plist)
{
const char* path = CCFileUtils::fullPathFromRelativePath(plist);
CCDictionary* dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(path);
CCDictionary* dict = CCDictionary::dictionaryWithContentsOfFileThreadSafe(path);
removeSpriteFramesFromDictionary((CCDictionary*)dict);
@ -331,27 +331,23 @@ void CCSpriteFrameCache::removeSpriteFramesFromFile(const char* plist)
void CCSpriteFrameCache::removeSpriteFramesFromDictionary(CCDictionary* dictionary)
{
CCDictionary* framesDict = (CCDictionary*)dictionary->objectForKey("frames");
vector<string> keysToRemove;
CCArray* keysToRemove = CCArray::array();
CCDictElement* pElement = NULL;
CCDICT_FOREACH(framesDict, pElement)
{
if (m_pSpriteFrames->objectForKey(pElement->getStrKey()))
{
keysToRemove.push_back(pElement->getStrKey());
keysToRemove->addObject(CCString::stringWithCString(pElement->getStrKey()));
}
}
vector<string>::iterator iter;
for (iter = keysToRemove.begin(); iter != keysToRemove.end(); ++iter)
{
m_pSpriteFrames->removeObjectForKey((*iter).c_str());
}
m_pSpriteFrames->removeObjectsForKeys(keysToRemove);
}
void CCSpriteFrameCache::removeSpriteFramesFromTexture(CCTexture2D* texture)
{
vector<string> keysToRemove;
CCArray* keysToRemove = CCArray::array();
CCDictElement* pElement = NULL;
CCDICT_FOREACH(m_pSpriteFrames, pElement)
@ -360,15 +356,11 @@ void CCSpriteFrameCache::removeSpriteFramesFromTexture(CCTexture2D* texture)
CCSpriteFrame* frame = (CCSpriteFrame*)m_pSpriteFrames->objectForKey(key.c_str());
if (frame && (frame->getTexture() == texture))
{
keysToRemove.push_back(key);
keysToRemove->addObject(CCString::stringWithCString(pElement->getStrKey()));
}
}
vector<string>::iterator iter;
for (iter = keysToRemove.begin(); iter != keysToRemove.end(); ++iter)
{
m_pSpriteFrames->removeObjectForKey((*iter).c_str());
}
m_pSpriteFrames->removeObjectsForKeys(keysToRemove);
}
CCSpriteFrame* CCSpriteFrameCache::spriteFrameByName(const char *pszName)
@ -380,7 +372,7 @@ CCSpriteFrame* CCSpriteFrameCache::spriteFrameByName(const char *pszName)
CCString *key = (CCString*)m_pSpriteFramesAliases->objectForKey(pszName);
if (key)
{
frame = (CCSpriteFrame*)m_pSpriteFrames->objectForKey(key->c_str());
frame = (CCSpriteFrame*)m_pSpriteFrames->objectForKey(*key);
if (! frame)
{
CCLOG("cocos2d: CCSpriteFrameCahce: Frame '%s' not found", pszName);
@ -390,14 +382,4 @@ CCSpriteFrame* CCSpriteFrameCache::spriteFrameByName(const char *pszName)
return frame;
}
const char * CCSpriteFrameCache::valueForKey(const char *key, CCDictionary *dict)
{
if (dict)
{
CCString *pString = (CCString*)dict->objectForKey(key);
return pString ? pString->m_sString.c_str() : "";
}
return "";
}
NS_CC_END

View File

@ -233,11 +233,9 @@ bool CCTexture2D::initWithData(const void *data, CCTexture2DPixelFormat pixelFor
}
char * CCTexture2D::description(void)
const char* CCTexture2D::description(void)
{
char *ret = new char[100];
sprintf(ret, "<CCTexture2D | Name = %u | Dimensions = %u x %u | Coordinates = (%.2f, %.2f)>", m_uName, m_uPixelsWide, m_uPixelsHigh, m_fMaxS, m_fMaxT);
return ret;
return CCString::stringWithFormat("<CCTexture2D | Name = %u | Dimensions = %u x %u | Coordinates = (%.2f, %.2f)>", m_uName, m_uPixelsWide, m_uPixelsHigh, m_fMaxS, m_fMaxT)->getCString();
}
// implementation CCTexture2D (Image)

View File

@ -187,12 +187,7 @@ bool CCTextureAtlas::initWithTexture(CCTexture2D *texture, unsigned int capacity
const char* CCTextureAtlas::description()
{
char* pszDescription = (char*)malloc(100*sizeof(char));
sprintf(pszDescription, "<CCTextureAtlas | totalQuads = %u>", m_uTotalQuads);
CCString* pRet = new CCString(pszDescription);
pRet->autorelease();
CC_SAFE_FREE(pszDescription);
return pRet->c_str();
return CCString::stringWithFormat("<CCTextureAtlas | totalQuads = %u>", m_uTotalQuads)->getCString();
}

View File

@ -189,11 +189,9 @@ void CCTextureCache::purgeSharedTextureCache()
}
char * CCTextureCache::description()
const char* CCTextureCache::description()
{
char *ret = new char[100];
sprintf(ret, "<CCTextureCache | Number of textures = %u>", m_pTextures->count());
return ret;
return CCString::stringWithFormat("<CCTextureCache | Number of textures = %u>", m_pTextures->count())->getCString();
}
void CCTextureCache::addImageAsync(const char *path, CCObject *target, SEL_CallFuncO selector)
@ -547,7 +545,7 @@ void CCTextureCache::removeUnusedTextures()
if (value->retainCount() == 1)
{
CCLOG("cocos2d: CCTextureCache: removing unused texture: %s", pElement->getStrKey());
m_pTextures->removeObjectForKey(pElement->getStrKey());
m_pTextures->removeObjectForElememt(pElement);
}
}
}
@ -555,15 +553,12 @@ void CCTextureCache::removeUnusedTextures()
void CCTextureCache::removeTexture(CCTexture2D* texture)
{
if( ! texture )
{
return;
}
CCArray* keys = m_pTextures->allKeysForObject(texture);
CCObject* pObj;
CCARRAY_FOREACH(keys, pObj)
{
CCString* pKey = (CCString*)pObj;
m_pTextures->removeObjectForKey(pKey->c_str());
}
m_pTextures->removeObjectsForKeys(keys);
}
void CCTextureCache::removeTextureForKey(const char *textureKeyName)

View File

@ -214,7 +214,7 @@ void CCTMXLayer::parseInternalProperties()
float alphaFuncValue = 0.0f;
if (alphaFuncVal != NULL)
{
alphaFuncValue = alphaFuncVal->toFloat();
alphaFuncValue = alphaFuncVal->floatValue();
}
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColorAlphaTest));
@ -225,7 +225,7 @@ void CCTMXLayer::parseInternalProperties()
}
else
{
m_nVertexZvalue = vertexz->toInt();
m_nVertexZvalue = vertexz->intValue();
}
}
}

View File

@ -1,7 +1,7 @@
#!/bin/bash
# set params
NDK_ROOT_LOCAL=/cygdrive/d/programe/android/ndk/android-ndk-r7b
COCOS2DX_ROOT_LOCAL=/cygdrive/e/cocos2d-x
NDK_ROOT_LOCAL=/cygdrive/e/android/android-ndk-r7b
COCOS2DX_ROOT_LOCAL=/cygdrive/f/Project/dumganhar/cocos2d-x
# try to get global variable
if [ $NDK_ROOT"aaa" != "aaa" ]; then

View File

@ -1 +1 @@
80c6fdfc6a77f7b0475ad0c3edbc6097d604d506
b9cda1260ba870cf512eba65876376a4b0f6c7df

View File

@ -1239,7 +1239,7 @@ std::string BMFontOneAtlas::subtitle()
/// BMFontUnicode
BMFontUnicode::BMFontUnicode()
{
CCDictionary *strings = CCFileUtils::dictionaryWithContentsOfFile("fonts/strings.xml");
CCDictionary *strings = CCDictionary::dictionaryWithContentsOfFile("fonts/strings.xml");
const char *chinese = ((CCString*)strings->objectForKey("chinese1"))->m_sString.c_str();
const char *japanese = ((CCString*)strings->objectForKey("japanese"))->m_sString.c_str();
const char *spanish = ((CCString*)strings->objectForKey("spanish"))->m_sString.c_str();

View File

@ -649,13 +649,13 @@ void TMXOrthoObjectsTest::draw()
if(!dict)
break;
const char* key = "x";
int x = ((CCString*)dict->objectForKey(key))->toInt();
int x = ((CCString*)dict->objectForKey(key))->intValue();
key = "y";
int y = ((CCString*)dict->objectForKey(key))->toInt();//dynamic_cast<NSNumber*>(dict->objectForKey("y"))->getNumber();
int y = ((CCString*)dict->objectForKey(key))->intValue();//dynamic_cast<NSNumber*>(dict->objectForKey("y"))->getNumber();
key = "width";
int width = ((CCString*)dict->objectForKey(key))->toInt();//dynamic_cast<NSNumber*>(dict->objectForKey("width"))->getNumber();
int width = ((CCString*)dict->objectForKey(key))->intValue();//dynamic_cast<NSNumber*>(dict->objectForKey("width"))->getNumber();
key = "height";
int height = ((CCString*)dict->objectForKey(key))->toInt();//dynamic_cast<NSNumber*>(dict->objectForKey("height"))->getNumber();
int height = ((CCString*)dict->objectForKey(key))->intValue();//dynamic_cast<NSNumber*>(dict->objectForKey("height"))->getNumber();
glLineWidth(3);
@ -726,13 +726,13 @@ void TMXIsoObjectsTest::draw()
if(!dict)
break;
const char* key = "x";
int x = ((CCString*)dict->objectForKey(key))->toInt();//dynamic_cast<NSNumber*>(dict->objectForKey("x"))->getNumber();
int x = ((CCString*)dict->objectForKey(key))->intValue();//dynamic_cast<NSNumber*>(dict->objectForKey("x"))->getNumber();
key = "y";
int y = ((CCString*)dict->objectForKey(key))->toInt();//dynamic_cast<NSNumber*>(dict->objectForKey("y"))->getNumber();
int y = ((CCString*)dict->objectForKey(key))->intValue();//dynamic_cast<NSNumber*>(dict->objectForKey("y"))->getNumber();
key = "width";
int width = ((CCString*)dict->objectForKey(key))->toInt();//dynamic_cast<NSNumber*>(dict->objectForKey("width"))->getNumber();
int width = ((CCString*)dict->objectForKey(key))->intValue();//dynamic_cast<NSNumber*>(dict->objectForKey("width"))->getNumber();
key = "height";
int height = ((CCString*)dict->objectForKey(key))->toInt();//dynamic_cast<NSNumber*>(dict->objectForKey("height"))->getNumber();
int height = ((CCString*)dict->objectForKey(key))->intValue();//dynamic_cast<NSNumber*>(dict->objectForKey("height"))->getNumber();
glLineWidth(3);
@ -1254,10 +1254,10 @@ TMXOrthoFromXMLTest::TMXOrthoFromXMLTest()
string resources = "TileMaps"; // partial paths are OK as resource paths.
string file = resources + "/orthogonal-test1.tmx";
char* str = CCString::stringWithContentsOfFile(CCFileUtils::fullPathFromRelativePath(file.c_str()));
CCString* str = CCString::stringWithContentsOfFile(CCFileUtils::fullPathFromRelativePath(file.c_str()));
CCAssert(str != NULL, "Unable to open file");
CCTMXTiledMap *map = CCTMXTiledMap::tiledMapWithXML(str ,resources.c_str());
CCTMXTiledMap *map = CCTMXTiledMap::tiledMapWithXML(str->getCString() ,resources.c_str());
addChild(map, 0, kTagTileMap);
CCSize s = map->getContentSize();
@ -1589,13 +1589,13 @@ void TMXGIDObjectsTest::draw()
}
const char* key = "x";
int x = ((CCString*)dict->objectForKey(key))->toInt();
int x = ((CCString*)dict->objectForKey(key))->intValue();
key = "y";
int y = ((CCString*)dict->objectForKey(key))->toInt();
int y = ((CCString*)dict->objectForKey(key))->intValue();
key = "width";
int width = ((CCString*)dict->objectForKey(key))->toInt();
int width = ((CCString*)dict->objectForKey(key))->intValue();
key = "height";
int height = ((CCString*)dict->objectForKey(key))->toInt();
int height = ((CCString*)dict->objectForKey(key))->intValue();
glLineWidth(3);

View File

@ -187,6 +187,9 @@ void TestController::menuCallback(CCObject * pSender)
void TestController::closeCallback(CCObject * pSender)
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
void TestController::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)