From 56f8bf3c40b8b220f5dc817513c0aa7ca66294d5 Mon Sep 17 00:00:00 2001 From: Walzer Date: Fri, 31 Dec 2010 16:07:52 +0800 Subject: [PATCH 1/2] [uphone] use a shared TWindow to draw bitmaps and texts, this optimization promotes the performance on uphone about 5%~6% --- cocos2dx/platform/uphone/CCXBitmapDC.cpp | 152 +++++++++++------- cocos2dx/platform/uphone/CCXBitmapDC.h | 1 - .../platform/uphone/CCXUIImage_uphone.cpp | 6 + 3 files changed, 103 insertions(+), 56 deletions(-) diff --git a/cocos2dx/platform/uphone/CCXBitmapDC.cpp b/cocos2dx/platform/uphone/CCXBitmapDC.cpp index 90b01f0162..023d32006b 100644 --- a/cocos2dx/platform/uphone/CCXBitmapDC.cpp +++ b/cocos2dx/platform/uphone/CCXBitmapDC.cpp @@ -29,78 +29,120 @@ THE SOFTWARE. namespace cocos2d { + static TWindow * s_pMemWnd = NULL; + CCXBitmapDC::CCXBitmapDC(int width, int height) + : m_pBitmap(NULL) { - m_pBitmap->Create(width, height, 32); - m_pWindow = NULL; + m_pBitmap = TBitmap::Create(width, height, 32); } + CCXBitmapDC::CCXBitmapDC(const char *text, CGSize dimensions, UITextAlignment alignment, const char *fontName, float fontSize) + : m_pBitmap(NULL) { - // create font - TFont font; - font.Create(0, (Int32)fontSize); + TUChar *pText = NULL; + do + { + // create font + TFont font; + CCX_BREAK_IF(! font.Create(0, (Int32)fontSize)); - // text - Int32 len = strlen(text) + 1; - TUChar *pText = new TUChar[len]; - TUString::StrGBToUnicode(pText, (Char*)text); + // text + Int32 len = strlen(text) + 1; + CCX_BREAK_IF(! (pText = new TUChar[len])); + TUString::StrGBToUnicode(pText, (Char*)text); - // calculate text size - if (CGSize::CGSizeEqualToSize(dimensions, CGSizeZero)) - { - m_tSize.width = font.CharsWidth(pText,len); - m_tSize.height = font.LineHeight(); - }else - { - m_tSize = dimensions; - } + // calculate text size + if (CGSize::CGSizeEqualToSize(dimensions, CGSizeZero)) + { + m_tSize.width = font.CharsWidth(pText,len); + m_tSize.height = font.LineHeight(); + }else + { + m_tSize = dimensions; + } - Int16 width = (Int16)m_tSize.width; - Int16 height = (Int16)m_tSize.height; + Int16 width = (Int16)m_tSize.width; + Int16 height = (Int16)m_tSize.height; - // create memory window - m_pWindow = new TWindow(CCXApplication::sharedApplication()); - m_pWindow->CreateMemWindow(width, height,screenTransparentFormat); + // create bitmap + CCX_BREAK_IF(! (m_pBitmap = TBitmap::Create(width, height, 32))); - // create DC - TDC dc(m_pWindow); - // set DC styles - 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; + // create memory window + if (s_pMemWnd) + { + TRectangle rcMemWnd(0, 0, 0, 0); + s_pMemWnd->GetClientBounds(&rcMemWnd); + if (rcMemWnd.Width() < width || rcMemWnd.Height() < height) + { + s_pMemWnd->CloseWindow(); + s_pMemWnd = NULL; + } + } - switch (alignment) - { - case UITextAlignmentLeft: - styles |= GUI_API_STYLE_ALIGNMENT_LEFT; - break; - case UITextAlignmentCenter: - styles |= GUI_API_STYLE_ALIGNMENT_CENTER; - break; - case UITextAlignmentRight: - styles |= GUI_API_STYLE_ALIGNMENT_RIGHT; - break; - default: - styles |= GUI_API_STYLE_ALIGNMENT_CENTER; - break; - } - TRectangle rect; - m_pWindow->GetWindowFrameRect(&rect); + do + { + // if memery window is already break + CCX_BREAK_IF(s_pMemWnd); - m_pBitmap = m_pWindow->GetMemWindowTBitmapPtr(); - m_pBitmap->Fill32(RGBA(0, 0, 0, 0)); - - dc.DrawTextInRectangleEx(pText, 0, RGBA(255,255,255,255), RGBA(0,0,0,255), font, &rect, styles); + CCX_BREAK_IF(! (s_pMemWnd = new TWindow(CCXApplication::sharedApplication()))); - delete [] pText; + 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 + TDC dc(s_pMemWnd); + // set DC styles + 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; + + switch (alignment) + { + case UITextAlignmentLeft: + styles |= GUI_API_STYLE_ALIGNMENT_LEFT; + break; + case UITextAlignmentCenter: + styles |= GUI_API_STYLE_ALIGNMENT_CENTER; + break; + case UITextAlignmentRight: + styles |= GUI_API_STYLE_ALIGNMENT_RIGHT; + break; + default: + styles |= GUI_API_STYLE_ALIGNMENT_CENTER; + break; + } + + s_pMemWnd->GetMemWindowTBitmapPtr()->Fill32(RGBA(0, 0, 0, 0), 0, 0, width, height); + + TRectangle rect(0, 0, width, height); + 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; + pText = NULL; + } } CCXBitmapDC::~CCXBitmapDC(void) { - if (m_pWindow) - { - m_pWindow->CloseWindowNow(); - m_pWindow = NULL; - } + if (m_pBitmap) + { + m_pBitmap->Destroy(); + m_pBitmap = NULL; + } } void *CCXBitmapDC::getBuffer() diff --git a/cocos2dx/platform/uphone/CCXBitmapDC.h b/cocos2dx/platform/uphone/CCXBitmapDC.h index 01caa992b2..fefadfa2c2 100644 --- a/cocos2dx/platform/uphone/CCXBitmapDC.h +++ b/cocos2dx/platform/uphone/CCXBitmapDC.h @@ -47,7 +47,6 @@ namespace cocos2d{ protected: TBitmap *m_pBitmap; CGSize m_tSize; - TWindow *m_pWindow; }; } diff --git a/cocos2dx/platform/uphone/CCXUIImage_uphone.cpp b/cocos2dx/platform/uphone/CCXUIImage_uphone.cpp index a7014a37fe..0b205aed79 100644 --- a/cocos2dx/platform/uphone/CCXUIImage_uphone.cpp +++ b/cocos2dx/platform/uphone/CCXUIImage_uphone.cpp @@ -83,6 +83,12 @@ UIImage::UIImage(void) 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 { CCX_BREAK_IF(! pBmpDC); From 4a6989b6ff58dbbc528cc572c8c13ce4310473f5 Mon Sep 17 00:00:00 2001 From: Walzer Date: Fri, 31 Dec 2010 18:15:30 +0800 Subject: [PATCH 2/2] [uphone] add keyMenuBack to CCKeypadDelegate --- cocos2dx/include/CCKeypadDelegate.h | 3 + cocos2dx/include/CCKeypadDispatcher.h | 1 + .../keypad_dispatcher/CCKeypadDispatcher.cpp | 12 ++-- .../platform/uphone/CCXEGLView_uphone.cpp | 5 ++ tests/tests/KeypadTest/KeypadTest.cpp | 72 ++++++++++--------- tests/tests/KeypadTest/KeypadTest.h | 45 ++++++------ 6 files changed, 79 insertions(+), 59 deletions(-) diff --git a/cocos2dx/include/CCKeypadDelegate.h b/cocos2dx/include/CCKeypadDelegate.h index d666be936d..5597f1d443 100644 --- a/cocos2dx/include/CCKeypadDelegate.h +++ b/cocos2dx/include/CCKeypadDelegate.h @@ -41,6 +41,9 @@ public: // The back key clicked virtual void keyBackClicked() {} + + // The menu key clicked. only avialble on uphone & android + virtual void keyMenuClicked() {}; }; /** diff --git a/cocos2dx/include/CCKeypadDispatcher.h b/cocos2dx/include/CCKeypadDispatcher.h index 4812f5f4d7..15ea73d724 100644 --- a/cocos2dx/include/CCKeypadDispatcher.h +++ b/cocos2dx/include/CCKeypadDispatcher.h @@ -33,6 +33,7 @@ namespace cocos2d { typedef enum { // the back key clicked msg kTypeBackClicked = 1, + kTypeMenuClicked, } ccKeypadMSGType; struct _ccCArray; diff --git a/cocos2dx/keypad_dispatcher/CCKeypadDispatcher.cpp b/cocos2dx/keypad_dispatcher/CCKeypadDispatcher.cpp index 277558c3b7..64111e5a4f 100644 --- a/cocos2dx/keypad_dispatcher/CCKeypadDispatcher.cpp +++ b/cocos2dx/keypad_dispatcher/CCKeypadDispatcher.cpp @@ -29,10 +29,10 @@ THE SOFTWARE. namespace cocos2d { static CCKeypadDispatcher* s_KeypadDispatcher = NULL; -//------------------------------------------------------------------ -// -// CCKeypadDispatcher -// +//------------------------------------------------------------------ +// +// CCKeypadDispatcher +// //------------------------------------------------------------------ CCKeypadDispatcher::CCKeypadDispatcher() : m_bLocked(false) @@ -154,7 +154,9 @@ bool CCKeypadDispatcher::dispatchKeypadMSG(ccKeypadMSGType nMsgType) case kTypeBackClicked: pDelegate->keyBackClicked(); break; - + case kTypeMenuClicked: + pDelegate->keyMenuClicked(); + break; default: break; } diff --git a/cocos2dx/platform/uphone/CCXEGLView_uphone.cpp b/cocos2dx/platform/uphone/CCXEGLView_uphone.cpp index 7cfba7dcbd..0bde3ec1b4 100644 --- a/cocos2dx/platform/uphone/CCXEGLView_uphone.cpp +++ b/cocos2dx/platform/uphone/CCXEGLView_uphone.cpp @@ -257,6 +257,11 @@ Boolean CCXEGLView::EventHandler(TApplication * pApp, EventType * pEvent) { bHandled = CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(kTypeBackClicked); } + else if (pEvent->sParam1 == SYS_KEY_SOFTKEY_LEFT_UP || + pEvent->sParam1 == SYS_KEY_SOFTKEY_LEFT_LONG) + { + bHandled == CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(kTypeMenuClicked); + } } break; diff --git a/tests/tests/KeypadTest/KeypadTest.cpp b/tests/tests/KeypadTest/KeypadTest.cpp index bad326bca0..71261c3ab6 100644 --- a/tests/tests/KeypadTest/KeypadTest.cpp +++ b/tests/tests/KeypadTest/KeypadTest.cpp @@ -1,37 +1,45 @@ #include "KeypadTest.h" -KeypadTest::KeypadTest() -: m_bShow(true) -{ - CGSize s = CCDirector::sharedDirector()->getWinSize(); - CCLabel* label = CCLabel::labelWithString("Keypad Test", "Arial", 28); - addChild(label, 0); - label->setPosition( ccp(s.width/2, s.height-50) ); - - CCLabel* subtitle = CCLabel::labelWithString("only goes well in cocos2d-uphone now!", "Arial", 18); - addChild(subtitle, 0); - subtitle->setPosition( ccp(s.width/2, s.height-100) ); - - setIsKeypadEnabled(true); - - // create a label to display the tip string - m_pLabel = CCLabel::labelWithString("Key Back Clicked!", "Arial", 22); - m_pLabel->setPosition(ccp(s.width / 2, s.height / 2)); - addChild(m_pLabel, 0); - m_pLabel->setIsVisible(false); - m_pLabel->retain(); -} - -KeypadTest::~KeypadTest() -{ - m_pLabel->release(); -} - -void KeypadTest::keyBackClicked() -{ - m_pLabel->setIsVisible(m_bShow); - m_bShow = !m_bShow; -} +KeypadTest::KeypadTest() +: m_bShow(true) +{ + CGSize s = CCDirector::sharedDirector()->getWinSize(); + CCLabel* label = CCLabel::labelWithString("Keypad Test", "Arial", 28); + addChild(label, 0); + label->setPosition( ccp(s.width/2, s.height-50) ); + + CCLabel* subtitle = CCLabel::labelWithString("only goes well in cocos2d-uphone now!", "Arial", 18); + addChild(subtitle, 0); + subtitle->setPosition( ccp(s.width/2, s.height-100) ); + + setIsKeypadEnabled(true); + + // create a label to display the tip string + m_pLabel = CCLabel::labelWithString("Please press any key...", "Arial", 22); + m_pLabel->setPosition(ccp(s.width / 2, s.height / 2)); + addChild(m_pLabel, 0); + // m_pLabel->setIsVisible(false); + m_pLabel->retain(); +} + +KeypadTest::~KeypadTest() +{ + m_pLabel->release(); +} + +void KeypadTest::keyBackClicked() +{ + m_pLabel->setString("BACK clicked!"); + // m_pLabel->setIsVisible(m_bShow); + // m_bShow = !m_bShow; +} + +void KeypadTest::keyMenuClicked() +{ + m_pLabel->setString("MENU clicked!"); + // m_pLabel->setIsVisible(m_bShow); + // m_bShow = !m_bShow; +} void KeypadTestScene::runThisTest() { diff --git a/tests/tests/KeypadTest/KeypadTest.h b/tests/tests/KeypadTest/KeypadTest.h index 6f329a16a5..098bb95c6e 100644 --- a/tests/tests/KeypadTest/KeypadTest.h +++ b/tests/tests/KeypadTest/KeypadTest.h @@ -1,26 +1,27 @@ -#ifndef _KEYPAD_TEST_H_ -#define _KEYPAD_TEST_H_ +#ifndef _KEYPAD_TEST_H_ +#define _KEYPAD_TEST_H_ #include "cocos2d.h" #include "../testBasic.h" -class KeypadTest : public CCLayer -{ -public: - KeypadTest(); - ~KeypadTest(); - - virtual void keyBackClicked(); - -private: - CCLabel* m_pLabel; - bool m_bShow; -}; - -class KeypadTestScene : public TestScene -{ -public: - virtual void runThisTest(); -}; - -#endif +class KeypadTest : public CCLayer +{ +public: + KeypadTest(); + ~KeypadTest(); + + virtual void keyBackClicked(); + virtual void keyMenuClicked(); + +private: + CCLabel* m_pLabel; + bool m_bShow; +}; + +class KeypadTestScene : public TestScene +{ +public: + virtual void runThisTest(); +}; + +#endif