2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCLabelAtlas.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "textures/CCTextureAtlas.h"
|
|
|
|
#include "support/CCPointExtension.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCDrawingPrimitives.h"
|
|
|
|
#include "ccConfig.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "shaders/CCShaderCache.h"
|
|
|
|
#include "shaders/CCGLProgram.h"
|
|
|
|
#include "shaders/ccGLStateCache.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "support/TransformUtils.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCInteger.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
// external
|
|
|
|
#include "kazmath/GL/matrix.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
//CCLabelAtlas - Creation & Init
|
2012-07-23 22:49:11 +08:00
|
|
|
CCLabelAtlas* CCLabelAtlas::labelWithString(const char *string, const char *charMapFile, unsigned int itemWidth, int unsigned itemHeight, unsigned int startCharMap)
|
2012-06-14 17:18:05 +08:00
|
|
|
{
|
2012-07-23 22:49:11 +08:00
|
|
|
return CCLabelAtlas::create(string, charMapFile, itemWidth, itemHeight, startCharMap);
|
2012-06-14 17:18:05 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-07-23 22:49:11 +08:00
|
|
|
CCLabelAtlas* CCLabelAtlas::create(const char *string, const char *charMapFile, unsigned int itemWidth, int unsigned itemHeight, unsigned int startCharMap)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLabelAtlas *pRet = new CCLabelAtlas();
|
2012-07-23 22:49:11 +08:00
|
|
|
if(pRet && pRet->initWithString(string, charMapFile, itemWidth, itemHeight, startCharMap))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2012-07-23 22:49:11 +08:00
|
|
|
bool CCLabelAtlas::initWithString(const char *string, const char *charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2012-07-23 22:49:11 +08:00
|
|
|
CCAssert(string != NULL, "");
|
|
|
|
if (CCAtlasNode::initWithTileFile(charMapFile, itemWidth, itemHeight, strlen(string)))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-08 15:29:47 +08:00
|
|
|
m_uMapStartChar = startCharMap;
|
2012-07-23 22:49:11 +08:00
|
|
|
this->setString(string);
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCLabelAtlas* CCLabelAtlas::labelWithString(const char *string, const char *fntFile)
|
|
|
|
{
|
|
|
|
return CCLabelAtlas::create(string, fntFile);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCLabelAtlas* CCLabelAtlas::create(const char *string, const char *fntFile)
|
2012-06-08 15:29:47 +08:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2012-07-23 22:49:11 +08:00
|
|
|
CCDictionary *dict = CCDictionary::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(fntFile));
|
2012-06-08 15:29:47 +08:00
|
|
|
|
2012-06-12 14:33:53 +08:00
|
|
|
CCAssert(((CCString*)dict->objectForKey("version"))->intValue() == 1, "Unsupported version. Upgrade cocos2d version");
|
2012-06-08 15:29:47 +08:00
|
|
|
|
|
|
|
CCString *textureFilename = (CCString*)dict->objectForKey("textureFilename");
|
2012-06-12 14:33:53 +08:00
|
|
|
unsigned int width = ((CCString*)dict->objectForKey("itemWidth"))->intValue() / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
unsigned int height = ((CCString*)dict->objectForKey("itemHeight"))->intValue() / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
unsigned int startChar = ((CCString*)dict->objectForKey("firstChar"))->intValue();
|
2012-06-08 15:29:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
this->initWithString(theString, textureFilename->getCString(), width, height, startChar);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
//CCLabelAtlas - Atlas generation
|
|
|
|
void CCLabelAtlas::updateAtlasValues()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int n = m_sString.length();
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccV3F_C4B_T2F_Quad quad;
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
const unsigned char *s = (unsigned char*)m_sString.c_str();
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTexture2D *texture = m_pTextureAtlas->getTexture();
|
|
|
|
float textureWide = (float) texture->getPixelsWide();
|
2012-04-18 18:43:45 +08:00
|
|
|
float textureHigh = (float) texture->getPixelsHigh();
|
2012-04-19 14:35:52 +08:00
|
|
|
float itemWidthInPixels = m_uItemWidth * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
float itemHeightInPixels = m_uItemHeight * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < n; i++) {
|
2012-06-08 15:29:47 +08:00
|
|
|
unsigned char a = s[i] - m_uMapStartChar;
|
2012-04-19 14:35:52 +08:00
|
|
|
float row = (float) (a % m_uItemsPerRow);
|
|
|
|
float col = (float) (a / m_uItemsPerRow);
|
|
|
|
|
|
|
|
#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
|
|
|
|
// Issue #938. Don't use texStepX & texStepY
|
|
|
|
float left = (2 * row * itemWidthInPixels + 1) / (2 * textureWide);
|
|
|
|
float right = left + (itemWidthInPixels * 2 - 2) / (2 * textureWide);
|
|
|
|
float top = (2 * col * itemHeightInPixels + 1) / (2 * textureHigh);
|
|
|
|
float bottom = top + (itemHeightInPixels * 2 - 2) / (2 * textureHigh);
|
|
|
|
#else
|
|
|
|
float left = row * itemWidthInPixels / textureWide;
|
|
|
|
float right = left + itemWidthInPixels / textureWide;
|
|
|
|
float top = col * itemHeightInPixels / textureHigh;
|
|
|
|
float bottom = top + itemHeightInPixels / textureHigh;
|
2010-12-24 17:07:31 +08:00
|
|
|
#endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
quad.tl.texCoords.u = left;
|
|
|
|
quad.tl.texCoords.v = top;
|
|
|
|
quad.tr.texCoords.u = right;
|
|
|
|
quad.tr.texCoords.v = top;
|
|
|
|
quad.bl.texCoords.u = left;
|
|
|
|
quad.bl.texCoords.v = bottom;
|
|
|
|
quad.br.texCoords.u = right;
|
2012-04-18 18:43:45 +08:00
|
|
|
quad.br.texCoords.v = bottom;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
quad.bl.vertices.x = (float) (i * m_uItemWidth);
|
|
|
|
quad.bl.vertices.y = 0;
|
|
|
|
quad.bl.vertices.z = 0.0f;
|
|
|
|
quad.br.vertices.x = (float)(i * m_uItemWidth + m_uItemWidth);
|
|
|
|
quad.br.vertices.y = 0;
|
|
|
|
quad.br.vertices.z = 0.0f;
|
|
|
|
quad.tl.vertices.x = (float)(i * m_uItemWidth);
|
|
|
|
quad.tl.vertices.y = (float)(m_uItemHeight);
|
|
|
|
quad.tl.vertices.z = 0.0f;
|
|
|
|
quad.tr.vertices.x = (float)(i * m_uItemWidth + m_uItemWidth);
|
|
|
|
quad.tr.vertices.y = (float)(m_uItemHeight);
|
|
|
|
quad.tr.vertices.z = 0.0f;
|
|
|
|
ccColor4B c = { m_tColor.r, m_tColor.g, m_tColor.b, m_cOpacity };
|
|
|
|
quad.tl.colors = c;
|
|
|
|
quad.tr.colors = c;
|
|
|
|
quad.bl.colors = c;
|
|
|
|
quad.br.colors = c;
|
|
|
|
m_pTextureAtlas->updateQuad(&quad, i);
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
//CCLabelAtlas - CCLabelProtocol
|
|
|
|
void CCLabelAtlas::setString(const char *label)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int len = strlen(label);
|
|
|
|
if (len > m_pTextureAtlas->getTotalQuads())
|
|
|
|
{
|
|
|
|
m_pTextureAtlas->resizeCapacity(len);
|
|
|
|
}
|
|
|
|
m_sString.clear();
|
|
|
|
m_sString = label;
|
|
|
|
this->updateAtlasValues();
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = CCSizeMake(len * m_uItemWidth, m_uItemHeight);
|
2011-07-12 11:20:41 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setContentSize(s);
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_uQuadsToDraw = len;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-28 15:05:55 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
const char* CCLabelAtlas::getString(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_sString.c_str();
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//CCLabelAtlas - draw
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#if CC_LABELATLAS_DEBUG_DRAW
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCLabelAtlas::draw()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAtlasNode::draw();
|
|
|
|
|
|
|
|
const CCSize& s = this->getContentSize();
|
|
|
|
CCPoint vertices[4]={
|
|
|
|
ccp(0,0),ccp(s.width,0),
|
|
|
|
ccp(s.width,s.height),ccp(0,s.height),
|
|
|
|
};
|
|
|
|
ccDrawPoly(vertices, 4, true);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2011-07-12 11:20:41 +08:00
|
|
|
#endif
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
NS_CC_END
|