From 3fa4d2dcc45e2c77e1356150fb1d01498cd7018d Mon Sep 17 00:00:00 2001 From: Walzer Date: Mon, 16 Aug 2010 03:59:53 +0000 Subject: [PATCH] issue $48, implement CCXBitmapDC and implement CCTexture2D.cpp --- cocos2dx/cocos2dx-uphone.vcproj | 8 +++ cocos2dx/platform/platform.h | 1 + cocos2dx/platform/uphone/CCXBitmapDC.cpp | 28 +++++++++ cocos2dx/platform/uphone/CCXBitmapDC.h | 43 ++++++++++++++ cocos2dx/textures/CCTexture2D.cpp | 76 +++--------------------- 5 files changed, 89 insertions(+), 67 deletions(-) create mode 100644 cocos2dx/platform/uphone/CCXBitmapDC.cpp create mode 100644 cocos2dx/platform/uphone/CCXBitmapDC.h diff --git a/cocos2dx/cocos2dx-uphone.vcproj b/cocos2dx/cocos2dx-uphone.vcproj index 0a1a5171e2..5d3e61c153 100644 --- a/cocos2dx/cocos2dx-uphone.vcproj +++ b/cocos2dx/cocos2dx-uphone.vcproj @@ -736,6 +736,14 @@ RelativePath=".\platform\uphone\CCXApplication_uphone.h" > + + + + diff --git a/cocos2dx/platform/platform.h b/cocos2dx/platform/platform.h index 1bbe04204c..ddfac7b855 100644 --- a/cocos2dx/platform/platform.h +++ b/cocos2dx/platform/platform.h @@ -34,6 +34,7 @@ THE SOFTWARE. #include "uphone/UIImage.h" #include "uphone/Cocos2dTypes.h" #include "uphone/CCXGLExtFunc.h" + #include "uphone/CCXBitmapDC.h" #else #error #endif diff --git a/cocos2dx/platform/uphone/CCXBitmapDC.cpp b/cocos2dx/platform/uphone/CCXBitmapDC.cpp new file mode 100644 index 0000000000..47511b2c21 --- /dev/null +++ b/cocos2dx/platform/uphone/CCXBitmapDC.cpp @@ -0,0 +1,28 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#include "CCXBitmapDC.h" +namespace cocos2d { + + +} \ No newline at end of file diff --git a/cocos2dx/platform/uphone/CCXBitmapDC.h b/cocos2dx/platform/uphone/CCXBitmapDC.h new file mode 100644 index 0000000000..fd83a493b4 --- /dev/null +++ b/cocos2dx/platform/uphone/CCXBitmapDC.h @@ -0,0 +1,43 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#ifndef __CCXBITMAP_DC_H__ +#define __CCXBITMAP_DC_H__ +#include "CCTexture2D.h" +namespace cocos2d{ + class CCX_DLL CCXBitmapDC + { + public: + CCXBitmapDC(const char *text, CGSize dimensions, const char *fontName, CGFloat fontSize, UITextAlignment alignment); + CCXBitmapDC(int nWidth, int nHeight); + ~CCXBitmapDC(void); + + void CreateBitmapDC(int nWidth, int nHeight); + void* GetBuffer(void); + CGSize GetSize(); + CGSize GetTextSize(); + protected: + }; +} + +#endif //__CCXBITMAP_DC_H__ \ No newline at end of file diff --git a/cocos2dx/textures/CCTexture2D.cpp b/cocos2dx/textures/CCTexture2D.cpp index 4876e6fc0f..4cfae65242 100644 --- a/cocos2dx/textures/CCTexture2D.cpp +++ b/cocos2dx/textures/CCTexture2D.cpp @@ -375,77 +375,19 @@ CCTexture2D * CCTexture2D::initPremultipliedATextureWithImage(UIImage *image, un // implementation CCTexture2D (Text) CCTexture2D * CCTexture2D::initWithString(const char *str, const char *fontName, float fontSize) { - /** @todo about UIFont - CGSize dim; - - #if CC_FONT_LABEL_SUPPORT - ZFont *zFont = [[FontManager sharedManager] zFontWithName:name pointSize:size]; - if (zFont != nil) - dim = [string sizeWithZFont:zFont]; - else - #endif - dim = [string sizeWithFont:[UIFont fontWithName:name size:size]]; - - return [self initWithString:string dimensions:dim alignment:UITextAlignmentCenter fontName:name fontSize:size];*/ - return NULL;// tobe deleted + return initWithString(str, CGSizeMake(0,0), UITextAlignmentCenter, fontName, fontSize); } CCTexture2D * CCTexture2D::initWithString(const char *str, CGSize dimensions, UITextAlignment alignment, const char *fontName, float fontSize) - { -/** @todo UITextAlignment - NSUInteger width, - height, - i; - CGContextRef context; - void* data; - CGColorSpaceRef colorSpace; - id uiFont; +{ + CCXBitmapDC *pBitmapDC = new CCXBitmapDC(str, dimensions, fontName, fontSize, alignment); - width = dimensions.width; - if((width != 1) && (width & (width - 1))) { - i = 1; - while(i < width) - i *= 2; - width = i; - } - height = dimensions.height; - if((height != 1) && (height & (height - 1))) { - i = 1; - while(i < height) - i *= 2; - height = i; - } + void* pBitData = pBitmapDC->GetBuffer(); + CGSize szImage = pBitmapDC->GetSize(); + CGSize szText = pBitmapDC->GetTextSize(); - colorSpace = CGColorSpaceCreateDeviceGray(); - data = calloc(height, width); - context = CGBitmapContextCreate(data, width, height, 8, width, colorSpace, kCGImageAlphaNone); - CGColorSpaceRelease(colorSpace); + initWithData(pBitData, kCCTexture2DPixelFormat_RGBA8888, (UINT)szImage.width, (UINT)szImage.height, szText); - - CGContextSetGrayFillColor(context, 1.0f, 1.0f); - CGContextTranslateCTM(context, 0.0f, height); - CGContextScaleCTM(context, 1.0f, -1.0f); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential - UIGraphicsPushContext(context); - - - #if CC_FONT_LABEL_SUPPORT - uiFont = [[FontManager sharedManager] zFontWithName:name pointSize:size]; - if (uiFont != nil) - [string drawInRect:CGRectMake(0, 0, dimensions.width, dimensions.height) withZFont:uiFont lineBreakMode:UILineBreakModeWordWrap alignment:alignment]; - else - #endif // CC_FONT_LABEL_SUPPORT - { - uiFont = [UIFont fontWithName:name size:size]; - [string drawInRect:CGRectMake(0, 0, dimensions.width, dimensions.height) withFont:uiFont lineBreakMode:UILineBreakModeWordWrap alignment:alignment]; - } - if( ! uiFont ) - CCLOG(@"cocos2d: Texture2D: Font '%@' not found", name); - UIGraphicsPopContext(); - - self = [self initWithData:data pixelFormat:kCCTexture2DPixelFormat_A8 pixelsWide:width pixelsHigh:height contentSize:dimensions]; - - CGContextRelease(context); - free(data); - */ + delete pBitmapDC; return this; } @@ -586,7 +528,7 @@ void CCTexture2D::generateMipmap() { NSAssert( m_uPixelsWide == nextPOT(m_uPixelsWide) && m_uPixelsHigh == nextPOT(m_uPixelsHigh), "Mimpap texture only works in POT textures"); glBindTexture( GL_TEXTURE_2D, this->m_uName ); - /// @todo include what??? glGenerateMipmapOES(GL_TEXTURE_2D); + glGenerateMipmapOES(GL_TEXTURE_2D); } void CCTexture2D::setTexParameters(ccTexParams *texParams)