issue $48, implement CCXBitmapDC

This commit is contained in:
Walzer 2010-08-17 04:04:25 +00:00
parent 7c3ccd5600
commit 55f01578ea
3 changed files with 30 additions and 14 deletions

View File

@ -23,18 +23,23 @@ THE SOFTWARE.
****************************************************************************/
#include <cstring>
#include "CCXBitmapDC.h"
#include "CCXApplication_uphone.h"
#include "CCDirector.h"
#include "ImageToolKit/IT_ImageDrawingTool.h"
namespace cocos2d {
CCXBitmapDC::CCXBitmapDC(const char *text, CGSize dimensions, UITextAlignment alignment, const char *fontName, float fontSize)
{
TFont font;
font.Create(0, fontSize);
int len = strlen(text) + 1;
TUChar * pText = new TUChar[len];
TUString::StrGBToUnicode(pText, (Char*)text);
TUChar *pText = new TUChar[len];
TUString::StrUtf8ToStrUnicode(pText, (Char*)text);
if (CGSize::CGSizeEqualToSize(dimensions, CGSizeZero))
{
m_tTextSize.width = font.CharsWidth(pText, len);
m_tTextSize.width = font.CharsWidth(pText,len);
m_tTextSize.height = font.LineHeight();
}else
{
@ -48,30 +53,35 @@ namespace cocos2d {
{
i = 1;
while(i < width)
i>>=1;
i<<=1;
width = i;
}
if((height != 1) && (height & (height - 1)))
{
i = 1;
while(i < height)
i >>= 1;
i <<= 1;
height = i;
}
m_tScaleSize.width = (float)width;
m_tScaleSize.height = (float)height;
m_pBitmap = TBitmap::Create(width, height, 32);
TDC dc(NULL);
dc.DrawCharsInBitmap(pText, len, m_pBitmap, RGBA(255,0,0,255), RGBA(0,255,0,255), font, GUI_API_STYLE_ALIGNMENT_CENTER);
TWindow *pWindow = new TWindow(CCXApplication::getSharedApplication());
pWindow->CreateMemWindow(width, height, screenAlphaTransparentFormat);
TDC dc(pWindow);
dc.SetFont(font);
dc.DrawTextXYEx(pText, 0, 0, 0, RGBA(255,255,0,255), RGBA(0,0,0,0), font,
GUI_API_STYLE_ROP_MODE_TRANSPARENT | GUI_API_STYLE_SPECIFY_FORE_COLOR |
GUI_API_STYLE_ALIGNMENT_CENTER | GUI_API_STYLE_ALIGNMENT_MIDDLE);
m_pBitmap = pWindow->GetBitmap()->DupBitmapTo32();
}
void *CCXBitmapDC::GetBuffer()
{
return m_pBitmap->GetDataPtr();
UINT8 *data = m_pBitmap->GetDataPtr();
return data;
}
CGSize CCXBitmapDC::GetSize()
CGSize CCXBitmapDC::GetScaleSize()
{
return m_tScaleSize;
}

View File

@ -27,12 +27,17 @@ THE SOFTWARE.
#include "TG3.h"
#include "ccTypes.h"
#include "Cocos2dDefine.h"
#include "ImageToolKit/IT_Image.h"
namespace cocos2d{
class CCX_DLL CCXBitmapDC
{
public:
CCXBitmapDC(const char *text, CGSize dimensions, UITextAlignment alignment, const char *fontName, float fontSize);
CCXBitmapDC(const char *text,
CGSize dimensions = CGSizeZero,
UITextAlignment alignment = UITextAlignmentCenter,
const char *fontName = NULL,
float fontSize = 0);
virtual ~CCXBitmapDC(void)
{
if (m_pBitmap)
@ -43,10 +48,11 @@ namespace cocos2d{
}
void* GetBuffer(void);
CGSize GetSize();
CGSize GetScaleSize();
CGSize GetTextSize();
protected:
TBitmap *m_pBitmap;
ImageToolKit::Image *image;
CGSize m_tTextSize;
CGSize m_tScaleSize;
};

View File

@ -382,7 +382,7 @@ CCTexture2D * CCTexture2D::initWithString(const char *text, CGSize dimensions, U
CCXBitmapDC *pBitmapDC = new CCXBitmapDC(text, dimensions, alignment, fontName, fontSize);
void* pBitData = pBitmapDC->GetBuffer();
CGSize szImage = pBitmapDC->GetSize();
CGSize szImage = pBitmapDC->GetScaleSize();
CGSize szText = pBitmapDC->GetTextSize();
initWithData(pBitData, kCCTexture2DPixelFormat_RGBA8888, (UINT)szImage.width, (UINT)szImage.height, szText);