2011-03-19 10:59:01 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __CCBITMAP_FONT_ATLAS_H__
|
|
|
|
#define __CCBITMAP_FONT_ATLAS_H__
|
|
|
|
#include "CCSpriteBatchNode.h"
|
|
|
|
namespace cocos2d{
|
|
|
|
|
2010-08-12 17:39:25 +08:00
|
|
|
struct _KerningHashElement;
|
|
|
|
|
2010-09-29 15:24:51 +08:00
|
|
|
/**
|
2011-03-19 10:59:01 +08:00
|
|
|
@struct ccBMFontDef
|
2010-12-24 17:07:31 +08:00
|
|
|
BMFont definition
|
2010-08-12 17:39:25 +08:00
|
|
|
*/
|
2010-12-24 17:07:31 +08:00
|
|
|
typedef struct _BMFontDef {
|
2010-08-12 17:39:25 +08:00
|
|
|
//! ID of the character
|
|
|
|
unsigned int charID;
|
|
|
|
//! origin and size of the font
|
2011-03-07 17:11:57 +08:00
|
|
|
CCRect rect;
|
2010-08-12 17:39:25 +08:00
|
|
|
//! The X amount the image should be offset when drawing the image (in pixels)
|
|
|
|
int xOffset;
|
|
|
|
//! The Y amount the image should be offset when drawing the image (in pixels)
|
|
|
|
int yOffset;
|
|
|
|
//! The amount to move the current position after drawing the character (in pixels)
|
|
|
|
int xAdvance;
|
2010-12-24 17:07:31 +08:00
|
|
|
} ccBMFontDef;
|
2010-08-12 17:39:25 +08:00
|
|
|
|
2011-03-19 10:59:01 +08:00
|
|
|
/** @struct ccBMFontPadding
|
2010-12-24 17:07:31 +08:00
|
|
|
BMFont padding
|
2010-08-12 17:39:25 +08:00
|
|
|
@since v0.8.2
|
|
|
|
*/
|
2010-12-24 17:07:31 +08:00
|
|
|
typedef struct _BMFontPadding {
|
2010-08-12 17:39:25 +08:00
|
|
|
/// padding left
|
|
|
|
int left;
|
|
|
|
/// padding top
|
|
|
|
int top;
|
|
|
|
/// padding right
|
|
|
|
int right;
|
|
|
|
/// padding bottom
|
|
|
|
int bottom;
|
2010-12-24 17:07:31 +08:00
|
|
|
} ccBMFontPadding;
|
2010-08-12 17:39:25 +08:00
|
|
|
|
|
|
|
enum {
|
|
|
|
// how many characters are supported
|
2010-12-24 17:07:31 +08:00
|
|
|
kCCBMFontMaxChars = 2048, //256,
|
2010-08-12 17:39:25 +08:00
|
|
|
};
|
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
/** @brief CCBMFontConfiguration has parsed configuration of the the .fnt file
|
2010-08-12 17:39:25 +08:00
|
|
|
@since v0.8
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCBMFontConfiguration : public CCObject
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
// XXX: Creating a public interface so that the bitmapFontArray[] is accesible
|
|
|
|
public://@public
|
2010-09-29 12:03:21 +08:00
|
|
|
//! The characters building up the font
|
2010-12-24 17:07:31 +08:00
|
|
|
ccBMFontDef m_pBitmapFontArray[kCCBMFontMaxChars];
|
2010-09-29 12:03:21 +08:00
|
|
|
//! FNTConfig: Common Height
|
2010-08-25 11:14:34 +08:00
|
|
|
unsigned int m_uCommonHeight;
|
2010-09-29 12:03:21 +08:00
|
|
|
//! Padding
|
2010-12-24 17:07:31 +08:00
|
|
|
ccBMFontPadding m_tPadding;
|
2010-09-29 12:03:21 +08:00
|
|
|
//! atlas name
|
2010-08-12 17:39:25 +08:00
|
|
|
std::string m_sAtlasName;
|
2010-09-29 12:03:21 +08:00
|
|
|
//! values for kerning
|
2010-08-12 17:39:25 +08:00
|
|
|
struct _KerningHashElement *m_pKerningDictionary;
|
|
|
|
public:
|
2010-12-24 17:07:31 +08:00
|
|
|
CCBMFontConfiguration()
|
2011-04-01 16:06:53 +08:00
|
|
|
: m_pKerningDictionary(NULL)
|
|
|
|
, m_uCommonHeight(0)
|
2010-08-13 09:26:52 +08:00
|
|
|
{}
|
2010-12-24 17:07:31 +08:00
|
|
|
virtual ~CCBMFontConfiguration();
|
2010-08-12 17:39:25 +08:00
|
|
|
char * description();
|
2010-12-24 17:07:31 +08:00
|
|
|
/** allocates a CCBMFontConfiguration with a FNT file */
|
|
|
|
static CCBMFontConfiguration * configurationWithFNTFile(const char *FNTfile);
|
2010-08-12 17:39:25 +08:00
|
|
|
/** initializes a BitmapFontConfiguration with a FNT file */
|
2010-09-04 12:02:52 +08:00
|
|
|
bool initWithFNTfile(const char *FNTfile);
|
2010-08-12 17:39:25 +08:00
|
|
|
private:
|
|
|
|
void parseConfigFile(const char *controlFile);
|
2010-12-24 17:07:31 +08:00
|
|
|
void parseCharacterDefinition(std::string line, ccBMFontDef *characterDefinition);
|
2010-08-12 17:39:25 +08:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
/** @brief CCLabelBMFont is a subclass of CCSpriteSheet.
|
2010-08-12 17:39:25 +08:00
|
|
|
|
|
|
|
Features:
|
|
|
|
- Treats each character like a CCSprite. This means that each individual character can be:
|
|
|
|
- rotated
|
|
|
|
- scaled
|
|
|
|
- translated
|
|
|
|
- tinted
|
|
|
|
- chage the opacity
|
|
|
|
- It can be used as part of a menu item.
|
|
|
|
- anchorPoint can be used to align the "label"
|
|
|
|
- Supports AngelCode text format
|
|
|
|
|
|
|
|
Limitations:
|
|
|
|
- All inner characters are using an anchorPoint of (0.5f, 0.5f) and it is not recommend to change it
|
|
|
|
because it might affect the rendering
|
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
CCLabelBMFont implements the protocol CCLabelProtocol, like CCLabel and CCLabelAtlas.
|
|
|
|
CCLabelBMFont has the flexibility of CCLabel, the speed of CCLabelAtlas and all the features of CCSprite.
|
|
|
|
If in doubt, use CCLabelBMFont instead of CCLabelAtlas / CCLabel.
|
2010-08-12 17:39:25 +08:00
|
|
|
|
|
|
|
Supported editors:
|
|
|
|
- http://www.n4te.com/hiero/hiero.jnlp
|
|
|
|
- http://slick.cokeandcode.com/demos/hiero.jnlp
|
|
|
|
- http://www.angelcode.com/products/bmfont/
|
|
|
|
|
|
|
|
@since v0.8
|
|
|
|
*/
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCLabelBMFont : public CCSpriteBatchNode, public CCLabelProtocol, public CCRGBAProtocol
|
2010-08-12 17:39:25 +08:00
|
|
|
{
|
|
|
|
/** conforms to CCRGBAProtocol protocol */
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY(GLubyte, m_cOpacity, Opacity)
|
2010-08-12 17:39:25 +08:00
|
|
|
/** conforms to CCRGBAProtocol protocol */
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY(ccColor3B, m_tColor, Color)
|
2010-08-12 17:39:25 +08:00
|
|
|
/** conforms to CCRGBAProtocol protocol */
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY(bool, m_bIsOpacityModifyRGB, IsOpacityModifyRGB)
|
2010-08-12 17:39:25 +08:00
|
|
|
protected:
|
|
|
|
// string to render
|
|
|
|
std::string m_sString;
|
2010-12-24 17:07:31 +08:00
|
|
|
CCBMFontConfiguration *m_pConfiguration;
|
2010-08-12 17:39:25 +08:00
|
|
|
public:
|
2010-12-24 17:07:31 +08:00
|
|
|
CCLabelBMFont()
|
2011-04-01 16:06:53 +08:00
|
|
|
: m_pConfiguration(NULL)
|
|
|
|
, m_bIsOpacityModifyRGB(false)
|
|
|
|
, m_cOpacity(0)
|
|
|
|
, m_sString("")
|
2010-08-13 09:26:52 +08:00
|
|
|
{}
|
2010-12-24 17:07:31 +08:00
|
|
|
virtual ~CCLabelBMFont();
|
2010-08-12 17:39:25 +08:00
|
|
|
/** Purges the cached data.
|
|
|
|
Removes from memory the cached configurations and the atlas name dictionary.
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2010-08-17 14:25:41 +08:00
|
|
|
static void purgeCachedData();
|
2010-08-12 17:39:25 +08:00
|
|
|
/** creates a bitmap font altas with an initial string and the FNT file */
|
2010-12-24 17:07:31 +08:00
|
|
|
static CCLabelBMFont * labelWithString(const char *str, const char *fntFile);
|
|
|
|
|
2011-03-19 10:59:01 +08:00
|
|
|
/** creates a BMFont label with an initial string and the FNT file
|
|
|
|
@deprecated Will be removed in 1.0.1. Use "labelWithString" instead.
|
|
|
|
@since v0.99.5
|
2010-12-24 17:07:31 +08:00
|
|
|
*/
|
|
|
|
static CCLabelBMFont * bitmapFontAtlasWithString(const char *str, const char *fntFile);
|
|
|
|
|
2010-08-12 17:39:25 +08:00
|
|
|
/** init a bitmap font altas with an initial string and the FNT file */
|
2010-09-04 12:02:52 +08:00
|
|
|
bool initWithString(const char *str, const char *fntFile);
|
2010-08-12 17:39:25 +08:00
|
|
|
/** updates the font chars based on the string to render */
|
|
|
|
void createFontChars();
|
|
|
|
// super method
|
|
|
|
virtual void setString(const char *label);
|
2010-12-28 15:05:55 +08:00
|
|
|
virtual const char* getString(void);
|
2010-12-24 17:07:31 +08:00
|
|
|
virtual void setCString(const char *label);
|
2011-03-07 17:11:57 +08:00
|
|
|
virtual void setAnchorPoint(CCPoint var);
|
2010-08-31 11:20:37 +08:00
|
|
|
virtual CCRGBAProtocol* convertToRGBAProtocol() { return (CCRGBAProtocol*)this; }
|
|
|
|
virtual CCLabelProtocol* convertToLabelProtocol() { return (CCLabelProtocol*)this; }
|
2010-08-12 17:39:25 +08:00
|
|
|
#if CC_BITMAPFONTATLAS_DEBUG_DRAW
|
|
|
|
virtual void draw();
|
|
|
|
#endif // CC_BITMAPFONTATLAS_DEBUG_DRAW
|
|
|
|
private:
|
|
|
|
char * atlasNameFromFntFile(const char *fntFile);
|
2010-08-25 11:14:34 +08:00
|
|
|
int kerningAmountForFirst(unsigned short first, unsigned short second);
|
2010-08-12 17:39:25 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Free function that parses a FNT file a place it on the cache
|
|
|
|
*/
|
2011-05-12 08:40:19 +08:00
|
|
|
CC_DLL CCBMFontConfiguration * FNTConfigLoadFile( const char *file );
|
2010-08-12 17:39:25 +08:00
|
|
|
/** Purges the FNT config cache
|
|
|
|
*/
|
2011-05-12 08:40:19 +08:00
|
|
|
CC_DLL void FNTConfigRemoveCache( void );
|
2011-03-19 10:59:01 +08:00
|
|
|
|
|
|
|
/** CCBitmapFontAtlas
|
|
|
|
@deprecated Use CCLabelBMFont instead. Will be removed 1.0.1
|
|
|
|
*/
|
|
|
|
class CCBitmapFontAtlas : public CCLabelBMFont
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
}// namespace cocos2d
|
|
|
|
|
|
|
|
#endif //__CCBITMAP_FONT_ATLAS_H__
|