Merge pull request #629 from gzito/master

Optimized ttf to bitmap, cached last loaded ttf & fixed ttf alpha. Other minor fixes. Added authors info.
This commit is contained in:
dumganhar 2011-12-19 18:46:07 -08:00
commit a8d5b6544a
10 changed files with 1024 additions and 769 deletions

View File

@ -44,7 +44,10 @@ Developers:
authors of lua binding authors of lua binding
Max Aksenov Max Aksenov
author and maintainer of Marmalade(Airplay) port author and maintainer of Airplay port
Giovanni Zito
Francis Styck
maintainers of Marmalade port
Carlos Sessa Carlos Sessa
implement the accelerometer module onto Android implement the accelerometer module onto Android

View File

@ -47,10 +47,6 @@ namespace CocosDenshion
static int16* g_AudioBuffer = 0; static int16* g_AudioBuffer = 0;
static int32 g_AudioFileSize = 0; static int32 g_AudioFileSize = 0;
// static int g_SoundChannel = 0;
// static bool g_SoundIsPlaying = false;
// static s3eBool g_MP3Support = S3E_FALSE;
typedef map<string,SoundFx> SoundFxMap ; typedef map<string,SoundFx> SoundFxMap ;
static SoundFxMap* g_pSoundFxMap = 0 ; static SoundFxMap* g_pSoundFxMap = 0 ;
@ -101,7 +97,7 @@ namespace CocosDenshion
void SimpleAudioEngine::setResource(const char* pszZipFileName) void SimpleAudioEngine::setResource(const char* pszZipFileName)
{ {
// todo
} }
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
@ -125,14 +121,7 @@ namespace CocosDenshion
if ( result == S3E_RESULT_ERROR) if ( result == S3E_RESULT_ERROR)
{ {
if (bLoop) result = s3eAudioPlay(pszFilePath, bLoop ? 0 : 1);
{
result = s3eAudioPlay(pszFilePath, 0);
}
else
{
result = s3eAudioPlay(pszFilePath, 1);
}
} }
if ( result == S3E_RESULT_ERROR) if ( result == S3E_RESULT_ERROR)
@ -164,11 +153,12 @@ namespace CocosDenshion
void SimpleAudioEngine::rewindBackgroundMusic() void SimpleAudioEngine::rewindBackgroundMusic()
{ {
//todo
} }
bool SimpleAudioEngine::willPlayBackgroundMusic() bool SimpleAudioEngine::willPlayBackgroundMusic()
{ {
// todo
return true; return true;
} }
@ -256,26 +246,26 @@ namespace CocosDenshion
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId) void SimpleAudioEngine::pauseEffect(unsigned int nSoundId)
{ {
s3eSoundChannelPause (nSoundId); s3eSoundChannelPause(nSoundId);
} }
void SimpleAudioEngine::pauseAllEffects() void SimpleAudioEngine::pauseAllEffects()
{ {
s3eSoundPauseAllChannels (); s3eSoundPauseAllChannels();
} }
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId) void SimpleAudioEngine::resumeEffect(unsigned int nSoundId)
{ {
s3eSoundChannelResume (nSoundId); s3eSoundChannelResume(nSoundId);
} }
void SimpleAudioEngine::resumeAllEffects() void SimpleAudioEngine::resumeAllEffects()
{ {
s3eSoundResumeAllChannels (); s3eSoundResumeAllChannels();
} }
void SimpleAudioEngine::stopAllEffects() void SimpleAudioEngine::stopAllEffects()
{ {
s3eSoundStopAllChannels (); s3eSoundStopAllChannels();
} }
} }

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org
Copyright (c) 2011 Максим Аксенов Copyright (c) 2011 Максим Аксенов
Copyright (c) 2011 Giovanni Zito, Francis Styck
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org
Copyright (c) 2011 Максим Аксенов Copyright (c) 2011 Максим Аксенов
Copyright (c) 2011 Giovanni Zito, Francis Styck
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org
Copyright (c) 2011 Максим Аксенов Copyright (c) 2011 Максим Аксенов
Copyright (c) 2011 Giovanni Zito, Francis Styck
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -81,7 +82,8 @@ int CCApplication::Run()
ccAccelerationUpdate(); ccAccelerationUpdate();
if( (quitRequested = s3eDeviceCheckQuitRequest()) ) { quitRequested = s3eDeviceCheckQuitRequest() ;
if( quitRequested && CCDirector::sharedDirector()->getOpenGLView() != NULL ) {
CCDirector::sharedDirector()->end() ; CCDirector::sharedDirector()->end() ;
// end status will be processed in CCDirector::sharedDirector()->mainLoop(); // end status will be processed in CCDirector::sharedDirector()->mainLoop();
} }

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org
Copyright (c) 2011 Максим Аксенов Copyright (c) 2011 Максим Аксенов
Copyright (c) 2011 Giovanni Zito, Francis Styck
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org
Copyright (c) 2011 Максим Аксенов Copyright (c) 2011 Максим Аксенов
Copyright (c) 2011 Giovanni Zito, Francis Styck
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -44,7 +45,6 @@ CCEGLView::CCEGLView()
: m_pDelegate(NULL) : m_pDelegate(NULL)
, m_fScreenScaleFactor(1.0) , m_fScreenScaleFactor(1.0)
, m_bNotHVGA(false) , m_bNotHVGA(false)
, m_bCaptured(false) , m_bCaptured(false)
, m_bAccelState(false) , m_bAccelState(false)
, m_Key(s3eKeyFirst) , m_Key(s3eKeyFirst)
@ -60,11 +60,27 @@ CCEGLView::CCEGLView()
m_pSet = new CCSet; m_pSet = new CCSet;
m_pTouch = new CCTouch; m_pTouch = new CCTouch;
// Determine if the device supports multi-touch
m_isMultiTouch = s3ePointerGetInt(S3E_POINTER_MULTI_TOUCH_AVAILABLE) ? true : false;
// For multi-touch devices we handle touch and motion events using different callbacks
if (m_isMultiTouch)
{
s3ePointerRegister(S3E_POINTER_TOUCH_EVENT, &MultiTouchEventHandler, this);
s3ePointerRegister(S3E_POINTER_TOUCH_MOTION_EVENT, &MultiMotionEventHandler, this);
for (int i = 0; i < S3E_POINTER_TOUCH_MAX; i++) {
touchSet[i] = NULL;
}
}
else
{
// Register pointer touch button event handler // Register pointer touch button event handler
s3ePointerRegister(S3E_POINTER_BUTTON_EVENT, &TouchEventHandler, this); s3ePointerRegister(S3E_POINTER_BUTTON_EVENT, &TouchEventHandler, this);
// Register pointer motion button event handler // Register pointer motion button event handler
s3ePointerRegister(S3E_POINTER_MOTION_EVENT, &MotionEventHandler, this); s3ePointerRegister(S3E_POINTER_MOTION_EVENT, &MotionEventHandler, this);
}
// Register keyboard event handler // Register keyboard event handler
s3eKeyboardRegister(S3E_KEYBOARD_KEY_EVENT, &KeyEventHandler, this); s3eKeyboardRegister(S3E_KEYBOARD_KEY_EVENT, &KeyEventHandler, this);
@ -122,6 +138,7 @@ CCSize CCEGLView::getSize()
} }
} }
void CCEGLView::setTouch(void* systemData) void CCEGLView::setTouch(void* systemData)
{ {
s3ePointerEvent* event =(s3ePointerEvent*)systemData; s3ePointerEvent* event =(s3ePointerEvent*)systemData;
@ -146,6 +163,7 @@ void CCEGLView::setTouch(void* systemData)
break; break;
} }
} }
void CCEGLView::setMotionTouch(void* systemData) void CCEGLView::setMotionTouch(void* systemData)
{ {
s3ePointerMotionEvent* event =(s3ePointerMotionEvent*)systemData; s3ePointerMotionEvent* event =(s3ePointerMotionEvent*)systemData;
@ -156,6 +174,65 @@ void CCEGLView::setMotionTouch(void* systemData)
} }
} }
void CCEGLView::setMultiTouch(void* systemData)
{
s3ePointerTouchEvent* event =(s3ePointerTouchEvent*)systemData;
if (touchSet[event->m_TouchID] == NULL) {
m_pTouch = new CCTouch;
touchSet[event->m_TouchID] = m_pTouch;
}
else {
m_pTouch = touchSet[event->m_TouchID];
}
switch (event->m_Pressed)
{
case S3E_POINTER_STATE_DOWN :
m_pTouch->SetTouchInfo(0, (float)event->m_x, (float)event->m_y);
m_pSet->addObject(m_pTouch);
m_pDelegate->touchesBegan(m_pSet, NULL);
break;
case S3E_POINTER_STATE_UP :
{
m_pTouch->SetTouchInfo(0, (float)event->m_x, (float)event->m_y);
m_pDelegate->touchesEnded(m_pSet, NULL);
m_pSet->removeObject(m_pTouch);
touchSet[event->m_TouchID] = NULL;
}
break;
}
}
void CCEGLView::setMultiMotionTouch(void* systemData)
{
s3ePointerTouchMotionEvent* event =(s3ePointerTouchMotionEvent*)systemData;
m_pTouch = touchSet[event->m_TouchID];
if (m_pTouch)
{
m_pTouch->SetTouchInfo((int)event, (float)event->m_x, (float)event->m_y);
m_pDelegate->touchesMoved(m_pSet, NULL);
}
}
CCTouch* CCEGLView::findTouch(int id)
{
CCSetIterator iter;
for (iter = m_pSet->begin(); iter != m_pSet->end(); ++iter)
{
CCTouch *touch = (CCTouch*)*iter;
if(touch->view() == id)
return touch;
}
return NULL;
}
void CCEGLView::setKeyTouch(void* systemData) void CCEGLView::setKeyTouch(void* systemData)
{ {
s3eKeyboardEvent* event = (s3eKeyboardEvent*)systemData; s3eKeyboardEvent* event = (s3eKeyboardEvent*)systemData;
@ -184,8 +261,17 @@ void CCEGLView::release()
{ {
IW_CALLSTACK("CCEGLView::release"); IW_CALLSTACK("CCEGLView::release");
if (m_isMultiTouch)
{
s3ePointerUnRegister(S3E_POINTER_TOUCH_EVENT, &MultiTouchEventHandler);
s3ePointerUnRegister(S3E_POINTER_TOUCH_MOTION_EVENT, &MultiMotionEventHandler);
}
else
{
s3ePointerUnRegister(S3E_POINTER_BUTTON_EVENT, &TouchEventHandler); s3ePointerUnRegister(S3E_POINTER_BUTTON_EVENT, &TouchEventHandler);
s3ePointerUnRegister(S3E_POINTER_MOTION_EVENT, &MotionEventHandler); s3ePointerUnRegister(S3E_POINTER_MOTION_EVENT, &MotionEventHandler);
}
s3eKeyboardUnRegister(S3E_KEYBOARD_KEY_EVENT, &KeyEventHandler); s3eKeyboardUnRegister(S3E_KEYBOARD_KEY_EVENT, &KeyEventHandler);
if (IwGLIsInitialised()) if (IwGLIsInitialised())

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org
Copyright (c) 2011 Максим Аксенов Copyright (c) 2011 Максим Аксенов
Copyright (c) 2011 Giovanni Zito, Francis Styck
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -26,8 +27,8 @@
#include "CCGeometry.h" #include "CCGeometry.h"
#include "s3eKeyboard.h" #include "s3eKeyboard.h"
#include "s3ePointer.h"
#include "IwUtil.h" #include "IwUtil.h"
#include "IwUtilInitTerm.h"
NS_CC_BEGIN; NS_CC_BEGIN;
class CCSet; class CCSet;
@ -79,7 +80,7 @@ private:
CCRect m_rcViewPort; CCRect m_rcViewPort;
bool m_bNotHVGA; bool m_bNotHVGA;
EGLTouchDelegate *m_pDelegate; EGLTouchDelegate* m_pDelegate;
float m_fScreenScaleFactor; float m_fScreenScaleFactor;
bool m_bAccelState; bool m_bAccelState;
@ -88,22 +89,43 @@ private:
CCSet * m_pSet; CCSet * m_pSet;
CCTouch * m_pTouch; CCTouch * m_pTouch;
bool m_isMultiTouch;
static CCEGLView* m_pInstance ; static CCEGLView* m_pInstance ;
void setTouch(void* systemData); void setTouch(void* systemData);
void setMotionTouch(void* systemData); void setMotionTouch(void* systemData);
void setMultiTouch(void* systemData);
void setMultiMotionTouch(void* systemData);
void setKeyTouch(void* systemData); void setKeyTouch(void* systemData);
CCTouch* findTouch(int id);
CCTouch* touchSet[S3E_POINTER_TOUCH_MAX];
static int32 TouchEventHandler(void* systemData, void* userData) static int32 TouchEventHandler(void* systemData, void* userData)
{ {
((CCEGLView*)userData)->setTouch(systemData); ((CCEGLView*)userData)->setTouch(systemData);
return 0; return 0;
} }
static int32 MotionEventHandler(void* systemData, void* userData) static int32 MotionEventHandler(void* systemData, void* userData)
{ {
((CCEGLView*)userData)->setMotionTouch(systemData); ((CCEGLView*)userData)->setMotionTouch(systemData);
return 0; return 0;
} }
static int32 MultiTouchEventHandler(void* systemData, void* userData)
{
((CCEGLView*)userData)->setMultiTouch(systemData);
return 0;
}
static int32 MultiMotionEventHandler(void* systemData, void* userData)
{
((CCEGLView*)userData)->setMultiMotionTouch(systemData);
return 0;
}
static int32 KeyEventHandler(void* systemData, void* userData) static int32 KeyEventHandler(void* systemData, void* userData)
{ {
((CCEGLView*)userData)->setKeyTouch(systemData); ((CCEGLView*)userData)->setKeyTouch(systemData);

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org
Copyright (c) 2011 Максим Аксенов Copyright (c) 2011 Максим Аксенов
Copyright (c) 2011 Giovanni Zito, Francis Styck
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org
Copyright (c) 2011 Максим Аксенов Copyright (c) 2011 Максим Аксенов
Copyright (c) 2011 Giovanni Zito, Francis Styck
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -33,8 +34,8 @@
#include "png.h" #include "png.h"
#include "ft2build.h" #include "ft2build.h"
#include FT_FREETYPE_H #include FT_FREETYPE_H
#define szFont_kenning 2 #define FONT_KERNING 2
#define SHIFT6(num) ((num)>>6) #define RSHIFT6(num) ((num)>>6)
#include <strings.h> #include <strings.h>
@ -46,12 +47,12 @@ extern "C"
#include <string> #include <string>
typedef struct // typedef struct
{ // {
unsigned char* data; // unsigned char* data;
int size; // int size;
int offset; // int offset;
}tImageSource; // }tImageSource;
struct TextLine { struct TextLine {
string sLineStr; string sLineStr;
@ -86,32 +87,121 @@ public:
} }
}; };
class BitmapDC class BitmapDC
{ {
public: public:
BitmapDC() { BitmapDC();
libError = FT_Init_FreeType( &library ); ~BitmapDC();
iInterval = szFont_kenning;
m_pData = NULL; void reset();
reset(); bool getBitmap(const char *text, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask, const char * pFontName, uint fontSize);
public:
unsigned char* m_pData;
int m_iMaxLineWidth;
int m_iMaxLineHeight;
private:
void buildLine(stringstream& ss, FT_Face face, int iCurXCursor, char cLastChar);
bool divideString(FT_Face face, const char* sText, int iMaxWidth, int iMaxHeight);
/**
* compute the start pos of every line
* return value>0 represents the start x pos of the line, while -1 means fail
*/
int computeLineStart(FT_Face face, CCImage::ETextAlign eAlignMask, char cText, int iLineIndex);
bool startsWith(const std::string& str, const std::string& what);
bool endsWith(const std::string& str, const std::string& what);
std::string fileNameExtension(const std::string& pathName);
std::string basename(const std::string& pathName);
int openFont(const std::string& fontName, uint fontSize);
private:
FT_Library m_library;
FT_Face m_face ;
std::string m_fontName ;
uint m_fontSize ;
int m_libError;
int m_iInterval;
vector<TextLine> m_vLines;
};
bool BitmapDC::startsWith(const std::string& str, const std::string& what)
{
bool result = false ;
if( what.size() <= str.size() ) {
result = (str.substr( 0, what.size() ) == what) ;
}
return result ;
}
bool BitmapDC::endsWith(const std::string& str, const std::string& what)
{
bool result = false ;
if( what.size() <= str.size() ) {
result = (str.substr( str.size() - what.size() ) == what) ;
}
return result ;
}
std::string BitmapDC::fileNameExtension(const std::string& pathName)
{
std::string ext ;
std::string::size_type pos = pathName.find_last_of(".") ;
if( pos != std::string::npos && pos != pathName.size()-1 ) {
ext = pathName.substr(pos+1) ;
} }
~BitmapDC() { return ext ;
FT_Done_FreeType(library); }
std::string BitmapDC::basename(const std::string& pathName)
{
int pos = pathName.rfind("/");
std::string bn = pathName.substr(pos + 1, pathName.length() - pos + 1);
return bn ;
}
BitmapDC::BitmapDC() :
m_face(NULL)
,m_fontName()
,m_fontSize(0)
,m_iInterval(FONT_KERNING)
,m_pData(NULL)
{
m_libError = FT_Init_FreeType( &m_library );
reset();
}
BitmapDC::~BitmapDC()
{
// free face
if( m_face ) {
FT_Done_Face(m_face);
m_face = NULL;
}
FT_Done_FreeType(m_library);
//data will be deleted by CCImage //data will be deleted by CCImage
// if (m_pData) { // if (m_pData) {
// delete [] m_pData; // delete [] m_pData;
// } // }
}
} void BitmapDC::reset()
{
m_iMaxLineWidth = 0;
m_iMaxLineHeight = 0;
m_vLines.clear();
}
void reset() { void BitmapDC::buildLine( stringstream& ss, FT_Face face, int iCurXCursor, char cLastChar )
iMaxLineWidth = 0; {
iMaxLineHeight = 0;
vLines.clear();
}
void buildLine(stringstream& ss, FT_Face face, int iCurXCursor, char cLastChar) {
TextLine oTempLine; TextLine oTempLine;
ss << '\0'; ss << '\0';
oTempLine.sLineStr = ss.str(); oTempLine.sLineStr = ss.str();
@ -120,17 +210,18 @@ public:
oTempLine.iLineWidth = oTempLine.iLineWidth =
iCurXCursor - iCurXCursor -
SHIFT6( face->glyph->metrics.horiAdvance + RSHIFT6( face->glyph->metrics.horiAdvance +
face->glyph->metrics.horiBearingX face->glyph->metrics.horiBearingX
- face->glyph->metrics.width)/*-iInterval*/; //TODO interval - face->glyph->metrics.width)/*-iInterval*/; //TODO interval
iMaxLineWidth = MAX(iMaxLineWidth, oTempLine.iLineWidth); m_iMaxLineWidth = MAX(m_iMaxLineWidth, oTempLine.iLineWidth);
ss.clear(); ss.clear();
ss.str(""); ss.str("");
vLines.push_back(oTempLine); m_vLines.push_back(oTempLine);
} }
bool divideString(FT_Face face, const char* sText, int iMaxWidth, int iMaxHeight) { bool BitmapDC::divideString( FT_Face face, const char* sText, int iMaxWidth, int iMaxHeight )
{
const char* pText = sText; const char* pText = sText;
int iError = 0; int iError = 0;
int iCurXCursor; int iCurXCursor;
@ -138,7 +229,7 @@ public:
if (iError) { if (iError) {
return false; return false;
} }
iCurXCursor = -SHIFT6(face->glyph->metrics.horiBearingX); iCurXCursor = -RSHIFT6(face->glyph->metrics.horiBearingX);
//init stringstream //init stringstream
stringstream ss; stringstream ss;
@ -153,7 +244,7 @@ public:
if (iError) { if (iError) {
return false; return false;
} }
iCurXCursor = -SHIFT6(face->glyph->metrics.horiBearingX); iCurXCursor = -RSHIFT6(face->glyph->metrics.horiBearingX);
continue; continue;
} }
@ -165,19 +256,44 @@ public:
} }
//check its width //check its width
//divide it when exceeding //divide it when exceeding
if ((iMaxWidth > 0 && iCurXCursor + SHIFT6(face->glyph->metrics.width) > iMaxWidth)) { if ((iMaxWidth > 0 && iCurXCursor + RSHIFT6(face->glyph->metrics.width) > iMaxWidth)) {
buildLine(ss, face , iCurXCursor, cLastCh); buildLine(ss, face , iCurXCursor, cLastCh);
iCurXCursor = -SHIFT6(face->glyph->metrics.horiBearingX); iCurXCursor = -RSHIFT6(face->glyph->metrics.horiBearingX);
} }
cLastCh = *pText; cLastCh = *pText;
ss << *pText; ss << *pText;
iCurXCursor += SHIFT6(face->glyph->metrics.horiAdvance) + iInterval; iCurXCursor += RSHIFT6(face->glyph->metrics.horiAdvance) + m_iInterval;
pText++; pText++;
/*
if (cLastCh == ' ' || cLastCh == ',' || cLastCh == '.' || cLastCh == '!' || cLastCh == '?')
{
char *pText_temp = (char *)pText;
int iCurXCursor_temp = 0;
while((strlen(pText_temp) > 0) && (*pText_temp!=' ') && (*pText_temp !=',') && (*pText_temp != '.') && (*pText_temp != '!') && (*pText_temp != '?') && (*pText_temp != '/0') && (*pText_temp != '/n'))
{
iError = FT_Load_Glyph(face, FT_Get_Char_Index(face, *pText_temp), FT_LOAD_DEFAULT);
if (iError) {
return false;
//break;
} }
iCurXCursor_temp += SHIFT6(face->glyph->metrics.horiAdvance) + iInterval;
if (iCurXCursor + iCurXCursor_temp > iMaxWidth && iMaxWidth > 0)
{
buildLine(ss, face , iCurXCursor, cLastCh);
iCurXCursor = -SHIFT6(face->glyph->metrics.horiBearingX);
}
pText_temp++;
}
}
*/
}
if (iError) { if (iError) {
return false; return false;
} }
@ -185,17 +301,10 @@ public:
buildLine(ss,face, iCurXCursor, cLastCh); buildLine(ss,face, iCurXCursor, cLastCh);
return true; return true;
} }
/** int BitmapDC::computeLineStart( FT_Face face, CCImage::ETextAlign eAlignMask, char cText, int iLineIndex )
* compute the start pos of every line {
*
* return >0 represent the start x pos of the line
* while -1 means fail
*
*/
int computeLineStart(FT_Face face, CCImage::ETextAlign eAlignMask, char cText,
int iLineIndex) {
int iRet; int iRet;
int iError = FT_Load_Glyph(face, FT_Get_Char_Index(face, cText), FT_LOAD_DEFAULT); int iError = FT_Load_Glyph(face, FT_Get_Char_Index(face, cText), FT_LOAD_DEFAULT);
if (iError) { if (iError) {
@ -203,127 +312,168 @@ public:
} }
if (eAlignMask == CCImage::kAlignCenter) { if (eAlignMask == CCImage::kAlignCenter) {
iRet = (iMaxLineWidth - vLines[iLineIndex].iLineWidth) / 2 - SHIFT6(face->glyph->metrics.horiBearingX ); iRet = (m_iMaxLineWidth - m_vLines[iLineIndex].iLineWidth) / 2 - RSHIFT6(face->glyph->metrics.horiBearingX );
} else if (eAlignMask == CCImage::kAlignRight) { } else if (eAlignMask == CCImage::kAlignRight) {
iRet = (iMaxLineWidth - vLines[iLineIndex].iLineWidth) - SHIFT6(face->glyph->metrics.horiBearingX ); iRet = (m_iMaxLineWidth - m_vLines[iLineIndex].iLineWidth) - RSHIFT6(face->glyph->metrics.horiBearingX );
} else { } else {
// left or other situation // left or other situation
iRet = -SHIFT6(face->glyph->metrics.horiBearingX ); iRet = -RSHIFT6(face->glyph->metrics.horiBearingX );
} }
return iRet; return iRet;
}
int BitmapDC::openFont(const string& fontName, uint fontSize)
{
FT_Face aFace ;
int iError = 0 ;
if( m_fontName != basename(fontName) || m_fontSize != fontSize ) {
iError = FT_New_Face( m_library, fontName.c_str(), 0, &aFace );
if( !iError ) {
if(m_face) {
FT_Done_Face(m_face);
} }
bool getBitmap(const char *text, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask, const char * pFontName, float fontSize) { m_face = aFace ;
FT_Face face; m_fontName = basename(fontName) ;
m_fontSize = fontSize ;
}
}
return iError ;
}
bool BitmapDC::getBitmap( const char *text, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask, const char * pFontName, uint fontSize )
{
FT_Error iError; FT_Error iError;
const char* pText = text; unsigned char cTemp ;
int iY, iX, iTemp ;
uint32 offset, rowOffset ;
//data will be deleted by CCImage //data will be deleted by CCImage
// if (m_pData) { // if (m_pData) {
// delete m_pData; // delete m_pData;
// } // }
int iCurXCursor, iCurYCursor; int iCurXCursor, iCurYCursor;
bool bRet = false; bool bRet = false;
if (libError) { if (m_libError) {
return false; return false;
} }
do { do {
iError = FT_New_Face( library, pFontName, 0, &face ); std::string fName = pFontName ;
if (iError) {
//no valid font found use default string ext = fileNameExtension(fName) ;
// CCLog("no valid font, use default %s\n", pFontName); if( ext.empty() || (ext != "ttf" && ext != "TTF") ) {
iError = FT_New_Face( library, "fonts/Marker Felt.ttf", 0, &face ); fName += ".ttf" ;
}
if( !m_face || (m_fontName != basename(fName) || m_fontSize != fontSize) ) {
iError = openFont( fName, fontSize );
if (iError) { // try loading from "fonts" folder
if( !startsWith(fName,"fonts/") ) {
fName = string("fonts/") + fName ;
}
iError = openFont( fName, fontSize );
if (iError) { //no valid font found, try to use default
fName = "fonts/Marker Felt.ttf" ;
//CCLog("No valid font, use default %s\n", fName.c_str());
iError = openFont( fName, fontSize );
}
} }
CC_BREAK_IF(iError); CC_BREAK_IF(iError);
//select utf8 charmap //select utf8 charmap
iError = FT_Select_Charmap(face,FT_ENCODING_UNICODE); iError = FT_Select_Charmap(m_face,FT_ENCODING_UNICODE);
CC_BREAK_IF(iError); CC_BREAK_IF(iError);
iError = FT_Set_Pixel_Sizes(face, fontSize,fontSize); iError = FT_Set_Pixel_Sizes(m_face, fontSize,fontSize);
CC_BREAK_IF(iError); CC_BREAK_IF(iError);
}
iError = divideString(face, text, nWidth, nHeight) ? 0 : 1 ; iError = divideString(m_face, text, nWidth, nHeight) ? 0 : 1 ;
//compute the final line width //compute the final line width
iMaxLineWidth = MAX(iMaxLineWidth, nWidth); m_iMaxLineWidth = MAX(m_iMaxLineWidth, nWidth);
FT_Pos ascenderPixels = SHIFT6(face->size->metrics.ascender) ; FT_Pos ascenderPixels = RSHIFT6(m_face->size->metrics.ascender) ;
FT_Pos descenderPixels = SHIFT6(face->size->metrics.descender) ; FT_Pos descenderPixels = RSHIFT6(m_face->size->metrics.descender) ;
iMaxLineHeight = ascenderPixels - descenderPixels; m_iMaxLineHeight = ascenderPixels - descenderPixels;
iMaxLineHeight *= vLines.size(); m_iMaxLineHeight *= m_vLines.size();
//compute the final line height //compute the final line height
iMaxLineHeight = MAX(iMaxLineHeight, nHeight); m_iMaxLineHeight = MAX(m_iMaxLineHeight, nHeight);
uint bitmapSize = iMaxLineWidth * iMaxLineHeight*4 ; uint bitmapSize = m_iMaxLineWidth * m_iMaxLineHeight*4 ;
m_pData = new unsigned char[bitmapSize]; m_pData = new unsigned char[bitmapSize];
iCurYCursor = ascenderPixels;
memset(m_pData,0, bitmapSize); memset(m_pData,0, bitmapSize);
for (size_t i = 0; i < vLines.size(); i++) { const char* pText = text;
pText = vLines[i].sLineStr.c_str(); iCurYCursor = ascenderPixels;
for (size_t i = 0; i < m_vLines.size(); i++) {
pText = m_vLines[i].sLineStr.c_str();
//initialize the origin cursor //initialize the origin cursor
iCurXCursor = computeLineStart(face, eAlignMask, *pText, i); iCurXCursor = computeLineStart(m_face, eAlignMask, *pText, i);
while (*pText != 0) { while (*pText != 0) {
int iError = FT_Load_Glyph(face, FT_Get_Char_Index(face, *pText), FT_LOAD_RENDER); int iError = FT_Load_Glyph(m_face, FT_Get_Char_Index(m_face, *pText), FT_LOAD_RENDER);
if (iError) { if (iError) {
break; break;
} }
// convert glyph to bitmap with 256 gray // convert glyph to bitmap with 256 gray
// and get the bitmap // and get the bitmap
FT_Bitmap & bitmap = face->glyph->bitmap; FT_Bitmap & bitmap = m_face->glyph->bitmap;
FT_Pos horiBearingYPixels = SHIFT6(face->glyph->metrics.horiBearingY) ; FT_Pos horiBearingYPixels = RSHIFT6(m_face->glyph->metrics.horiBearingY) ;
FT_Pos horiBearingXPixels = SHIFT6(face->glyph->metrics.horiBearingX) ; FT_Pos horiBearingXPixels = RSHIFT6(m_face->glyph->metrics.horiBearingX) ;
FT_Pos horiAdvancePixels = SHIFT6(face->glyph->metrics.horiAdvance) ; FT_Pos horiAdvancePixels = RSHIFT6(m_face->glyph->metrics.horiAdvance) ;
for (int i = 0; i < bitmap.rows; ++i) { for (int i = 0; i < bitmap.rows; ++i) {
for (int j = 0; j < bitmap.width; ++j) {
// if it has gray>0 we set show it as 1, o otherwise
int iY = iCurYCursor + i - horiBearingYPixels;
int iX = iCurXCursor + j + horiBearingXPixels;
if (iY < 0 || iY>=iMaxLineHeight) { iY = iCurYCursor + i - horiBearingYPixels;
if (iY < 0 || iY>=m_iMaxLineHeight) {
//exceed the height truncate //exceed the height truncate
continue; continue;
} }
IwAssert( GAME, (((iY * iMaxLineWidth + iX) * 4 + 3) < bitmapSize) ) ; rowOffset = iY * m_iMaxLineWidth ;
// m_pData[(iY * iMaxLineWidth + iX) * 4 + 3] = bitmap.buffer[i * bitmap.width + j] ? 0xff : 0;//alpha // if it has gray>0 we set show it as 1, otherwise 0
// m_pData[(iY * iMaxLineWidth + iX) * 4 + 1] = bitmap.buffer[i * bitmap.width + j];//R for (int j = 0; j < bitmap.width; ++j) {
// m_pData[(iY * iMaxLineWidth + iX) * 4 + 2] = bitmap.buffer[i * bitmap.width + j];//G cTemp = bitmap.buffer[i * bitmap.width + j];
// m_pData[(iY * iMaxLineWidth + iX) * 4 + 0] = bitmap.buffer[i * bitmap.width + j];//B if( cTemp )
{
iX = iCurXCursor + j + horiBearingXPixels;
int iTemp = 0; offset = (rowOffset + iX) * 4 ;
unsigned char cTemp = bitmap.buffer[i * bitmap.width + j];
iTemp |= (cTemp ? 0xff : 0) << 24; IwAssert( GAME, ((offset + 3) < bitmapSize) ) ;
iTemp |= cTemp << 16 | cTemp << 8 | cTemp;
*(int*) &m_pData[ (iY * iMaxLineWidth + iX) * 4 ] = iTemp; iTemp = cTemp << 24 | cTemp << 16 | cTemp << 8 | cTemp;
*(int*) &m_pData[ offset ] = iTemp ; // ARGB
}
} }
} }
//step to next glyph //step to next glyph
iCurXCursor += horiAdvancePixels + iInterval; iCurXCursor += horiAdvancePixels + m_iInterval;
pText++; pText++;
} }
iCurYCursor += ascenderPixels - descenderPixels ; iCurYCursor += ascenderPixels - descenderPixels ;
} }
// free face
FT_Done_Face(face);
face = NULL;
//clear all lines //clear all lines
vLines.clear(); m_vLines.clear();
//success; //success;
if (iError) { if (iError) {
@ -333,16 +483,14 @@ public:
}while(0); }while(0);
return bRet; return bRet;
} }
public:
FT_Library library;
unsigned char *m_pData;
int libError;
vector<TextLine> vLines;
int iInterval;
int iMaxLineWidth;
int iMaxLineHeight;
};
static BitmapDC& sharedBitmapDC() static BitmapDC& sharedBitmapDC()
{ {
@ -663,14 +811,14 @@ bool CCImage::initWithString(
const char* pFullFontName = CCFileUtils::fullPathFromRelativePath(pFontName); const char* pFullFontName = CCFileUtils::fullPathFromRelativePath(pFontName);
CC_BREAK_IF(! dc.getBitmap(pText, nWidth, nHeight, eAlignMask, pFullFontName, (float)nSize)); CC_BREAK_IF(! dc.getBitmap(pText, nWidth, nHeight, eAlignMask, pFullFontName, nSize));
// assign the dc.m_pData to m_pData in order to save time // assign the dc.m_pData to m_pData in order to save time
m_pData = dc.m_pData; m_pData = dc.m_pData;
CC_BREAK_IF(! m_pData); CC_BREAK_IF(! m_pData);
m_nWidth = (short)dc.iMaxLineWidth; m_nWidth = (short)dc.m_iMaxLineWidth;
m_nHeight = (short)dc.iMaxLineHeight; m_nHeight = (short)dc.m_iMaxLineHeight;
m_bHasAlpha = true; m_bHasAlpha = true;
m_bPreMulti = true; m_bPreMulti = true;
m_nBitsPerComponent = 8; m_nBitsPerComponent = 8;