CCDevice-android.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "platform/CCPlatformConfig.h"
  22. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  23. #include "platform/CCDevice.h"
  24. #include <string.h>
  25. #include <android/log.h>
  26. #include <jni.h>
  27. #include "base/ccTypes.h"
  28. #include "platform/android/jni/JniHelper.h"
  29. #include "platform/CCFileUtils.h"
  30. static const std::string helperClassName = "org/cocos2dx/lib/Cocos2dxHelper";
  31. NS_CC_BEGIN
  32. int Device::getDPI()
  33. {
  34. static int dpi = -1;
  35. if (dpi == -1)
  36. {
  37. dpi = JniHelper::callStaticIntMethod(helperClassName, "getDPI");
  38. }
  39. return dpi;
  40. }
  41. void Device::setAccelerometerEnabled(bool isEnabled)
  42. {
  43. if (isEnabled)
  44. {
  45. JniHelper::callStaticVoidMethod(helperClassName, "enableAccelerometer");
  46. }
  47. else
  48. {
  49. JniHelper::callStaticVoidMethod(helperClassName, "disableAccelerometer");
  50. }
  51. }
  52. void Device::setAccelerometerInterval(float interval)
  53. {
  54. JniHelper::callStaticVoidMethod(helperClassName, "setAccelerometerInterval", interval);
  55. }
  56. class BitmapDC
  57. {
  58. public:
  59. BitmapDC()
  60. : _width(0)
  61. , _height(0)
  62. , _data(nullptr)
  63. {
  64. }
  65. ~BitmapDC(void)
  66. {
  67. }
  68. bool getBitmapFromJavaShadowStroke( const char *text,
  69. int nWidth,
  70. int nHeight,
  71. Device::TextAlign eAlignMask,
  72. const FontDefinition& textDefinition )
  73. {
  74. JniMethodInfo methodInfo;
  75. if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmapShadowStroke",
  76. "([BLjava/lang/String;IIIIIIIIZFFFFZIIIIFZI)Z"))
  77. {
  78. CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__);
  79. return false;
  80. }
  81. // 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,
  82. // or the path has been mapped to a different location in the app package:
  83. std::string fullPathOrFontName = textDefinition._fontName;
  84. if(FileUtils::getInstance()->isFileExist(fullPathOrFontName)) {
  85. fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(textDefinition._fontName);
  86. // If the path name returned includes the 'assets' dir then that needs to be removed, because the android.content.Context
  87. // requires this portion of the path to be omitted for assets inside the app package.
  88. if (fullPathOrFontName.find("assets/") == 0)
  89. {
  90. fullPathOrFontName = fullPathOrFontName.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path.
  91. }
  92. }
  93. /**create bitmap
  94. * this method call Cococs2dx.createBitmap()(java code) to create the bitmap, the java code
  95. * will call Java_org_cocos2dx_lib_Cocos2dxBitmap_nativeInitBitmapDC() to init the width, height
  96. * and data.
  97. * use this approach to decrease the jni call number
  98. */
  99. int count = strlen(text);
  100. jbyteArray strArray = methodInfo.env->NewByteArray(count);
  101. methodInfo.env->SetByteArrayRegion(strArray, 0, count, reinterpret_cast<const jbyte*>(text));
  102. jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str());
  103. if(!methodInfo.env->CallStaticBooleanMethod(methodInfo.classID, methodInfo.methodID, strArray,
  104. jstrFont, textDefinition._fontSize, textDefinition._fontFillColor.r, textDefinition._fontFillColor.g,
  105. textDefinition._fontFillColor.b, textDefinition._fontAlpha,
  106. eAlignMask, nWidth, nHeight,
  107. textDefinition._shadow._shadowEnabled, textDefinition._shadow._shadowOffset.width, -textDefinition._shadow._shadowOffset.height,
  108. textDefinition._shadow._shadowBlur, textDefinition._shadow._shadowOpacity,
  109. textDefinition._stroke._strokeEnabled, textDefinition._stroke._strokeColor.r, textDefinition._stroke._strokeColor.g,
  110. textDefinition._stroke._strokeColor.b, textDefinition._stroke._strokeAlpha, textDefinition._stroke._strokeSize,
  111. textDefinition._enableWrap, textDefinition._overflow))
  112. {
  113. return false;
  114. }
  115. methodInfo.env->DeleteLocalRef(strArray);
  116. methodInfo.env->DeleteLocalRef(jstrFont);
  117. methodInfo.env->DeleteLocalRef(methodInfo.classID);
  118. return true;
  119. }
  120. public:
  121. int _width;
  122. int _height;
  123. unsigned char *_data;
  124. };
  125. static BitmapDC& sharedBitmapDC()
  126. {
  127. static BitmapDC s_BmpDC;
  128. return s_BmpDC;
  129. }
  130. Data Device::getTextureDataForText(const char * text, const FontDefinition& textDefinition, TextAlign align, int &width, int &height, bool& hasPremultipliedAlpha)
  131. {
  132. Data ret;
  133. do
  134. {
  135. BitmapDC &dc = sharedBitmapDC();
  136. if(! dc.getBitmapFromJavaShadowStroke(text,
  137. (int)textDefinition._dimensions.width,
  138. (int)textDefinition._dimensions.height,
  139. align, textDefinition )) { break;};
  140. width = dc._width;
  141. height = dc._height;
  142. ret.fastSet(dc._data,width * height * 4);
  143. hasPremultipliedAlpha = true;
  144. } while (0);
  145. return ret;
  146. }
  147. void Device::setKeepScreenOn(bool value)
  148. {
  149. JniHelper::callStaticVoidMethod(helperClassName, "setKeepScreenOn", value);
  150. }
  151. void Device::vibrate(float duration)
  152. {
  153. JniHelper::callStaticVoidMethod(helperClassName, "vibrate", duration);
  154. }
  155. NS_CC_END
  156. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  157. // this method is called by Cocos2dxBitmap
  158. extern "C"
  159. {
  160. /**
  161. * this method is called by java code to init width, height and pixels data
  162. */
  163. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxBitmap_nativeInitBitmapDC(JNIEnv* env, jobject thiz, int width, int height, jbyteArray pixels)
  164. {
  165. int size = width * height * 4;
  166. cocos2d::BitmapDC& bitmapDC = cocos2d::sharedBitmapDC();
  167. bitmapDC._width = width;
  168. bitmapDC._height = height;
  169. bitmapDC._data = (unsigned char*)malloc(sizeof(unsigned char) * size);
  170. env->GetByteArrayRegion(pixels, 0, size, (jbyte*)bitmapDC._data);
  171. }
  172. };