Merge branch 'gles20' of https://github.com/cocos2d/cocos2d-x into gles20

This commit is contained in:
James Chen 2012-06-08 15:31:23 +08:00
commit 35e9e44e39
6 changed files with 154 additions and 82 deletions

View File

@ -72,7 +72,7 @@ CCNode::CCNode(void)
, m_bIsRunning(false)
, m_pParent(NULL)
// "whole screen" objects. like Scenes and Layers, should set isRelativeAnchorPoint to false
, m_bIsRelativeAnchorPoint(true)
, m_bIgnoreAnchorPointForPosition(false)
, m_nTag(kCCNodeTagInvalid)
// userData is always inited as nil
, m_pUserData(NULL)
@ -392,15 +392,18 @@ void CCNode::setParent(CCNode * var)
}
/// isRelativeAnchorPoint getter
bool CCNode::getIsRelativeAnchorPoint()
bool CCNode::getIgnoreAnchorPointForPosition()
{
return m_bIsRelativeAnchorPoint;
return m_bIgnoreAnchorPointForPosition;
}
/// isRelativeAnchorPoint setter
void CCNode::setIsRelativeAnchorPoint(bool newValue)
void CCNode::setIgnoreAnchorPointForPosition(bool newValue)
{
m_bIsRelativeAnchorPoint = newValue;
m_bIsTransformDirty = m_bIsInverseDirty = true;
if (newValue != m_bIgnoreAnchorPointForPosition)
{
m_bIgnoreAnchorPointForPosition = newValue;
m_bIsTransformDirty = m_bIsInverseDirty = true;
}
}
/// tag getter
@ -969,20 +972,23 @@ void CCNode::pauseSchedulerAndActions()
CCAffineTransform CCNode::nodeToParentTransform(void)
{
if ( m_bIsTransformDirty ) {
if (m_bIsTransformDirty)
{
// Translate values
float x = m_tPosition.x;
float y = m_tPosition.y;
if ( !m_bIsRelativeAnchorPoint ) {
if (m_bIgnoreAnchorPointForPosition)
{
x += m_tAnchorPointInPoints.x;
y += m_tAnchorPointInPoints.y;
}
// Rotation values
float c = 1, s = 0;
if( m_fRotation ) {
if (m_fRotation)
{
float radians = -CC_DEGREES_TO_RADIANS(m_fRotation);
c = cosf(radians);
s = sinf(radians);
@ -993,7 +999,8 @@ CCAffineTransform CCNode::nodeToParentTransform(void)
// optimization:
// inline anchor point calculation if skew is not needed
if( !needsSkewMatrix && !CCPoint::CCPointEqualToPoint(m_tAnchorPointInPoints, CCPointZero) ) {
if (! needsSkewMatrix && !CCPoint::CCPointEqualToPoint(m_tAnchorPointInPoints, CCPointZero))
{
x += c * -m_tAnchorPointInPoints.x * m_fScaleX + -s * -m_tAnchorPointInPoints.y * m_fScaleY;
y += s * -m_tAnchorPointInPoints.x * m_fScaleX + c * -m_tAnchorPointInPoints.y * m_fScaleY;
}
@ -1006,15 +1013,18 @@ CCAffineTransform CCNode::nodeToParentTransform(void)
// XXX: Try to inline skew
// If skew is needed, apply skew and then anchor point
if( needsSkewMatrix ) {
if (needsSkewMatrix)
{
CCAffineTransform skewMatrix = CCAffineTransformMake(1.0f, tanf(CC_DEGREES_TO_RADIANS(m_fSkewY)),
tanf(CC_DEGREES_TO_RADIANS(m_fSkewX)), 1.0f,
0.0f, 0.0f );
m_tTransform = CCAffineTransformConcat(skewMatrix, m_tTransform);
// adjust anchor point
if( ! CCPoint::CCPointEqualToPoint(m_tAnchorPointInPoints, CCPointZero) )
if (! CCPoint::CCPointEqualToPoint(m_tAnchorPointInPoints, CCPointZero))
{
m_tTransform = CCAffineTransformTranslate(m_tTransform, -m_tAnchorPointInPoints.x, -m_tAnchorPointInPoints.y);
}
}
m_bIsTransformDirty = false;

View File

@ -225,11 +225,9 @@ class CC_DLL CCNode : public CCObject
/** A weak reference to the parent */
CC_PROPERTY(CCNode *, m_pParent, Parent)
/** If true the transformtions will be relative to it's anchor point.
* Sprites, Labels and any other sizeble object use it have it enabled by default.
* Scenes, Layers and other "whole screen" object don't use it, have it disabled by default.
*/
CC_PROPERTY(bool, m_bIsRelativeAnchorPoint, IsRelativeAnchorPoint)
// If ture, the Anchor Point will be (0,0) when you position the CCNode.
// Used by CCLayer and CCScene
CC_PROPERTY(bool, m_bIgnoreAnchorPointForPosition, IgnoreAnchorPointForPosition);
/** A tag used to identify the node easily */
CC_PROPERTY(int, m_nTag, Tag)

View File

@ -33,13 +33,15 @@ THE SOFTWARE.
#include "ccGLStateCache.h"
#include "CCDirector.h"
#include "support/TransformUtils.h"
#include "CCInteger.h"
#include "CCFileUtils.h"
// external
#include "kazmath/GL/matrix.h"
NS_CC_BEGIN
//CCLabelAtlas - Creation & Init
CCLabelAtlas * CCLabelAtlas::labelWithString(const char *label, const char *charMapFile, unsigned int itemWidth, int unsigned itemHeight, unsigned char startCharMap)
CCLabelAtlas* CCLabelAtlas::labelWithString(const char *label, const char *charMapFile, unsigned int itemWidth, int unsigned itemHeight, unsigned int startCharMap)
{
CCLabelAtlas *pRet = new CCLabelAtlas();
if(pRet && pRet->initWithString(label, charMapFile, itemWidth, itemHeight, startCharMap))
@ -51,18 +53,53 @@ CCLabelAtlas * CCLabelAtlas::labelWithString(const char *label, const char *char
return NULL;
}
bool CCLabelAtlas::initWithString(const char *label, const char *charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned char startCharMap)
bool CCLabelAtlas::initWithString(const char *label, const char *charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap)
{
CCAssert(label != NULL, "");
if (CCAtlasNode::initWithTileFile(charMapFile, itemWidth, itemHeight, strlen(label)))
{
m_cMapStartChar = startCharMap;
m_uMapStartChar = startCharMap;
this->setString(label);
return true;
}
return false;
}
CCLabelAtlas* CCLabelAtlas::labelWithString(const char *string, const char *fntFile)
{
CCLabelAtlas *ret = new CCLabelAtlas();
if (ret)
{
if (ret->initWithString(string, fntFile))
{
ret->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(ret);
}
}
return ret;
}
bool CCLabelAtlas::initWithString(const char *theString, const char *fntFile)
{
CCDictionary *dict = CCDictionary::dictionaryWithContentsOfFile(CCFileUtils::fullPathFromRelativePath(fntFile));
CCAssert(((CCInteger*)dict->objectForKey("version"))->getValue() == 1, "Unsupported version. Upgrade cocos2d version");
CCString *textureFilename = (CCString*)dict->objectForKey("textureFilename");
unsigned int width = ((CCInteger*)dict->objectForKey("itemWidth"))->getValue() / CC_CONTENT_SCALE_FACTOR();
unsigned int height = ((CCInteger*)dict->objectForKey("itemHeight"))->getValue() / CC_CONTENT_SCALE_FACTOR();
unsigned int startChar = ((CCInteger*)dict->objectForKey("firstChar"))->getValue();
this->initWithString(theString, textureFilename->getCString(), width, height, startChar);
return true;
}
//CCLabelAtlas - Atlas generation
void CCLabelAtlas::updateAtlasValues()
{
@ -79,7 +116,7 @@ void CCLabelAtlas::updateAtlasValues()
float itemHeightInPixels = m_uItemHeight * CC_CONTENT_SCALE_FACTOR();
for(unsigned int i = 0; i < n; i++) {
unsigned char a = s[i] - m_cMapStartChar;
unsigned char a = s[i] - m_uMapStartChar;
float row = (float) (a % m_uItemsPerRow);
float col = (float) (a / m_uItemsPerRow);

View File

@ -52,10 +52,20 @@ public:
m_sString.clear();
}
/** creates the CCLabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas */
static CCLabelAtlas * labelWithString(const char *label, const char *charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned char startCharMap);
static CCLabelAtlas * labelWithString(const char *label, const char *charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap);
/** creates the CCLabelAtlas with a string and a configuration file
@since v2.0
*/
static CCLabelAtlas* labelWithString(const char *sring, const char *fntFile);
/** initializes the CCLabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas */
bool initWithString(const char *label, const char *charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned char startCharMap);
bool initWithString(const char *label, const char *charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap);
/** initializes the CCLabelAtlas with a string and a configuration file
@since v2.0
*/
bool initWithString(const char *string, const char *fntFile);
// super methods
virtual void updateAtlasValues();
virtual void setString(const char *label);
@ -69,7 +79,7 @@ protected:
// string to render
std::string m_sString;
// the first char in the charmap
unsigned char m_cMapStartChar;
unsigned int m_uMapStartChar;
};
NS_CC_END

View File

@ -41,6 +41,7 @@ http://www.angelcode.com/products/bmfont/ (Free, Windows only)
#include "CCFileUtils.h"
#include "support/data_support/uthash.h"
#include "CCDirector.h"
#include "CCTextureCache.h"
using namespace std;
@ -372,7 +373,10 @@ CCBMFontConfiguration* FNTConfigLoadFile( const char *fntFile)
if( pRet == NULL )
{
pRet = CCBMFontConfiguration::configurationWithFNTFile(fntFile);
configurations->setObject(pRet, fntFile);
if (pRet)
{
configurations->setObject(pRet, fntFile);
}
}
return pRet;
@ -415,15 +419,19 @@ CCBMFontConfiguration * CCBMFontConfiguration::configurationWithFNTFile(const ch
bool CCBMFontConfiguration::initWithFNTfile(const char *FNTfile)
{
CCAssert(FNTfile != NULL && strlen(FNTfile)!=0, "");
m_pKerningDictionary = NULL;
this->parseConfigFile(FNTfile);
m_pFontDefDictionary = NULL;
if (! this->parseConfigFile(FNTfile))
{
return false;
}
return true;
}
CCBMFontConfiguration::CCBMFontConfiguration()
: m_pFontDefDictionary(NULL)
, m_uCommonHeight(0)
, m_nCommonHeight(0)
, m_pKerningDictionary(NULL)
{
@ -470,23 +478,22 @@ void CCBMFontConfiguration::purgeFontDefDictionary()
}
void CCBMFontConfiguration::parseConfigFile(const char *controlFile)
bool CCBMFontConfiguration::parseConfigFile(const char *controlFile)
{
std::string fullpath = CCFileUtils::fullPathFromRelativePath(controlFile);
unsigned long nBufSize;
char *pBuffer = (char*)CCFileUtils::getFileData(fullpath.c_str(), "rb", &nBufSize);
CCString *contents = CCString::stringWithContentsOfFile(fullpath.c_str());
CCAssert(pBuffer, "CCBMFontConfiguration::parseConfigFile | Open file error.");
CCAssert(contents, "CCBMFontConfiguration::parseConfigFile | Open file error.");
if (!pBuffer)
if (!contents)
{
return;
CCLOG("cocos2d: Error parsing FNTfile %s", controlFile);
return false;
}
// parse spacing / padding
std::string line;
std::string strLeft(pBuffer, nBufSize);
std::string strLeft = contents->getCString();
while (strLeft.length() > 0)
{
int pos = strLeft.find('\n');
@ -533,15 +540,17 @@ void CCBMFontConfiguration::parseConfigFile(const char *controlFile)
element->key = element->fontDef.charID;
HASH_ADD_INT(m_pFontDefDictionary, key, element);
}
else if(line.substr(0,strlen("kernings count")) == "kernings count")
{
this->parseKerningCapacity(line);
}
// else if(line.substr(0,strlen("kernings count")) == "kernings count")
// {
// this->parseKerningCapacity(line);
// }
else if(line.substr(0,strlen("kerning first")) == "kerning first")
{
this->parseKerningEntry(line);
}
}
return true;
}
void CCBMFontConfiguration::parseImageFileName(std::string line, const char *fntFile)
@ -591,7 +600,7 @@ void CCBMFontConfiguration::parseCommonArguments(std::string line)
int index = line.find("lineHeight=");
int index2 = line.find(' ', index);
std::string value = line.substr(index, index2-index);
sscanf(value.c_str(), "lineHeight=%u", &m_uCommonHeight);
sscanf(value.c_str(), "lineHeight=%d", &m_nCommonHeight);
// scaleW. sanity check
index = line.find("scaleW=") + strlen("scaleW=");
index2 = line.find(' ', index);
@ -661,28 +670,6 @@ void CCBMFontConfiguration::parseCharacterDefinition(std::string line, ccBMFontD
sscanf(value.c_str(), "xadvance=%d", &characterDefinition->xAdvance);
}
void CCBMFontConfiguration::parseKerningCapacity(std::string line)
{
// When using uthash there is not need to parse the capacity.
// CCAssert(!kerningDictionary, @"dictionary already initialized");
//
// // Break the values for this line up using =
// CCMutableArray *values = [line componentsSeparatedByString:@"="];
// NSEnumerator *nse = [values objectEnumerator];
// CCString *propertyValue;
//
// // We need to move past the first entry in the array before we start assigning values
// [nse nextObject];
//
// // count
// propertyValue = [nse nextObject];
// int capacity = [propertyValue intValue];
//
// if( capacity != -1 )
// kerningDictionary = ccHashSetNew(capacity, targetSetEql);
}
void CCBMFontConfiguration::parseKerningEntry(std::string line)
{
//////////////////////////////////////////////////////////////////////////
@ -777,13 +764,31 @@ bool CCLabelBMFont::initWithString(const char *theString, const char *fntFile, f
bool CCLabelBMFont::initWithString(const char *theString, const char *fntFile, float width, CCTextAlignment alignment, CCPoint imageOffset)
{
CCAssert(theString != NULL, "");
CC_SAFE_RELEASE(m_pConfiguration);// allow re-init
m_pConfiguration = FNTConfigLoadFile(fntFile);
m_pConfiguration->retain();
CCAssert( m_pConfiguration, "Error creating config for LabelBMFont");
CCAssert(!m_pConfiguration, "re-init is no longer supported");
CCAssert( (theString && fntFile) || (theString==NULL && fntFile==NULL), "Invalid params for CCLabelBMFont");
CCTexture2D *texture = NULL;
if (fntFile)
{
CCBMFontConfiguration *newConf = FNTConfigLoadFile(fntFile);
CCAssert(newConf, "CCLabelBMFont: Impossible to create font. Please check file");
newConf->retain();
CC_SAFE_RELEASE(m_pConfiguration);
m_pConfiguration = newConf;
m_sFntFile = fntFile;
texture = CCTextureCache::sharedTextureCache()->addImage(m_pConfiguration->getAtlasName());
}
else
{
texture = new CCTexture2D();
texture->autorelease();
}
if (CCSpriteBatchNode::initWithFile(m_pConfiguration->m_sAtlasName.c_str(), strlen(theString)))
if (CCSpriteBatchNode::initWithTexture(texture, strlen(theString)))
{
m_pAlignment = alignment;
m_tImageOffset = imageOffset;
@ -862,8 +867,8 @@ void CCLabelBMFont::createFontChars()
}
}
totalHeight = m_pConfiguration->m_uCommonHeight * quantityOfLines;
nextFontPositionY = 0-(m_pConfiguration->m_uCommonHeight - m_pConfiguration->m_uCommonHeight * quantityOfLines);
totalHeight = m_pConfiguration->m_nCommonHeight * quantityOfLines;
nextFontPositionY = 0-(m_pConfiguration->m_nCommonHeight - m_pConfiguration->m_nCommonHeight * quantityOfLines);
for (unsigned int i= 0; i < stringLen; i++)
{
@ -872,7 +877,7 @@ void CCLabelBMFont::createFontChars()
if (c == '\n')
{
nextFontPositionX = 0;
nextFontPositionY -= m_pConfiguration->m_uCommonHeight;
nextFontPositionY -= m_pConfiguration->m_nCommonHeight;
continue;
}
@ -911,7 +916,8 @@ void CCLabelBMFont::createFontChars()
fontChar->setOpacity(255);
}
float yOffset = (float)(m_pConfiguration->m_uCommonHeight) - fontDef.yOffset;
// See issue 1343. cast( signed short + unsigned integer ) == unsigned integer (sign is lost!)
int yOffset = m_pConfiguration->m_nCommonHeight - fontDef.yOffset;
CCPoint fontPos = ccp( (float)nextFontPositionX + fontDef.xOffset + fontDef.rect.size.width*0.5f + kerningAmount,
(float)nextFontPositionY + yOffset - rect.size.height*0.5f * CC_CONTENT_SCALE_FACTOR() );
fontChar->setPosition(CC_POINT_PIXELS_TO_POINTS(fontPos));
@ -955,7 +961,7 @@ void CCLabelBMFont::setString(const char *newString, bool fromUpdate)
{
CC_SAFE_DELETE_ARRAY(m_sString);
m_sString = cc_utf16_from_utf8(newString);
m_sString_initial = newString;
m_sInitialString = newString;
updateString(fromUpdate);
}
@ -982,7 +988,7 @@ void CCLabelBMFont::updateString(bool fromUpdate)
const char* CCLabelBMFont::getString(void)
{
return m_sString_initial.c_str();
return m_sInitialString.c_str();
}
void CCLabelBMFont::setCString(const char *label)
@ -1074,7 +1080,7 @@ void CCLabelBMFont::setAnchorPoint(const CCPoint& point)
// LabelBMFont - Alignment
void CCLabelBMFont::updateLabel()
{
this->setString(m_sString_initial.c_str(), true);
this->setString(m_sInitialString.c_str(), true);
if (m_fWidth > 0)
{

View File

@ -91,8 +91,8 @@ public://@public
// BMFont definitions
struct _FontDefHashElement* m_pFontDefDictionary;
//! FNTConfig: Common Height
unsigned int m_uCommonHeight;
//! FNTConfig: Common Height Should be signed (issue #1343)
int m_nCommonHeight;
//! Padding
ccBMFontPadding m_tPadding;
//! atlas name
@ -107,13 +107,15 @@ public:
static CCBMFontConfiguration * configurationWithFNTFile(const char *FNTfile);
/** initializes a BitmapFontConfiguration with a FNT file */
bool initWithFNTfile(const char *FNTfile);
inline const char* getAtlasName(){ return m_sAtlasName.c_str(); }
inline void setAtlasName(const char* atlasName) { m_sAtlasName = atlasName; }
private:
void parseConfigFile(const char *controlFile);
bool parseConfigFile(const char *controlFile);
void parseCharacterDefinition(std::string line, ccBMFontDef *characterDefinition);
void parseInfoArguments(std::string line);
void parseCommonArguments(std::string line);
void parseImageFileName(std::string line, const char *fntFile);
void parseKerningCapacity(std::string line);
void parseKerningEntry(std::string line);
void purgeKerningDictionary();
void purgeFontDefDictionary();
@ -160,10 +162,19 @@ class CC_DLL CCLabelBMFont : public CCSpriteBatchNode, public CCLabelProtocol, p
protected:
// string to render
unsigned short* m_sString;
std::string m_sString_initial;
CCBMFontConfiguration *m_pConfiguration;
// name of fntFile
std::string m_sFntFile;
// initial string without line breaks
std::string m_sInitialString;
// alignment of all lines
CCTextAlignment m_pAlignment;
// max width until a line break is added
float m_fWidth;
CCBMFontConfiguration *m_pConfiguration;
bool m_bLineBreakWithoutSpaces;
// offset of the texture atlas
CCPoint m_tImageOffset;