[android] fixed #623: refactor Cocos2dJni.cpp

This commit is contained in:
minggo 2011-07-25 14:12:30 +08:00
parent 62ea6a2d46
commit b105b6005e
18 changed files with 685 additions and 482 deletions

View File

@ -65,8 +65,12 @@ platform/platform.cpp \
platform/android/CCEGLView_android.cpp \
platform/android/CCAccelerometer_android.cpp \
platform/android/CCApplication_android.cpp \
platform/android/Cocos2dJni.cpp \
platform/android/jni/JniHelper.cpp \
platform/android/jni/IMEJni.cpp \
platform/android/jni/MessageJni.cpp \
platform/android/jni/SensorJni.cpp \
platform/android/jni/SystemInfoJni.cpp \
platform/android/jni/TouchesJni.cpp \
script_support/CCScriptSupport.cpp \
sprite_nodes/CCAnimation.cpp \
sprite_nodes/CCAnimationCache.cpp \
@ -98,7 +102,7 @@ tileMap_parallax_nodes/CCTMXTiledMap.cpp \
tileMap_parallax_nodes/CCTMXXMLParser.cpp \
tileMap_parallax_nodes/CCTileMapAtlas.cpp \
touch_dispatcher/CCTouchDispatcher.cpp \
touch_dispatcher/CCTouchHandler.cpp \
touch_dispatcher/CCTouchHandler.cpp

View File

@ -159,8 +159,9 @@ NS_CC_END;
#include <android/log.h>
#include <stdio.h>
#include <jni.h>
#include "android/Cocos2dJni.h"
#include "android/jni/MessageJni.h"
NS_CC_BEGIN;

View File

@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCAccelerometer_android.h"
#include "jni/SensorJni.h"
#include <stdio.h>
#include <android/log.h>

View File

