axmol/core/platform/android/Device-android.cpp

233 lines
8.5 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
2019-11-23 20:27:39 +08:00
https://axmol.dev/
2019-11-23 20:27:39 +08:00
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 "platform/Device.h"
2019-11-23 20:27:39 +08:00
#include <string.h>
#include <android/log.h>
#include <jni.h>
#include "base/Types.h"
2019-11-23 20:27:39 +08:00
#include "platform/android/jni/JniHelper.h"
#include "platform/FileUtils.h"
2023-06-24 21:18:27 +08:00
#include "yasio/string_view.hpp"
2019-11-23 20:27:39 +08:00
static const char* deviceHelperClassName = "org.axmol.lib.AxmolEngine";
2019-11-23 20:27:39 +08:00
namespace ax
{
2019-11-23 20:27:39 +08:00
int Device::getDPI()
{
static int dpi = -1;
if (dpi == -1)
{
dpi = JniHelper::callStaticIntMethod(deviceHelperClassName, "getDPI");
2019-11-23 20:27:39 +08:00
}
return dpi;
}
float Device::getPixelRatio()
2023-11-06 00:19:44 +08:00
{
// refer to: https://developer.android.com/training/multiscreen/screendensities?hl=en-us
return Device::getDPI() / 160.0f;
}
2019-11-23 20:27:39 +08:00
void Device::setAccelerometerEnabled(bool isEnabled)
{
if (isEnabled)
{
JniHelper::callStaticVoidMethod(deviceHelperClassName, "enableAccelerometer");
2019-11-23 20:27:39 +08:00
}
else
{
JniHelper::callStaticVoidMethod(deviceHelperClassName, "disableAccelerometer");
2019-11-23 20:27:39 +08:00
}
}
void Device::setAccelerometerInterval(float interval)
{
JniHelper::callStaticVoidMethod(deviceHelperClassName, "setAccelerometerInterval", interval);
2019-11-23 20:27:39 +08:00
}
class BitmapDC
{
public:
2021-12-25 10:04:45 +08:00
BitmapDC() : _width(0), _height(0), _data(nullptr) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
~BitmapDC() {}
2019-11-23 20:27:39 +08:00
bool getBitmapFromJavaShadowStroke(std::string_view text,
2021-12-25 10:04:45 +08:00
int nWidth,
int nHeight,
Device::TextAlign eAlignMask,
const FontDefinition& textDefinition)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
JniMethodInfo methodInfo;
2022-10-01 16:24:52 +08:00
if (!JniHelper::getStaticMethodInfo(methodInfo, "org.axmol.lib.BitmapHelper",
2021-12-25 10:04:45 +08:00
"createTextBitmapShadowStroke",
"([BLjava/lang/String;IIIIIIIIZFFFFZIIIIFZI)Z"))
{
AXLOGE("{} {}: error to get methodInfo", __FILE__, __LINE__);
2021-12-25 10:04:45 +08:00
return false;
}
// 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, or the path has been mapped to a different location in the app package:
std::string fullPathOrFontName = textDefinition._fontName;
if (FileUtils::getInstance()->isFileExist(fullPathOrFontName))
{
fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(textDefinition._fontName);
// 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.
using namespace cxx17;
if (cxx20::starts_with(cxx17::string_view{fullPathOrFontName}, "assets/"_sv))
{
fullPathOrFontName =
fullPathOrFontName.substr(sizeof("assets/") - 1); // Chop out the 'assets/' portion of the path.
}
}
/**create bitmap
* this method call Cococs2dx.createBitmap()(java code) to create the bitmap, the java code
2022-10-01 16:24:52 +08:00
* will call Java_org_axmol_lib_BitmapHelper_nativeInitBitmapDC() to init the width, height
2021-12-25 10:04:45 +08:00
* and data.
* use this approach to decrease the jni call number
*/
int count = static_cast<int>(text.length());
2021-12-25 10:04:45 +08:00
jbyteArray strArray = methodInfo.env->NewByteArray(count);
methodInfo.env->SetByteArrayRegion(strArray, 0, count, reinterpret_cast<const jbyte*>(text.data()));
2021-12-25 10:04:45 +08:00
jstring jstrFont = methodInfo.env->NewStringUTF(fullPathOrFontName.c_str());
if (!methodInfo.env->CallStaticBooleanMethod(
methodInfo.classID, methodInfo.methodID, strArray, jstrFont, textDefinition._fontSize,
textDefinition._fontFillColor.r, textDefinition._fontFillColor.g, textDefinition._fontFillColor.b,
textDefinition._fontAlpha, eAlignMask, nWidth, nHeight, textDefinition._shadow._shadowEnabled,
textDefinition._shadow._shadowOffset.width, -textDefinition._shadow._shadowOffset.height,
textDefinition._shadow._shadowBlur, textDefinition._shadow._shadowOpacity,
textDefinition._stroke._strokeEnabled, textDefinition._stroke._strokeColor.r,
textDefinition._stroke._strokeColor.g, textDefinition._stroke._strokeColor.b,
textDefinition._stroke._strokeAlpha, textDefinition._stroke._strokeSize, textDefinition._enableWrap,
textDefinition._overflow))
{
return false;
}
methodInfo.env->DeleteLocalRef(strArray);
methodInfo.env->DeleteLocalRef(jstrFont);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
return true;
2019-11-23 20:27:39 +08:00
}
public:
int _width;
int _height;
2021-12-25 10:04:45 +08:00
unsigned char* _data;
2019-11-23 20:27:39 +08:00
};
static BitmapDC& sharedBitmapDC()
{
static BitmapDC s_BmpDC;
return s_BmpDC;
}
Data Device::getTextureDataForText(std::string_view text,
2021-12-25 10:04:45 +08:00
const FontDefinition& textDefinition,
TextAlign align,
int& width,
int& height,
bool& hasPremultipliedAlpha)
2019-11-23 20:27:39 +08:00
{
Data ret;
2021-12-25 10:04:45 +08:00
do
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
BitmapDC& dc = sharedBitmapDC();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (!dc.getBitmapFromJavaShadowStroke(text, (int)textDefinition._dimensions.width,
(int)textDefinition._dimensions.height, align, textDefinition))
{
break;
};
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
width = dc._width;
2019-11-23 20:27:39 +08:00
height = dc._height;
2021-12-25 10:04:45 +08:00
ret.fastSet(dc._data, width * height * 4);
2019-11-23 20:27:39 +08:00
hasPremultipliedAlpha = true;
} while (0);
return ret;
}
void Device::setKeepScreenOn(bool value)
{
JniHelper::callStaticVoidMethod(deviceHelperClassName, "setKeepScreenOn", value);
2019-11-23 20:27:39 +08:00
}
void Device::vibrate(float duration)
{
JniHelper::callStaticVoidMethod(deviceHelperClassName, "vibrate", duration);
2019-11-23 20:27:39 +08:00
}
void Device::prepareImpactFeedbackGenerator(ImpactFeedbackStyle style) {}
void Device::impactOccurred(ImpactFeedbackStyle style)
{
JniHelper::callStaticVoidMethod(deviceHelperClassName, "impactOccurred", (int)style);
}
void Device::prepareNotificationFeedbackGenerator() {}
void Device::notificationOccurred(NotificationFeedbackType type)
{
JniHelper::callStaticVoidMethod(deviceHelperClassName, "notificationOccurred", (int)type);
}
void Device::prepareSelectionFeedbackGenerator() {}
void Device::selectionChanged()
{
JniHelper::callStaticVoidMethod(deviceHelperClassName, "selectionChanged");
}
}
2019-11-23 20:27:39 +08:00
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
// this method is called by BitmapHelper
2021-12-25 10:04:45 +08:00
extern "C" {
/**
* this method is called by java code to init width, height and pixels data
*/
JNIEXPORT void JNICALL
2022-10-01 16:24:52 +08:00
Java_org_axmol_lib_BitmapHelper_nativeInitBitmapDC(JNIEnv* env, jclass, jint width, jint height, jbyteArray pixels)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
int size = width * height * 4;
2022-08-08 18:02:17 +08:00
ax::BitmapDC& bitmapDC = ax::sharedBitmapDC();
2021-12-25 10:04:45 +08:00
bitmapDC._width = width;
bitmapDC._height = height;
bitmapDC._data = (unsigned char*)malloc(sizeof(unsigned char) * size);
env->GetByteArrayRegion(pixels, 0, size, (jbyte*)bitmapDC._data);
}
2019-11-23 20:27:39 +08:00
};