From f8a22bb13431a22b2ca9b2221e11cedad1127e6f Mon Sep 17 00:00:00 2001 From: zhangbin Date: Tue, 25 Jun 2013 18:05:03 +0800 Subject: [PATCH] closed #2317, Add sample project HelloUser. --- .../samples/HelloUser/Classes/AppDelegate.cpp | 54 +++++++ .../samples/HelloUser/Classes/AppDelegate.h | 38 +++++ .../HelloUser/Classes/HelloWorldScene.cpp | 109 +++++++++++++ .../HelloUser/Classes/HelloWorldScene.h | 30 ++++ .../HelloUser/Classes/MyUserManager.cpp | 149 ++++++++++++++++++ .../samples/HelloUser/Classes/MyUserManager.h | 61 +++++++ .../alipay_plugin.apk.REMOVED.git-id | 1 + .../Resources/pro.jar.REMOVED.git-id | 1 + .../samples/HelloUser/proj.android/.classpath | 11 ++ .../samples/HelloUser/proj.android/.project | 45 ++++++ .../proj.android/AndroidManifest.xml | 41 +++++ .../samples/HelloUser/proj.android/README.md | 87 ++++++++++ .../HelloUser/proj.android/ant.properties | 17 ++ .../samples/HelloUser/proj.android/build.xml | 83 ++++++++++ .../HelloUser/proj.android/build_native.sh | 78 +++++++++ .../HelloUser/proj.android/jni/Android.mk | 23 +++ .../HelloUser/proj.android/jni/Application.mk | 3 + .../proj.android/jni/hellocpp/main.cpp | 48 ++++++ .../proj.android/proguard-project.txt | 20 +++ .../HelloUser/proj.android/project.properties | 13 ++ .../proj.android/res/values/strings.xml | 4 + .../src/org/cocos2dx/HelloUser/HelloUser.java | 52 ++++++ plugin/tools/config.sh | 3 +- 23 files changed, 970 insertions(+), 1 deletion(-) create mode 100644 plugin/samples/HelloUser/Classes/AppDelegate.cpp create mode 100644 plugin/samples/HelloUser/Classes/AppDelegate.h create mode 100644 plugin/samples/HelloUser/Classes/HelloWorldScene.cpp create mode 100644 plugin/samples/HelloUser/Classes/HelloWorldScene.h create mode 100644 plugin/samples/HelloUser/Classes/MyUserManager.cpp create mode 100755 plugin/samples/HelloUser/Classes/MyUserManager.h create mode 100644 plugin/samples/HelloUser/Resources/alipay_plugin.apk.REMOVED.git-id create mode 100644 plugin/samples/HelloUser/Resources/pro.jar.REMOVED.git-id create mode 100644 plugin/samples/HelloUser/proj.android/.classpath create mode 100644 plugin/samples/HelloUser/proj.android/.project create mode 100644 plugin/samples/HelloUser/proj.android/AndroidManifest.xml create mode 100644 plugin/samples/HelloUser/proj.android/README.md create mode 100644 plugin/samples/HelloUser/proj.android/ant.properties create mode 100644 plugin/samples/HelloUser/proj.android/build.xml create mode 100755 plugin/samples/HelloUser/proj.android/build_native.sh create mode 100644 plugin/samples/HelloUser/proj.android/jni/Android.mk create mode 100644 plugin/samples/HelloUser/proj.android/jni/Application.mk create mode 100644 plugin/samples/HelloUser/proj.android/jni/hellocpp/main.cpp create mode 100644 plugin/samples/HelloUser/proj.android/proguard-project.txt create mode 100644 plugin/samples/HelloUser/proj.android/project.properties create mode 100644 plugin/samples/HelloUser/proj.android/res/values/strings.xml create mode 100644 plugin/samples/HelloUser/proj.android/src/org/cocos2dx/HelloUser/HelloUser.java diff --git a/plugin/samples/HelloUser/Classes/AppDelegate.cpp b/plugin/samples/HelloUser/Classes/AppDelegate.cpp new file mode 100644 index 0000000000..d48938c82b --- /dev/null +++ b/plugin/samples/HelloUser/Classes/AppDelegate.cpp @@ -0,0 +1,54 @@ +#include "AppDelegate.h" +#include "HelloWorldScene.h" +#include "MyUserManager.h" + +USING_NS_CC; + +AppDelegate::AppDelegate() { + +} + +AppDelegate::~AppDelegate() +{ +} + +bool AppDelegate::applicationDidFinishLaunching() { + MyUserManager::sharedManager()->loadPlugin(); + + // initialize director + Director* pDirector = Director::sharedDirector(); + EGLView* pEGLView = EGLView::sharedOpenGLView(); + + pDirector->setOpenGLView(pEGLView); + + pEGLView->setDesignResolutionSize(960.0f, 640.0f, kResolutionNoBorder); + // 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); + + // create a scene. it's an autorelease object + Scene *pScene = HelloWorld::scene(); + + // run + pDirector->runWithScene(pScene); + + return true; +} + +// This function will be called when the app is inactive. When comes a phone call,it's be invoked too +void AppDelegate::applicationDidEnterBackground() { + Director::sharedDirector()->stopAnimation(); + + // if you use SimpleAudioEngine, it must be pause + // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); +} + +// this function will be called when the app is active again +void AppDelegate::applicationWillEnterForeground() { + Director::sharedDirector()->startAnimation(); + + // if you use SimpleAudioEngine, it must resume here + // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); +} diff --git a/plugin/samples/HelloUser/Classes/AppDelegate.h b/plugin/samples/HelloUser/Classes/AppDelegate.h new file mode 100644 index 0000000000..18ee8aeb63 --- /dev/null +++ b/plugin/samples/HelloUser/Classes/AppDelegate.h @@ -0,0 +1,38 @@ +#ifndef _APP_DELEGATE_H_ +#define _APP_DELEGATE_H_ + +#include "cocos2d.h" + +/** +@brief The cocos2d Application. + +The reason for implement as private inheritance is to hide some interface call by Director. +*/ +class AppDelegate : private cocos2d::Application +{ +public: + AppDelegate(); + virtual ~AppDelegate(); + + /** + @brief Implement Director and Scene 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(); +}; + +#endif // _APP_DELEGATE_H_ + diff --git a/plugin/samples/HelloUser/Classes/HelloWorldScene.cpp b/plugin/samples/HelloUser/Classes/HelloWorldScene.cpp new file mode 100644 index 0000000000..f80939e7cb --- /dev/null +++ b/plugin/samples/HelloUser/Classes/HelloWorldScene.cpp @@ -0,0 +1,109 @@ +#include "HelloWorldScene.h" +#include "MyUserManager.h" + +USING_NS_CC; + +const std::string s_aTestCases[] = { + "QH360", +}; + +Scene* HelloWorld::scene() +{ + // 'scene' is an autorelease object + Scene *scene = Scene::create(); + + // 'layer' is an autorelease object + HelloWorld *layer = HelloWorld::create(); + + // add layer as a child to scene + scene->addChild(layer); + + // return the scene + return scene; +} + +// on "init" you need to initialize your instance +bool HelloWorld::init() +{ + ////////////////////////////// + // 1. super init first + if ( !Layer::init() ) + { + return false; + } + + Size visibleSize = Director::sharedDirector()->getVisibleSize(); + Point origin = Director::sharedDirector()->getVisibleOrigin(); + Point posMid = ccp(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2); + + ///////////////////////////// + // 2. add a menu item with "X" image, which is clicked to quit the program + // you may modify it. + + // add a "close" icon to exit the progress. it's an autorelease object + MenuItemImage *pCloseItem = MenuItemImage::create( + "CloseNormal.png", + "CloseSelected.png", + CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); + + pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , + origin.y + pCloseItem->getContentSize().height/2)); + + // create menu, it's an autorelease object + Menu* pMenu = Menu::create(pCloseItem, NULL); + pMenu->setPosition(PointZero); + + LabelTTF* label1 = LabelTTF::create("Login", "Arial", 32); + MenuItemLabel* pItemLogin = MenuItemLabel::create(label1, CC_CALLBACK_1(HelloWorld::testLogin, this)); + pItemLogin->setAnchorPoint(ccp(0.5f, 0)); + pMenu->addChild(pItemLogin, 0); + pItemLogin->setPosition(ccpAdd(posMid, ccp(-100, -120))); + + LabelTTF* label2 = LabelTTF::create("Logout", "Arial", 32); + MenuItemLabel* pItemLogout = MenuItemLabel::create(label2, CC_CALLBACK_1(HelloWorld::testLogout, this)); + pItemLogout->setAnchorPoint(ccp(0.5f, 0)); + pMenu->addChild(pItemLogout, 0); + pItemLogout->setPosition(ccpAdd(posMid, ccp(100, -120))); + + // create optional menu + // cases item + _caseItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(HelloWorld::caseChanged, this), + MenuItemFont::create( s_aTestCases[0].c_str() ), + NULL ); + int caseLen = sizeof(s_aTestCases) / sizeof(std::string); + for (int i = 1; i < caseLen; ++i) + { + _caseItem->getSubItems()->addObject( MenuItemFont::create( s_aTestCases[i].c_str() ) ); + } + _caseItem->setPosition(ccpAdd(posMid, ccp(0, 120))); + pMenu->addChild(_caseItem); + + _selectedCase = 0; + + this->addChild(pMenu, 1); + return true; +} + +void HelloWorld::caseChanged(Object* pSender) +{ + _selectedCase = _caseItem->getSelectedIndex(); +} + +void HelloWorld::testLogin(Object* pSender) +{ + MyUserManager::sharedManager()->loginByMode((MyUserManager::MyUserMode) (_selectedCase + 1)); +} + +void HelloWorld::testLogout(Object* pSender) +{ + MyUserManager::sharedManager()->logoutByMode((MyUserManager::MyUserMode) (_selectedCase + 1)); +} + +void HelloWorld::menuCloseCallback(Object* pSender) +{ + Director::sharedDirector()->end(); + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) + exit(0); +#endif +} diff --git a/plugin/samples/HelloUser/Classes/HelloWorldScene.h b/plugin/samples/HelloUser/Classes/HelloWorldScene.h new file mode 100644 index 0000000000..948b144d61 --- /dev/null +++ b/plugin/samples/HelloUser/Classes/HelloWorldScene.h @@ -0,0 +1,30 @@ +#ifndef __HELLOWORLD_SCENE_H__ +#define __HELLOWORLD_SCENE_H__ + +#include "cocos2d.h" + +class HelloWorld : public cocos2d::Layer +{ +public: + // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone + virtual bool init(); + + // there's no 'id' in cpp, so we recommend returning the class instance pointer + static cocos2d::Scene* scene(); + + // a selector callback + void menuCloseCallback(Object* pSender); + void testLogin(Object* pSender); + void testLogout(Object* pSender); + + void caseChanged(Object* pSender); + + // implement the "static node()" method manually + CREATE_FUNC(HelloWorld); + +private: + cocos2d::MenuItemToggle* _caseItem; + int _selectedCase; +}; + +#endif // __HELLOWORLD_SCENE_H__ diff --git a/plugin/samples/HelloUser/Classes/MyUserManager.cpp b/plugin/samples/HelloUser/Classes/MyUserManager.cpp new file mode 100644 index 0000000000..9d8db8d2ad --- /dev/null +++ b/plugin/samples/HelloUser/Classes/MyUserManager.cpp @@ -0,0 +1,149 @@ +/**************************************************************************** +Copyright (c) 2012-2013 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. +****************************************************************************/ +#include "MyUserManager.h" +#include "PluginManager.h" +#include "cocos2d.h" + +using namespace cocos2d::plugin; +using namespace cocos2d; + +MyUserManager* MyUserManager::s_pManager = NULL; + +MyUserManager::MyUserManager() +: _retListener(NULL) +, _qh360(NULL) +{ + +} + +MyUserManager::~MyUserManager() +{ + unloadPlugin(); + if (_retListener) + { + delete _retListener; + _retListener = NULL; + } +} + +MyUserManager* MyUserManager::sharedManager() +{ + if (s_pManager == NULL) { + s_pManager = new MyUserManager(); + } + return s_pManager; +} + +void MyUserManager::purgeManager() +{ + if (s_pManager) + { + delete s_pManager; + s_pManager = NULL; + } + PluginManager::end(); +} + +void MyUserManager::loadPlugin() +{ + if (_retListener == NULL) + { + _retListener = new MyUserActionResult(); + } + + { + // init qh360 plugin + _qh360 = dynamic_cast(PluginManager::getInstance()->loadPlugin("UserQH360")); + if (NULL != _qh360) + { + _qh360->setDebugMode(true); + _qh360->setActionListener(_retListener); + } + } +} + +void MyUserManager::unloadPlugin() +{ + if (_qh360) + { + PluginManager::getInstance()->unloadPlugin("UserQH360"); + _qh360 = NULL; + } +} + +void MyUserManager::loginByMode(MyUserMode mode) +{ + ProtocolUser* pUser = NULL; + switch(mode) + { + case kQH360: + pUser = _qh360; + break; + default: + break; + } + + if (pUser) { + pUser->login(); + } +} + +void MyUserManager::logoutByMode(MyUserMode mode) +{ + ProtocolUser* pUser = NULL; + switch(mode) + { + case kQH360: + pUser = _qh360; + break; + default: + break; + } + + if (pUser) { + pUser->logout(); + } +} + +void MyUserActionResult::onActionResult(ProtocolUser* pPlugin, UserActionResultCode code, const char* msg) +{ + char userStatus[1024] = { 0 }; + switch (code) + { + case kLoginSucceed: + case kLoginFailed: + sprintf(userStatus, "User of \"%s\" login %s", pPlugin->getPluginName(), (code == kLoginSucceed)? "Successed" : "Failed"); + break; + case kLogoutSucceed: + sprintf(userStatus, "User of \"%s\" logout", pPlugin->getPluginName()); + break; + default: + break; + } + MessageBox(msg, userStatus); + + // get session ID + std::string sessionID = pPlugin->getSessionID(); + CCLog("User Session ID of plugin %s is : %s", pPlugin->getPluginName(), sessionID.c_str()); +} diff --git a/plugin/samples/HelloUser/Classes/MyUserManager.h b/plugin/samples/HelloUser/Classes/MyUserManager.h new file mode 100755 index 0000000000..90eb5bc95f --- /dev/null +++ b/plugin/samples/HelloUser/Classes/MyUserManager.h @@ -0,0 +1,61 @@ +/**************************************************************************** +Copyright (c) 2012-2013 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 __MY_USER_MANAGER_H__ +#define __MY_USER_MANAGER_H__ + +#include "ProtocolUser.h" + +class MyUserActionResult : public cocos2d::plugin::UserActionListener +{ +public: + virtual void onActionResult(cocos2d::plugin::ProtocolUser* pPlugin, cocos2d::plugin::UserActionResultCode code, const char* msg); +}; + +class MyUserManager +{ +public: + static MyUserManager* sharedManager(); + static void purgeManager(); + + typedef enum { + kNoneMode = 0, + kQH360, + } MyUserMode; + + void unloadPlugin(); + void loadPlugin(); + void loginByMode(MyUserMode mode); + void logoutByMode(MyUserMode mode); + +private: + MyUserManager(); + virtual ~MyUserManager(); + + static MyUserManager* s_pManager; + + cocos2d::plugin::ProtocolUser* _qh360; + MyUserActionResult* _retListener; +}; + +#endif // __MY_USER_MANAGER_H__ diff --git a/plugin/samples/HelloUser/Resources/alipay_plugin.apk.REMOVED.git-id b/plugin/samples/HelloUser/Resources/alipay_plugin.apk.REMOVED.git-id new file mode 100644 index 0000000000..8b0739a130 --- /dev/null +++ b/plugin/samples/HelloUser/Resources/alipay_plugin.apk.REMOVED.git-id @@ -0,0 +1 @@ +761ab0ca76abcdc06890e579f77fcdea6afa83c6 \ No newline at end of file diff --git a/plugin/samples/HelloUser/Resources/pro.jar.REMOVED.git-id b/plugin/samples/HelloUser/Resources/pro.jar.REMOVED.git-id new file mode 100644 index 0000000000..23c54c6ba3 --- /dev/null +++ b/plugin/samples/HelloUser/Resources/pro.jar.REMOVED.git-id @@ -0,0 +1 @@ +0cb89ef70f9172baa023fcbae9b87da09af2e776 \ No newline at end of file diff --git a/plugin/samples/HelloUser/proj.android/.classpath b/plugin/samples/HelloUser/proj.android/.classpath new file mode 100644 index 0000000000..069475ad2b --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/plugin/samples/HelloUser/proj.android/.project b/plugin/samples/HelloUser/proj.android/.project new file mode 100644 index 0000000000..b8d9f4a665 --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/.project @@ -0,0 +1,45 @@ + + + HelloUser + + + + + + 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 + + + + Classes + 2 + PARENT-1-PROJECT_LOC/Classes + + + plugin-x + 2 + PARENT-3-PROJECT_LOC/publish + + + diff --git a/plugin/samples/HelloUser/proj.android/AndroidManifest.xml b/plugin/samples/HelloUser/proj.android/AndroidManifest.xml new file mode 100644 index 0000000000..7781b4c114 --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/AndroidManifest.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugin/samples/HelloUser/proj.android/README.md b/plugin/samples/HelloUser/proj.android/README.md new file mode 100644 index 0000000000..312835611a --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/README.md @@ -0,0 +1,87 @@ +## Prerequisites: + +* Android NDK +* Android SDK **OR** Eclipse ADT Bundle +* Android AVD target installed + +## Building project + +There are two ways of building Android projects. + +1. Eclipse +2. Command Line + +### Import Project in Eclipse + +#### Features: + +1. Complete workflow from Eclipse, including: + * Build C++. + * Clean C++. + * Build and Run whole project. + * Logcat view. + * Debug Java code. + * Javascript editor. + * Project management. +2. True C++ editing, including: + * Code completion. + * Jump to definition. + * Refactoring tools etc. + * Quick open C++ files. + + +#### Setup Eclipse Environment (only once) + + +**NOTE:** This step needs to be done only once to setup the Eclipse environment for cocos2d-x projects. Skip this section if you've done this before. + +1. Download Eclipse ADT bundle from [Google ADT homepage](http://developer.android.com/sdk/index.html) + + **OR** + + Install Eclipse with Java. Add ADT and CDT plugins. + +2. Only for Windows + 1. Install [Cygwin](http://www.cygwin.com/) with make (select make package from the list during the install). + 2. Add `Cygwin\bin` directory to system PATH variable. + 3. Add this line `none /cygdrive cygdrive binary,noacl,posix=0,user 0 0` to `Cygwin\etc\fstab` file. + +3. Set up Variables: + 1. Path Variable `COCOS2DX`: + * Eclipse->Preferences->General->Workspace->**Linked Resources** + * Click **New** button to add a Path Variable `COCOS2DX` pointing to the root cocos2d-x directory. + ![Example](https://lh5.googleusercontent.com/-oPpk9kg3e5w/UUOYlq8n7aI/AAAAAAAAsdQ/zLA4eghBH9U/s400/cocos2d-x-eclipse-vars.png) + + 2. C/C++ Environment Variable `NDK_ROOT`: + * Eclipse->Preferences->C/C++->Build->**Environment**. + * Click **Add** button and add a new variable `NDK_ROOT` pointing to the root NDK directory. + ![Example](https://lh3.googleusercontent.com/-AVcY8IAT0_g/UUOYltoRobI/AAAAAAAAsdM/22D2J9u3sig/s400/cocos2d-x-eclipse-ndk.png) + * Only for Windows: Add new variables **CYGWIN** with value `nodosfilewarning` and **SHELLOPTS** with value `igncr` + +4. Import libcocos2dx library project: + 1. File->New->Project->Android Project From Existing Code. + 2. Click **Browse** button and open `cocos2d-x/cocos2dx/platform/android/java` directory. + 3. Click **Finish** to add project. + +#### Adding and running from Eclipse + +![Example](https://lh3.googleusercontent.com/-SLBOu6e3QbE/UUOcOXYaGqI/AAAAAAAAsdo/tYBY2SylOSM/s288/cocos2d-x-eclipse-project-from-code.png) ![Import](https://lh5.googleusercontent.com/-XzC9Pn65USc/UUOcOTAwizI/AAAAAAAAsdk/4b6YM-oim9Y/s400/cocos2d-x-eclipse-import-project.png) + +1. File->New->Project->Android Project From Existing Code +2. **Browse** to your project directory. eg: `cocos2d-x/cocos2dx/samples/Cpp/TestCpp/proj.android/` +3. Add the project +4. Click **Run** or **Debug** to compile C++ followed by Java and to run on connected device or emulator. + + +### Running project from Command Line + + $ cd cocos2d-x/samples/Cpp/TestCpp/proj.android/ + $ export NDK_ROOT=/path/to/ndk + $ ./build_native.sh + $ ant debug install + +If the last command results in sdk.dir missing error then do: + + $ android list target + $ android update project -p . -t (id from step 6) + $ android update project -p cocos2d-x/cocos2dx/platform/android/java/ -t (id from step 6) diff --git a/plugin/samples/HelloUser/proj.android/ant.properties b/plugin/samples/HelloUser/proj.android/ant.properties new file mode 100644 index 0000000000..b0971e891e --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/ant.properties @@ -0,0 +1,17 @@ +# This file is used to override default values used by the Ant build system. +# +# This file must be checked into Version Control Systems, as it is +# integral to the build system of your project. + +# This file is only used by the Ant script. + +# You can use this to override default values such as +# 'source.dir' for the location of your java source folder and +# 'out.dir' for the location of your output folder. + +# You can also use it define how the release builds are signed by declaring +# the following properties: +# 'key.store' for the location of your keystore and +# 'key.alias' for the name of the key to use. +# The password will be asked during the build when you use the 'release' target. + diff --git a/plugin/samples/HelloUser/proj.android/build.xml b/plugin/samples/HelloUser/proj.android/build.xml new file mode 100644 index 0000000000..28a0fcf8df --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/build.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugin/samples/HelloUser/proj.android/build_native.sh b/plugin/samples/HelloUser/proj.android/build_native.sh new file mode 100755 index 0000000000..e1dec28a1d --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/build_native.sh @@ -0,0 +1,78 @@ +APPNAME="HelloUser" + +# options + +buildexternalsfromsource= + +usage(){ +cat << EOF +usage: $0 [options] + +Build C/C++ code for $APPNAME using Android NDK + +OPTIONS: +-s Build externals from source +-h this help +EOF +} + +while getopts "sh" OPTION; do +case "$OPTION" in +s) +buildexternalsfromsource=1 +;; +h) +usage +exit 0 +;; +esac +done + +# 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 +PLUGIN_ROOT="$DIR/../../.." +COCOS2DX_ROOT="$DIR/../../../.." +APP_ROOT="$DIR/.." +APP_ANDROID_ROOT="$DIR" + +echo "NDK_ROOT = $NDK_ROOT" +echo "COCOS2DX_ROOT = $COCOS2DX_ROOT" +echo "APP_ROOT = $APP_ROOT" +echo "APP_ANDROID_ROOT = $APP_ANDROID_ROOT" + +# 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 resources +for file in "$APP_ROOT"/Resources/* +do +if [ -d "$file" ]; then + cp -rf "$file" "$APP_ANDROID_ROOT"/assets +fi + +if [ -f "$file" ]; then + cp "$file" "$APP_ANDROID_ROOT"/assets +fi +done + +# run ndk-build +if [[ "$buildexternalsfromsource" ]]; then + echo "Building external dependencies from source" + "$NDK_ROOT"/ndk-build -C "$APP_ANDROID_ROOT" $* \ + "NDK_MODULE_PATH=${PLUGIN_ROOT}/publish:${COCOS2DX_ROOT}:${COCOS2DX_ROOT}/cocos2dx/platform/third_party/android/source" +else + echo "Using prebuilt externals" + "$NDK_ROOT"/ndk-build -C "$APP_ANDROID_ROOT" $* \ + "NDK_MODULE_PATH=${PLUGIN_ROOT}/publish:${COCOS2DX_ROOT}:${COCOS2DX_ROOT}/cocos2dx/platform/third_party/android/prebuilt" +fi diff --git a/plugin/samples/HelloUser/proj.android/jni/Android.mk b/plugin/samples/HelloUser/proj.android/jni/Android.mk new file mode 100644 index 0000000000..4679003bc3 --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/jni/Android.mk @@ -0,0 +1,23 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := cocos2dcpp_shared + +LOCAL_MODULE_FILENAME := libcocos2dcpp + +LOCAL_SRC_FILES := hellocpp/main.cpp \ + ../../Classes/AppDelegate.cpp \ + ../../Classes/HelloWorldScene.cpp \ + ../../Classes/MyUserManager.cpp + +LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes + +LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static \ +PluginProtocolStatic + +include $(BUILD_SHARED_LIBRARY) + +$(call import-module,cocos2dx) + +$(call import-module,protocols/android) diff --git a/plugin/samples/HelloUser/proj.android/jni/Application.mk b/plugin/samples/HelloUser/proj.android/jni/Application.mk new file mode 100644 index 0000000000..43499d7113 --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/jni/Application.mk @@ -0,0 +1,3 @@ +APP_STL := gnustl_static +APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -DCOCOS2D_DEBUG=1 -std=c++11 +NDK_TOOLCHAIN_VERSION=4.7 diff --git a/plugin/samples/HelloUser/proj.android/jni/hellocpp/main.cpp b/plugin/samples/HelloUser/proj.android/jni/hellocpp/main.cpp new file mode 100644 index 0000000000..9d2d9ef439 --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/jni/hellocpp/main.cpp @@ -0,0 +1,48 @@ +#include "AppDelegate.h" +#include "cocos2d.h" +#include "CCEventType.h" +#include "platform/android/jni/JniHelper.h" +#include +#include +#include "PluginJniHelper.h" + +#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); + PluginJniHelper::setJavaVM(vm); + return JNI_VERSION_1_4; +} + +void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h) +{ + if (!Director::sharedDirector()->getOpenGLView()) + { + EGLView *view = EGLView::sharedOpenGLView(); + view->setFrameSize(w, h); + + AppDelegate *pAppDelegate = new AppDelegate(); + Application::sharedApplication()->run(); + } + /* + else + { + ccDrawInit(); + ccGLInvalidateStateCache(); + + ShaderCache::sharedShaderCache()->reloadDefaultShaders(); + TextureCache::reloadAllTextures(); + NotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); + Director::sharedDirector()->setGLDefaultValues(); + } + */ +} + +} diff --git a/plugin/samples/HelloUser/proj.android/proguard-project.txt b/plugin/samples/HelloUser/proj.android/proguard-project.txt new file mode 100644 index 0000000000..f2fe1559a2 --- /dev/null +++ b/plugin/samples/HelloUser/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/plugin/samples/HelloUser/proj.android/project.properties b/plugin/samples/HelloUser/proj.android/project.properties new file mode 100644 index 0000000000..b93e70be46 --- /dev/null +++ b/plugin/samples/HelloUser/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-17 + +android.library.reference.1=../../../../cocos2dx/platform/android/java diff --git a/plugin/samples/HelloUser/proj.android/res/values/strings.xml b/plugin/samples/HelloUser/proj.android/res/values/strings.xml new file mode 100644 index 0000000000..35d82d2614 --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/res/values/strings.xml @@ -0,0 +1,4 @@ + + + HelloUser + diff --git a/plugin/samples/HelloUser/proj.android/src/org/cocos2dx/HelloUser/HelloUser.java b/plugin/samples/HelloUser/proj.android/src/org/cocos2dx/HelloUser/HelloUser.java new file mode 100644 index 0000000000..aeb10ef581 --- /dev/null +++ b/plugin/samples/HelloUser/proj.android/src/org/cocos2dx/HelloUser/HelloUser.java @@ -0,0 +1,52 @@ +/**************************************************************************** +Copyright (c) 2010-2011 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. +****************************************************************************/ +package org.cocos2dx.HelloUser; + +import org.cocos2dx.lib.Cocos2dxActivity; +import org.cocos2dx.lib.Cocos2dxGLSurfaceView; +import org.cocos2dx.plugin.PluginWrapper; + +import android.os.Bundle; + +public class HelloUser extends Cocos2dxActivity{ + + protected void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + + PluginWrapper.init(this); + PluginWrapper.setGLSurfaceView(Cocos2dxGLSurfaceView.getInstance()); + } + + public Cocos2dxGLSurfaceView onCreateView() { + Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this); + // HelloUser should create stencil buffer + glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8); + + return glSurfaceView; + } + + static { + System.loadLibrary("cocos2dcpp"); + } +} diff --git a/plugin/tools/config.sh b/plugin/tools/config.sh index 3f21a31b41..a6edee3084 100755 --- a/plugin/tools/config.sh +++ b/plugin/tools/config.sh @@ -2,7 +2,8 @@ export ALL_PLUGINS=("flurry" "umeng" \ "alipay" "nd91" \ "admob" \ -"twitter" "weibo") +"twitter" "weibo" \ +"qh360") # define the plugin root directory & publish target directory export TARGET_DIR_NAME="publish"