/**************************************************************************** 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. ****************************************************************************/ //#define COCOS2D_DEBUG 1 #include #include #include #include "CCPlatformMacros.h" #include "CCImage.h" #include "jni/JniHelper.h" NS_CC_BEGIN class BitmapDC { public: BitmapDC() : m_pData(NULL) , m_nWidth(0) , m_nHeight(0) { } ~BitmapDC(void) { if (m_pData) { delete [] m_pData; } } bool getBitmapFromJava(const char *text, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask, const char * pFontName, float fontSize) { JniMethodInfo methodInfo; if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmap", "(Ljava/lang/String;Ljava/lang/String;IIII)V")) { CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__); return false; } /**create bitmap * this method call Cococs2dx.createBitmap()(java code) to create the bitmap, the java code * will call Java_org_cocos2dx_lib_Cocos2dxBitmap_nativeInitBitmapDC() to init the width, height * and data. * use this appoach to decrease the jni call number */ jstring jstrText = methodInfo.env->NewStringUTF(text); jstring jstrFont = methodInfo.env->NewStringUTF(pFontName); methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, jstrText, jstrFont, (int)fontSize, eAlignMask, nWidth, nHeight); methodInfo.env->DeleteLocalRef(jstrText); methodInfo.env->DeleteLocalRef(jstrFont); methodInfo.env->DeleteLocalRef(methodInfo.classID); return true; } // ARGB -> RGBA unsigned int swapAlpha(unsigned int value) { return ((value << 8 & 0xffffff00) | (value >> 24 & 0x000000ff)); } public: int m_nWidth; int m_nHeight; unsigned char *m_pData; JNIEnv *env; }; static BitmapDC& sharedBitmapDC() { static BitmapDC s_BmpDC; return s_BmpDC; } bool CCImage::initWithString( const char * pText, int nWidth/* = 0*/, int nHeight/* = 0*/, ETextAlign eAlignMask/* = kAlignCenter*/, const char * pFontName/* = nil*/, int nSize/* = 0*/) { bool bRet = false; do { CC_BREAK_IF(! pText); BitmapDC &dc = sharedBitmapDC(); CC_BREAK_IF(! dc.getBitmapFromJava(pText, nWidth, nHeight, eAlignMask, pFontName, nSize)); // assign the dc.m_pData to m_pData in order to save time m_pData = dc.m_pData; CC_BREAK_IF(! m_pData); m_nWidth = (short)dc.m_nWidth; m_nHeight = (short)dc.m_nHeight; m_bHasAlpha = true; m_bPreMulti = true; m_nBitsPerComponent = 8; bRet = true; } while (0); return bRet; } NS_CC_END // this method is called by Cocos2dxBitmap extern "C" { /** * this method is called by java code to init width, height and pixels data */ void Java_org_cocos2dx_lib_Cocos2dxBitmap_nativeInitBitmapDC(JNIEnv* env, jobject thiz, int width, int height, jbyteArray pixels) { int size = width * height * 4; cocos2d::sharedBitmapDC().m_nWidth = width; cocos2d::sharedBitmapDC().m_nHeight = height; cocos2d::sharedBitmapDC().m_pData = new unsigned char[size]; env->GetByteArrayRegion(pixels, 0, size, (jbyte*)cocos2d::sharedBitmapDC().m_pData); // swap data unsigned int *tempPtr = (unsigned int*)cocos2d::sharedBitmapDC().m_pData; unsigned int tempdata = 0; for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { tempdata = *tempPtr; *tempPtr++ = cocos2d::sharedBitmapDC().swapAlpha(tempdata); } } } };