2010-08-11 18:09:10 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2011-07-04 10:59:35 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2010-08-11 18:09:10 +08:00
|
|
|
|
|
|
|
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"
|
|
|
|
#include "CCTextureAtlas.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCPointExtension.h"
|
2010-12-17 23:18:03 +08:00
|
|
|
#include "CCDrawingPrimitives.h"
|
2010-12-28 15:17:59 +08:00
|
|
|
#include "ccConfig.h"
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "CCShaderCache.h"
|
|
|
|
#include "CCGLProgram.h"
|
2012-03-15 10:42:22 +08:00
|
|
|
#include "ccGLStateCache.h"
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "support/TransformUtils.h"
|
|
|
|
// external
|
|
|
|
#include "kazmath/GL/matrix.h"
|
2010-12-17 23:18:03 +08:00
|
|
|
|
2010-08-11 18:09:10 +08:00
|
|
|
namespace cocos2d{
|
|
|
|
|
|
|
|
//CCLabelAtlas - Creation & Init
|
2011-07-13 10:20:24 +08:00
|
|
|
CCLabelAtlas * CCLabelAtlas::labelWithString(const char *label, const char *charMapFile, unsigned int itemWidth, int unsigned itemHeight, unsigned char startCharMap)
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
|
|
|
CCLabelAtlas *pRet = new CCLabelAtlas();
|
2010-08-19 14:09:40 +08:00
|
|
|
if(pRet && pRet->initWithString(label, charMapFile, itemWidth, itemHeight, startCharMap))
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
CC_SAFE_DELETE(pRet);
|
2010-08-11 18:09:10 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-24 17:07:31 +08:00
|
|
|
|
2011-07-13 10:20:24 +08:00
|
|
|
bool CCLabelAtlas::initWithString(const char *label, const char *charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned char startCharMap)
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
2011-11-28 17:28:43 +08:00
|
|
|
CCAssert(label != NULL, "");
|
2010-09-02 14:54:42 +08:00
|
|
|
if (CCAtlasNode::initWithTileFile(charMapFile, itemWidth, itemHeight, strlen(label)))
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
|
|
|
m_cMapStartChar = startCharMap;
|
|
|
|
this->setString(label);
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-11 18:09:10 +08:00
|
|
|
}
|
2010-09-04 12:02:52 +08:00
|
|
|
return false;
|
2010-08-11 18:09:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//CCLabelAtlas - Atlas generation
|
|
|
|
void CCLabelAtlas::updateAtlasValues()
|
|
|
|
{
|
2011-07-13 10:20:24 +08:00
|
|
|
unsigned int n = m_sString.length();
|
2010-08-11 18:09:10 +08:00
|
|
|
|
|
|
|
ccV3F_C4B_T2F_Quad quad;
|
|
|
|
|
2011-07-13 10:20:24 +08:00
|
|
|
const unsigned char *s = (unsigned char*)m_sString.c_str();
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
CCTexture2D *texture = m_pTextureAtlas->getTexture();
|
|
|
|
float textureWide = (float) texture->getPixelsWide();
|
|
|
|
float textureHigh = (float) texture->getPixelsHigh();
|
2012-03-14 14:55:17 +08:00
|
|
|
float itemWidthInPixels = m_uItemWidth * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
float itemHeightInPixels = m_uItemHeight * CC_CONTENT_SCALE_FACTOR();
|
2010-12-24 17:07:31 +08:00
|
|
|
|
2011-07-13 10:20:24 +08:00
|
|
|
for(unsigned int i = 0; i < n; i++) {
|
2010-08-11 18:09:10 +08:00
|
|
|
unsigned char a = s[i] - m_cMapStartChar;
|
2011-07-01 16:24:23 +08:00
|
|
|
float row = (float) (a % m_uItemsPerRow);
|
|
|
|
float col = (float) (a / m_uItemsPerRow);
|
2010-12-24 17:07:31 +08:00
|
|
|
|
|
|
|
#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
|
|
|
|
// Issue #938. Don't use texStepX & texStepY
|
2012-03-14 14:55:17 +08:00
|
|
|
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);
|
2010-12-24 17:07:31 +08:00
|
|
|
#else
|
2012-03-14 14:55:17 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
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;
|
|
|
|
quad.br.texCoords.v = bottom;
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2011-07-01 16:24:23 +08:00
|
|
|
quad.bl.vertices.x = (float) (i * m_uItemWidth);
|
2010-08-11 18:09:10 +08:00
|
|
|
quad.bl.vertices.y = 0;
|
|
|
|
quad.bl.vertices.z = 0.0f;
|
2011-07-01 16:24:23 +08:00
|
|
|
quad.br.vertices.x = (float)(i * m_uItemWidth + m_uItemWidth);
|
2010-08-11 18:09:10 +08:00
|
|
|
quad.br.vertices.y = 0;
|
|
|
|
quad.br.vertices.z = 0.0f;
|
2011-07-01 16:24:23 +08:00
|
|
|
quad.tl.vertices.x = (float)(i * m_uItemWidth);
|
|
|
|
quad.tl.vertices.y = (float)(m_uItemHeight);
|
2010-08-11 18:09:10 +08:00
|
|
|
quad.tl.vertices.z = 0.0f;
|
2011-07-01 16:24:23 +08:00
|
|
|
quad.tr.vertices.x = (float)(i * m_uItemWidth + m_uItemWidth);
|
|
|
|
quad.tr.vertices.y = (float)(m_uItemHeight);
|
2010-08-11 18:09:10 +08:00
|
|
|
quad.tr.vertices.z = 0.0f;
|
2012-03-14 14:55:17 +08:00
|
|
|
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;
|
2010-08-11 18:09:10 +08:00
|
|
|
m_pTextureAtlas->updateQuad(&quad, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//CCLabelAtlas - CCLabelProtocol
|
|
|
|
void CCLabelAtlas::setString(const char *label)
|
|
|
|
{
|
2011-07-04 10:59:35 +08:00
|
|
|
unsigned int len = strlen(label);
|
|
|
|
if (len > m_pTextureAtlas->getTotalQuads())
|
2010-08-11 18:09:10 +08:00
|
|
|
{
|
2011-07-04 10:59:35 +08:00
|
|
|
m_pTextureAtlas->resizeCapacity(len);
|
2010-08-11 18:09:10 +08:00
|
|
|
}
|
|
|
|
m_sString.clear();
|
|
|
|
m_sString = label;
|
|
|
|
this->updateAtlasValues();
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CCSize s = CCSizeMake(len * m_uItemWidth, m_uItemHeight);
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
this->setContentSize(s);
|
2011-07-12 11:20:41 +08:00
|
|
|
|
|
|
|
m_uQuadsToDraw = len;
|
2010-08-11 18:09:10 +08:00
|
|
|
}
|
|
|
|
|
2010-12-28 15:05:55 +08:00
|
|
|
const char* CCLabelAtlas::getString(void)
|
|
|
|
{
|
|
|
|
return m_sString.c_str();
|
|
|
|
}
|
|
|
|
|
2010-08-11 18:09:10 +08:00
|
|
|
//CCLabelAtlas - draw
|
|
|
|
|
2011-07-12 11:20:41 +08:00
|
|
|
#if CC_LABELATLAS_DEBUG_DRAW
|
2010-08-11 18:09:10 +08:00
|
|
|
void CCLabelAtlas::draw()
|
|
|
|
{
|
2011-07-12 11:20:41 +08:00
|
|
|
CCAtlasNode::draw();
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
const CCSize& s = this->getContentSize();
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPoint vertices[4]={
|
2010-08-11 18:09:10 +08:00
|
|
|
ccp(0,0),ccp(s.width,0),
|
|
|
|
ccp(s.width,s.height),ccp(0,s.height),
|
|
|
|
};
|
|
|
|
ccDrawPoly(vertices, 4, true);
|
|
|
|
}
|
2011-07-12 11:20:41 +08:00
|
|
|
#endif
|
2010-08-11 18:09:10 +08:00
|
|
|
} // namespace cocos2d
|