2012-04-19 14:35:52 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
//#define COCOS2D_DEBUG 1
|
|
|
|
|
2012-04-25 16:18:04 +08:00
|
|
|
#define __CC_PLATFORM_IMAGE_CPP__
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "platform/CCImageCommon_cpp.h"
|
|
|
|
#include "platform/CCPlatformMacros.h"
|
2012-06-19 17:22:55 +08:00
|
|
|
#include "platform/CCImage.h"
|
2013-05-09 21:29:42 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "jni/JniHelper.h"
|
2012-04-25 16:18:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#include <android/log.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <jni.h>
|
|
|
|
|
2013-05-01 07:36:14 +08:00
|
|
|
// prototype
|
|
|
|
void swapAlphaChannel(unsigned int *pImageMemory, unsigned int numPixels);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class BitmapDC
|
|
|
|
{
|
|
|
|
public:
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
BitmapDC()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _data(NULL)
|
|
|
|
, _width(0)
|
|
|
|
, _height(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~BitmapDC(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_data)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
delete [] _data;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-27 02:10:23 +08:00
|
|
|
bool getBitmapFromJavaShadowStroke( const char *text,
|
|
|
|
int nWidth,
|
|
|
|
int nHeight,
|
2013-07-25 23:09:18 +08:00
|
|
|
Image::TextAlign eAlignMask,
|
2013-04-27 02:10:23 +08:00
|
|
|
const char * pFontName,
|
|
|
|
float fontSize,
|
2013-05-01 07:36:14 +08:00
|
|
|
float textTintR = 1.0,
|
|
|
|
float textTintG = 1.0,
|
|
|
|
float textTintB = 1.0,
|
2013-04-27 05:54:12 +08:00
|
|
|
bool shadow = false,
|
|
|
|
float shadowDeltaX = 0.0,
|
|
|
|
float shadowDeltaY = 0.0,
|
|
|
|
float shadowBlur = 0.0,
|
2013-07-10 06:20:40 +08:00
|
|
|
float shadowOpacity = 0.0,
|
2013-04-27 05:54:12 +08:00
|
|
|
bool stroke = false,
|
|
|
|
float strokeColorR = 0.0,
|
|
|
|
float strokeColorG = 0.0,
|
|
|
|
float strokeColorB = 0.0,
|
|
|
|
float strokeSize = 0.0 )
|
|
|
|
{
|
2013-04-27 02:10:23 +08:00
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmapShadowStroke",
|
2013-07-10 06:20:40 +08:00
|
|
|
"(Ljava/lang/String;Ljava/lang/String;IFFFIIIZFFFFZFFFF)V"))
|
2013-04-27 02:10:23 +08:00
|
|
|
{
|
|
|
|
CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__);
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-27 05:54:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// Do a full lookup for the font path using FileUtils in case the given font name is a relative path to a font file asset,
|
2013-04-27 05:54:12 +08:00
|
|
|
// or the path has been mapped to a different location in the app package:
|
2013-07-12 12:03:39 +08:00
|
|
|
std::string fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(pFontName);
|
2013-04-27 05:54:12 +08:00
|
|
|
|
|
|
|
// If the path name returned includes the 'assets' dir then that needs to be removed, because the android.content.Context
|
|
|
|
// requires this portion of the path to be omitted for assets inside the app package.
|
|
|
|
if (fullPathOrFontName.find("assets/") == 0)
|
|
|
|
{
|
|
|
|
fullPathOrFontName = fullPathOrFontName.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path.
|
|
|
|
}
|
2013-04-27 02:10:23 +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 approach to decrease the jni call number
|
|
|
|
*/
|
|
|
|
jstring jstrText = methodInfo.env->NewStringUTF(text);
|
2013-04-27 05:54:12 +08:00
|
|
|
jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str());
|
2013-04-27 02:10:23 +08:00
|
|
|
|
|
|
|
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, jstrText,
|
2013-07-10 06:20:40 +08:00
|
|
|
jstrFont, (int)fontSize, textTintR, textTintG, textTintB, eAlignMask, nWidth, nHeight, shadow, shadowDeltaX, -shadowDeltaY, shadowBlur, shadowOpacity, stroke, strokeColorR, strokeColorG, strokeColorB, strokeSize);
|
2013-04-27 02:10:23 +08:00
|
|
|
|
|
|
|
methodInfo.env->DeleteLocalRef(jstrText);
|
|
|
|
methodInfo.env->DeleteLocalRef(jstrFont);
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
|
|
|
|
return true;
|
2013-04-27 05:54:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-25 23:09:18 +08:00
|
|
|
bool getBitmapFromJava(const char *text, int nWidth, int nHeight, Image::TextAlign eAlignMask, const char * pFontName, float fontSize)
|
2013-04-27 05:54:12 +08:00
|
|
|
{
|
|
|
|
return getBitmapFromJavaShadowStroke( text, nWidth, nHeight, eAlignMask, pFontName, fontSize );
|
|
|
|
}
|
2013-04-27 02:10:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// ARGB -> RGBA
|
2013-04-07 17:17:19 +08:00
|
|
|
inline unsigned int swapAlpha(unsigned int value)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return ((value << 8 & 0xffffff00) | (value >> 24 & 0x000000ff));
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2013-06-15 14:03:30 +08:00
|
|
|
int _width;
|
|
|
|
int _height;
|
|
|
|
unsigned char *_data;
|
2012-04-19 14:35:52 +08:00
|
|
|
JNIEnv *env;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BitmapDC& sharedBitmapDC()
|
|
|
|
{
|
|
|
|
static BitmapDC s_BmpDC;
|
|
|
|
return s_BmpDC;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Image::initWithString(
|
2012-04-19 14:35:52 +08:00
|
|
|
const char * pText,
|
|
|
|
int nWidth/* = 0*/,
|
|
|
|
int nHeight/* = 0*/,
|
2013-07-25 23:09:18 +08:00
|
|
|
TextAlign eAlignMask/* = kAlignCenter*/,
|
2012-04-19 14:35:52 +08:00
|
|
|
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));
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// assign the dc._data to _data in order to save time
|
|
|
|
_data = dc._data;
|
|
|
|
CC_BREAK_IF(! _data);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_width = (short)dc._width;
|
|
|
|
_height = (short)dc._height;
|
|
|
|
_hasAlpha = true;
|
|
|
|
_preMulti = true;
|
|
|
|
_bitsPerComponent = 8;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Image::initWithStringShadowStroke(
|
2013-04-26 09:22:26 +08:00
|
|
|
const char * pText,
|
|
|
|
int nWidth ,
|
|
|
|
int nHeight ,
|
2013-07-25 23:09:18 +08:00
|
|
|
TextAlign eAlignMask ,
|
2013-04-26 09:22:26 +08:00
|
|
|
const char * pFontName ,
|
2013-05-01 07:36:14 +08:00
|
|
|
int nSize ,
|
|
|
|
float textTintR,
|
|
|
|
float textTintG,
|
|
|
|
float textTintB,
|
2013-04-26 09:22:26 +08:00
|
|
|
bool shadow,
|
|
|
|
float shadowOffsetX,
|
|
|
|
float shadowOffsetY,
|
|
|
|
float shadowOpacity,
|
|
|
|
float shadowBlur,
|
|
|
|
bool stroke,
|
|
|
|
float strokeR,
|
|
|
|
float strokeG,
|
|
|
|
float strokeB,
|
|
|
|
float strokeSize)
|
|
|
|
{
|
2013-04-27 02:10:23 +08:00
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(! pText);
|
|
|
|
|
|
|
|
BitmapDC &dc = sharedBitmapDC();
|
|
|
|
|
2013-05-01 07:36:14 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(! dc.getBitmapFromJavaShadowStroke(pText, nWidth, nHeight, eAlignMask, pFontName,
|
|
|
|
nSize, textTintR, textTintG, textTintB, shadow,
|
|
|
|
shadowOffsetX, shadowOffsetY, shadowBlur, shadowOpacity,
|
|
|
|
stroke, strokeR, strokeG, strokeB, strokeSize ));
|
|
|
|
|
2013-04-27 02:10:23 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// assign the dc._data to _data in order to save time
|
|
|
|
_data = dc._data;
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(! _data);
|
2013-04-27 02:10:23 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_width = (short)dc._width;
|
|
|
|
_height = (short)dc._height;
|
|
|
|
_hasAlpha = true;
|
|
|
|
_preMulti = true;
|
|
|
|
_bitsPerComponent = 8;
|
2013-04-27 02:10:23 +08:00
|
|
|
|
2013-05-01 07:36:14 +08:00
|
|
|
// swap the alpha channel (ARGB to RGBA)
|
2013-06-15 14:03:30 +08:00
|
|
|
swapAlphaChannel((unsigned int *)_data, (_width * _height) );
|
2013-05-01 07:36:14 +08:00
|
|
|
|
|
|
|
// ok
|
2013-04-27 02:10:23 +08:00
|
|
|
bRet = true;
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2013-04-27 02:10:23 +08:00
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
2013-04-26 09:22:26 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2013-05-01 07:36:14 +08:00
|
|
|
// swap the alpha channel in an 32 bit image (from ARGB to RGBA)
|
|
|
|
void swapAlphaChannel(unsigned int *pImageMemory, unsigned int numPixels)
|
|
|
|
{
|
|
|
|
for(int c = 0; c < numPixels; ++c, ++pImageMemory)
|
|
|
|
{
|
|
|
|
// copy the current pixel
|
|
|
|
unsigned int currenPixel = (*pImageMemory);
|
|
|
|
// swap channels and store back
|
|
|
|
char *pSource = (char *) ¤Pixel;
|
|
|
|
*pImageMemory = (pSource[0] << 24) | (pSource[3]<<16) | (pSource[2]<<8) | pSource[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// this method is called by Cocos2dxBitmap
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* this method is called by java code to init width, height and pixels data
|
|
|
|
*/
|
2013-04-03 04:15:12 +08:00
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxBitmap_nativeInitBitmapDC(JNIEnv* env, jobject thiz, int width, int height, jbyteArray pixels)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int size = width * height * 4;
|
2013-04-07 17:17:19 +08:00
|
|
|
cocos2d::BitmapDC& bitmapDC = cocos2d::sharedBitmapDC();
|
2013-06-15 14:03:30 +08:00
|
|
|
bitmapDC._width = width;
|
|
|
|
bitmapDC._height = height;
|
|
|
|
bitmapDC._data = new unsigned char[size];
|
|
|
|
env->GetByteArrayRegion(pixels, 0, size, (jbyte*)bitmapDC._data);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// swap data
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int *tempPtr = (unsigned int*)bitmapDC._data;
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int tempdata = 0;
|
|
|
|
for (int i = 0; i < height; ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < width; ++j)
|
|
|
|
{
|
|
|
|
tempdata = *tempPtr;
|
2013-04-07 17:17:19 +08:00
|
|
|
*tempPtr++ = bitmapDC.swapAlpha(tempdata);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|