2011-02-24 19:42:45 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-06-22 16:58:50 +08:00
|
|
|
#define COCOS2D_DEBUG 1
|
2011-02-24 19:42:45 +08:00
|
|
|
|
2011-06-22 16:58:50 +08:00
|
|
|
#include <android/log.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <jni.h>
|
|
|
|
|
|
|
|
#include "CCPlatformMacros.h"
|
|
|
|
#include "CCImage.h"
|
2011-07-22 18:14:29 +08:00
|
|
|
#include "jni/JniHelper.h"
|
2011-02-24 19:42:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
NS_CC_BEGIN;
|
|
|
|
|
|
|
|
class BitmapDC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BitmapDC()
|
2011-06-22 16:58:50 +08:00
|
|
|
: m_pData(NULL)
|
|
|
|
, m_nWidth(0)
|
|
|
|
, m_nHeight(0)
|
2011-02-24 19:42:45 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~BitmapDC(void)
|
|
|
|
{
|
2011-06-22 16:58:50 +08:00
|
|
|
if (m_pData)
|
2011-02-24 19:42:45 +08:00
|
|
|
{
|
2011-06-22 16:58:50 +08:00
|
|
|
delete [] m_pData;
|
2011-02-24 19:42:45 +08:00
|
|
|
}
|
2011-06-22 16:58:50 +08:00
|
|
|
}
|
2011-02-24 19:42:45 +08:00
|
|
|
|
2011-06-22 16:58:50 +08:00
|
|
|
bool getBitmapFromJava(const char *text, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask, const char * pFontName, float fontSize)
|
2011-02-24 19:42:45 +08:00
|
|
|
{
|
2011-07-22 18:14:29 +08:00
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmap",
|
|
|
|
"(Ljava/lang/String;Ljava/lang/String;IIII)V"))
|
2011-02-24 19:42:45 +08:00
|
|
|
{
|
2011-07-22 18:14:29 +08:00
|
|
|
CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__);
|
2011-06-22 16:58:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
2011-02-24 19:42:45 +08:00
|
|
|
|
2011-06-22 16:58:50 +08:00
|
|
|
/**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
|
|
|
|
*/
|
2011-09-20 14:25:16 +08:00
|
|
|
jstring jstrText = methodInfo.env->NewStringUTF(text);
|
|
|
|
jstring jstrFont = methodInfo.env->NewStringUTF(pFontName);
|
|
|
|
|
2011-07-22 18:14:29 +08:00
|
|
|
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, methodInfo.env->NewStringUTF(text),
|
|
|
|
methodInfo.env->NewStringUTF(pFontName), (int)fontSize, eAlignMask, nWidth, nHeight);
|
2011-02-24 19:42:45 +08:00
|
|
|
|
2011-09-20 14:25:16 +08:00
|
|
|
methodInfo.env->DeleteLocalRef(jstrText);
|
|
|
|
methodInfo.env->DeleteLocalRef(jstrFont);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
|
2011-06-22 16:58:50 +08:00
|
|
|
return true;
|
2011-02-24 19:42:45 +08:00
|
|
|
}
|
|
|
|
|
2011-06-22 16:58:50 +08:00
|
|
|
// ARGB -> RGBA
|
|
|
|
unsigned int swapAlpha(unsigned int value)
|
2011-02-24 19:42:45 +08:00
|
|
|
{
|
2011-06-22 16:58:50 +08:00
|
|
|
return ((value << 8 & 0xffffff00) | (value >> 24 & 0x000000ff));
|
2011-02-24 19:42:45 +08:00
|
|
|
}
|
|
|
|
|
2011-06-22 16:58:50 +08:00
|
|
|
public:
|
|
|
|
int m_nWidth;
|
|
|
|
int m_nHeight;
|
|
|
|
unsigned char *m_pData;
|
|
|
|
JNIEnv *env;
|
2011-02-24 19:42:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static BitmapDC& sharedBitmapDC()
|
|
|
|
{
|
|
|
|
static BitmapDC s_BmpDC;
|
|
|
|
return s_BmpDC;
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:49:58 +08:00
|
|
|
bool CCImage::initWithString(
|
2011-02-24 19:42:45 +08:00
|
|
|
const char * pText,
|
|
|
|
int nWidth/* = 0*/,
|
|
|
|
int nHeight/* = 0*/,
|
|
|
|
ETextAlign eAlignMask/* = kAlignCenter*/,
|
|
|
|
const char * pFontName/* = nil*/,
|
|
|
|
int nSize/* = 0*/)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_BREAK_IF(! pText);
|
2011-02-24 19:42:45 +08:00
|
|
|
|
|
|
|
BitmapDC &dc = sharedBitmapDC();
|
|
|
|
|
2011-06-22 16:58:50 +08:00
|
|
|
CC_BREAK_IF(! dc.getBitmapFromJava(pText, nWidth, nHeight, eAlignMask, pFontName, nSize));
|
2011-02-24 19:42:45 +08:00
|
|
|
|
2011-06-22 16:58:50 +08:00
|
|
|
// assign the dc.m_pData to m_pData in order to save time
|
|
|
|
m_pData = dc.m_pData;
|
2011-03-28 11:09:49 +08:00
|
|
|
CC_BREAK_IF(! m_pData);
|
2011-02-24 19:42:45 +08:00
|
|
|
|
2011-06-22 16:58:50 +08:00
|
|
|
m_nWidth = (short)dc.m_nWidth;
|
|
|
|
m_nHeight = (short)dc.m_nHeight;
|
2011-02-24 19:42:45 +08:00
|
|
|
m_bHasAlpha = true;
|
|
|
|
m_bPreMulti = true;
|
2011-06-22 16:58:50 +08:00
|
|
|
m_nBitsPerComponent = 8;
|
2011-02-24 19:42:45 +08:00
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END;
|
2011-06-22 16:58:50 +08:00
|
|
|
|
|
|
|
// 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];
|
2011-07-22 18:14:29 +08:00
|
|
|
env->GetByteArrayRegion(pixels, 0, size, (jbyte*)cocos2d::sharedBitmapDC().m_pData);
|
2011-06-22 16:58:50 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|