cocos2dx 3.5でのメモ
cocos2dx公式のplugin-x(公式だよね。。)をluaだとどう使うのか試したメモ
を参考にflurry,twitterを入れてみた
$ cd frameworks/cocos2d-x/plugin/tools/
$ ./publish.sh flurry
$ ./publish.sh twitter
ndk,sdk,antのパスを聞かれるので随時
flurry twitterとか書くといけるらしいけどcppプロジェクトのときからうまくいったことがない
framework/runtime-src/proj.android/jni/Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cocos2dlua_shared
LOCAL_MODULE_FILENAME := libcocos2dlua
LOCAL_SRC_FILES := \
../../Classes/AppDelegate.cpp \
../../Classes/ide-support/SimpleConfigParser.cpp \
../../../cocos2d-x/plugin/luabindings/auto/lua_cocos2dx_pluginx_auto.cpp \
../../../cocos2d-x/plugin/luabindings/manual/lua_pluginx_basic_conversions.cpp \
../../../cocos2d-x/plugin/luabindings/manual/lua_pluginx_manual_callback.cpp \
../../../cocos2d-x/plugin/luabindings/manual/lua_pluginx_manual_protocols.cpp \
hellolua/main.cpp
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/../../Classes/protobuf-lite \
$(LOCAL_PATH)/../../Classes/runtime \
$(LOCAL_PATH)/../../Classes \
$(LOCAL_PATH)/../../../cocos2d-x/external \
$(LOCAL_PATH)/../../../cocos2d-x/tools/simulator/libsimulator/lib \
$(LOCAL_PATH)/../../../cocos2d-x/plugin/luabindings/auto \
$(LOCAL_PATH)/../../../cocos2d-x/plugin/luabindings/manual
# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END
LOCAL_STATIC_LIBRARIES := cocos2d_lua_static
LOCAL_STATIC_LIBRARIES += cocos2d_simulator_static
LOCAL_STATIC_LIBRARIES += PluginProtocolStatic
# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END
include $(BUILD_SHARED_LIBRARY)
$(call import-module,scripting/lua-bindings/proj.android)
$(call import-module,tools/simulator/libsimulator/proj.android)
$(call import-module,plugin/protocols/proj.android/jni)
# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END
LOCAL_SRC_FILESとLOCAL_C_INCLUDESに追加
frameworks/runtime-src/proj.android/jni/hellolua/main.cpp
#include "AppDelegate.h"
#include "cocos2d.h"
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#include <android/log.h>
#include "ide-support/SimpleConfigParser.h"
#include "PluginJniHelper.h"
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
using namespace cocos2d;
void cocos_android_app_init (JNIEnv* env, jobject thiz) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
JavaVM* vm;
env->GetJavaVM(&vm);
PluginJniHelper::setJavaVM(vm);
}
extern "C"
{
bool Java_org_cocos2dx_lua_AppActivity_nativeIsLandScape(JNIEnv *env, jobject thisz)
{
return SimpleConfigParser::getInstance()->isLanscape();
}
bool Java_org_cocos2dx_lua_AppActivity_nativeIsDebug(JNIEnv *env, jobject thisz)
{
return false;
}
}
pluginjnihelperを追加した
frameworks/runtime-src/proj.android/src/org/cocos2dx/lua/AppActivity.java
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.plugin.PluginWrapper;
@Override
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = super.onCreateView();
PluginWrapper.init(this);
PluginWrapper.setGLSurfaceView(glSurfaceView);
return glSurfaceView;
}
importとonCreateViewを追加した
frameworks/runtime-src/proj.android/project.properties
android.library.reference.2=../../cocos2d-x/plugin/protocols/proj.android
android.library.reference.3=../../cocos2d-x/plugin/plugins/flurry/proj.android
android.library.reference.4=../../cocos2d-x/plugin/plugins/twitter/proj.android
libraryのパス追加
mv frameworks/cocos2d-x/plugin/plugins/flurry/proj.android/sdk frameworks/cocos2d-x/plugin/plugins/flurry/proj.android/libs
mv frameworks/cocos2d-x/plugin/plugins/twitter/proj.android/sdk frameworks/cocos2d-x/plugin/plugins/twitter/proj.android/libs
昔はsdkのパスでもよかったらしいけど今のandroid-sdkだとlibs以下におかないとだめみたいなので。
本当はもっといい方法があるのかもしれない。
frameworks/runtime-src/Classes/AppDelegate.cpp
#include "lua_cocos2dx_pluginx_auto.hpp"
#include "lua_pluginx_manual_callback.h"
#include "lua_pluginx_manual_protocols.h"
bool AppDelegate::applicationDidFinishLaunching()
{
// set default FPS
Director::getInstance()->setAnimationInterval(1.0 / 60.0f);
// register lua module
auto engine = LuaEngine::getInstance();
ScriptEngineManager::getInstance()->setScriptEngine(engine);
auto stack = engine->getLuaStack();
auto L = stack->getLuaState();
lua_module_register(L);
register_all_pluginx_protocols(L);
register_all_pluginx_manual_callback(L);
register_all_pluginx_manual_protocols(L);
register_all_packages();
stack->setXXTEAKeyAndSign("2dxLua", strlen("2dxLua"), "XXTEA", strlen("XXTEA"));
if (engine->executeScriptFile("src/main.lua"))
{
return false;
}
return true;
}
これでやっとluaから命令をなげれるように。
src/main.lua
local analytics = plugin.PluginManager:getInstance():loadPlugin("AnalyticsFlurry")
if DEBUG > 1 then
analytics:setDebugMode(true)
end
analytics:startSession("todo")
analytics:setCaptureUncaughtException(true)
analytics:setSessionContinueMillis(10000)
こんな感じで使えるようになった。
twitterはこんな感じ
src/app/views/ShopScene.lua
local ShopScene = class("ShopScene", cc.load("mvc").ViewBase)
ShopScene.RESOURCE_FILENAME = "ShopScene.csb"
function ShopScene:onCreate()
local twitter = plugin.PluginManager:getInstance():loadPlugin("ShareTwitter")
if DEBUG > 1 then
twitter:setDebugMode(true);
end
twitter:configDeveloperInfo({ TwitterKey = "todo", TwitterSecret = "todo"})
local s = cc.Director:getInstance():getWinSize()
local target = cc.RenderTexture:create(s.width, s.height, cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888)
target:retain()
target:setPosition(cc.p(s.width / 2, s.height / 2))
root:getChildByName("ScrollView"):getChildByName("Share"):addTouchEventListener(function(sender, eventType)
printInfo("share touch")
if eventType == 2 then
target:clear(math.random(), math.random(), math.random(), math.random())
target:begin()
cc.Director:getInstance():getRunningScene():visit()
target:endToLua()
target:saveToFile("t.png", cc.IMAGE_FORMAT_PNG)
twitter:share({SharedText = "share test", SharedImagePath = "t.png"}, function(ret, msg)
printInfo("share:%d:%s", ret, msg)
end)
end
end)
end
end)
end
return ShopScene