From 43229cb440d025bb7a21cd40bf59458f83632a64 Mon Sep 17 00:00:00 2001 From: Rohan Kuruvilla Date: Thu, 24 Jan 2013 18:29:39 -0800 Subject: [PATCH] Adding CocosPlayerJS for Android --- .../CocosPlayer/Classes/AppDelegate.cpp | 152 ++++++++++ .../CocosPlayer/Classes/AppDelegate.h | 53 ++++ ...cocos2dx_cocosplayer_CocosPlayerSocket.cpp | 34 +++ ...g_cocos2dx_cocosplayer_CocosPlayerSocket.h | 34 +++ .../CocosPlayer/Classes/MainSceneHelper.cpp | 87 ++++++ .../CocosPlayer/Classes/MainSceneHelper.h | 29 ++ .../CocosPlayer/Classes/PlayerStatus.cpp | 156 ++++++++++ .../CocosPlayer/Classes/PlayerStatus.h | 56 ++++ .../CocosPlayer/Classes/PlayerStatusLoader.h | 17 ++ .../ccb_btn-play.png.REMOVED.git-id | 1 + .../ccb_btn-run-dis.png.REMOVED.git-id | 1 + .../ccb_btn-run.png.REMOVED.git-id | 1 + .../CocosPlayer/proj.android/.classpath | 8 + .../CocosPlayer/proj.android/.project | 33 +++ .../proj.android/AndroidManifest.xml | 31 ++ .../CocosPlayer/proj.android/ant.properties | 1 + .../CocosPlayer/proj.android/build.xml | 92 ++++++ .../CocosPlayer/proj.android/build_native.sh | 88 ++++++ .../CocosPlayer/proj.android/jni/Android.mk | 32 +++ .../proj.android/jni/Application.mk | 3 + .../proj.android/jni/cocosplayer/main.cpp | 79 +++++ .../CocosPlayer/proj.android/ndkgdb.sh | 47 +++ .../proj.android/proguard-project.txt | 20 ++ .../proj.android/project.properties | 13 + .../proj.android/res/layout/activity_main.xml | 36 +++ .../proj.android/res/values/strings.xml | 4 + .../cocosplayer/CCBFileUtilsHelper.java | 175 +++++++++++ .../cocosplayer/CCBStreamHandler.java | 107 +++++++ .../org/cocos2dx/cocosplayer/CocosPlayer.java | 44 +++ .../cocosplayer/CocosPlayerPresence.java | 206 +++++++++++++ .../cocosplayer/CocosPlayerSocket.java | 271 ++++++++++++++++++ 31 files changed, 1911 insertions(+) create mode 100644 samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp create mode 100644 samples/Javascript/CocosPlayer/Classes/AppDelegate.h create mode 100644 samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.cpp create mode 100644 samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.h create mode 100644 samples/Javascript/CocosPlayer/Classes/MainSceneHelper.cpp create mode 100644 samples/Javascript/CocosPlayer/Classes/MainSceneHelper.h create mode 100644 samples/Javascript/CocosPlayer/Classes/PlayerStatus.cpp create mode 100644 samples/Javascript/CocosPlayer/Classes/PlayerStatus.h create mode 100644 samples/Javascript/CocosPlayer/Classes/PlayerStatusLoader.h create mode 100644 samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-play.png.REMOVED.git-id create mode 100644 samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run-dis.png.REMOVED.git-id create mode 100644 samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run.png.REMOVED.git-id create mode 100644 samples/Javascript/CocosPlayer/proj.android/.classpath create mode 100644 samples/Javascript/CocosPlayer/proj.android/.project create mode 100644 samples/Javascript/CocosPlayer/proj.android/AndroidManifest.xml create mode 100644 samples/Javascript/CocosPlayer/proj.android/ant.properties create mode 100644 samples/Javascript/CocosPlayer/proj.android/build.xml create mode 100755 samples/Javascript/CocosPlayer/proj.android/build_native.sh create mode 100644 samples/Javascript/CocosPlayer/proj.android/jni/Android.mk create mode 100644 samples/Javascript/CocosPlayer/proj.android/jni/Application.mk create mode 100644 samples/Javascript/CocosPlayer/proj.android/jni/cocosplayer/main.cpp create mode 100755 samples/Javascript/CocosPlayer/proj.android/ndkgdb.sh create mode 100644 samples/Javascript/CocosPlayer/proj.android/proguard-project.txt create mode 100644 samples/Javascript/CocosPlayer/proj.android/project.properties create mode 100644 samples/Javascript/CocosPlayer/proj.android/res/layout/activity_main.xml create mode 100644 samples/Javascript/CocosPlayer/proj.android/res/values/strings.xml create mode 100644 samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBFileUtilsHelper.java create mode 100644 samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBStreamHandler.java create mode 100644 samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayer.java create mode 100644 samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayerPresence.java create mode 100644 samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayerSocket.java diff --git a/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp b/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp new file mode 100644 index 0000000000..acbbb5a16a --- /dev/null +++ b/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp @@ -0,0 +1,152 @@ +#include "AppDelegate.h" +#include "PlayerStatus.h" +#include "cocos2d.h" +#include "SimpleAudioEngine.h" +#include "ScriptingCore.h" +#include "generated/cocos2dx.hpp" +#include "cocos2d_specifics.hpp" +#include "js_bindings_chipmunk_registration.h" +#include "js_bindings_ccbreader.h" +#include "MainSceneHelper.h" + +#ifdef JSLOG +#undef JSLOG +#endif +#define JSLOG CocosBuilder_log + +char *_js_log_buf_ccbuilder = NULL; + +USING_NS_CC; +using namespace CocosDenshion; + +AppDelegate::AppDelegate() +{ +} + +AppDelegate::~AppDelegate() +{ + CCScriptEngineManager::sharedManager()->purgeSharedManager(); +} + +void handle_ccb_run() { + ScriptingCore::getInstance()->runScript("main.js"); +} + +void handle_connected() { + CCBHelper::setStatusMessage("Connected!"); +} + +void handle_disconnected() { + CCBHelper::setStatusMessage("Disconnected"); +} + +void handle_ccb_stop() { + CCLOG("STOP UNIMPLEMENTED"); +} + +extern "C" { + +bool runMainScene() { + PlayerStatus::loadMainScene("StatusLayer.ccbi"); + return true; +} + +bool AppDelegate::applicationDidFinishLaunching() +{ + + // initialize director + CCDirector *pDirector = CCDirector::sharedDirector(); + pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); + pDirector->setProjection(kCCDirectorProjection2D); + + + CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize(); + + CCSize designSize = CCSizeMake(320, 480); + CCSize resourceSize = CCSizeMake(320, 480); + + + if (screenSize.height > 1024) + { + resourceSize = CCSizeMake(1280, 1920); + CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(CCArray::create(CCString::create("resources-xlarge"), CCString::create(""), NULL)); + } + else if (screenSize.height > 960) + { + resourceSize = CCSizeMake(640, 960); + CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(CCArray::create(CCString::create("resources-large"), CCString::create(""), NULL)); + } + else if (screenSize.height > 480) + { + resourceSize = CCSizeMake(480, 720); + CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(CCArray::create(CCString::create("resources-medium"), CCString::create(""), NULL)); + } + else + { + resourceSize = CCSizeMake(320, 568); + CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(CCArray::create(CCString::create("resources-small"), CCString::create(""), NULL)); + } + + pDirector->setContentScaleFactor(resourceSize.height/designSize.height); + + CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder); + + CCFileUtils::sharedFileUtils()->setSearchPath(CCArray::create(CCString::create(CCFileUtils::sharedFileUtils()->getWriteablePath()), + CCString::create("assets/"), CCString::create(""), NULL)); + // turn on display FPS + pDirector->setDisplayStats(true); + + // set FPS. the default value is 1.0/60 if you don't call this + pDirector->setAnimationInterval(1.0 / 60); + + ScriptingCore* sc = ScriptingCore::getInstance(); + sc->addRegisterCallback(register_all_cocos2dx); + sc->addRegisterCallback(register_cocos2dx_js_extensions); + sc->addRegisterCallback(register_CCBuilderReader); + sc->addRegisterCallback(jsb_register_chipmunk); + + sc->start(); + CCScriptEngineProtocol *pEngine = ScriptingCore::getInstance(); + CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine); + runMainScene(); +} + + + +void handle_signal(int signal) { + static int internal_state = 0; + ScriptingCore* sc = ScriptingCore::getInstance(); + // should start everything back + CCDirector* director = CCDirector::sharedDirector(); + if (director->getRunningScene()) { + director->popToRootScene(); + } else { + CCPoolManager::sharedPoolManager()->finalize(); + if (internal_state == 0) { + //sc->dumpRoot(NULL, 0, NULL); + sc->start(); + internal_state = 1; + } else { + sc->runScript("hello.js"); + internal_state = 0; + } + } +} + +// This function will be called when the app is inactive. When comes a phone call,it's be invoked too +void AppDelegate::applicationDidEnterBackground() +{ + CCDirector::sharedDirector()->stopAnimation(); + SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); + SimpleAudioEngine::sharedEngine()->pauseAllEffects(); +} + +// this function will be called when the app is active again +void AppDelegate::applicationWillEnterForeground() +{ + CCDirector::sharedDirector()->startAnimation(); + SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); + SimpleAudioEngine::sharedEngine()->resumeAllEffects(); +} + +} diff --git a/samples/Javascript/CocosPlayer/Classes/AppDelegate.h b/samples/Javascript/CocosPlayer/Classes/AppDelegate.h new file mode 100644 index 0000000000..1f4d5439a0 --- /dev/null +++ b/samples/Javascript/CocosPlayer/Classes/AppDelegate.h @@ -0,0 +1,53 @@ +// +// GCTestAppDelegate.h +// GCTest +// +// Created by Rohan Kuruvilla on 06/08/2012. +// Copyright __MyCompanyName__ 2012. All rights reserved. +// + +#ifndef _APP_DELEGATE_H_ +#define _APP_DELEGATE_H_ + +#include "CCApplication.h" +/** + @brief The cocos2d Application. + + The reason for implement as private inheritance is to hide some interface call by CCDirector. + */ + + +class AppDelegate : private cocos2d::CCApplication +{ +public: + AppDelegate(); + virtual ~AppDelegate(); + + /** + @brief Implement CCDirector and CCScene init code here. + @return true Initialize success, app continue. + @return false Initialize failed, app terminate. + */ + virtual bool applicationDidFinishLaunching(); + + /** + @brief The function be called when the application enter background + @param the pointer of the application + */ + virtual void applicationDidEnterBackground(); + + /** + @brief The function be called when the application enter foreground + @param the pointer of the application + */ + virtual void applicationWillEnterForeground(); + +}; + + +void handle_ccb_run(); +void handle_ccb_stop(); +void handle_connected(); +void handle_disconnected(); +#endif // _APP_DELEGATE_H_ + diff --git a/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.cpp b/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.cpp new file mode 100644 index 0000000000..41ae2fface --- /dev/null +++ b/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include "platform/android/jni/JniHelper.h" +#include "cocoa/CCString.h" +#include "Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.h" + +#define LOG_TAG "Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.cpp" +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) + +#define SOCKET_CLASS_NAME "org/cocos2dx/cocosplayer/CocosPlayerSocket" +#define HELPER_CLASS_NAME "org/cocos2dx/cocosplayer/CCBFileUtilsHelper" + +using namespace cocos2d; +using namespace std; + +extern "C" { + void setPairingCodeJNI(int code) { + JniMethodInfo t; + if (JniHelper::getStaticMethodInfo(t, SOCKET_CLASS_NAME, "setPairingCode", "(I)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, code); + t.env->DeleteLocalRef(t.classID); + } + } + + void cleanCacheDirJNI() { + JniMethodInfo t; + if (JniHelper::getStaticMethodInfo(t, HELPER_CLASS_NAME, "cleanCache", "()V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID); + t.env->DeleteLocalRef(t.classID); + } + } +} diff --git a/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.h b/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.h new file mode 100644 index 0000000000..a810a3ac68 --- /dev/null +++ b/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.h @@ -0,0 +1,34 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +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. +****************************************************************************/ +#ifndef __Java_org_cocos2dx_cocosplayer_CocosPlayerSocket_H__ +#define __Java_org_cocos2dx_cocosplayer_CocosPlayerSocket_H__ + +#include + +extern "C" { + extern void setPairingCodeJNI(int code); + extern void cleanCacheDirJNI(); +} + +#endif diff --git a/samples/Javascript/CocosPlayer/Classes/MainSceneHelper.cpp b/samples/Javascript/CocosPlayer/Classes/MainSceneHelper.cpp new file mode 100644 index 0000000000..178b99f059 --- /dev/null +++ b/samples/Javascript/CocosPlayer/Classes/MainSceneHelper.cpp @@ -0,0 +1,87 @@ +#include "MainSceneHelper.h" + +CCMenuItemImage * CCBHelper::mBtnRun = NULL; +CCMenuItemImage * CCBHelper::mBtnPair = NULL; +CCMenuItemImage * CCBHelper::mBtnReset = NULL; + +CCLabelTTF * CCBHelper::mLblStatus = NULL; +CCLabelTTF * CCBHelper::mLblPair = NULL; +CCLabelTTF * CCBHelper::mLblInstructions = NULL; + +std::string CCBHelper::mLblStatus_background = ""; +std::string CCBHelper::mLblPair_background = ""; +bool CCBHelper::mLblInstructions_visible = true; + + +void CCBHelper::setStatusObject(CCLabelTTF *status) { + if(mLblStatus == NULL) { + mLblStatus = CCLabelTTF::create(); + } + + status->retain(); + mLblStatus = status; + + if(mLblStatus_background != "") { + status->setString(mLblStatus_background.c_str()); + } +} + + +void CCBHelper::setStatusMessage(std::string str) { + if(mLblStatus == NULL) { + mLblStatus_background = str; + } else { + mLblStatus->setString(str.c_str()); + } +} + +void CCBHelper::setInstructionsObject(CCLabelTTF *status) { + if(mLblInstructions == NULL) { + mLblInstructions = CCLabelTTF::create(); + } + status->retain(); + mLblInstructions = status; + if(!mLblInstructions_visible) { + mLblInstructions->setVisible(false); + } +} + +void CCBHelper::setInstructionsMessage(bool isVisible) { + if(mLblInstructions == NULL) { + mLblInstructions_visible = isVisible; + } else { + mLblInstructions_visible = isVisible; + mLblInstructions->setVisible(isVisible); + } +} + +void CCBHelper::setPairObject(CCLabelTTF *pair) { + + if(mLblPair == NULL) { + mLblPair = CCLabelTTF::create(); + } + pair->retain(); + mLblPair = pair; + if(mLblPair_background != "") { + mLblPair->setString(mLblPair_background.c_str()); + } + +} + +void CCBHelper::setPairMessage(std::string str) { + if(mLblPair == NULL) { + mLblPair_background = str; + } else { + mLblPair->setString(str.c_str()); + } +} + +bool CCBHelper::isMainJSPresent() { + std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("main.js"); + CCLOG("PATH RETURNED: %s", path.c_str()); + if(path == "") { + return false; + } + return true; +} + diff --git a/samples/Javascript/CocosPlayer/Classes/MainSceneHelper.h b/samples/Javascript/CocosPlayer/Classes/MainSceneHelper.h new file mode 100644 index 0000000000..3f5e4e717a --- /dev/null +++ b/samples/Javascript/CocosPlayer/Classes/MainSceneHelper.h @@ -0,0 +1,29 @@ +#include "cocos2d.h" +#include "string.h" + +USING_NS_CC; + +class CCBHelper { + public: + static CCMenuItemImage * mBtnRun; + static CCMenuItemImage * mBtnReset; + static CCMenuItemImage * mBtnPair; + + static CCLabelTTF *mLblStatus; + static std::string mLblStatus_background; + static CCLabelTTF *mLblPair; + static std::string mLblPair_background; + static CCLabelTTF *mLblInstructions; + static bool mLblInstructions_visible; + + static void setPairObject(CCLabelTTF *pair); + static void setPairMessage(std::string str); + + static void setInstructionsObject(CCLabelTTF *instr); + static void setInstructionsMessage(bool isVisible); + + static void setStatusObject(CCLabelTTF *status); + static void setStatusMessage(std::string str); + + static bool isMainJSPresent(); +}; diff --git a/samples/Javascript/CocosPlayer/Classes/PlayerStatus.cpp b/samples/Javascript/CocosPlayer/Classes/PlayerStatus.cpp new file mode 100644 index 0000000000..c37e2067ff --- /dev/null +++ b/samples/Javascript/CocosPlayer/Classes/PlayerStatus.cpp @@ -0,0 +1,156 @@ + +#include "PlayerStatus.h" +#include "PlayerStatusLoader.h" +#include "MainSceneHelper.h" +#include "AppDelegate.h" +#include "GUI/CCEditBox/CCEditBox.h" +#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h" + + +USING_NS_CC; +USING_NS_CC_EXT; + +PlayerStatus::PlayerStatus(): mBtnRun(NULL), mBtnReset(NULL), mBtnPair(NULL), + mLblStatus(NULL), mLblInstructions(NULL), mLblPair(NULL) +{ + +} + +PlayerStatus::~PlayerStatus() +{ + CC_SAFE_RELEASE(mBtnRun); + CC_SAFE_RELEASE(mBtnReset); + CC_SAFE_RELEASE(mBtnPair); + + CC_SAFE_RELEASE(mLblStatus); + CC_SAFE_RELEASE(mLblPair); + CC_SAFE_RELEASE(mLblInstructions); +} + + +void PlayerStatus::onNodeLoaded(cocos2d::CCNode * pNode, cocos2d::extension::CCNodeLoader * pNodeLoader) { + +} + + +SEL_CCControlHandler PlayerStatus::onResolveCCBCCControlSelector(CCObject * pTarget, const char * pSelectorName) { + return NULL; +} + +SEL_MenuHandler PlayerStatus::onResolveCCBCCMenuItemSelector(CCObject * pTarget, const char * pSelectorName) { + + CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "pressedPair:", PlayerStatus::pressedPair); + CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "pressedReset:", PlayerStatus::pressedReset); + CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "pressedRun:", PlayerStatus::pressedRun); + + return NULL; +} + +void editBoxCallbackFunc(const char* pText, void* ctx) +{ + PlayerStatus *thiz = (PlayerStatus *)ctx; + std::string text(pText); + if(text == "" || text == " ") { + thiz->mLblPair->setString("Auto"); + setPairingCodeJNI(-1); + } else { + thiz->mLblPair->setString(pText); + setPairingCodeJNI(atoi(pText)); + } +} + +void PlayerStatus::pressedPair(CCObject * pSender, cocos2d::extension::CCControlEvent pCCControlEvent) { + showEditTextDialogJNI("Enter pairing code", "", kEditBoxInputModeNumeric, kEditBoxInputFlagInitialCapsWord, + kKeyboardReturnTypeDone, 4, editBoxCallbackFunc, (void*)this); +} + +void PlayerStatus::pressedReset(CCObject * pSender, cocos2d::extension::CCControlEvent pCCControlEvent) { + cleanCacheDirJNI(); + this->mBtnRun->setEnabled(false); + this->mBtnReset->setEnabled(false); +} + + +void PlayerStatus::pressedRun(CCObject * pSender, cocos2d::extension::CCControlEvent pCCControlEvent) { + handle_ccb_run(); +} + + +bool PlayerStatus::onAssignCCBMemberVariable(CCObject * pTarget, const char * pMemberVariableName, CCNode * pNode) { + + CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "btnRun", CCMenuItemImage *, this->mBtnRun); + if(strcmp(pMemberVariableName, "btnRun")) { + if(this->mBtnRun == NULL) { + this->mBtnRun = CCMenuItemImage::create(); + } + if(CCBHelper::isMainJSPresent()) { + this->mBtnRun->setEnabled(true); + } else { + this->mBtnRun->setEnabled(false); + } + } + + + CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "btnReset", CCMenuItemImage *, this->mBtnReset); + + if(strcmp(pMemberVariableName, "btnReset")) { + if(this->mBtnReset == NULL) { + this->mBtnReset = CCMenuItemImage::create(); + } + if(CCBHelper::isMainJSPresent()) { + this->mBtnReset->setEnabled(true); + } else { + this->mBtnReset->setEnabled(false); + } + } + + CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "btnPair", CCMenuItemImage *, this->mBtnPair); + + CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "lblStatus", CCLabelTTF *, this->mLblStatus); + if(strcmp(pMemberVariableName, "lblStatus") && this->mLblStatus != NULL) { + CCBHelper::setStatusObject(this->mLblStatus); + } + + CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "lblPair", CCLabelTTF *, this->mLblPair); + if(strcmp(pMemberVariableName, "lblPair") && this->mLblPair != NULL) { + CCBHelper::setPairObject(this->mLblPair); + } + + CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "lblInstructions", CCLabelTTF *, this->mLblInstructions); + if(strcmp(pMemberVariableName, "lblInstructions") && this->mLblInstructions != NULL) { + CCBHelper::setInstructionsObject(this->mLblInstructions); + } + + return false; +} + + +void PlayerStatus::loadMainScene(const char *pCCBFileName) { + CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary(); + + ccNodeLoaderLibrary->registerCCNodeLoader("PlayerStatusLayer", PlayerStatusLoader::loader()); + + /* Create an autorelease CCBReader. */ + cocos2d::extension::CCBReader * ccbReader = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary); + ccbReader->autorelease(); + + /* Read a ccbi file. */ + // Load the scene from the ccbi-file, setting this class as + // the owner will cause lblTestTitle to be set by the CCBReader. + // lblTestTitle is in the TestHeader.ccbi, which is referenced + // from each of the test scenes. + CCNode * node = ccbReader->readNodeGraphFromFile(pCCBFileName, NULL); + + CCScene * scene = CCScene::create(); + if(node != NULL) { + scene->addChild(node); + } + + /* Push the new scene with a fancy transition. */ + ccColor3B transitionColor; + transitionColor.r = 0; + transitionColor.g = 0; + transitionColor.b = 0; + + CCDirector::sharedDirector()->runWithScene(CCTransitionFade::create(0.5f, scene, transitionColor)); +} diff --git a/samples/Javascript/CocosPlayer/Classes/PlayerStatus.h b/samples/Javascript/CocosPlayer/Classes/PlayerStatus.h new file mode 100644 index 0000000000..4219074595 --- /dev/null +++ b/samples/Javascript/CocosPlayer/Classes/PlayerStatus.h @@ -0,0 +1,56 @@ +#ifndef _PLAYERSTATUS_H_ +#define _PLAYERSTATUS_H_ + +#include "cocos2d.h" +#include "cocos-ext.h" + +#include "Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.h" + +/* + * Note: for some pretty hard fucked up reason, the order of inheritance is important! + * When CCLayer is the 'first' inherited object: + * During runtime the method call to the (pure virtual) 'interfaces' fails jumping into a bogus method or just doing nothing: + * #0 0x000cf840 in non-virtual thunk to HelloCocos.... + * #1 .... + * + * This thread describes the problem: + * http://www.cocoabuilder.com/archive/xcode/265549-crash-in-virtual-method-call.html + */ + + +class PlayerStatus + : public cocos2d::CCLayer + , public cocos2d::extension::CCBSelectorResolver + , public cocos2d::extension::CCBMemberVariableAssigner + , public cocos2d::extension::CCNodeLoaderListener +{ +public: + CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(PlayerStatus, create); + + PlayerStatus(); + virtual ~PlayerStatus(); + + void openTest(const char * pCCBFileName, const char * pCCNodeName = NULL, cocos2d::extension::CCNodeLoader * pCCNodeLoader = NULL); + + virtual cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSelector(cocos2d::CCObject * pTarget, const char * pSelectorName); + virtual cocos2d::extension::SEL_CCControlHandler onResolveCCBCCControlSelector(cocos2d::CCObject * pTarget, const char * pSelectorName); + virtual bool onAssignCCBMemberVariable(cocos2d::CCObject * pTarget, const char * pMemberVariableName, cocos2d::CCNode * pNode); + virtual void onNodeLoaded(cocos2d::CCNode * pNode, cocos2d::extension::CCNodeLoader * pNodeLoader); + + void pressedPair(CCObject * pSender, cocos2d::extension::CCControlEvent pCCControlEvent); + void pressedReset(CCObject * pSender, cocos2d::extension::CCControlEvent pCCControlEvent); + void pressedRun(CCObject * pSender, cocos2d::extension::CCControlEvent pCCControlEvent); + static void loadMainScene(const char *fileName); + + cocos2d::CCMenuItemImage* mBtnRun; + cocos2d::CCMenuItemImage* mBtnReset; + cocos2d::CCMenuItemImage* mBtnPair; + + cocos2d::CCLabelTTF* mLblStatus; + cocos2d::CCLabelTTF* mLblInstructions; + cocos2d::CCLabelTTF* mLblPair; + +}; + + +#endif diff --git a/samples/Javascript/CocosPlayer/Classes/PlayerStatusLoader.h b/samples/Javascript/CocosPlayer/Classes/PlayerStatusLoader.h new file mode 100644 index 0000000000..c83678b8cb --- /dev/null +++ b/samples/Javascript/CocosPlayer/Classes/PlayerStatusLoader.h @@ -0,0 +1,17 @@ +#ifndef _PLAYERSTATUSLOADER_H_ +#define _PLAYERSTATUSLOADER_H_ + +#include "PlayerStatus.h" + +/* Forward declaration. */ +class CCBReader; + +class PlayerStatusLoader : public cocos2d::extension::CCLayerLoader { +public: + CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(PlayerStatusLoader, loader); + +protected: + CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(PlayerStatus); +}; + +#endif diff --git a/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-play.png.REMOVED.git-id b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-play.png.REMOVED.git-id new file mode 100644 index 0000000000..4684ca15ef --- /dev/null +++ b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-play.png.REMOVED.git-id @@ -0,0 +1 @@ +349baa101ddbdf4edf6d20b17362bd22c0008284 \ No newline at end of file diff --git a/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run-dis.png.REMOVED.git-id b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run-dis.png.REMOVED.git-id new file mode 100644 index 0000000000..85b4d8e898 --- /dev/null +++ b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run-dis.png.REMOVED.git-id @@ -0,0 +1 @@ +fe7e93e8e974f39426858d2bfbed7dff26c2b9c4 \ No newline at end of file diff --git a/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run.png.REMOVED.git-id b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run.png.REMOVED.git-id new file mode 100644 index 0000000000..9c17067805 --- /dev/null +++ b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run.png.REMOVED.git-id @@ -0,0 +1 @@ +20213456280fa1bced942496820fd71cb35ddb64 \ No newline at end of file diff --git a/samples/Javascript/CocosPlayer/proj.android/.classpath b/samples/Javascript/CocosPlayer/proj.android/.classpath new file mode 100644 index 0000000000..a4763d1eec --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/samples/Javascript/CocosPlayer/proj.android/.project b/samples/Javascript/CocosPlayer/proj.android/.project new file mode 100644 index 0000000000..3ee65ad661 --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/.project @@ -0,0 +1,33 @@ + + + CocosDragonJS + + + + + + com.android.ide.eclipse.adt.ResourceManagerBuilder + + + + + com.android.ide.eclipse.adt.PreCompilerBuilder + + + + + org.eclipse.jdt.core.javabuilder + + + + + com.android.ide.eclipse.adt.ApkBuilder + + + + + + com.android.ide.eclipse.adt.AndroidNature + org.eclipse.jdt.core.javanature + + diff --git a/samples/Javascript/CocosPlayer/proj.android/AndroidManifest.xml b/samples/Javascript/CocosPlayer/proj.android/AndroidManifest.xml new file mode 100644 index 0000000000..5a884a8c34 --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/AndroidManifest.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/samples/Javascript/CocosPlayer/proj.android/ant.properties b/samples/Javascript/CocosPlayer/proj.android/ant.properties new file mode 100644 index 0000000000..f8af38bfb4 --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/ant.properties @@ -0,0 +1 @@ +aapt.ignore.assets="!*.pvr.gz:!*.gz:!.svn:!.git:.*:_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~" diff --git a/samples/Javascript/CocosPlayer/proj.android/build.xml b/samples/Javascript/CocosPlayer/proj.android/build.xml new file mode 100644 index 0000000000..a0b5e9a321 --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/build.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/Javascript/CocosPlayer/proj.android/build_native.sh b/samples/Javascript/CocosPlayer/proj.android/build_native.sh new file mode 100755 index 0000000000..e1856727dd --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/build_native.sh @@ -0,0 +1,88 @@ +#NDK_ROOT="/opt/android/android-ndk" +APPNAME="CocosPlayer" + +# options + +buildexternalsfromsource= +PARALLEL_BUILD_FLAG= + +usage(){ +cat << EOF +usage: $0 [options] + +Build C/C++ code for $APPNAME using Android NDK + +OPTIONS: +-s Build externals from source +-p Run make with -j8 option to take advantage of multiple processors +-h this help +EOF +} + +while getopts "sph" OPTION; do +case "$OPTION" in +s) +buildexternalsfromsource=1 +;; +p) +PARALLEL_BUILD_FLAG=\-j8 +;; +h) +usage +exit 0 +;; +esac +done + +# exit this script if any commmand fails +set -e + +# paths + +if [ -z "${NDK_ROOT+aaa}" ];then +echo "please define NDK_ROOT" +exit 1 +fi + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +# ... use paths relative to current directory +COCOS2DX_ROOT="$DIR/../../../../" +APP_ROOT="$DIR/.." +APP_ANDROID_ROOT="$DIR" +BINDINGS_JS_ROOT="$APP_ROOT/../../../scripting/javascript/bindings/js" +RESROUCE_ROOT="$APP_ROOT/Resources" + +echo +echo "Paths" +echo " NDK_ROOT = $NDK_ROOT" +echo " COCOS2DX_ROOT = $COCOS2DX_ROOT" +echo " APP_ROOT = $APP_ROOT" +echo " APP_ANDROID_ROOT = $APP_ANDROID_ROOT" +echo + +# Debug +set -x + +# make sure assets is exist +if [ -d "$APP_ANDROID_ROOT"/assets ]; then + rm -rf "$APP_ANDROID_ROOT"/assets +fi + +mkdir "$APP_ANDROID_ROOT"/assets + +# copy bindings/*.js into assets' root +cp -f "$BINDINGS_JS_ROOT"/*.js "$APP_ANDROID_ROOT"/assets + +# copy bindings/*.js into assets' root +cp -rf "$RESROUCE_ROOT"/ "$APP_ANDROID_ROOT"/assets + +#cp -rf "$CCB_RESOURCE_ROOT" "$APP_ANDROID_ROOT"/assets + +echo "Using prebuilt externals" +echo + +set -x + +"$NDK_ROOT"/ndk-build $PARALLEL_BUILD_FLAG -C "$APP_ANDROID_ROOT" $* \ + "NDK_MODULE_PATH=${COCOS2DX_ROOT}:${COCOS2DX_ROOT}/cocos2dx/platform/third_party/android/prebuilt" \ + NDK_LOG=0 V=0 diff --git a/samples/Javascript/CocosPlayer/proj.android/jni/Android.mk b/samples/Javascript/CocosPlayer/proj.android/jni/Android.mk new file mode 100644 index 0000000000..5167d729d0 --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/jni/Android.mk @@ -0,0 +1,32 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := cocosplayer_shared + +LOCAL_MODULE_FILENAME := libcocosplayer + +LOCAL_SRC_FILES := cocosplayer/main.cpp \ + ../../Classes/PlayerStatus.cpp\ + ../../Classes/AppDelegate.cpp \ + ../../Classes/MainSceneHelper.cpp \ + ../../Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.cpp + + +LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes + +LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static +LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static +LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static +LOCAL_WHOLE_STATIC_LIBRARIES += spidermonkey_static +LOCAL_WHOLE_STATIC_LIBRARIES += scriptingcore-spidermonkey + +LOCAL_EXPORT_CFLAGS := -DCOCOS2D_DEBUG=2 -DCOCOS2D_JAVASCRIPT + +include $(BUILD_SHARED_LIBRARY) + +$(call import-module,cocos2dx) +$(call import-module,CocosDenshion/android) +$(call import-module,external/chipmunk) +$(call import-module,scripting/javascript/spidermonkey-android) +$(call import-module,scripting/javascript/bindings) diff --git a/samples/Javascript/CocosPlayer/proj.android/jni/Application.mk b/samples/Javascript/CocosPlayer/proj.android/jni/Application.mk new file mode 100644 index 0000000000..1444a4d607 --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/jni/Application.mk @@ -0,0 +1,3 @@ +APP_STL := gnustl_static +APP_CPPFLAGS := -frtti -DCOCOS2D_JAVASCRIPT=1 +APP_CPPFLAGS += -DCOCOS2D_DEBUG=1 -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 diff --git a/samples/Javascript/CocosPlayer/proj.android/jni/cocosplayer/main.cpp b/samples/Javascript/CocosPlayer/proj.android/jni/cocosplayer/main.cpp new file mode 100644 index 0000000000..07a0733a7a --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/jni/cocosplayer/main.cpp @@ -0,0 +1,79 @@ +#include "AppDelegate.h" +#include "PlayerStatus.h" +#include "cocos2d.h" +#include "platform/android/jni/JniHelper.h" +#include "CCEventType.h" +#include +#include + +#define LOG_TAG "main" +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) + +using namespace cocos2d; + +extern "C" +{ + +jint JNI_OnLoad(JavaVM *vm, void *reserved) +{ + JniHelper::setJavaVM(vm); + + return JNI_VERSION_1_4; +} + +void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h) +{ + if (!CCDirector::sharedDirector()->getOpenGLView()) + { + CCEGLView *view = CCEGLView::sharedOpenGLView(); + view->setFrameSize(w, h); + + AppDelegate *pAppDelegate = new AppDelegate(); + CCApplication::sharedApplication()->run(); + } + else + { + ccDrawInit(); + ccGLInvalidateStateCache(); + + CCShaderCache::sharedShaderCache()->reloadDefaultShaders(); + CCTextureCache::reloadAllTextures(); + CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); + CCDirector::sharedDirector()->setGLDefaultValues(); + } +} + + + void Java_org_cocos2dx_lib_Cocos2dxGLSurfaceView_nativeRunCCB(JNIEnv* env, jobject thiz) + { + LOGD("INSIDE JNI RUN CALL"); + handle_ccb_run(); + } + + void Java_org_cocos2dx_cocosplayer_CocosPlayerSocket_nativeRunCCB(JNIEnv* env, jobject thiz) + { + LOGD("INSIDE JNI RUN CALL"); + handle_ccb_run(); + } + + void Java_org_cocos2dx_cocosplayer_CocosPlayerSocket_nativeStopCCB(JNIEnv* env, jobject thiz) + { + LOGD("INSIDE JNI STOP CALL"); + handle_ccb_stop(); + } + + void Java_org_cocos2dx_cocosplayer_CocosPlayerSocket_nativeRunScript(JNIEnv* env, jobject thiz, jstring jsString) + { + LOGD("INSIDE JNI RUN SCRIPT CALL"); + } + + void Java_org_cocos2dx_cocosplayer_CocosPlayerSocket_nativeConnected(JNIEnv* env, jobject thiz) + { + handle_connected(); + } + + void Java_org_cocos2dx_cocosplayer_CocosPlayerSocket_nativeDisconnected(JNIEnv* env, jobject thiz) + { + handle_disconnected(); + } +} diff --git a/samples/Javascript/CocosPlayer/proj.android/ndkgdb.sh b/samples/Javascript/CocosPlayer/proj.android/ndkgdb.sh new file mode 100755 index 0000000000..4004da2c55 --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/ndkgdb.sh @@ -0,0 +1,47 @@ +APPNAME="CocosPlayer" +APP_ANDROID_NAME="org.cocos2dx.cocosplayer" + +if [ -z "${SDK_ROOT+aaa}" ]; then +# ... if SDK_ROOT is not set, use "$HOME/bin/android-sdk" + SDK_ROOT="$HOME/bin/android-sdk" +fi + +if [ -z "${NDK_ROOT+aaa}" ]; then +# ... if NDK_ROOT is not set, use "$HOME/bin/android-ndk" + NDK_ROOT="$HOME/bin/android-ndk" +fi + +if [ -z "${COCOS2DX_ROOT+aaa}" ]; then +# ... if COCOS2DX_ROOT is not set +# ... find current working directory + DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +# ... use paths relative to current directory + COCOS2DX_ROOT="$DIR/../../.." + APP_ROOT="$DIR/.." + APP_ANDROID_ROOT="$DIR" +else + APP_ROOT="$COCOS2DX_ROOT/samples/$APPNAME" + APP_ANDROID_ROOT="$COCOS2DX_ROOT/samples/$APPNAME/proj.android" +fi + +echo "NDK_ROOT = $NDK_ROOT" +echo "SDK_ROOT = $SDK_ROOT" +echo "COCOS2DX_ROOT = $COCOS2DX_ROOT" +echo "APP_ROOT = $APP_ROOT" +echo "APP_ANDROID_ROOT = $APP_ANDROID_ROOT" +echo "APP_ANDROID_NAME = $APP_ANDROID_NAME" + +echo +echo "Killing and restarting ${APP_ANDROID_NAME}" +echo + +set -x + +"${SDK_ROOT}"/platform-tools/adb shell am force-stop "${APP_ANDROID_NAME}" + +NDK_MODULE_PATH="${COCOS2DX_ROOT}":"${COCOS2DX_ROOT}"/cocos2dx/platform/third_party/android/prebuilt \ + "${NDK_ROOT}"/ndk-gdb \ + --adb="${SDK_ROOT}"/platform-tools/adb \ + --verbose \ + --start \ + --force diff --git a/samples/Javascript/CocosPlayer/proj.android/proguard-project.txt b/samples/Javascript/CocosPlayer/proj.android/proguard-project.txt new file mode 100644 index 0000000000..f2fe1559a2 --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/proguard-project.txt @@ -0,0 +1,20 @@ +# To enable ProGuard in your project, edit project.properties +# to define the proguard.config property as described in that file. +# +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in ${sdk.dir}/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the ProGuard +# include property in project.properties. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/samples/Javascript/CocosPlayer/proj.android/project.properties b/samples/Javascript/CocosPlayer/proj.android/project.properties new file mode 100644 index 0000000000..9feb1bfb24 --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/project.properties @@ -0,0 +1,13 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system use, +# "ant.properties", and override values to adapt the script to your +# project structure. + +# Project target. +target=android-16 + +android.library.reference.1=../../../../cocos2dx/platform/android/java diff --git a/samples/Javascript/CocosPlayer/proj.android/res/layout/activity_main.xml b/samples/Javascript/CocosPlayer/proj.android/res/layout/activity_main.xml new file mode 100644 index 0000000000..88b5b16f95 --- /dev/null +++ b/samples/Javascript/CocosPlayer/proj.android/res/layout/activity_main.xml @@ -0,0 +1,36 @@ + + +