Skip to content

Cocos2d x 2.2.6 oh #20894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: cocos2d-x-2.2.6-oh
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion CocosDenshion/ohos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ include_directories(${COCOSDENSHION_HEADER}

#set(CMAKE_C_FLAGS -Wno-psabi -Wno-absolute-value -Wno-extra)
#set(CMAKE_CXX_FLAGS -Wno-overloaded-virtual -Wno-unused-function -Wno-unused-private-field -Wno-reorder-ctor -Wno-unsequenced -Wno-extra)
target_compile_definitions(${PROJECT_NAME} PUBLIC -DHarmonyOS_Next=1)
target_compile_definitions(${PROJECT_NAME} PUBLIC -DHARMONYOS_NEXT=1)
target_compile_options(${PROJECT_NAME} PUBLIC -Wno-psabi -Wno-absolute-value -Wno-extra -Wno-overloaded-virtual -Wno-unused-function -Wno-unused-private-field -Wno-reorder-ctor -Wno-unsequenced -Wno-extra -Wno-unused-command-line-argument -Wno-ignored-qualifiers)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../cocos2dx
Expand Down
4 changes: 2 additions & 2 deletions cocos2dx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ if(OHOS)
-Wno-unsequenced
-Wno-extra
-Wno-c++11-narrowing
-DHarmonyOS_Next
-DHARMONYOS_NEXT
)

add_library(${PROJECT_NAME} STATIC ${COCOS2DX_SRC})
Expand All @@ -200,7 +200,7 @@ if(OHOS)

target_link_libraries(${PROJECT_NAME} PUBLIC ext_png ext_zlib libtiff ext_jpeg)
target_include_directories(${PROJECT_NAME} PUBLIC ${COCOS2DX_HEADER_PUBLIC})
target_compile_definitions(${PROJECT_NAME} PUBLIC -DUSE_FILE32API -DHarmonyOS_Next)
target_compile_definitions(${PROJECT_NAME} PUBLIC -DUSE_FILE32API -DHARMONYOS_NEXT)
target_compile_options(${PROJECT_NAME} PUBLIC -Wno-psabi)

find_library( # Sets the name of the path variable.
Expand Down
21 changes: 20 additions & 1 deletion cocos2dx/platform/ohos/napi/helper/NapiHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ class JSFunction {
napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...};
napi_value return_val;
status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val);

if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
ReturnType value;
if (!NapiValueConverter::ToCppValue(env, return_val, value)) {
// Handle error here
Expand All @@ -136,6 +140,11 @@ class JSFunction {
napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...};
napi_value return_val;
status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val);
if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
}

static void callFunctionWithParams(WorkParam *param) {
Expand Down Expand Up @@ -167,6 +176,11 @@ class JSFunction {
}
if (status != napi_ok) {
LOGI("XXXXXX:napi_call_function getClassObject != napi_ok %{public}d", status);
if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
}

napi_value thenFunc = nullptr;
Expand All @@ -187,6 +201,11 @@ class JSFunction {
status = napi_call_function(env, promise, thenFunc, 1, &successFunc, &ret);
if (status != napi_ok) {
LOGI("XXXXXX:napi_call_function thenFunc failed, ret: %{public}d", status);
if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
}
}
// Callback Function Type
Expand Down
7 changes: 6 additions & 1 deletion cocos2dx/platform/ohos/napi/plugin_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info)

int64_t value;
NAPI_CALL(env, napi_get_value_int64(env, args[0], &value));

napi_handle_scope scope = nullptr;
NAPI_CALL(env, napi_open_handle_scope(env, &scope));
if(scope == nullptr){
return nullptr;
}
NAPI_CALL(env, napi_create_object(env, &exports));

switch (value) {
Expand Down Expand Up @@ -181,6 +185,7 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info)
default:
OHOS_LOGE("unknown type");
}
NAPI_CALL(env, napi_close_handle_scope(env, scope));
return exports;
}

