From 9ad241c8f858e209f462dc1d074c038d2b7eb3dc Mon Sep 17 00:00:00 2001 From: mustime Date: Thu, 26 Jul 2012 12:03:37 +0800 Subject: [PATCH 1/6] issue #1381: change the setContentSize interface in the subclasses of CCNode --- cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp | 2 +- cocos2dx/layers_scenes_transitions_nodes/CCLayer.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index cf0e38aa49..294ff155ce 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -500,7 +500,7 @@ bool CCLayerColor::initWithColor(const ccColor4B& color) } /// override contentSize -void CCLayerColor::setContentSize(CCSize size) +void CCLayerColor::setContentSize(CCSize & size) { m_pSquareVertices[1].x = size.width; m_pSquareVertices[2].y = size.height; diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h index df7267d6cb..0eb3f13ae1 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h @@ -225,7 +225,7 @@ public: virtual ~CCLayerColor(); virtual void draw(); - virtual void setContentSize(CCSize var); + virtual void setContentSize(CCSize & var); /** creates a CCLayer with color, width and height in Points @deprecated: This interface will be deprecated sooner or later. From bfc53e3453ab0bbd04f4c6399948ecd298389142 Mon Sep 17 00:00:00 2001 From: mustime Date: Thu, 26 Jul 2012 15:30:09 +0800 Subject: [PATCH 2/6] issue #1284: using OpenSL for playing effects on I9100 --- CocosDenshion/android/Android.mk | 23 +- CocosDenshion/android/SimpleAudioEngine.cpp | 83 ++- CocosDenshion/android/opensl/OpenSLEngine.cpp | 518 ++++++++++++++++++ CocosDenshion/android/opensl/OpenSLEngine.h | 65 +++ .../opensl/SimpleAudioEngineOpenSL.cpp | 122 +++++ .../android/opensl/SimpleAudioEngineOpenSL.h | 42 ++ .../org/cocos2dx/lib/Cocos2dxActivity.java | 10 + 7 files changed, 847 insertions(+), 16 deletions(-) create mode 100644 CocosDenshion/android/opensl/OpenSLEngine.cpp create mode 100644 CocosDenshion/android/opensl/OpenSLEngine.h create mode 100644 CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp create mode 100644 CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.h diff --git a/CocosDenshion/android/Android.mk b/CocosDenshion/android/Android.mk index e2ab457728..93b0d820fb 100644 --- a/CocosDenshion/android/Android.mk +++ b/CocosDenshion/android/Android.mk @@ -6,7 +6,26 @@ LOCAL_MODULE := cocosdenshion_static LOCAL_MODULE_FILENAME := libcocosdenshion LOCAL_SRC_FILES := SimpleAudioEngine.cpp \ -jni/SimpleAudioEngineJni.cpp + jni/SimpleAudioEngineJni.cpp + +# for OpenSLES +ifeq ($(APP_PLATFORM), android-9) + +# define # define the macro to compile through CocosDenshion/android/SimpleAudioEngine.cpp +LOCAL_CFLAGS := -DENABLE_OPENSL + +LOCAL_SRC_FILES += opensl/OpenSLEngine.cpp \ + opensl/SimpleAudioEngineOpenSL.cpp + +LOCAL_EXPORT_LDLIBS := -lOpenSLES\ + -landroid + +LOCAL_LDLIBS := -lOpenSLES +LOCAL_LDLIBS += -llog +LOCAL_LDLIBS += -landroid + +endif + LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../include @@ -14,7 +33,5 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../include \ $(LOCAL_PATH)/../../cocos2dx \ $(LOCAL_PATH)/../../cocos2dx/include \ $(LOCAL_PATH)/../../cocos2dx/platform/android - -LOCAL_LDLIBS := -llog include $(BUILD_STATIC_LIBRARY) diff --git a/CocosDenshion/android/SimpleAudioEngine.cpp b/CocosDenshion/android/SimpleAudioEngine.cpp index ebcac6cf09..1a74baac9d 100644 --- a/CocosDenshion/android/SimpleAudioEngine.cpp +++ b/CocosDenshion/android/SimpleAudioEngine.cpp @@ -25,18 +25,27 @@ THE SOFTWARE. #include "SimpleAudioEngine.h" #include "jni/SimpleAudioEngineJni.h" +/* + * for OpenSLES on Android 2.3 and over + */ +#ifdef ENABLE_OPENSL +#include "opensl/SimpleAudioEngineOpenSL.h" +#endif + + namespace CocosDenshion { static SimpleAudioEngine *s_pEngine = 0; SimpleAudioEngine::SimpleAudioEngine() { - } SimpleAudioEngine::~SimpleAudioEngine() { - +#ifdef ENABLE_OPENSL + SimpleAudioEngineOpenSL::sharedEngine()->end(); +#endif } SimpleAudioEngine* SimpleAudioEngine::sharedEngine() @@ -52,6 +61,10 @@ SimpleAudioEngine* SimpleAudioEngine::sharedEngine() void SimpleAudioEngine::end() { endJNI(); + +#ifdef ENABLE_OPENSL + SimpleAudioEngineOpenSL::sharedEngine()->end(); +#endif } void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) @@ -106,57 +119,101 @@ void SimpleAudioEngine::setBackgroundMusicVolume(float volume) float SimpleAudioEngine::getEffectsVolume() { - return getEffectsVolumeJNI(); +#ifdef ENABLE_OPENSL + return SimpleAudioEngineOpenSL::sharedEngine()->getEffectsVolume(); +#else + return getEffectsVolumeJNI(); +#endif } void SimpleAudioEngine::setEffectsVolume(float volume) { - setEffectsVolumeJNI(volume); +#ifdef ENABLE_OPENSL + SimpleAudioEngineOpenSL::sharedEngine()->setEffectsVolume(volume); +#else + setEffectsVolumeJNI(volume); +#endif } unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop) { - return playEffectJNI(pszFilePath, bLoop); +#ifdef ENABLE_OPENSL + return SimpleAudioEngineOpenSL::sharedEngine()->playEffect(pszFilePath, bLoop); +#else + return playEffectJNI(pszFilePath, bLoop); +#endif } void SimpleAudioEngine::stopEffect(unsigned int nSoundId) { - stopEffectJNI(nSoundId); +#ifdef ENABLE_OPENSL + SimpleAudioEngineOpenSL::sharedEngine()->stopEffect(nSoundId); +#else + stopEffectJNI(nSoundId); +#endif } void SimpleAudioEngine::preloadEffect(const char* pszFilePath) { - preloadEffectJNI(pszFilePath); +#ifdef ENABLE_OPENSL + SimpleAudioEngineOpenSL::sharedEngine()->preloadEffect(pszFilePath); +#else + preloadEffectJNI(pszFilePath); +#endif } void SimpleAudioEngine::unloadEffect(const char* pszFilePath) { - unloadEffectJNI(pszFilePath); +#ifdef ENABLE_OPENSL + SimpleAudioEngineOpenSL::sharedEngine()->unloadEffect(pszFilePath); +#else + unloadEffectJNI(pszFilePath); +#endif } void SimpleAudioEngine::pauseEffect(unsigned int nSoundId) { - pauseEffectJNI(nSoundId); +#ifdef ENABLE_OPENSL + SimpleAudioEngineOpenSL::sharedEngine()->pauseEffect(nSoundId); +#else + pauseEffectJNI(nSoundId); +#endif } void SimpleAudioEngine::pauseAllEffects() { - pauseAllEffectsJNI(); +#ifdef ENABLE_OPENSL + SimpleAudioEngineOpenSL::sharedEngine()->pauseAllEffects(); +#else + pauseAllEffectsJNI(); +#endif } void SimpleAudioEngine::resumeEffect(unsigned int nSoundId) { - resumeEffectJNI(nSoundId); +#ifdef ENABLE_OPENSL + SimpleAudioEngineOpenSL::sharedEngine()->resumeEffect(nSoundId); +#else + resumeEffectJNI(nSoundId); +#endif } void SimpleAudioEngine::resumeAllEffects() { - resumeAllEffectsJNI(); +#ifdef ENABLE_OPENSL + SimpleAudioEngineOpenSL::sharedEngine()->resumeAllEffects(); +#else + resumeAllEffectsJNI(); +#endif } void SimpleAudioEngine::stopAllEffects() { - stopAllEffectsJNI(); +#ifdef ENABLE_OPENSL + SimpleAudioEngineOpenSL::sharedEngine()->stopAllEffects(); +#else + stopAllEffectsJNI(); +#endif } } diff --git a/CocosDenshion/android/opensl/OpenSLEngine.cpp b/CocosDenshion/android/opensl/OpenSLEngine.cpp new file mode 100644 index 0000000000..dcad63c60f --- /dev/null +++ b/CocosDenshion/android/opensl/OpenSLEngine.cpp @@ -0,0 +1,518 @@ +#include "OpenSLEngine.h" + +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"OPENSL_ENGINE.CPP", __VA_ARGS__) + +using namespace std; + + +OpenSLEngine::OpenSLEngine() + :m_musicVolume(0), + m_effectVolume(0) +{} + +OpenSLEngine::~OpenSLEngine() +{ + closeEngine(); +} + +/********************************************************************************** + * jni + **********************************************************************************/ +#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxActivity" + +typedef struct JniMethodInfo_ +{ + JNIEnv * env; + jclass classID; + jmethodID methodID; +} JniMethodInfo; + +extern "C" { + static JNIEnv* getJNIEnv(void) + { + + JavaVM* jvm = cocos2d::JniHelper::getJavaVM(); + if (NULL == jvm) { + LOGD("Failed to get JNIEnv. JniHelper::getJavaVM() is NULL"); + return NULL; + } + + JNIEnv *env = NULL; + // get jni environment + jint ret = jvm->GetEnv((void**)&env, JNI_VERSION_1_4); + + switch (ret) { + case JNI_OK : + // Success! + return env; + + case JNI_EDETACHED : + // Thread not attached + + // TODO : If calling AttachCurrentThread() on a native thread + // must call DetachCurrentThread() in future. + // see: http://developer.android.com/guide/practices/design/jni.html + + if (jvm->AttachCurrentThread(&env, NULL) < 0) + { + LOGD("Failed to get the environment using AttachCurrentThread()"); + return NULL; + } else { + // Success : Attached and obtained JNIEnv! + return env; + } + + case JNI_EVERSION : + // Cannot recover from this error + LOGD("JNI interface version 1.4 not supported"); + default : + LOGD("Failed to get the environment using GetEnv()"); + return NULL; + } + } + + static jclass getClassID(JNIEnv *pEnv) + { + jclass ret = pEnv->FindClass(CLASS_NAME); + if (! ret) + { + LOGD("Failed to find class of %s", CLASS_NAME); + } + + return ret; + } + + static bool getStaticMethodInfo(JniMethodInfo &methodinfo, const char *methodName, const char *paramCode) + { + jmethodID methodID = 0; + JNIEnv *pEnv = 0; + bool bRet = false; + + do + { + pEnv = getJNIEnv(); + if (! pEnv) + { + break; + } + + jclass classID = getClassID(pEnv); + + methodID = pEnv->GetStaticMethodID(classID, methodName, paramCode); + if (! methodID) + { + LOGD("Failed to find static method id of %s", methodName); + break; + } + + methodinfo.classID = classID; + methodinfo.env = pEnv; + methodinfo.methodID = methodID; + + bRet = true; + } while (0); + + return bRet; + } +}; + + +/********************************************************************************* + * helper + ********************************************************************************/ +#define FILE_NOT_FOUND -1 +#define ASSET_MANAGER_GETTER "getAssetManager" + +struct AudioPlayer +{ + AAsset* Asset; + SLDataSource audioSrc; + SLObjectItf fdPlayerObject; + SLPlayItf fdPlayerPlay; + SLSeekItf fdPlayerSeek; + SLVolumeItf fdPlayerVolume; +} musicPlayer; /* for background music */ + +typedef map EffectList; +typedef pair Effect; + +static EffectList& sharedList() +{ + static EffectList s_List; + return s_List; +} + +unsigned int _Hash(const char *key) +{ + unsigned int len = strlen(key); + const char *end=key+len; + unsigned int hash; + + for (hash = 0; key < end; key++) + { + hash *= 16777619; + hash ^= (unsigned int) (unsigned char) toupper(*key); + } + return (hash); +} + +int getFileDescriptor(AudioPlayer * player, const char * filename, off_t & start, off_t & length) +{ + JniMethodInfo methodInfo; + if (! getStaticMethodInfo(methodInfo, ASSET_MANAGER_GETTER, "()Landroid/content/res/AssetManager;")) + { + return FILE_NOT_FOUND; + } + jobject assetManager = methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID); + methodInfo.env->DeleteLocalRef(methodInfo.classID); + + AAssetManager* mgr = AAssetManager_fromJava(methodInfo.env, assetManager); + assert(NULL != mgr); + player->Asset = AAssetManager_open(mgr, filename, AASSET_MODE_UNKNOWN); + + if (player->Asset == NULL) + { + LOGD("file not found! Stop preload file: %s", filename); + return FILE_NOT_FOUND; + } + + // open asset as file descriptor + int fd = AAsset_openFileDescriptor(player->Asset, &start, &length); + assert(0 <= fd); + AAsset_close(player->Asset); + + return fd; +} + + +/********************************************************************************** + * engine + **********************************************************************************/ +static SLObjectItf engineObject = NULL; +static SLEngineItf engineEngine; +static SLObjectItf outputMixObject = NULL; + +bool initAudioPlayer(AudioPlayer* player, const char* filename) +{ + // configure audio sink + SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject}; + SLDataSink audioSnk = {&loc_outmix, NULL}; + + // configure audio source + off_t start, length; + int fd = getFileDescriptor(player, filename, start, length); + if (FILE_NOT_FOUND == fd) + { + return false; + } + SLDataLocator_AndroidFD loc_fd = {SL_DATALOCATOR_ANDROIDFD, fd, start, length}; + SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED}; + (player->audioSrc) = {&loc_fd, &format_mime}; + + // create audio player + const SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_MUTESOLO, SL_IID_VOLUME}; + const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}; + + SLresult result = (*engineEngine)->CreateAudioPlayer(engineEngine, &(player->fdPlayerObject), &(player->audioSrc), &audioSnk, 3, ids, req); + assert(SL_RESULT_SUCCESS == result); + + // realize the player + result = (*(player->fdPlayerObject))->Realize(player->fdPlayerObject, SL_BOOLEAN_FALSE); + assert(SL_RESULT_SUCCESS == result); + + // get the play interface + result = (*(player->fdPlayerObject))->GetInterface(player->fdPlayerObject, SL_IID_PLAY, &(player->fdPlayerPlay)); + assert(SL_RESULT_SUCCESS == result); + + // get the volume interface + result = (*(player->fdPlayerObject))->GetInterface(player->fdPlayerObject, SL_IID_VOLUME, &(player->fdPlayerVolume)); + assert(SL_RESULT_SUCCESS == result); + + // get the volume interface + result = (*(player->fdPlayerObject))->GetInterface(player->fdPlayerObject, SL_IID_SEEK, &(player->fdPlayerSeek)); + assert(SL_RESULT_SUCCESS == result); + + return true; +} + +void destroyAudioPlayer(AudioPlayer * player) +{ + if (player->fdPlayerObject != NULL){ + (*(player->fdPlayerObject))->Destroy(player->fdPlayerObject); + player->fdPlayerObject = NULL; + player->fdPlayerPlay = NULL; + player->fdPlayerSeek = NULL; + player->fdPlayerVolume = NULL; + } +} + +void OpenSLEngine::createEngine() +{ + SLresult result; + LOGD("createEngine"); + + // create engine + if (engineObject == NULL) + { + result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL); + assert(SL_RESULT_SUCCESS == result); + + // realize the engine + result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE); + assert(SL_RESULT_SUCCESS == result); + + // get the engine interface, which is needed in order to create other objects + result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine); + assert(SL_RESULT_SUCCESS == result); + + // create output mix + const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB}; + const SLboolean req[1] = {SL_BOOLEAN_FALSE}; + result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, ids, req); + assert(SL_RESULT_SUCCESS == result); + } + + // realize the output mix + result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE); + assert(SL_RESULT_SUCCESS == result); +} + +void OpenSLEngine::closeEngine() +{ + // destroy background players + destroyAudioPlayer(&musicPlayer); + + // destroy effect players + EffectList::iterator p = sharedList().begin(); + while (p != sharedList().end()) + { + destroyAudioPlayer(p->second); + p++; + } + sharedList().clear(); + + // destroy output mix interface + if (outputMixObject != NULL) { + (*outputMixObject)->Destroy(outputMixObject); + outputMixObject = NULL; + } + + // destroy opensl engine + if (engineObject != NULL) { + (*engineObject)->Destroy(engineObject); + engineObject = NULL; + engineEngine = NULL; + } +} + + +/********************************************************************************** + * background music + **********************************************************************************/ +bool OpenSLEngine::preloadBackgroundMusic(const char * filename) +{ + if (musicPlayer.Asset != NULL) + { + if (musicPlayer.fdPlayerPlay != NULL) + { + SLresult result = (*(musicPlayer.fdPlayerPlay))->SetPlayState(musicPlayer.fdPlayerPlay, SL_PLAYSTATE_STOPPED); + assert(SL_RESULT_SUCCESS == result); + } + free(musicPlayer.Asset); + musicPlayer.Asset = NULL; + } + + return initAudioPlayer(&musicPlayer, filename); +} + +void OpenSLEngine::setBackgroundMusicState(int state) +{ + SLresult result; + if (NULL != musicPlayer.fdPlayerPlay) { + result = (*(musicPlayer.fdPlayerPlay))->SetPlayState(musicPlayer.fdPlayerPlay, state); + assert(SL_RESULT_SUCCESS == result); + } +} + +int OpenSLEngine::getBackgroundMusicState() +{ + SLresult result; + SLuint32 state; + if (musicPlayer.fdPlayerPlay != NULL) + { + result = (*(musicPlayer.fdPlayerPlay))->GetPlayState(musicPlayer.fdPlayerPlay, &state); + assert(result == SL_RESULT_SUCCESS); + } + + return (int)state; +} + +void OpenSLEngine::rewindBackgroundMusic() +{ + SLresult result; + + result = (*(musicPlayer.fdPlayerSeek))->SetPosition(musicPlayer.fdPlayerSeek, 0, SL_SEEKMODE_FAST); + assert(SL_RESULT_SUCCESS == result); + + result = (*(musicPlayer.fdPlayerPlay))->SetPlayState(musicPlayer.fdPlayerPlay, SL_PLAYSTATE_PLAYING); + assert(SL_RESULT_SUCCESS == result); +} + +void OpenSLEngine::setBackgroundMusicLooping(bool isLooping) +{ + SLresult result; + if (NULL != musicPlayer.fdPlayerSeek) + { + result = (*(musicPlayer.fdPlayerSeek))->SetLoop(musicPlayer.fdPlayerSeek, (SLboolean) isLooping, 0, SL_TIME_UNKNOWN); + assert(SL_RESULT_SUCCESS == result); + } +} + +void OpenSLEngine::setBackgroundVolume(int volume) +{ + m_musicVolume = volume; + + SLresult result; + if (NULL != musicPlayer.fdPlayerVolume) { + result = (*(musicPlayer.fdPlayerVolume))->SetVolumeLevel(musicPlayer.fdPlayerVolume, m_musicVolume); + assert(SL_RESULT_SUCCESS == result); + } +} + +int OpenSLEngine::getBackgroundVolume() +{ + return m_musicVolume; +} + + +/********************************************************************************** + * sound effect + **********************************************************************************/ +#define NORMAL_EFFECT_CALLBACK 1 +#define REPLAY_EFFECT_CALLBACK 2 + +//typedef struct _CallbackContext +//{ +// int type; +// AudioPlayer* player; +//} CallbackContext; +// +//void effectPlayCallback(SLPlayItf caller, void * context, SLuint32 event) +//{ +// CallbackContext* callback = (CallbackContext*)context; +// switch(callback->type) +// { +// case NORMAL_EFFECT_CALLBACK : +// // TO-DO +// break; +// case REPLAY_EFFECT_CALLBACK : +// destroyAudioPlayer(callback->player); +// break; +// default: +// break; +// } +//} + +void setSingleEffectState(AudioPlayer * player, int state) +{ + SLresult result; + if (player->fdPlayerPlay != NULL){ + result = (*(player->fdPlayerPlay))->SetPlayState(player->fdPlayerPlay, state); + assert(SL_RESULT_SUCCESS == result); + } +} + +unsigned int OpenSLEngine::preloadEffect(const char * filename) +{ + unsigned int nID = _Hash(filename); + // if already exists + if (sharedList().find(nID) != sharedList().end()) + { + /*AudioPlayer* newPlayer = new AudioPlayer(sharedList()[nID]); + CallbackContext* context = new CallbackContext(); + context->player = newPlayer; + context-> + (newPlayer->fdPlayerPlay)->RegisterCallback(newPlayer->fdPlayerPlay, effectPlayCallback, );*/ + return nID; + } + + AudioPlayer* player = new AudioPlayer(); + if (!initAudioPlayer(player, filename)) + { + free(player); + return FILE_NOT_FOUND; + } + + // set the new player's volume as others' + SLresult result; + result = (*(player->fdPlayerVolume))->SetVolumeLevel(player->fdPlayerVolume, m_effectVolume); + assert(result == SL_RESULT_SUCCESS); + + sharedList().insert(Effect(nID, player)); + return nID; +} + +void OpenSLEngine::unloadEffect(const char * filename) +{ + unsigned int nID = _Hash(filename); + + EffectList::iterator p = sharedList().find(nID); + if (p != sharedList().end()) + { + destroyAudioPlayer(p->second); + sharedList().erase(nID); + + LOGD("effect unloaded"); + } +} + +void OpenSLEngine::setEffectState(unsigned int effectID, int state) +{ + EffectList::iterator p = sharedList().find(effectID); + if (p != sharedList().end()) + { + setSingleEffectState(p->second, state); + } +} + +void OpenSLEngine::setAllEffectState(int state) +{ + EffectList::iterator iter; + for (iter = sharedList().begin(); iter != sharedList().end(); iter++) + { + setSingleEffectState(iter->second, state); + } +} + +void OpenSLEngine::setEffectLooping(unsigned int effectID, bool isLooping) +{ + SLresult result; + AudioPlayer * player = sharedList()[effectID]; + assert(player != NULL); + + if (NULL != player->fdPlayerSeek) + { + result = (*(player->fdPlayerSeek))->SetLoop(player->fdPlayerSeek, (SLboolean) isLooping, 0, SL_TIME_UNKNOWN); + assert(SL_RESULT_SUCCESS == result); + } +} + +void OpenSLEngine::setEffectsVolume(int volume) +{ + m_effectVolume = volume; + + SLresult result; + EffectList::iterator iter; + AudioPlayer * player; + for (iter = sharedList().begin() ; iter != sharedList().end() ; ++ iter) + { + player = iter->second; + result = (*(player->fdPlayerVolume))->SetVolumeLevel(player->fdPlayerVolume, m_effectVolume); + assert(SL_RESULT_SUCCESS == result); + } +} + +int OpenSLEngine::getEffectsVolume() +{ + return m_effectVolume; +} \ No newline at end of file diff --git a/CocosDenshion/android/opensl/OpenSLEngine.h b/CocosDenshion/android/opensl/OpenSLEngine.h new file mode 100644 index 0000000000..817ef9df6c --- /dev/null +++ b/CocosDenshion/android/opensl/OpenSLEngine.h @@ -0,0 +1,65 @@ +#ifndef _OPENSL_ENGINE_H_ +#define _OPENSL_ENGINE_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +class OpenSLEngine +{ +public: + OpenSLEngine(); + ~OpenSLEngine(); + + void createEngine(); + + void closeEngine(); + + + bool preloadBackgroundMusic(const char * filename); + + void setBackgroundMusicState(int state); + + int getBackgroundMusicState(); + + void rewindBackgroundMusic(); + + void setBackgroundMusicLooping(bool isLooping); + + void setBackgroundVolume(int volume); + + int getBackgroundVolume(); + + + unsigned int preloadEffect(const char * filename); + + void unloadEffect(const char * filename); + + void setEffectState(unsigned int effectID, int state); + + void setAllEffectState(int state); + + void setEffectLooping(unsigned int effectID, bool isLooping); + + void setEffectsVolume(int volume); + + int getEffectsVolume(); + +private: + SLmillibel m_musicVolume; + SLmillibel m_effectVolume; + +}; + +#endif \ No newline at end of file diff --git a/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp b/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp new file mode 100644 index 0000000000..03983e6a4e --- /dev/null +++ b/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp @@ -0,0 +1,122 @@ +#include "SimpleAudioEngineOpenSL.h" + +#define PLAYSTATE_STOPPED 1 +#define PLAYSTATE_PAUSED 2 +#define PLAYSTATE_PLAYING 3 +#define FILE_NOT_FOUND -1 + +OpenSLEngine * s_pOpenSL = 0; +static SimpleAudioEngineOpenSL * s_pEngine = 0; + +SimpleAudioEngineOpenSL::SimpleAudioEngineOpenSL() +{ +} + +SimpleAudioEngineOpenSL::~SimpleAudioEngineOpenSL() +{ + if (s_pOpenSL) + { + s_pOpenSL->closeEngine(); + + delete s_pOpenSL; + s_pOpenSL = 0; + } +} + +bool SimpleAudioEngineOpenSL::initEngine() +{ + bool bRet = false; + do + { + s_pOpenSL = new OpenSLEngine(); + s_pOpenSL->createEngine(); + + bRet = true; + } while (0); + return bRet; +} + +SimpleAudioEngineOpenSL* SimpleAudioEngineOpenSL::sharedEngine() +{ + if (! s_pEngine) + { + s_pEngine = new SimpleAudioEngineOpenSL(); + s_pEngine->initEngine(); + } + return s_pEngine; +} + +void SimpleAudioEngineOpenSL::end() +{ + if (s_pOpenSL) + { + s_pOpenSL->closeEngine(); + } +} + +float SimpleAudioEngineOpenSL::getEffectsVolume() +{ + // TODO + return 1.0f; +} + +void SimpleAudioEngineOpenSL::setEffectsVolume(float volume) +{ + int attenuation = (1 - volume) * 100; + int millibel = attenuation * -15; + s_pOpenSL->setEffectsVolume(millibel); +} + +unsigned int SimpleAudioEngineOpenSL::playEffect(const char* pszFilePath, bool bLoop) +{ + unsigned int soundID = s_pOpenSL->preloadEffect(pszFilePath); + if (soundID != FILE_NOT_FOUND) + { + s_pOpenSL->setEffectState(soundID, PLAYSTATE_STOPPED); + s_pOpenSL->setEffectLooping(soundID, bLoop); + s_pOpenSL->setEffectState(soundID, PLAYSTATE_PLAYING); + } + + return soundID; +} + +void SimpleAudioEngineOpenSL::pauseEffect(unsigned int nSoundId) +{ + s_pOpenSL->setEffectState(nSoundId, PLAYSTATE_PAUSED); +} + +void SimpleAudioEngineOpenSL::pauseAllEffects() +{ + s_pOpenSL->setAllEffectState(PLAYSTATE_PAUSED); +} + +void SimpleAudioEngineOpenSL::resumeEffect(unsigned int nSoundId) +{ + s_pOpenSL->setEffectState(nSoundId, PLAYSTATE_PLAYING); +} + +void SimpleAudioEngineOpenSL::resumeAllEffects() +{ + s_pOpenSL->setAllEffectState(PLAYSTATE_PLAYING); +} + +void SimpleAudioEngineOpenSL::stopEffect(unsigned int nSoundId) +{ + s_pOpenSL->setEffectState(nSoundId, PLAYSTATE_STOPPED); +} + +void SimpleAudioEngineOpenSL::stopAllEffects() +{ + s_pOpenSL->setAllEffectState(PLAYSTATE_STOPPED); +} + +void SimpleAudioEngineOpenSL::preloadEffect(const char* pszFilePath) +{ + s_pOpenSL->preloadEffect(pszFilePath); +} + +void SimpleAudioEngineOpenSL::unloadEffect(const char* pszFilePath) +{ + s_pOpenSL->unloadEffect(pszFilePath); +} + diff --git a/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.h b/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.h new file mode 100644 index 0000000000..58289e87c5 --- /dev/null +++ b/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.h @@ -0,0 +1,42 @@ +#ifndef _SIMPLE_AUDIO_ENGINE_OPENSL_H_ +#define _SIMPLE_AUDIO_ENGINE_OPENSL_H_ + +#include "OpenSLEngine.h" + +class SimpleAudioEngineOpenSL +{ +public: + SimpleAudioEngineOpenSL(); + ~SimpleAudioEngineOpenSL(); + + bool initEngine(); + + static SimpleAudioEngineOpenSL* sharedEngine(); + + static void end(); + + float getEffectsVolume(); + + void setEffectsVolume(float volume); + + unsigned int playEffect(const char* pszFilePath, bool bLoop = false); + + void pauseEffect(unsigned int nSoundId); + + void pauseAllEffects(); + + void resumeEffect(unsigned int nSoundId); + + void resumeAllEffects(); + + void stopEffect(unsigned int nSoundId); + + void stopAllEffects(); + + void preloadEffect(const char* pszFilePath); + + void unloadEffect(const char* pszFilePath); +}; + + +#endif diff --git a/cocos2dx/platform/android/java/src_common/org/cocos2dx/lib/Cocos2dxActivity.java b/cocos2dx/platform/android/java/src_common/org/cocos2dx/lib/Cocos2dxActivity.java index c5b65df061..1418545197 100644 --- a/cocos2dx/platform/android/java/src_common/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/cocos2dx/platform/android/java/src_common/org/cocos2dx/lib/Cocos2dxActivity.java @@ -31,6 +31,7 @@ import android.content.DialogInterface; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; +import android.content.res.AssetManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -41,6 +42,7 @@ public class Cocos2dxActivity extends Activity{ private static Cocos2dxMusic backgroundMusicPlayer; private static Cocos2dxSound soundPlayer; private static Cocos2dxAccelerometer accelerometer; + private static AssetManager assetManager; private static boolean accelerometerEnabled = false; private static Handler handler; private final static int HANDLER_SHOW_DIALOG = 1; @@ -61,6 +63,9 @@ public class Cocos2dxActivity extends Activity{ backgroundMusicPlayer = new Cocos2dxMusic(this); soundPlayer = new Cocos2dxSound(this); + // init asset manager for jni call + assetManager = getAssets(); + // init bitmap context Cocos2dxBitmap.setContext(this); @@ -75,6 +80,11 @@ public class Cocos2dxActivity extends Activity{ }; } + + public static AssetManager getAssetManager() { + return assetManager; + } + public static String getCurrentLanguage() { String languageName = java.util.Locale.getDefault().getLanguage(); return languageName; From 2e335f6deed9247e68746bf28d0951da9cf67f77 Mon Sep 17 00:00:00 2001 From: mustime Date: Thu, 26 Jul 2012 16:22:23 +0800 Subject: [PATCH 3/6] issue #1284: changed some names to make the code more readable --- CocosDenshion/android/opensl/OpenSLEngine.cpp | 37 +++++++++---------- .../opensl/SimpleAudioEngineOpenSL.cpp | 2 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/CocosDenshion/android/opensl/OpenSLEngine.cpp b/CocosDenshion/android/opensl/OpenSLEngine.cpp index dcad63c60f..e22bde719e 100644 --- a/CocosDenshion/android/opensl/OpenSLEngine.cpp +++ b/CocosDenshion/android/opensl/OpenSLEngine.cpp @@ -188,14 +188,14 @@ int getFileDescriptor(AudioPlayer * player, const char * filename, off_t & start /********************************************************************************** * engine **********************************************************************************/ -static SLObjectItf engineObject = NULL; -static SLEngineItf engineEngine; -static SLObjectItf outputMixObject = NULL; +static SLObjectItf g_pEngineObject = NULL; +static SLEngineItf g_pEngineEngine = NULL; +static SLObjectItf g_pOutputMixObject = NULL; bool initAudioPlayer(AudioPlayer* player, const char* filename) { // configure audio sink - SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject}; + SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, g_pOutputMixObject}; SLDataSink audioSnk = {&loc_outmix, NULL}; // configure audio source @@ -212,8 +212,7 @@ bool initAudioPlayer(AudioPlayer* player, const char* filename) // create audio player const SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_MUTESOLO, SL_IID_VOLUME}; const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}; - - SLresult result = (*engineEngine)->CreateAudioPlayer(engineEngine, &(player->fdPlayerObject), &(player->audioSrc), &audioSnk, 3, ids, req); + SLresult result = (*g_pEngineEngine)->CreateAudioPlayer(g_pEngineEngine, &(player->fdPlayerObject), &(player->audioSrc), &audioSnk, 3, ids, req); assert(SL_RESULT_SUCCESS == result); // realize the player @@ -252,28 +251,28 @@ void OpenSLEngine::createEngine() LOGD("createEngine"); // create engine - if (engineObject == NULL) + if (g_pEngineObject == NULL) { - result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL); + result = slCreateEngine(&g_pEngineObject, 0, NULL, 0, NULL, NULL); assert(SL_RESULT_SUCCESS == result); // realize the engine - result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE); + result = (*g_pEngineObject)->Realize(g_pEngineObject, SL_BOOLEAN_FALSE); assert(SL_RESULT_SUCCESS == result); // get the engine interface, which is needed in order to create other objects - result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine); + result = (*g_pEngineObject)->GetInterface(g_pEngineObject, SL_IID_ENGINE, &g_pEngineEngine); assert(SL_RESULT_SUCCESS == result); // create output mix const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB}; const SLboolean req[1] = {SL_BOOLEAN_FALSE}; - result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, ids, req); + result = (*g_pEngineEngine)->CreateOutputMix(g_pEngineEngine, &g_pOutputMixObject, 1, ids, req); assert(SL_RESULT_SUCCESS == result); } // realize the output mix - result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE); + result = (*g_pOutputMixObject)->Realize(g_pOutputMixObject, SL_BOOLEAN_FALSE); assert(SL_RESULT_SUCCESS == result); } @@ -292,16 +291,16 @@ void OpenSLEngine::closeEngine() sharedList().clear(); // destroy output mix interface - if (outputMixObject != NULL) { - (*outputMixObject)->Destroy(outputMixObject); - outputMixObject = NULL; + if (g_pOutputMixObject != NULL) { + (*g_pOutputMixObject)->Destroy(g_pOutputMixObject); + g_pOutputMixObject = NULL; } // destroy opensl engine - if (engineObject != NULL) { - (*engineObject)->Destroy(engineObject); - engineObject = NULL; - engineEngine = NULL; + if (g_pEngineObject != NULL) { + (*g_pEngineObject)->Destroy(g_pEngineObject); + g_pEngineObject = NULL; + g_pEngineEngine = NULL; } } diff --git a/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp b/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp index 03983e6a4e..1aa3981407 100644 --- a/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp +++ b/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp @@ -5,7 +5,7 @@ #define PLAYSTATE_PLAYING 3 #define FILE_NOT_FOUND -1 -OpenSLEngine * s_pOpenSL = 0; +static OpenSLEngine * s_pOpenSL = 0; static SimpleAudioEngineOpenSL * s_pEngine = 0; SimpleAudioEngineOpenSL::SimpleAudioEngineOpenSL() From 5dc604ebfc8c859c6277899a48fff69af156e9a3 Mon Sep 17 00:00:00 2001 From: Caidongyang Date: Thu, 26 Jul 2012 16:25:54 +0800 Subject: [PATCH 4/6] Issue #1404 Add automatic testing tools --- tools/JenkinsScript/androidtest-debug3.bat | 123 ++++++++++++++++ tools/JenkinsScript/androidtest-debug4.bat | 98 +++++++++++++ tools/JenkinsScript/androidtest-release3.bat | 147 +++++++++++++++++++ tools/JenkinsScript/androidtest-release4.bat | 109 ++++++++++++++ tools/JenkinsScript/androidtestcommon.bat | 89 +++++++++++ tools/JenkinsScript/ant.properties | 20 +++ tools/JenkinsScript/build.xml | 95 ++++++++++++ tools/JenkinsScript/rootconfig.sh | 35 +++++ 8 files changed, 716 insertions(+) create mode 100644 tools/JenkinsScript/androidtest-debug3.bat create mode 100644 tools/JenkinsScript/androidtest-debug4.bat create mode 100644 tools/JenkinsScript/androidtest-release3.bat create mode 100644 tools/JenkinsScript/androidtest-release4.bat create mode 100644 tools/JenkinsScript/androidtestcommon.bat create mode 100644 tools/JenkinsScript/ant.properties create mode 100644 tools/JenkinsScript/build.xml create mode 100644 tools/JenkinsScript/rootconfig.sh diff --git a/tools/JenkinsScript/androidtest-debug3.bat b/tools/JenkinsScript/androidtest-debug3.bat new file mode 100644 index 0000000000..f626f36f95 --- /dev/null +++ b/tools/JenkinsScript/androidtest-debug3.bat @@ -0,0 +1,123 @@ +::This script is used to finish a android automated compiler. +::You should make sure have finished the environment setting. +::Here are the environment variables you should set. +::%ANT_HOME% %ANDROID_HOME% %JAVA_HOME% %CYGWIN% %GITBIN% %ANDROID_NDK% +:: Don't change it until you know what you do. + +::In order to prevent the problem of permission,make the current user group get the ownership of project. +::Set the current user name. +::set _USERNAME= + +set _PROJECTNAME=TestCpp +cd ..\.. + +:project +::Copy build Configuration files to target directory +copy %cd%\tools\JenkinsScript\ant.properties %cd%\samples\%_PROJECTNAME%\proj.android +copy %cd%\tools\JenkinsScript\build.xml %cd%\samples\%_PROJECTNAME%\proj.android +copy %cd%\tools\JenkinsScript\rootconfig.sh %cd%\samples\%_PROJECTNAME%\proj.android + +::Modify the configuration files +cd samples\%_PROJECTNAME%\proj.android +rootconfig.sh %_PROJECTNAME% +cd .. +set _PROJECTLOCATION=%cd% + +::A command line that make the current user get the ownrship of project. +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F + +::Use cygwin compile the source code. +cygpath "%_PROJECTLOCATION%\proj.android\build_native.sh"|call %CYGWIN%\Cygwin.bat + +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F +::echo "%_PROJECTION%/proj.android/build_native.sh"|call %CYGWIN%\Cygwin.bat + +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F +call android update project -p proj.android +cd proj.android + +::Make sure the original android build target is android-8 +for /f "delims=" %%a in ('findstr /i "target=android-" ant.properties') do set xx=%%a +echo %xx% +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="%xx%" (echo/target=android-8)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +::Android ant build(debug,API level:8). +call ant debug +set result8=%ERROELEVEL% + +::Change API level.(API level:10) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-8" (echo/target=android-10)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +::Android ant build(debug,API level:10). +call ant debug +set result10=%ERRORLEVEL% + +::Change API level.(API level:11) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-10" (echo/target=android-11)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +::Android ant build(debug,API level:11). +call ant debug +set result11=%ERRORlEVEL% + +::Change API level.(API level:12) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-11" (echo/target=android-12)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +::Android ant build(debug,API level:12). +call ant debug +set result12=%ERRORLEVEL% + +::Change API level.(API level:13) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-12" (echo/target=android-13)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +::Android ant build(debug,API level:13). +call ant debug +set result13=%ERRORLEVEL% + +::After all test versions completed,changed current API level to the original.(API level:8) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-13" (echo/target=android-8)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +::Calculate the errorlevel and change build target. +cd ..\..\.. +IF "%_PROJECTNAME%"=="TestCpp" set /a testresult1=(result8+result10+result11+result12+result13) && set _PROJECTNAME=HelloCpp&& goto project +IF "%_PROJECTNAME%"=="HelloCpp" set /a testresult2=(result8+result10+result11+result12+result13) && set _PROJECTNAME=HelloLua&& goto project +IF "%_PROJECTNAME%"=="HelloLua" set /a testresult3=(result8+result10+result11+result12+result13) +set /a testresult=(testresult1+result2+result3) +IF %testresult% NEQ 0 goto error + +goto success + +:error +echo Error. +git checkout -f|%GITBIN%\sh.exe --login +git clean -df -x|%GITBIN%\sh.exe --login +pause +exit 1 +goto end + +:success +echo Success. +git checkout -f|%GITBIN%\sh.exe --login +git clean -df|%GITBIN%\sh.exe --login +pause +exit 0 +goto end + +::End. \ No newline at end of file diff --git a/tools/JenkinsScript/androidtest-debug4.bat b/tools/JenkinsScript/androidtest-debug4.bat new file mode 100644 index 0000000000..4e07b7479b --- /dev/null +++ b/tools/JenkinsScript/androidtest-debug4.bat @@ -0,0 +1,98 @@ +::This script is used to finish a android automated compiler. +::You should make sure have finished the environment setting. +::Here are the environment variables you should set. +::%ANT_HOME% %ANDROID_HOME% %JAVA_HOME% %CYGWIN% %GITBIN% %ANDROID_NDK% +:: Don't change it until you know what you do. + +::In order to prevent the problem of permission,make the current user group get the ownership of project. +::Set the current user name. +::set _USERNAME= + +set _PROJECTNAME=TestCpp +cd ..\.. + +:project +::Copy build Configuration files to target directory +copy %cd%\tools\JenkinsScript\ant.properties %cd%\samples\%_PROJECTNAME%\proj.android +copy %cd%\tools\JenkinsScript\build.xml %cd%\samples\%_PROJECTNAME%\proj.android +copy %cd%\tools\JenkinsScript\rootconfig.sh %cd%\samples\%_PROJECTNAME%\proj.android + +::Modify the configuration files +cd samples\%_PROJECTNAME%\proj.android +rootconfig.sh %_PROJECTNAME% +cd .. +set _PROJECTLOCATION=%cd% + +::A command line that make the current user get the ownrship of project. +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F + +::Use cygwin compile the source code. +cygpath "%_PROJECTLOCATION%\proj.android\build_native.sh"|call %CYGWIN%\Cygwin.bat + +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F +::echo "%_PROJECTION%/proj.android/build_native.sh"|call %CYGWIN%\Cygwin.bat + +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F +call android update project -p proj.android +cd proj.android + +for /f "delims=" %%a in ('findstr /i "target=android-" ant.properties') do set xx=%%a +echo %xx% +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="%xx%" (echo/target=android-8)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +::Change API level.(API level:14) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-8" (echo/target=android-14)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +::Android ant build(debug,API level:14). +call ant debug +set result14=%ERRORLEVEL% + +::Change API level.(API level:15) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-14" (echo/target=android-15)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +::Android ant build(debug,API level:15). +call ant debug +set result15=%ERRORLEVEL% + +::After all test versions completed,changed current API level to the original.(API level:8) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-15" (echo/target=android-8)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +::Calculate the errorlevel and change build target. +cd ../../.. +IF "%_PROJECTNAME%"=="TestCpp" set /a testresult1=(result8+result10+result11+result12+result13) && set _PROJECTNAME=HelloCpp&& goto project +IF "%_PROJECTNAME%"=="HelloCpp" set /a testresult2=(result8+result10+result11+result12+result13) && set _PROJECTNAME=HelloLua&& goto project +IF "%_PROJECTNAME%"=="HelloLua" set /a testresult3=(result8+result10+result11+result12+result13) +set /a testresult=(testresult1+result2+result3) +IF %testresult% NEQ 0 goto error + +goto success + +:error +echo Error. +git checkout -f|%GITBIN%\sh.exe --login +git clean -df -x|%GITBIN%\sh.exe --login +pause +exit 1 +goto end + +:success +echo Success. +git checkout -f|%GITBIN%\sh.exe --login +git clean -df|%GITBIN%\sh.exe --login +pause +exit 0 +goto end + +::End. \ No newline at end of file diff --git a/tools/JenkinsScript/androidtest-release3.bat b/tools/JenkinsScript/androidtest-release3.bat new file mode 100644 index 0000000000..f84cb65cf7 --- /dev/null +++ b/tools/JenkinsScript/androidtest-release3.bat @@ -0,0 +1,147 @@ +::This script is used to finish a android automated compiler. +::You should make sure have finished the environment setting. +::Here are the environment variables you should set. +::%ANT_HOME% %ANDROID_HOME% %JAVA_HOME% %CYGWIN% %GITBIN% %ANDROID_NDK% +:: Don't change it until you know what you do. + +::In order to prevent the problem of permission,make the current user group get the ownership of project. +::Set the current user name. +::set _USERNAME= + +set _PROJECTNAME=TestCpp +cd ..\.. + +:project +::Copy build Configuration files to target directory +copy %cd%\tools\JenkinsScript\ant.properties %cd%\samples\%_PROJECTNAME%\proj.android +copy %cd%\tools\JenkinsScript\build.xml %cd%\samples\%_PROJECTNAME%\proj.android +copy %cd%\tools\JenkinsScript\rootconfig.sh %cd%\samples\%_PROJECTNAME%\proj.android + +::Modify the configuration files +cd samples\%_PROJECTNAME%\proj.android +rootconfig.sh %_PROJECTNAME% +cd .. +set _PROJECTLOCATION=%cd% + +::A command line that make the current user get the ownrship of project. +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F + +::Use cygwin compile the source code. +cygpath "%_PROJECTLOCATION%\proj.android\build_native.sh"|call %CYGWIN%\Cygwin.bat + +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F +::echo "%_PROJECTION%/proj.android/build_native.sh"|call %CYGWIN%\Cygwin.bat + +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F +call android update project -p proj.android +cd proj.android + +::Make sure the original android build target is android-8 +for /f "delims=" %%a in ('findstr /i "target=android-" ant.properties') do set xx=%%a +echo %xx% +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="%xx%" (echo/target=android-8)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +for /f "delims=" %%a in (ant.properties) do set num=%%a&call :lis +move ant1.properties ant.properties + +::Android ant build(release,API level:8). +call ant release +set result8=%ERRORLEVEL% + +::Change API level.(API level:10) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-8" (echo/target=android-10)else echo/%%a +)>>"ant1.properties" +move ant1.properties ant.properties + +for /f "delims=" %%a in (ant.properties) do set num=%%a&call :lis +move ant1.properties ant.properties + +::Android ant build(release,API level:10). +call ant release +set result10=%ERRORLEVEL% + +::Change API level.(API level:11) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-10" (echo/target=android-11)else echo/%%a +)>>"ant1.properties" +move ant1.properties ant.properties + +for /f "delims=" %%a in (ant.properties) do set num=%%a&call :lis +move ant1.properties ant.properties + +::Android ant build(release,API level:11). +call ant release +set result11=%ERRORLEVEL% + +::Change API level.(API level:12) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-11" (echo/target=android-12)else echo/%%a +)>>"ant1.properties" +move ant1.properties ant.properties + +for /f "delims=" %%a in (ant.properties) do set num=%%a&call :lis +move ant1.properties ant.properties + +::Android ant build(release,API level:12). +call ant release +set result12=%ERRORLEVEL% + +::Change API level.(API level:13) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-12" (echo/target=android-13)else echo/%%a +)>>"ant1.properties" +move ant1.properties ant.properties + +for /f "delims=" %%a in (ant.properties) do set num=%%a&call :lis +move ant1.properties ant.properties + +::Android ant build(release,API level:13). +call ant release +set result13=%ERRORLEVEL% + +::After all test versions completed,changed current API level to the original.(API level:8) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-13" (echo/target=android-8)else echo/%%a +)>>"ant1.properties" +move ant1.properties ant.properties + +for /f "delims=" %%a in (ant.properties) do set num=%%a&call :lis +move ant1.properties ant.properties + +::Calculate the errorlevel and change build target. +cd ..\..\.. +IF "%_PROJECTNAME%"=="TestCpp" set /a testresult1=(result8+result10+result11+result12+result13) && set _PROJECTNAME=HelloCpp&& goto project +IF "%_PROJECTNAME%"=="HelloCpp" set /a testresult2=(result8+result10+result11+result12+result13) && set _PROJECTNAME=HelloLua&& goto project +IF "%_PROJECTNAME%"=="HelloLua" set /a testresult3=(result8+result10+result11+result12+result13) +set /a testresult=(testresult1+result2+result3) +IF %testresult% NEQ 0 goto error + +goto success + +:lis +if "%num%"=="" goto :eof +if "%num:~-1%"==" " set num=%num:~0,-1%&goto lis +echo %num%>>ant1.properties +goto :eof + +:error +echo Error. +git checkout -f|%GITBIN%\sh.exe --login +git clean -df|%GITBIN%\sh.exe --login +pause +exit 1 +goto end + +:success +echo Success. +git checkout -f|%GITBIN%\sh.exe --login +git clean -df|%GITBIN%\sh.exe --login +pause +exit 0 +goto end + +::End. \ No newline at end of file diff --git a/tools/JenkinsScript/androidtest-release4.bat b/tools/JenkinsScript/androidtest-release4.bat new file mode 100644 index 0000000000..b9f1f37ca8 --- /dev/null +++ b/tools/JenkinsScript/androidtest-release4.bat @@ -0,0 +1,109 @@ +::This script is used to finish a android automated compiler. +::You should make sure have finished the environment setting. +::Here are the environment variables you should set. +::%ANT_HOME% %ANDROID_HOME% %JAVA_HOME% %CYGWIN% %GITBIN% %ANDROID_NDK% +:: Don't change it until you know what you do. + +::In order to prevent the problem of permission,make the current user group get the ownership of project. +::Set the current user name. +::set _USERNAME= + +set _PROJECTNAME=TestCpp +cd ..\.. + +:project +copy %cd%\tools\JenkinsScript\ant.properties %cd%\samples\%_PROJECTNAME%\proj.android +copy %cd%\tools\JenkinsScript\build.xml %cd%\samples\%_PROJECTNAME%\proj.android +copy %cd%\tools\JenkinsScript\rootconfig.sh %cd%\samples\%_PROJECTNAME%\proj.android +cd samples\%_PROJECTNAME%\proj.android +rootconfig.sh %_PROJECTNAME% +cd .. +set _PROJECTLOCATION=%cd% + +::A command line that make the current user get the ownrship of project. +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F + +::Use cygwin compile the source code. +cygpath "%_PROJECTLOCATION%\proj.android\build_native.sh"|call %CYGWIN%\Cygwin.bat + +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F +::echo "%_PROJECTION%/proj.android/build_native.sh"|call %CYGWIN%\Cygwin.bat + +::cacls proj.android\*.* /T /E /C /P %_USERNAME%:F +call android update project -p proj.android +cd proj.android + +for /f "delims=" %%a in ('findstr /i "target=android-" ant.properties') do set xx=%%a +echo %xx% +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="%xx%" (echo/target=android-8)else echo/%%a +)>>"anttmp.properties" +move anttmp.properties ant.properties + +::Change API level.(API level:14) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-8" (echo/target=android-14)else echo/%%a +)>>"ant1.properties" +move ant1.properties ant.properties + +for /f "delims=" %%a in (ant.properties) do set num=%%a&call :lis +move ant1.properties ant.properties + +::Android ant build(release,API level:14). +call ant release +set result14=%ERRORLEVEL% + +::Change API level.(API level:15) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-14" (echo/target=android-15)else echo/%%a +)>>"ant1.properties" +move ant1.properties ant.properties + +for /f "delims=" %%a in (ant.properties) do set num=%%a&call :lis +move ant1.properties ant.properties + +::Android ant build(release,API level:15). +call ant release +set result15=%ERRORLEVEL% + +::After all test versions completed,changed current API level to the original.(API level:8) +for /f "delims=" %%a in (ant.properties) do ( +if "%%a"=="target=android-15" (echo/target=android-8)else echo/%%a +)>>"ant1.properties" +move ant1.properties ant.properties + +for /f "delims=" %%a in (ant.properties) do set num=%%a&call :lis +move ant1.properties ant.properties + +cd ..\..\.. +IF "%_PROJECTNAME%"=="TestCpp" set /a testresult1=(result14+result15) && set _PROJECTNAME=HelloCpp&& goto project +IF "%_PROJECTNAME%"=="HelloCpp" set /a testresult2=(result14+result15) && set _PROJECTNAME=HelloLua&& goto project +IF "%_PROJECTNAME%"=="HelloLua" set /a testresult3=(result14+result15) +set /a testresult=(testresult1+result2+result3) +IF %testresult% NEQ 0 goto error + +goto success + +:lis +if "%num%"=="" goto :eof +if "%num:~-1%"==" " set num=%num:~0,-1%&goto lis +echo %num%>>ant1.properties +goto :eof + +:error +echo Error. +git checkout -f|%GITBIN%\sh.exe --login +git clean -df|%GITBIN%\sh.exe --login +pause +exit 1 +goto end + +:success +echo Success. +git checkout -f|%GITBIN%\sh.exe --login +git clean -df|%GITBIN%\sh.exe --login +pause +exit 0 +goto end + +::End. \ No newline at end of file diff --git a/tools/JenkinsScript/androidtestcommon.bat b/tools/JenkinsScript/androidtestcommon.bat new file mode 100644 index 0000000000..fab374d236 --- /dev/null +++ b/tools/JenkinsScript/androidtestcommon.bat @@ -0,0 +1,89 @@ +echo off +rem =========Basic parameters============ +rem jdk path +set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_31 +rem jdk version +set JDK_Version=1.6 +rem sdk path +set AndroidHome=D:\Windows7\android-sdk-windows +rem android version path +set AndroidVersion=\platforms\android-8 +rem android project path +set AndroidProject=D:\cocos2d-x\tests\proj.android +rem unsigned apk name +set unsign_apk=Tests.apk +rem signed apk name +set sign_apk=Tests-sign.apk +rem sign keystore +set apk_key=cdykeystore +set apk_keypass=123456 +set apk_keystore=D:\cdykeystore + +for %%x in ("%AndroidProject%") do set AndroidProject=%%~sx +for %%x in ("%JAVA_HOME%") do set JAVA_HOME=%%~sx +for %%x in ("%AndroidHome%") do set AndroidHome=%%~sx + +rem jdk kit +set EXE_JAVA=%JAVA_HOME%\bin\java +set JAVAC=%JAVA_HOME%\bin\javac +set JAR=%JAVA_HOME%\bin\jar +set KeyTool=%JAVA_HOME%\bin\keytool +set Jarsigner=%JAVA_HOME%\bin\jarsigner + +rem sdk kit +set AndroidAAPT=%AndroidHome%\platform-tools\aapt.exe +set AndroidDx=%AndroidHome%\platform-tools\dx.bat +set AndroidApkBuilder=%AndroidHome%\tools\apkbuilder.bat +set AndroidJar=%AndroidHome%%AndroidVersion%\android.jar + +rem android project directory +set AndroidProjectDrive=D: +set AndroidProjectRes=%AndroidProject%\res +set AndroidProjectGen=%AndroidProject%\gen +set AndroidProjectBin=%AndroidProject%\bin +set AndroidProjectAsset=%AndroidProject%\assets +set AndroidProjectLibs=%AndroidProject%\libs +set AndroidProjectAndroidMainfest=%AndroidProject%\AndroidManifest.xml +set AndroidProjectSrc=%AndroidProject%\src\org\cocos2dx\tests\*.java +set AndroidProjectSrc=%AndroidProjectSrc% %AndroidProject%\src\org\cocos2dx\lib\*.java +set AndroidProjectSrc=%AndroidProjectSrc% %AndroidProject%\gen\org\cocos2dx\tests\*.java + +rem output file +set AndroidProjectClassDex=%AndroidProject%\bin\classes.dex +set AndroidProjectResources=%AndroidProject%\bin\resources.ap_ +set AndroidProjectApk="%AndroidProject%\bin\%unsign_apk%" +set AndroidProjectSignApk="%AndroidProject%\bin\%sign_apk%" + +mkdir %AndroidProject%\gen +mkdir %AndroidProject%\src\org\cocos2dx\lib +mkdir %AndroidProject%\bin +::mkdir %AndroidProject%\bin\classes +xcopy D:\cocos2d-x\cocos2dx\platform\android\java\src_common\org\cocos2dx\lib %AndroidProject%\src\org\cocos2dx\lib /s + +echo generate R.java file +%AndroidAAPT% package -f -m -J %AndroidProjectGen% -S %AndroidProjectRes% -I %AndroidJar% -M %AndroidProjectAndroidMainfest% + +echo generate class file +%JAVAC% -encoding UTF-8 -target %JDK_Version% -bootclasspath %AndroidJar% -d %AndroidProjectBin% %AndroidProjectSrc% %AndroidProjectGen%\org\cocos2dx\tests\R.java + +echo generate dex file +echo on +%AndroidProjectDrive% +cd %AndroidProjectBin% +rem packaging the *.class file into *.jar file +%JAR% cvf %AndroidProjectBin%\classes.jar *.* +cd %AndroidProject% +rem generate *.dex file +call %AndroidDx% --dex --output=%AndroidProjectClassDex% %AndroidProjectBin%\classes.jar + +echo package resources files +%AndroidAAPT% package -f -M %AndroidProjectAndroidMainfest% -S %AndroidProjectRes% -A %AndroidProjectAsset% -I %AndroidJar% -F %AndroidProjectResources% + +echo generate unsigned apk file +call %AndroidApkBuilder% %AndroidProjectApk% -v -u -z %AndroidProjectResources% -f %AndroidProjectClassDex% -rf %AndroidProject%\src -nf %AndroidProjectLibs% -rj %AndroidProjectLibs% + +echo generate signed apk file +%Jarsigner% -verbose -keystore %apk_keystore% -keypass %apk_keypass% -storepass %apk_keypass% -signedjar %AndroidProjectSignApk% %AndroidProjectApk% cdykeystore + +echo sign success! +pause \ No newline at end of file diff --git a/tools/JenkinsScript/ant.properties b/tools/JenkinsScript/ant.properties new file mode 100644 index 0000000000..de55aea457 --- /dev/null +++ b/tools/JenkinsScript/ant.properties @@ -0,0 +1,20 @@ +# This file is used to override default values used by the Ant build system. +# +# This file must be checked in 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. +key.store=C:/.android/debug.keystore +key.alias=androiddebugkey +key.store.password=android +key.alias.password=android +#proguard.config=proguard.cfg +# Project target. +target=android-8 \ No newline at end of file diff --git a/tools/JenkinsScript/build.xml b/tools/JenkinsScript/build.xml new file mode 100644 index 0000000000..dc1291ca26 --- /dev/null +++ b/tools/JenkinsScript/build.xml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tools/JenkinsScript/rootconfig.sh b/tools/JenkinsScript/rootconfig.sh new file mode 100644 index 0000000000..6c72629f23 --- /dev/null +++ b/tools/JenkinsScript/rootconfig.sh @@ -0,0 +1,35 @@ +#!/bin/bash +#Change the path expression of current path to cygwin path. +NDK_ROOT=$($CYGWIN/bin/cygpath.exe $ANDROID_NDK) +echo $NDK_ROOT +CUR=$(pwd) +cd ../../.. +tmp=$(pwd) +COCOS2DX=$($CYGWIN/bin/cygpath.exe $tmp) +echo $COCOS2DX + +var1=NDK_ROOT_LOCAL= +_NDK_ROOT=${var1}${NDK_ROOT} + +var2=COCOS2DX_ROOT_LOCAL= +_COCOS2DX_ROOT=${var2}${COCOS2DX} +echo $_NDK_ROOT +echo $_COCOS2DX_ROOT + +#Modify the configuration files +sed -i '3,4d' $CUR/build_native.sh +sed -i '13d' $CUR/project.properties +sed -i "3 i\\$_NDK_ROOT" $CUR/build_native.sh +sed -i "4 i\\$_COCOS2DX_ROOT" $CUR/build_native.sh + +#Modify the project name +if [ $1 = TestCpp ]; then + sed -i '2d' $CUR/build.xml + sed -i '2 i\' $CUR/build.xml +elif [ $1 = HelloCpp ]; then + sed -i '2d' $CUR/build.xml + sed -i '2 i\' $CUR/build.xml +else + sed -i '2d' $CUR/build.xml + sed -i '2 i\' $CUR/build.xml +fi \ No newline at end of file From 491c05b25ca8fde3809c6247464f4cc7a2826c8d Mon Sep 17 00:00:00 2001 From: mustime Date: Thu, 26 Jul 2012 16:45:21 +0800 Subject: [PATCH 5/6] issue #1284: unify the static variables' name to s_pXX --- CocosDenshion/android/opensl/OpenSLEngine.cpp | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/CocosDenshion/android/opensl/OpenSLEngine.cpp b/CocosDenshion/android/opensl/OpenSLEngine.cpp index e22bde719e..609c332d1b 100644 --- a/CocosDenshion/android/opensl/OpenSLEngine.cpp +++ b/CocosDenshion/android/opensl/OpenSLEngine.cpp @@ -188,14 +188,14 @@ int getFileDescriptor(AudioPlayer * player, const char * filename, off_t & start /********************************************************************************** * engine **********************************************************************************/ -static SLObjectItf g_pEngineObject = NULL; -static SLEngineItf g_pEngineEngine = NULL; -static SLObjectItf g_pOutputMixObject = NULL; +static SLObjectItf s_pEngineObject = NULL; +static SLEngineItf s_pEngineEngine = NULL; +static SLObjectItf s_pOutputMixObject = NULL; bool initAudioPlayer(AudioPlayer* player, const char* filename) { // configure audio sink - SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, g_pOutputMixObject}; + SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, s_pOutputMixObject}; SLDataSink audioSnk = {&loc_outmix, NULL}; // configure audio source @@ -212,7 +212,7 @@ bool initAudioPlayer(AudioPlayer* player, const char* filename) // create audio player const SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_MUTESOLO, SL_IID_VOLUME}; const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}; - SLresult result = (*g_pEngineEngine)->CreateAudioPlayer(g_pEngineEngine, &(player->fdPlayerObject), &(player->audioSrc), &audioSnk, 3, ids, req); + SLresult result = (*s_pEngineEngine)->CreateAudioPlayer(s_pEngineEngine, &(player->fdPlayerObject), &(player->audioSrc), &audioSnk, 3, ids, req); assert(SL_RESULT_SUCCESS == result); // realize the player @@ -251,28 +251,28 @@ void OpenSLEngine::createEngine() LOGD("createEngine"); // create engine - if (g_pEngineObject == NULL) + if (s_pEngineObject == NULL) { - result = slCreateEngine(&g_pEngineObject, 0, NULL, 0, NULL, NULL); + result = slCreateEngine(&s_pEngineObject, 0, NULL, 0, NULL, NULL); assert(SL_RESULT_SUCCESS == result); // realize the engine - result = (*g_pEngineObject)->Realize(g_pEngineObject, SL_BOOLEAN_FALSE); + result = (*s_pEngineObject)->Realize(s_pEngineObject, SL_BOOLEAN_FALSE); assert(SL_RESULT_SUCCESS == result); // get the engine interface, which is needed in order to create other objects - result = (*g_pEngineObject)->GetInterface(g_pEngineObject, SL_IID_ENGINE, &g_pEngineEngine); + result = (*s_pEngineObject)->GetInterface(s_pEngineObject, SL_IID_ENGINE, &s_pEngineEngine); assert(SL_RESULT_SUCCESS == result); // create output mix const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB}; const SLboolean req[1] = {SL_BOOLEAN_FALSE}; - result = (*g_pEngineEngine)->CreateOutputMix(g_pEngineEngine, &g_pOutputMixObject, 1, ids, req); + result = (*s_pEngineEngine)->CreateOutputMix(s_pEngineEngine, &s_pOutputMixObject, 1, ids, req); assert(SL_RESULT_SUCCESS == result); } // realize the output mix - result = (*g_pOutputMixObject)->Realize(g_pOutputMixObject, SL_BOOLEAN_FALSE); + result = (*s_pOutputMixObject)->Realize(s_pOutputMixObject, SL_BOOLEAN_FALSE); assert(SL_RESULT_SUCCESS == result); } @@ -291,16 +291,16 @@ void OpenSLEngine::closeEngine() sharedList().clear(); // destroy output mix interface - if (g_pOutputMixObject != NULL) { - (*g_pOutputMixObject)->Destroy(g_pOutputMixObject); - g_pOutputMixObject = NULL; + if (s_pOutputMixObject != NULL) { + (*s_pOutputMixObject)->Destroy(s_pOutputMixObject); + s_pOutputMixObject = NULL; } // destroy opensl engine - if (g_pEngineObject != NULL) { - (*g_pEngineObject)->Destroy(g_pEngineObject); - g_pEngineObject = NULL; - g_pEngineEngine = NULL; + if (s_pEngineObject != NULL) { + (*s_pEngineObject)->Destroy(s_pEngineObject); + s_pEngineObject = NULL; + s_pEngineEngine = NULL; } } From b4e7e1152f1c0971413a55c40b6419ccaef1ef73 Mon Sep 17 00:00:00 2001 From: mustime Date: Fri, 27 Jul 2012 10:36:05 +0800 Subject: [PATCH 6/6] issue #1284: fix the problem that effect still plays after stop->resume --- CocosDenshion/android/Android.mk | 2 +- CocosDenshion/android/SimpleAudioEngine.cpp | 6 ++- CocosDenshion/android/opensl/OpenSLEngine.cpp | 45 ++++++++++++++++++- CocosDenshion/android/opensl/OpenSLEngine.h | 4 ++ .../opensl/SimpleAudioEngineOpenSL.cpp | 4 +- .../android/opensl/SimpleAudioEngineOpenSL.h | 30 ++++++------- 6 files changed, 69 insertions(+), 22 deletions(-) diff --git a/CocosDenshion/android/Android.mk b/CocosDenshion/android/Android.mk index 93b0d820fb..89020bc647 100644 --- a/CocosDenshion/android/Android.mk +++ b/CocosDenshion/android/Android.mk @@ -12,7 +12,7 @@ LOCAL_SRC_FILES := SimpleAudioEngine.cpp \ ifeq ($(APP_PLATFORM), android-9) # define # define the macro to compile through CocosDenshion/android/SimpleAudioEngine.cpp -LOCAL_CFLAGS := -DENABLE_OPENSL +# LOCAL_CFLAGS := -DENABLE_OPENSL LOCAL_SRC_FILES += opensl/OpenSLEngine.cpp \ opensl/SimpleAudioEngineOpenSL.cpp diff --git a/CocosDenshion/android/SimpleAudioEngine.cpp b/CocosDenshion/android/SimpleAudioEngine.cpp index 1a74baac9d..800be06b15 100644 --- a/CocosDenshion/android/SimpleAudioEngine.cpp +++ b/CocosDenshion/android/SimpleAudioEngine.cpp @@ -26,7 +26,7 @@ THE SOFTWARE. #include "jni/SimpleAudioEngineJni.h" /* - * for OpenSLES on Android 2.3 and over + * for OpenSLES on Android 2.3 and above */ #ifdef ENABLE_OPENSL #include "opensl/SimpleAudioEngineOpenSL.h" @@ -129,7 +129,9 @@ float SimpleAudioEngine::getEffectsVolume() void SimpleAudioEngine::setEffectsVolume(float volume) { #ifdef ENABLE_OPENSL - SimpleAudioEngineOpenSL::sharedEngine()->setEffectsVolume(volume); + // @TO-DO + // Here may crash, fixing. + // SimpleAudioEngineOpenSL::sharedEngine()->setEffectsVolume(volume); #else setEffectsVolumeJNI(volume); #endif diff --git a/CocosDenshion/android/opensl/OpenSLEngine.cpp b/CocosDenshion/android/opensl/OpenSLEngine.cpp index 609c332d1b..5bdacc5a80 100644 --- a/CocosDenshion/android/opensl/OpenSLEngine.cpp +++ b/CocosDenshion/android/opensl/OpenSLEngine.cpp @@ -120,6 +120,9 @@ extern "C" { /********************************************************************************* * helper ********************************************************************************/ +#define PLAYSTATE_STOPPED 1 +#define PLAYSTATE_PAUSED 2 +#define PLAYSTATE_PLAYING 3 #define FILE_NOT_FOUND -1 #define ASSET_MANAGER_GETTER "getAssetManager" @@ -421,6 +424,26 @@ void setSingleEffectState(AudioPlayer * player, int state) } } +int getEffectState(AudioPlayer * player) +{ + SLuint32 state = 0; + SLresult result; + result = (*(player->fdPlayerPlay))->GetPlayState(player->fdPlayerPlay, &state); + assert(result == SL_RESULT_SUCCESS); + + return (int)state; +} + +void resumeSingleEffect(AudioPlayer * player) +{ + int state = getEffectState(player); + // only resume the effect that has been paused + if (state == PLAYSTATE_PAUSED) + { + setSingleEffectState(player, PLAYSTATE_PLAYING); + } +} + unsigned int OpenSLEngine::preloadEffect(const char * filename) { unsigned int nID = _Hash(filename); @@ -460,8 +483,6 @@ void OpenSLEngine::unloadEffect(const char * filename) { destroyAudioPlayer(p->second); sharedList().erase(nID); - - LOGD("effect unloaded"); } } @@ -483,6 +504,26 @@ void OpenSLEngine::setAllEffectState(int state) } } +void OpenSLEngine::resumeEffect(unsigned int effectID) +{ + EffectList::iterator p = sharedList().find(effectID); + if (p == sharedList().end()) + { + return; + } + resumeSingleEffect(p->second); +} + +void OpenSLEngine::resumeAllEffects() +{ + int state; + EffectList::iterator iter; + for (iter = sharedList().begin(); iter != sharedList().end() ; ++ iter) + { + resumeSingleEffect(iter->second); + } +} + void OpenSLEngine::setEffectLooping(unsigned int effectID, bool isLooping) { SLresult result; diff --git a/CocosDenshion/android/opensl/OpenSLEngine.h b/CocosDenshion/android/opensl/OpenSLEngine.h index 817ef9df6c..884ff36d3b 100644 --- a/CocosDenshion/android/opensl/OpenSLEngine.h +++ b/CocosDenshion/android/opensl/OpenSLEngine.h @@ -50,6 +50,10 @@ public: void setAllEffectState(int state); + void resumeEffect(unsigned int effectID); + + void resumeAllEffects(); + void setEffectLooping(unsigned int effectID, bool isLooping); void setEffectsVolume(int volume); diff --git a/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp b/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp index 1aa3981407..0b4e9bdb12 100644 --- a/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp +++ b/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.cpp @@ -92,12 +92,12 @@ void SimpleAudioEngineOpenSL::pauseAllEffects() void SimpleAudioEngineOpenSL::resumeEffect(unsigned int nSoundId) { - s_pOpenSL->setEffectState(nSoundId, PLAYSTATE_PLAYING); + s_pOpenSL->resumeEffect(nSoundId); } void SimpleAudioEngineOpenSL::resumeAllEffects() { - s_pOpenSL->setAllEffectState(PLAYSTATE_PLAYING); + s_pOpenSL->resumeAllEffects(); } void SimpleAudioEngineOpenSL::stopEffect(unsigned int nSoundId) diff --git a/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.h b/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.h index 58289e87c5..31d44bcd25 100644 --- a/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.h +++ b/CocosDenshion/android/opensl/SimpleAudioEngineOpenSL.h @@ -6,36 +6,36 @@ class SimpleAudioEngineOpenSL { public: - SimpleAudioEngineOpenSL(); - ~SimpleAudioEngineOpenSL(); + SimpleAudioEngineOpenSL(); + ~SimpleAudioEngineOpenSL(); bool initEngine(); - static SimpleAudioEngineOpenSL* sharedEngine(); + static SimpleAudioEngineOpenSL* sharedEngine(); - static void end(); + static void end(); - float getEffectsVolume(); + float getEffectsVolume(); - void setEffectsVolume(float volume); + void setEffectsVolume(float volume); - unsigned int playEffect(const char* pszFilePath, bool bLoop = false); + unsigned int playEffect(const char* pszFilePath, bool bLoop = false); - void pauseEffect(unsigned int nSoundId); + void pauseEffect(unsigned int nSoundId); - void pauseAllEffects(); + void pauseAllEffects(); - void resumeEffect(unsigned int nSoundId); + void resumeEffect(unsigned int nSoundId); - void resumeAllEffects(); + void resumeAllEffects(); - void stopEffect(unsigned int nSoundId); + void stopEffect(unsigned int nSoundId); - void stopAllEffects(); + void stopAllEffects(); - void preloadEffect(const char* pszFilePath); + void preloadEffect(const char* pszFilePath); - void unloadEffect(const char* pszFilePath); + void unloadEffect(const char* pszFilePath); };