@ -27,7 +27,6 @@ THE SOFTWARE.
#include "CCCommon.h"
#include "CCAccelerometerDelegate.h"
#include "Cocos2dJni.h"
#include <list>
namespace cocos2d {

View File

@ -2,7 +2,7 @@
#include "jni/JniHelper.h"
#include "CCDirector.h"
#include "CCEGLView.h"
#include "Cocos2dJni.h"
#include "android/jni/SystemInfoJni.h"
#include <android/log.h>
#include <jni.h>

View File

@ -23,12 +23,12 @@ THE SOFTWARE.
****************************************************************************/
#include "CCEGLView_android.h"
#include "GLES/gl.h"
#include "CCSet.h"
#include "CCDirector.h"
#include "ccMacros.h"
#include "CCTouchDispatcher.h"
#include "Cocos2dJni.h"
#include "jni/IMEJni.h"
#include "jni/MessageJni.h"
#include <stdlib.h>

View File

@ -25,7 +25,7 @@ THE SOFTWARE.
NS_CC_BEGIN;
#include "CCCommon.h"
#include "Cocos2dJni.h"
#include "jni/SystemInfoJni.h"
#define MAX_PATH 256

View File

@ -29,7 +29,6 @@ THE SOFTWARE.
#include <jni.h>
#include "CCPlatformMacros.h"
#include "Cocos2dJni.h"
#include "CCImage.h"
#include "jni/JniHelper.h"

View File

@ -1,463 +0,0 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "Cocos2dJni.h"
#include "CCSet.h"
#include "CCDirector.h"
#include "CCKeypadDispatcher.h"
#include "CCTouch.h"
#include "CCTouchDispatcher.h"
#include "CCFileUtils.h"
#include "CCGeometry.h"
#include "CCAccelerometer.h"
#include "CCApplication.h"
#include "CCIMEDispatcher.h"
#include "CCAccelerometer_android.h"
#include <android/log.h>
#include "jni/JniHelper.h"
#if 0
#define LOG_TAG "Cocos2dJni"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
#define LOGD(...)
#endif
using namespace cocos2d;
extern "C"
{
//////////////////////////////////////////////////////////////////////////
// java vm helper function
//////////////////////////////////////////////////////////////////////////
static bool getMethodID(JniMethodInfo& t, const char *className, const char *methodName, const char *paramCode)
{
bool ret = 0;
do
{
if (JniHelper::getJavaVM()->GetEnv((void**)&t.env, JNI_VERSION_1_4) != JNI_OK)
{
LOGD("Failed to get the environment using GetEnv()");
break;
}
if (JniHelper::getJavaVM()->AttachCurrentThread(&t.env, 0) < 0)
{
LOGD("Failed to get the environment using AttachCurrentThread()");
break;
}
t.classID = t.env->FindClass(className);
if (! t.classID)
{
LOGD("Failed to find class of %s", className);
break;
}
t.methodID = t.env->GetStaticMethodID(t.classID, methodName, paramCode);
if (! t.methodID)
{
LOGD("Failed to find method id of %s", methodName);
break;
}
ret = true;
} while (0);
return ret;
}
//////////////////////////////////////////////////////////////////////////
// native renderer
//////////////////////////////////////////////////////////////////////////
#define MAX_TOUCHES 5
static CCTouch *s_pTouches[MAX_TOUCHES] = { NULL };
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender(JNIEnv* env)
{
cocos2d::CCDirector::sharedDirector()->mainLoop();
}
// handle touch event
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv* env, jobject thiz, jint id, jfloat x, jfloat y)
{
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
CCSet set;
CCTouch *pTouch = s_pTouches[id];
if (! pTouch)
{
LOGD("Beginning touches with id: %d, x=%f, y=%f", id, x, y);
pTouch = new CCTouch();
pTouch->SetTouchInfo(0, (x - rcRect.origin.x) / fScreenScaleFactor, (y - rcRect.origin.y) / fScreenScaleFactor);
s_pTouches[id] = pTouch;
set.addObject(pTouch);
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesBegan(&set, NULL);
}
else
{
LOGD("Beginnig touches with id: %d error", id);
}
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesEnd(JNIEnv* env, jobject thiz, jint id, jfloat x, jfloat y)
{
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
CCSet set;
/* Add to the set to send to the director */
CCTouch* pTouch = s_pTouches[id];
if (pTouch)
{
LOGD("Ending touches with id: %d, x=%f, y=%f", id, x, y);
pTouch->SetTouchInfo(0, (x - rcRect.origin.x) / fScreenScaleFactor , (y - rcRect.origin.y) / fScreenScaleFactor);
set.addObject(pTouch);
// release the object
pTouch->release();
s_pTouches[id] = NULL;
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesEnded(&set, NULL);
} else {
LOGD("Ending touches with id: %d error", id);
}
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesMove(JNIEnv* env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys)
{
int size = env->GetArrayLength(ids);
jint id[size];
jfloat x[size];
jfloat y[size];
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
CCSet set;
env->GetIntArrayRegion(ids, 0, size, id);
env->GetFloatArrayRegion(xs, 0, size, x);
env->GetFloatArrayRegion(ys, 0, size, y);
for( int i = 0 ; i < size ; i++ ) {
LOGD("Moving touches with id: %d, x=%f, y=%f", id[i], x[i], y[i]);
cocos2d::CCTouch* pTouch = s_pTouches[id[i]];
if (pTouch)
{
pTouch->SetTouchInfo(0, (x[i] - rcRect.origin.x) / fScreenScaleFactor ,
(y[i] - rcRect.origin.y) / fScreenScaleFactor);
set.addObject(pTouch);
}
else
{
// It is error, should return.
LOGD("Moving touches with id: %d error", id[i]);
return;
}
}
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesMoved(&set, NULL);
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesCancel(JNIEnv* env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys)
{
int size = env->GetArrayLength(ids);
jint id[size];
jfloat x[size];
jfloat y[size];
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
CCSet set;
env->GetIntArrayRegion(ids, 0, size, id);
env->GetFloatArrayRegion(xs, 0, size, x);
env->GetFloatArrayRegion(ys, 0, size, y);
for( int i = 0 ; i < size ; i++ ) {
cocos2d::CCTouch* pTouch = s_pTouches[id[i]];
if (pTouch)
{
pTouch->SetTouchInfo(0, (x[i] - rcRect.origin.x) / fScreenScaleFactor ,
(y[i] - rcRect.origin.y) / fScreenScaleFactor);
set.addObject(pTouch);
s_pTouches[id[i]] = NULL;
pTouch->release();
}
}
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesCancelled(&set, NULL);
}
// handle onPause and onResume
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnPause()
{
CCApplication::sharedApplication().applicationDidEnterBackground();
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnResume()
{
// Shared OpenGL View instance doesn't exist yet when Activity.onResume is first called
if (CCDirector::sharedDirector()->getOpenGLView())
CCApplication::sharedApplication().applicationWillEnterForeground();
}
#define KEYCODE_BACK 0x04
#define KEYCODE_MENU 0x52
// handle keydown event
jboolean Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyDown(JNIEnv* env, jobject thiz, jint keyCode)
{
switch (keyCode)
{
case KEYCODE_BACK:
if (CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(kTypeBackClicked))
return JNI_TRUE;
break;
case KEYCODE_MENU:
if (CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(kTypeMenuClicked))
return JNI_TRUE;
break;
default:
return JNI_FALSE;
}
return JNI_FALSE;
}
//////////////////////////////////////////////////////////////////////////
// handle accelerometer changes
//////////////////////////////////////////////////////////////////////////
void Java_org_cocos2dx_lib_Cocos2dxAccelerometer_onSensorChanged(JNIEnv* env, jobject thiz, jfloat x, jfloat y, jfloat z, jlong timeStamp)
{
// We need to invert to make it compatible with iOS.
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
cocos2d::CCAccelerometer::sharedAccelerometer()->update((x - rcRect.origin.x) / fScreenScaleFactor,
(y - rcRect.origin.y) / fScreenScaleFactor,
z,
timeStamp);
}
void Java_org_cocos2dx_lib_Cocos2dxActivity_nativeSetPaths(JNIEnv* env, jobject thiz, jstring apkPath)
{
const char* str;
jboolean isCopy;
str = env->GetStringUTFChars(apkPath, &isCopy);
if (isCopy) {
cocos2d::CCFileUtils::setResourcePath(str);
env->ReleaseStringUTFChars(apkPath, str);
}
}
void enableAccelerometerJNI()
{
JniMethodInfo t;
if (getMethodID(t
, "org/cocos2dx/lib/Cocos2dxActivity"
, "enableAccelerometer"
, "()V"))
{
t.env->CallStaticVoidMethod(t.classID, t.methodID);
}
}
void disableAccelerometerJNI()
{
JniMethodInfo t;
if (getMethodID(t
, "org/cocos2dx/lib/Cocos2dxActivity"
, "disableAccelerometer"
, "()V"))
{
t.env->CallStaticVoidMethod(t.classID, t.methodID);
}
}
void showMessageBoxJNI(const char * pszMsg, const char * pszTitle)
{
if (! pszMsg)
{
return;
}
JniMethodInfo t;
if (getMethodID(t
, "org/cocos2dx/lib/Cocos2dxActivity"
, "showMessageBox"
, "(Ljava/lang/String;Ljava/lang/String;)V"))
{
jstring StringArg1;
if (! pszTitle)
{
StringArg1 = t.env->NewStringUTF("");
}
else
{
StringArg1 = t.env->NewStringUTF(pszTitle);
}
jstring StringArg2 = t.env->NewStringUTF(pszMsg);
t.env->CallStaticVoidMethod(t.classID, t.methodID, StringArg1, StringArg2);
}
}
//////////////////////////////////////////////////////////////////////////
// handle IME message
//////////////////////////////////////////////////////////////////////////
void setKeyboardStateJNI(int bOpen)
{
JniMethodInfo t;
//jint open = bOpen;
if (getMethodID(t
, "org/cocos2dx/lib/Cocos2dxGLSurfaceView"
, (bOpen) ? "openIMEKeyboard" : "closeIMEKeyboard"
, "()V"))
{
t.env->CallStaticVoidMethod(t.classID, t.methodID);
}
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInsertText(JNIEnv* env, jobject thiz, jstring text)
{
jboolean isCopy = 0;
const char* pszText = env->GetStringUTFChars(text, &isCopy);
if (isCopy)
{
cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText, strlen(pszText));
env->ReleaseStringUTFChars(text, pszText);
}
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeDeleteBackward(JNIEnv* env, jobject thiz)
{
cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
}
jstring Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeGetContentText()
{
JNIEnv * env = 0;
if (JniHelper::getJavaVM()->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK || ! env)
{
return 0;
}
const char * pszText = cocos2d::CCIMEDispatcher::sharedDispatcher()->getContentText();
return env->NewStringUTF(pszText);
}
//////////////////////////////////////////////////////////////////////////
// get package name
//////////////////////////////////////////////////////////////////////////
static char* jstringTostring(JNIEnv* env, jstring jstr)
{
char* rtn = NULL;
// convert jstring to byte array
jclass clsstring = env->FindClass("java/lang/String");
jstring strencode = env->NewStringUTF("utf-8");
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
jsize alen = env->GetArrayLength(barr);
jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
// copy byte array into char[]
if (alen > 0)
{
rtn = new char[alen + 1];
memcpy(rtn, ba, alen);
rtn[alen] = 0;
}
env->ReleaseByteArrayElements(barr, ba, 0);
return rtn;
}
char* getPackageNameJNI()
{
JniMethodInfo t;
char* ret = NULL;
if (getMethodID(t,
"org/cocos2dx/lib/Cocos2dxActivity",
"getCocos2dxPackageName",
"()Ljava/lang/String;"))
{
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
ret = jstringTostring(t.env, str);
LOGD("package name %s", ret);
}
return ret;
}
//////////////////////////////////////////////////////////////////////////
// handle get current language
//////////////////////////////////////////////////////////////////////////
char* getCurrentLanguageJNI()
{
JniMethodInfo t;
char* ret = NULL;
if (getMethodID(t
, "org/cocos2dx/lib/Cocos2dxActivity"
, "getCurrentLanguage"
, "()Ljava/lang/String;"))
{
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
ret = jstringTostring(t.env, str);
LOGD("language name %s", ret);
}
return ret;
}
//////////////////////////////////////////////////////////////////////////
// terminate the process
//////////////////////////////////////////////////////////////////////////
void terminateProcessJNI()
{
JniMethodInfo t;
if (getMethodID(t
, "org/cocos2dx/lib/Cocos2dxActivity"
, "terminateProcess"
, "()V"))
{
t.env->CallStaticObjectMethod(t.classID, t.methodID);
}
}
}

View File

@ -0,0 +1,87 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "IMEJni.h"
#include "CCIMEDispatcher.h"
#include "JniHelper.h"
#include <android/log.h>
#include <string.h>
#include <jni.h>
#if 0
#define LOG_TAG "IMEJni"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
#define LOGD(...)
#endif
using namespace cocos2d;
extern "C"
{
//////////////////////////////////////////////////////////////////////////
// handle IME message
//////////////////////////////////////////////////////////////////////////
void setKeyboardStateJNI(int bOpen)
{
JniMethodInfo t;
if (JniHelper::getMethodInfo(t,
"org/cocos2dx/lib/Cocos2dxGLSurfaceView",
(bOpen) ? "openIMEKeyboard" : "closeIMEKeyboard",
"()V"))
{
t.env->CallStaticVoidMethod(t.classID, t.methodID);
}
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInsertText(JNIEnv* env, jobject thiz, jstring text)
{
jboolean isCopy = 0;
const char* pszText = env->GetStringUTFChars(text, &isCopy);
if (isCopy)
{
cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText, strlen(pszText));
env->ReleaseStringUTFChars(text, pszText);
}
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeDeleteBackward(JNIEnv* env, jobject thiz)
{
cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
}
jstring Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeGetContentText()
{
JNIEnv * env = 0;
if (JniHelper::getJavaVM()->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK || ! env)
{
return 0;
}
const char * pszText = cocos2d::CCIMEDispatcher::sharedDispatcher()->getContentText();
return env->NewStringUTF(pszText);
}
}

View File

@ -0,0 +1,33 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __ANDROID_IME_JNI_H__
#define __ANDROID_IME_JNI_H__
extern "C"
{
extern void setKeyboardStateJNI(int bOpen);
}
#endif // __ANDROID_IME_JNI_H__

View File

@ -0,0 +1,114 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "MessageJni.h"
#include "CCDirector.h"
#include "JniHelper.h"
#include "CCApplication.h"
#include <android/log.h>
#include <jni.h>
#if 0
#define LOG_TAG "MessageJni"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
#define LOGD(...)
#endif
using namespace cocos2d;
extern "C"
{
//////////////////////////////////////////////////////////////////////////
// native renderer
//////////////////////////////////////////////////////////////////////////
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender(JNIEnv* env)
{
cocos2d::CCDirector::sharedDirector()->mainLoop();
}
// handle onPause and onResume
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnPause()
{
CCApplication::sharedApplication().applicationDidEnterBackground();
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnResume()
{
// Shared OpenGL View instance doesn't exist yet when Activity.onResume is first called
if (CCDirector::sharedDirector()->getOpenGLView())
{
CCApplication::sharedApplication().applicationWillEnterForeground();
}
}
void showMessageBoxJNI(const char * pszMsg, const char * pszTitle)
{
if (! pszMsg)
{
return;
}
JniMethodInfo t;
if (JniHelper::getMethodInfo(t
, "org/cocos2dx/lib/Cocos2dxActivity"
, "showMessageBox"
, "(Ljava/lang/String;Ljava/lang/String;)V"))
{
jstring StringArg1;
if (! pszTitle)
{
StringArg1 = t.env->NewStringUTF("");
}
else
{
StringArg1 = t.env->NewStringUTF(pszTitle);
}
jstring StringArg2 = t.env->NewStringUTF(pszMsg);
t.env->CallStaticVoidMethod(t.classID, t.methodID, StringArg1, StringArg2);
}
}
//////////////////////////////////////////////////////////////////////////
// terminate the process
//////////////////////////////////////////////////////////////////////////
void terminateProcessJNI()
{
JniMethodInfo t;
if (JniHelper::getMethodInfo(t
, "org/cocos2dx/lib/Cocos2dxActivity"
, "terminateProcess"
, "()V"))
{
t.env->CallStaticObjectMethod(t.classID, t.methodID);
}
}
}

View File

@ -21,21 +21,14 @@ 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 __ANDROID_COCOS2D_JNI_H__
#define __ANDROID_COCOS2D_JNI_H__
#include <jni.h>
#ifndef __ANDROID_MESSAGE_JNI_H__
#define __ANDROID_MESSAGE_JNI_H__
extern "C"
{
extern void enableAccelerometerJNI();
extern void disableAccelerometerJNI();
extern void showMessageBoxJNI(const char * pszMsg, const char * pszTitle);
extern void setKeyboardStateJNI(int bOpen);
extern char* getCurrentLanguageJNI();
extern char* getPackageNameJNI();
extern void terminateProcessJNI();
}
#endif // __ANDROID_COCOS2D_JNI_H__
#endif // __ANDROID_MESSAGE_JNI_H__

View File

@ -0,0 +1,99 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "SensorJni.h"
#include "CCGeometry.h"
#include "CCAccelerometer.h"
#include "platform/android/CCAccelerometer_android.h"
#include "CCEGLView.h"
#include "CCFileUtils.h"
#include "JniHelper.h"
#include <android/log.h>
#include <jni.h>
#if 0
#define LOG_TAG "SensorJni"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
#define LOGD(...)
#endif
using namespace cocos2d;
extern "C"
{
//////////////////////////////////////////////////////////////////////////
// handle accelerometer changes
//////////////////////////////////////////////////////////////////////////
void Java_org_cocos2dx_lib_Cocos2dxAccelerometer_onSensorChanged(JNIEnv* env, jobject thiz, jfloat x, jfloat y, jfloat z, jlong timeStamp)
{
// We need to invert to make it compatible with iOS.
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
cocos2d::CCAccelerometer::sharedAccelerometer()->update((x - rcRect.origin.x) / fScreenScaleFactor,
(y - rcRect.origin.y) / fScreenScaleFactor,
z,
timeStamp);
}
void Java_org_cocos2dx_lib_Cocos2dxActivity_nativeSetPaths(JNIEnv* env, jobject thiz, jstring apkPath)
{
const char* str;
jboolean isCopy;
str = env->GetStringUTFChars(apkPath, &isCopy);
if (isCopy) {
cocos2d::CCFileUtils::setResourcePath(str);
env->ReleaseStringUTFChars(apkPath, str);
}
}
void enableAccelerometerJNI()
{
JniMethodInfo t;
if (JniHelper::getMethodInfo(t,
"org/cocos2dx/lib/Cocos2dxActivity",
"enableAccelerometer",
"()V"))
{
t.env->CallStaticVoidMethod(t.classID, t.methodID);
}
}
void disableAccelerometerJNI()
{
JniMethodInfo t;
if (JniHelper::getMethodInfo(t,
"org/cocos2dx/lib/Cocos2dxActivity",
"disableAccelerometer",
"()V"))
{
t.env->CallStaticVoidMethod(t.classID, t.methodID);
}
}
}

View File

@ -0,0 +1,34 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __ANDROID_SENSOR_JNI_H__
#define __ANDROID_SENSOR_JNI_H__
extern "C"
{
extern void enableAccelerometerJNI();
extern void disableAccelerometerJNI();
}
#endif // __ANDROID_SENSOR_JNI_H__

View File

@ -0,0 +1,82 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "SystemInfoJni.h"
#include "JniHelper.h"
#include <android/log.h>
#include <jni.h>
#if 0
#define LOG_TAG "SystemInfoJni"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
#define LOGD(...)
#endif
using namespace cocos2d;
extern "C"
{
char* getPackageNameJNI()
{
JniMethodInfo t;
char* ret = 0;
if (JniHelper::getMethodInfo(t,
"org/cocos2dx/lib/Cocos2dxActivity",
"getCocos2dxPackageName",
"()Ljava/lang/String;"))
{
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
ret = JniHelper::jstringToChar(str);
LOGD("package name %s", ret);
}
return ret;
}
//////////////////////////////////////////////////////////////////////////
// handle get current language
//////////////////////////////////////////////////////////////////////////
char* getCurrentLanguageJNI()
{
JniMethodInfo t;
char* ret = 0;
if (JniHelper::getMethodInfo(t
, "org/cocos2dx/lib/Cocos2dxActivity"
, "getCurrentLanguage"
, "()Ljava/lang/String;"))
{
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
ret = JniHelper::jstringToChar(str);
LOGD("language name %s", ret);
}
return ret;
}
}

View File

@ -0,0 +1,36 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __ANDROID_SYSTEM_INFO_JNI_H__
#define __ANDROID_SYSTEM_INFO_JNI_H__
#include <jni.h>
extern "C"
{
extern char* getCurrentLanguageJNI();
extern char* getPackageNameJNI();
}
#endif // __ANDROID_SYSTEM_INFO_JNI_H__

View File

@ -0,0 +1,184 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCSet.h"
#include "CCDirector.h"
#include "CCKeypadDispatcher.h"
#include "CCTouch.h"
#include "CCEGLView.h"
#include "CCTouchDispatcher.h"
#include <android/log.h>
#include <jni.h>
#if 0
#define LOG_TAG "NativeTouchesJni"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#else
#define LOGD(...)
#endif
using namespace cocos2d;
extern "C"
{
#define MAX_TOUCHES 5
static CCTouch *s_pTouches[MAX_TOUCHES] = { NULL };
// handle touch event
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv* env, jobject thiz, jint id, jfloat x, jfloat y)
{
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
CCSet set;
CCTouch *pTouch = s_pTouches[id];
if (! pTouch)
{
LOGD("Beginning touches with id: %d, x=%f, y=%f", id, x, y);
pTouch = new CCTouch();
pTouch->SetTouchInfo(0, (x - rcRect.origin.x) / fScreenScaleFactor, (y - rcRect.origin.y) / fScreenScaleFactor);
s_pTouches[id] = pTouch;
set.addObject(pTouch);
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesBegan(&set, NULL);
}
else
{
LOGD("Beginnig touches with id: %d error", id);
}
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesEnd(JNIEnv* env, jobject thiz, jint id, jfloat x, jfloat y)
{
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
CCSet set;
/* Add to the set to send to the director */
CCTouch* pTouch = s_pTouches[id];
if (pTouch)
{
LOGD("Ending touches with id: %d, x=%f, y=%f", id, x, y);
pTouch->SetTouchInfo(0, (x - rcRect.origin.x) / fScreenScaleFactor , (y - rcRect.origin.y) / fScreenScaleFactor);
set.addObject(pTouch);
// release the object
pTouch->release();
s_pTouches[id] = NULL;
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesEnded(&set, NULL);
} else {
LOGD("Ending touches with id: %d error", id);
}
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesMove(JNIEnv* env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys)
{
int size = env->GetArrayLength(ids);
jint id[size];
jfloat x[size];
jfloat y[size];
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
CCSet set;
env->GetIntArrayRegion(ids, 0, size, id);
env->GetFloatArrayRegion(xs, 0, size, x);
env->GetFloatArrayRegion(ys, 0, size, y);
for( int i = 0 ; i < size ; i++ ) {
LOGD("Moving touches with id: %d, x=%f, y=%f", id[i], x[i], y[i]);
cocos2d::CCTouch* pTouch = s_pTouches[id[i]];
if (pTouch)
{
pTouch->SetTouchInfo(0, (x[i] - rcRect.origin.x) / fScreenScaleFactor ,
(y[i] - rcRect.origin.y) / fScreenScaleFactor);
set.addObject(pTouch);
}
else
{
// It is error, should return.
LOGD("Moving touches with id: %d error", id[i]);
return;
}
}
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesMoved(&set, NULL);
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesCancel(JNIEnv* env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys)
{
int size = env->GetArrayLength(ids);
jint id[size];
jfloat x[size];
jfloat y[size];
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
CCSet set;
env->GetIntArrayRegion(ids, 0, size, id);
env->GetFloatArrayRegion(xs, 0, size, x);
env->GetFloatArrayRegion(ys, 0, size, y);
for( int i = 0 ; i < size ; i++ ) {
cocos2d::CCTouch* pTouch = s_pTouches[id[i]];
if (pTouch)
{
pTouch->SetTouchInfo(0, (x[i] - rcRect.origin.x) / fScreenScaleFactor ,
(y[i] - rcRect.origin.y) / fScreenScaleFactor);
set.addObject(pTouch);
s_pTouches[id[i]] = NULL;
pTouch->release();
}
}
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesCancelled(&set, NULL);
}
#define KEYCODE_BACK 0x04
#define KEYCODE_MENU 0x52
// handle keydown event
jboolean Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyDown(JNIEnv* env, jobject thiz, jint keyCode)
{
switch (keyCode)
{
case KEYCODE_BACK:
if (CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(kTypeBackClicked))
return JNI_TRUE;
break;
case KEYCODE_MENU:
if (CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(kTypeMenuClicked))
return JNI_TRUE;
break;
default:
return JNI_FALSE;
}
return JNI_FALSE;
}
}