Expand Down
2 changes: 1 addition & 1 deletion extensions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ if(OHOS)
add_library(${PROJECT_NAME} STATIC ${EXTENSION_SRC})
target_link_libraries(${PROJECT_NAME} box2d chipmunk cocosdenshion)
set(CMAKE_C_FLAGS -fexceptions)
target_compile_options(${PROJECT_NAME} PUBLIC -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -DHarmonyOS_Next=1)
target_compile_options(${PROJECT_NAME} PUBLIC -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -DHARMONYOS_NEXT=1)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/
${CMAKE_CURRENT_SOURCE_DIR}/CCBReader
${CMAKE_CURRENT_SOURCE_DIR}/GUI/CCControlExtension
Expand Down
2 changes: 1 addition & 1 deletion samples/Cpp/TestCpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ if(OHOS)

)
list(APPEND TEST_COMMON_SRC proj.ohos/entry/src/main/cpp/main.cpp)
add_definitions("-DHarmonyOS_Next")
add_definitions("-DHARMONYOS_NEXT")
add_definitions("-DCOCOS2D_DEBUG=3")

include_directories(${COCOS2DX_HEADER}
Expand Down
2 changes: 1 addition & 1 deletion samples/Cpp/TestCpp/proj.ohos/.clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Language: Cpp
# BasedOnStyle: LLVM
ColumnLimit: 120
SortIncludes: false
SortIncludes: CaseSensitive
TabWidth: 4
IndentWidth: 4
UseTab: Never
Expand Down
4 changes: 1 addition & 3 deletions samples/Cpp/TestCpp/proj.ohos/build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
{
"name": "default",
"signingConfig": "default",
"compatibleSdkVersion": "5.0.0(12)",
//指定HarmonyOS应用/服务目标版本。若没有设置,默认为compatibleSdkVersion
"compatibleSdkVersion": "5.0.3(15)",
"runtimeOS": "HarmonyOS"
//指定为HarmonyOS/OpenHarmony
}
],
"signingConfigs": [
Expand Down
2 changes: 1 addition & 1 deletion samples/Cpp/TestCpp/proj.ohos/entry/build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"sourceOption": {
"workers": [
'./src/main/ets/workers/CocosWorker.ts'
'./src/main/ets/workers/CocosWorker.ets'
]
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import window from '@ohos.window';
import UIAbility from '@ohos.app.ability.UIAbility';
import nativeRender from "libnativerender.so";
import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities"
import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities"
import { BusinessError } from '@kit.BasicServicesKit';
import { WorkerManager } from '../workers/WorkerManager';
import Want from '@ohos.app.ability.Want';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';

const nativeAppLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE);
const rawFileUtils: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.RAW_FILE_UTILS);
let cocosWorker = WorkerManager.getInstance().getWorker();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
export default class MainAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
nativeAppLifecycle.onCreate();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, this.context);
console.info('[LIFECYCLE-App] onCreate')
}

onDestroy() {
nativeAppLifecycle.onDestroy();
console.info('[LIFECYCLE-App] onDestroy')
}

onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
windowStage.loadContent('pages/Index', (err:BusinessError, data) => {
if (err.code) {
return;
}
rawFileUtils.nativeResourceManagerInit(this.context.resourceManager);
rawFileUtils.writablePathInit(this.context.filesDir);
});

windowStage.getMainWindow().then((windowIns: window.Window) => {
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW, windowIns);
// Set whether to display the status bar and navigation bar. If they are not displayed, [] is displayed.
let systemBarPromise = windowIns.setWindowSystemBarEnable([]);
// Whether the window layout is displayed in full screen mode
let fullScreenPromise = windowIns.setWindowLayoutFullScreen(true);
// Sets whether the screen is always on.
let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true);
Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => {
console.info('Succeeded in setting the window');
}).catch((err: BusinessError) => {
console.error('Failed to set the window, cause ', err.code, err.message);
});

