[迁移]cocos2d-x 中java调用c/c++函数

声明:因网易博客将关闭,移到此

这个是在JniHelper的基础上添加的,可以对比者看,java中的函数是backAction,

主要调用的函数
JNIEXPORT void JNICALL Java_org_cocos2dx_Game_JavaHelper_backAction(JNIEnv *env, jobject thiz, jstring key, jstring message)

这个是需要注意的,其他地方就。。。。。
还是上代码,还是代码加调试舒服点。

#ifndef __HELPER_H__
#define __HELPER_H__

#include <iostream>
#include "CCLuaStack.h"


USING_NS_CC;

namespace MGame
{
class PlatformHelper
{
public:
static PlatformHelper *shared();

// call from cocos2d-x
void doAction(const std::string& key, const std::string& message);
// register a lua function when doPlatformAction is called
void registerPlatformHandler(int handler);

// call from java or object-c
void backAction(const std::string& key, const std::string& message);

protected:
PlatformHelper();

CCLuaStack *mLuaStack;

int mPlatformCallBackHandler;// call from platform for java or object-c
};
}


#endif // __HELPER_H__

#include "PlatformHelper.h"
#include "cocos2d.h"
#include "CCLuaEngine.h"


//===================== android ======================
#define ANDROID_CLASS_NAME "org/cocos2dx/Game/JavaHelper"
#define ANDROID_FUNCTION_NAME "doAction"
#define ANDROID_PARAM_TYPE "(Ljava/lang/String;Ljava/lang/String;)V"


#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) 
#include <jni.h> 
#include "platform/android/jni/JniHelper.h" 
#include <android/log.h> 

extern "C"
{
JNIEXPORT void JNICALL Java_org_cocos2dx_Game_JavaHelper_backAction(JNIEnv *env, jobject thiz, jstring key, jstring message)
{
const char *tmpKey = env->GetStringUTFChars(key, 0);
std::string strKey(tmpKey);
env->ReleaseStringUTFChars(key, tmpKey);

const char *tmpMessage = env->GetStringUTFChars(message, 0);
std::string strMessage(tmpMessage);
env->ReleaseStringUTFChars(message, tmpMessage);

MGame::PlatformHelper::shared()->backAction(strKey, strMessage);
}
}
#endif

namespace MGame
{
USING_NS_CC;

static PlatformHelper * s_PlatformHelperShared = NULL;

//-------------------------------------------------
//
PlatformHelper *PlatformHelper::shared()
{
if (s_PlatformHelperShared == NULL)
s_PlatformHelperShared = new PlatformHelper;
return s_PlatformHelperShared;
}

//-------------------------------------------------
//
PlatformHelper::PlatformHelper()
: mPlatformCallBackHandler(0)
{
CCLuaEngine *pEngine = CCLuaEngine::defaultEngine();
mLuaStack = pEngine->getLuaStack();
}

//-------------------------------------------------
//
void PlatformHelper::doAction(const std::string& key, const std::string& message)
{
// android
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
USING_NS_CC;
JniMethodInfo methodInfo;
bool isHave = JniHelper::getStaticMethodInfo(methodInfo, ANDROID_CLASS_NAME,
ANDROID_FUNCTION_NAME,
ANDROID_PARAM_TYPE);
if (isHave)
{
jstring key_arg = methodInfo.env->NewStringUTF(key.c_str());
jstring msg_arg = methodInfo.env->NewStringUTF(message.c_str());

methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, key_arg, msg_arg);
}
else
{

}
#else

#endif 
}

//--------------------------------------------
//
void PlatformHelper::registerPlatformHandler(int handler)
{
mPlatformCallBackHandler = handler;
}

//--------------------------------------------
//
void PlatformHelper::backAction(const std::string& key, const std::string& message)
{
if (mPlatformCallBackHandler > 0)
{
mLuaStack->pushString(key.c_str());
mLuaStack->pushString(message.c_str());
mLuaStack->executeFunctionByHandler(mPlatformCallBackHandler, 2);
mLuaStack->clean();
}
}
}


java部分
package org.cocos2dx.Game;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class JavaHelper {

private static Context s_context;
/**
 * 初始化内容信息
 * @param context
 */
public static void initContext(Context context){
s_context = context;
}
/**
 * 这里是java调用cocos2d-x的地方
 * @param key
 * @param message
 */
public static native void backAction(final String key, final String message);
public static void doAction(String key, String message){
Message msg = new Message();
msg.what = Integer.parseInt(key);
msg.obj = message;
handler.sendMessage(msg);
}
static Handler handler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
}
String message = (String)msg.obj;
Toast.makeText(s_context, message, Toast.LENGTH_SHORT).show();
backAction(msg.what + "", message);
}
};
}

// --我这里使用了lua回调形式,所有添加了void registerPlatformHandler(int handler);

这个的话可以参考下tolua++我这里使用cocos2d-x -2.1.4的版本,虽然比较新,bug也可能有不少,
但是嘛,不少功能需要,自己写费事费力,也不一定到达效果,出了问题在处理了,只能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值