[uphone] use a shared TWindow to draw bitmaps and texts, this optimization promotes the performance on uphone about 5%~6%

This commit is contained in:
Walzer 2010-12-31 16:07:52 +08:00
parent eac572b2c9
commit 56f8bf3c40
3 changed files with 103 additions and 56 deletions

View File

@ -29,20 +29,27 @@ THE SOFTWARE.
namespace cocos2d { namespace cocos2d {
static TWindow * s_pMemWnd = NULL;
CCXBitmapDC::CCXBitmapDC(int width, int height) CCXBitmapDC::CCXBitmapDC(int width, int height)
: m_pBitmap(NULL)
{ {
m_pBitmap->Create(width, height, 32); m_pBitmap = TBitmap::Create(width, height, 32);
m_pWindow = NULL;
} }
CCXBitmapDC::CCXBitmapDC(const char *text, CGSize dimensions, UITextAlignment alignment, const char *fontName, float fontSize) CCXBitmapDC::CCXBitmapDC(const char *text, CGSize dimensions, UITextAlignment alignment, const char *fontName, float fontSize)
: m_pBitmap(NULL)
{
TUChar *pText = NULL;
do
{ {
// create font // create font
TFont font; TFont font;
font.Create(0, (Int32)fontSize); CCX_BREAK_IF(! font.Create(0, (Int32)fontSize));
// text // text
Int32 len = strlen(text) + 1; Int32 len = strlen(text) + 1;
TUChar *pText = new TUChar[len]; CCX_BREAK_IF(! (pText = new TUChar[len]));
TUString::StrGBToUnicode(pText, (Char*)text); TUString::StrGBToUnicode(pText, (Char*)text);
// calculate text size // calculate text size
@ -58,12 +65,42 @@ namespace cocos2d {
Int16 width = (Int16)m_tSize.width; Int16 width = (Int16)m_tSize.width;
Int16 height = (Int16)m_tSize.height; Int16 height = (Int16)m_tSize.height;
// create bitmap
CCX_BREAK_IF(! (m_pBitmap = TBitmap::Create(width, height, 32)));
// create memory window // create memory window
m_pWindow = new TWindow(CCXApplication::sharedApplication()); if (s_pMemWnd)
m_pWindow->CreateMemWindow(width, height,screenTransparentFormat); {
TRectangle rcMemWnd(0, 0, 0, 0);
s_pMemWnd->GetClientBounds(&rcMemWnd);
if (rcMemWnd.Width() < width || rcMemWnd.Height() < height)
{
s_pMemWnd->CloseWindow();
s_pMemWnd = NULL;
}
}
do
{
// if memery window is already break
CCX_BREAK_IF(s_pMemWnd);
CCX_BREAK_IF(! (s_pMemWnd = new TWindow(CCXApplication::sharedApplication())));
Coord nCurrentWidth = CCXApplication::GetCurrentApplication()->GetScreenWidth();
Coord nCurrentHeight = CCXApplication::GetCurrentApplication()->GetScreenHeight();
Coord nMemWndW = (width >= nCurrentWidth) ? width : nCurrentWidth;
Coord nMemWndH = (height >= nCurrentHeight) ? height : nCurrentHeight;
CCX_BREAK_IF(s_pMemWnd->CreateMemWindow(nMemWndW, nMemWndH,screenTransparentFormat));
delete s_pMemWnd;
s_pMemWnd = NULL;
} while (0);
CCX_BREAK_IF(! s_pMemWnd);
// create DC // create DC
TDC dc(m_pWindow); TDC dc(s_pMemWnd);
// set DC styles // set DC styles
UInt32 styles = GUI_API_STYLE_SPECIFY_FORE_COLOR | GUI_API_STYLE_ROP_MODE_TRANSPARENT | UInt32 styles = GUI_API_STYLE_SPECIFY_FORE_COLOR | GUI_API_STYLE_ROP_MODE_TRANSPARENT |
GUI_API_STYLE_SPECIFY_BACK_COLOR | GUI_API_STYLE_ALIGNMENT_MIDDLE | GUI_API_STYLE_SPECIFY_FONT; GUI_API_STYLE_SPECIFY_BACK_COLOR | GUI_API_STYLE_ALIGNMENT_MIDDLE | GUI_API_STYLE_SPECIFY_FONT;
@ -83,23 +120,28 @@ namespace cocos2d {
styles |= GUI_API_STYLE_ALIGNMENT_CENTER; styles |= GUI_API_STYLE_ALIGNMENT_CENTER;
break; break;
} }
TRectangle rect;
m_pWindow->GetWindowFrameRect(&rect);
m_pBitmap = m_pWindow->GetMemWindowTBitmapPtr(); s_pMemWnd->GetMemWindowTBitmapPtr()->Fill32(RGBA(0, 0, 0, 0), 0, 0, width, height);
m_pBitmap->Fill32(RGBA(0, 0, 0, 0));
TRectangle rect(0, 0, width, height);
dc.DrawTextInRectangleEx(pText, 0, RGBA(255,255,255,255), RGBA(0,0,0,255), font, &rect, styles); dc.DrawTextInRectangleEx(pText, 0, RGBA(255,255,255,255), RGBA(0,0,0,255), font, &rect, styles);
dc.ReadBitmap(m_pBitmap, 0, 0);
} while (0);
if (pText)
{
delete[] pText; delete[] pText;
pText = NULL;
}
} }
CCXBitmapDC::~CCXBitmapDC(void) CCXBitmapDC::~CCXBitmapDC(void)
{ {
if (m_pWindow) if (m_pBitmap)
{ {
m_pWindow->CloseWindowNow(); m_pBitmap->Destroy();
m_pWindow = NULL; m_pBitmap = NULL;
} }
} }

View File

@ -47,7 +47,6 @@ namespace cocos2d{
protected: protected:
TBitmap *m_pBitmap; TBitmap *m_pBitmap;
CGSize m_tSize; CGSize m_tSize;
TWindow *m_pWindow;
}; };
} }

View File

@ -83,6 +83,12 @@ UIImage::UIImage(void)
UIImage::UIImage(CCXBitmapDC * pBmpDC) UIImage::UIImage(CCXBitmapDC * pBmpDC)
{ {
m_imageInfo.hasAlpha = false;
m_imageInfo.isPremultipliedAlpha = false;
m_imageInfo.height = 0;
m_imageInfo.width = 0;
m_imageInfo.data = NULL;
m_imageInfo.bitsPerComponent = 0;
do do
{ {
CCX_BREAK_IF(! pBmpDC); CCX_BREAK_IF(! pBmpDC);