try {
DeviceUtils.calculateSafeArea(cocosWorker, windowIns.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT), windowIns.getWindowProperties().windowRect);
windowIns.on('avoidAreaChange', (data) => {
console.info('getSafeAreaRect Succeeded in enabling the listener for system avoid area changes. type:' +
JSON.stringify(data.type) + ', area: ' + JSON.stringify(data.area));

if(data.type == window.AvoidAreaType.TYPE_SYSTEM_GESTURE || data.type == window.AvoidAreaType.TYPE_KEYBOARD) {
return;
}

let mainWindow: window.Window = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW);
DeviceUtils.calculateSafeArea(cocosWorker, data.area, mainWindow.getWindowProperties().windowRect);
});
} catch (exception) {
console.error(`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}`);
}
})

windowStage.on("windowStageEvent", (data:window.WindowStageEventType) => {
let stageEventType: window.WindowStageEventType = data;
switch (stageEventType) {
case window.WindowStageEventType.RESUMED:
console.info('[LIFECYCLE-App] onShow_RESUMED')
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){
nativeAppLifecycle.onShow();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
}
break;
case window.WindowStageEventType.PAUSED:
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){
console.info('[LIFECYCLE-App] onHide_PAUSED')
nativeAppLifecycle.onHide();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
}
break;
default:
break;
}
});
}

onWindowStageDestroy() {
// Main window is destroyed, release UI related resources
}

onForeground() {
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){
// Ability has brought to foreground
console.info('[LIFECYCLE-App] onShow')
nativeAppLifecycle.onShow();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
}
}

onBackground() {
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){
// Ability has back to background
console.info('[LIFECYCLE-App] onHide')
nativeAppLifecycle.onHide();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
}
}
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export struct CocosVideoPlayer {
Video({
src: this.videoPlayerInfo.isUrl ? this.videoPlayerInfo.url : this.videoPlayerInfo.rawfile,
controller: this.videoPlayerInfo.controller
}).position({ x: this.videoPlayerInfo.x, y: this.videoPlayerInfo.y })
})
.width(this.videoPlayerInfo.w)
.height(this.videoPlayerInfo.h)
.visibility(this.videoPlayerInfo.visible ? Visibility.Visible : Visibility.None)
Expand Down
14 changes: 7 additions & 7 deletions samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/pages/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import promptAction from '@ohos.promptAction';
import process from '@ohos.process';
const nativePageLifecycle:nativeRender.CPPFunctions = nativeRender.getContext(ContextType.JSPAGE_LIFECYCLE);

let cocosWorker = WorkerManager.getInstance().getWorker();

@Entry
@Component
struct Index {

cocosWorker = WorkerManager.getInstance().getWorker();
xcomponentController: XComponentController = new XComponentController();

processMgr = new process.ProcessManager();
Expand Down Expand Up @@ -53,7 +53,7 @@ struct Index {
console.log('[LIFECYCLE-Page] onPageShow');
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_EDIT_BOX_ARRAY, this.editBoxArray);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_EDIT_BOX_INDEX_MAP, this.editBoxIndexMap);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_COCOS_WORKER, this.cocosWorker);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_COCOS_WORKER, cocosWorker);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_ARRAY, this.webViewArray);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_WEB_VIEW_INDEX_MAP, this.webViewIndexMap);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_VIDEO_PLAYER_ARRAY, this.videoPlayerInfoArray);
Expand Down Expand Up @@ -102,7 +102,7 @@ struct Index {
}

build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Stack() {
XComponent({
id: 'xcomponentId',
type: 'surface',
Expand All @@ -113,9 +113,9 @@ struct Index {
.focusOnTouch(true)
.onLoad((context) => {
console.log('[cocos] XComponent.onLoad Callback');
this.cocosWorker.postMessage({ type: "abilityContextInit", data: GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT)});
this.cocosWorker.postMessage({ type: "onXCLoad", data: "XComponent" });
this.cocosWorker.onmessage = WorkerMsgUtils.recvWorkerThreadMessage;
cocosWorker.postMessage({ type: "abilityContextInit", data: GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT)});
cocosWorker.postMessage({ type: "onXCLoad", data: "XComponent" });
cocosWorker.onmessage = WorkerMsgUtils.recvWorkerThreadMessage;
})
.onDestroy(() => {
})
Expand Down
Loading