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);