diff --git a/cocos2dx/Android.mk b/cocos2dx/Android.mk index fa26f984f8..deda4158da 100644 --- a/cocos2dx/Android.mk +++ b/cocos2dx/Android.mk @@ -111,7 +111,7 @@ support/CCNotificationCenter.cpp \ support/CCProfiling.cpp \ support/CCPointExtension.cpp \ support/TransformUtils.cpp \ -support/CCUserDefault.cpp \ +support/user_default/CCUserDefault.cpp \ support/base64.cpp \ support/ccUtils.cpp \ support/CCVertex.cpp \ diff --git a/cocos2dx/CCDirector.cpp b/cocos2dx/CCDirector.cpp index 6719569221..220e383f31 100644 --- a/cocos2dx/CCDirector.cpp +++ b/cocos2dx/CCDirector.cpp @@ -50,7 +50,7 @@ THE SOFTWARE. #include "CCAccelerometer.h" #include "sprite_nodes/CCAnimationCache.h" #include "touch_dispatcher/CCTouch.h" -#include "support/CCUserDefault/CCUserDefault.h" +#include "support/user_default/CCUserDefault.h" #include "shaders/ccGLStateCache.h" #include "shaders/CCShaderCache.h" #include "kazmath/kazmath.h" diff --git a/cocos2dx/include/cocos2d.h b/cocos2dx/include/cocos2d.h index cbf7eb1f4e..de8cdcf09f 100755 --- a/cocos2dx/include/cocos2d.h +++ b/cocos2dx/include/cocos2d.h @@ -221,7 +221,7 @@ THE SOFTWARE. #include "support/CCNotificationCenter.h" #include "support/CCPointExtension.h" #include "support/CCProfiling.h" -#include "support/CCUserDefault/CCUserDefault.h" +#include "support/user_default/CCUserDefault.h" #include "support/CCVertex.h" #include "support/tinyxml2/tinyxml2.h" diff --git a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index 6688c1630b..d89cbcba23 100644 --- a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -1,5 +1,5 @@ /**************************************************************************** -Copyright (c) 2010-2011 cocos2d-x.org +Copyright (c) 2010-2013 cocos2d-x.org http://www.cocos2d-x.org @@ -23,16 +23,15 @@ THE SOFTWARE. ****************************************************************************/ package org.cocos2dx.lib; -import java.io.File; import java.io.UnsupportedEncodingException; import java.util.Locale; import android.app.Activity; import android.content.Context; +import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.res.AssetManager; import android.os.Build; -import android.os.Environment; import android.util.DisplayMetrics; import android.view.Display; import android.view.WindowManager; @@ -41,6 +40,7 @@ public class Cocos2dxHelper { // =========================================================== // Constants // =========================================================== + private static final String PREFS_NAME = "Cocos2dxPrefsFile"; // =========================================================== // Fields @@ -270,6 +270,67 @@ public class Cocos2dxHelper { } return -1; } + + // =========================================================== + // Functions for CCUserDefault + // =========================================================== + + public static boolean getBoolForKey(String key) { + SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + return settings.getBoolean(key, false); + } + + public static int getIntegerForKey(String key) { + SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + return settings.getInt(key, 0); + } + + public static float getFloatForKey(String key) { + SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + return settings.getFloat(key, 0); + } + + public static double getDoubleForKey(String key) { + // SharedPreferences doesn't support saving float value + SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + return settings.getFloat(key, 0); + } + + public static String getStringForKey(String key) { + SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + return settings.getString(key, ""); + } + + public static void setBoolForKey(String key, boolean value) { + SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences.Editor editor = settings.edit(); + editor.putBoolean(key, value); + } + + public static void setIntegerForKey(String key, int value) { + SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences.Editor editor = settings.edit(); + editor.putInt(key, value); + } + + public static void setFloatForKey(String key, float value) { + SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences.Editor editor = settings.edit(); + editor.putFloat(key, value); + } + + public static void setDoubleForKey(String key, double value) { + // SharedPreferences doesn't support recording double value + SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences.Editor editor = settings.edit(); + editor.putFloat(key, (float)value); + } + + public static void setStringForKey(String key, String value) { + SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); + SharedPreferences.Editor editor = settings.edit(); + editor.putString(key, value); + } // =========================================================== // Inner and Anonymous Classes diff --git a/cocos2dx/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp b/cocos2dx/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp index 17c9006c2a..5229a9e0ef 100644 --- a/cocos2dx/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp +++ b/cocos2dx/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp @@ -183,4 +183,160 @@ extern "C" { t.env->DeleteLocalRef(t.classID); } } + + // functions for CCUserDefault + bool getBoolForKeyJNI(const char* pKey) + { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getBoolForKey", "(Ljava/lang/String;)Z")) { + jstring stringArg = t.env->NewStringUTF(pKey); + jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg); + + return ret; + } + + return false; + } + + int getIntegerForKeyJNI(const char* pKey) + { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getIntegerForKey", "(Ljava/lang/String;)I")) { + jstring stringArg = t.env->NewStringUTF(pKey); + jint ret = t.env->CallStaticIntMethod(t.classID, t.methodID, stringArg); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg); + + return ret; + } + + return 0; + } + + float getFloatForKeyJNI(const char* pKey) + { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getFloatForKey", "(Ljava/lang/String;)F")) { + jstring stringArg = t.env->NewStringUTF(pKey); + jfloat ret = t.env->CallStaticFloatMethod(t.classID, t.methodID, stringArg); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg); + + return ret; + } + + return 0; + } + + double getDoubleForKeyJNI(const char* pKey) + { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getDoubleForKey", "(Ljava/lang/String;)D")) { + jstring stringArg = t.env->NewStringUTF(pKey); + jdouble ret = t.env->CallStaticDoubleMethod(t.classID, t.methodID, stringArg); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg); + + return ret; + } + + return 0; + } + + const char* getStringForKeyJNI(const char* pKey) + { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getStringForKey", "(Ljava/lang/String;)Ljava/lang/String;")) { + jstring stringArg = t.env->NewStringUTF(pKey); + jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, stringArg); + CCString *ret = new CCString(JniHelper::jstring2string(str).c_str()); + ret->autorelease(); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg); + t.env->DeleteLocalRef(str); + + return ret->getCString(); + } + + return 0; + } + + void setBoolForKeyJNI(const char* pKey, bool value) + { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setBoolForKey", "(Ljava/lang/String;Z)V")) { + jstring stringArg = t.env->NewStringUTF(pKey); + t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg); + } + } + + void setIntegerForKeyJNI(const char* pKey, int value) + { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setIntegerForKey", "(Ljava/lang/String;I)V")) { + jstring stringArg = t.env->NewStringUTF(pKey); + t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg); + } + } + + void setFloatForKeyJNI(const char* pKey, float value) + { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setFloatForKey", "(Ljava/lang/String;F)V")) { + jstring stringArg = t.env->NewStringUTF(pKey); + t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg); + } + } + + void setDoubleForKeyJNI(const char* pKey, double value) + { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setDoubleForKey", "(Ljava/lang/String;D)V")) { + jstring stringArg = t.env->NewStringUTF(pKey); + t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg); + } + } + + void setStringForKeyJNI(const char* pKey, const char* value) + { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setStringForKeyKey", "(Ljava/lang/String;Ljava/lang/String;)V")) { + jstring stringArg1 = t.env->NewStringUTF(pKey); + jstring stringArg2 = t.env->NewStringUTF(value); + t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1, stringArg2); + + t.env->DeleteLocalRef(t.classID); + t.env->DeleteLocalRef(stringArg1); + t.env->DeleteLocalRef(stringArg2); + } + } } diff --git a/cocos2dx/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h b/cocos2dx/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h index 118b08dde6..884385d7d8 100644 --- a/cocos2dx/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h +++ b/cocos2dx/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h @@ -39,6 +39,17 @@ extern "C" { extern void enableAccelerometerJNI(); extern void disableAccelerometerJNI(); extern void setAccelerometerIntervalJNI(float interval); + // functions for CCUserDefault + extern bool getBoolForKeyJNI(const char* pKey); + extern int getIntegerForKeyJNI(const char* pKey); + extern float getFloatForKeyJNI(const char* pKey); + extern double getDoubleForKeyJNI(const char* pKey); + extern const char* getStringForKeyJNI(const char* pKey); + extern void setBoolForKeyJNI(const char* pKey, bool value); + extern void setIntegerForKeyJNI(const char* pKey, int value); + extern void setFloatForKeyJNI(const char* pKey, float value); + extern void setDoubleForKeyJNI(const char* pKey, double value); + extern void setStringForKeyJNI(const char* pKey, const char* value); } #endif diff --git a/cocos2dx/proj.ios/cocos2dx.xcodeproj/project.pbxproj.REMOVED.git-id b/cocos2dx/proj.ios/cocos2dx.xcodeproj/project.pbxproj.REMOVED.git-id index fc7a0379d1..08c377d20c 100644 --- a/cocos2dx/proj.ios/cocos2dx.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/cocos2dx/proj.ios/cocos2dx.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -6cfbb92a36ed9c81afdc3a72359e730ee588ba32 \ No newline at end of file +c7e210f8b33a816358feec83a71bfe927820a395 \ No newline at end of file diff --git a/cocos2dx/support/user_default/CCUserDefaultAndroid.cpp b/cocos2dx/support/user_default/CCUserDefaultAndroid.cpp new file mode 100644 index 0000000000..4f5e8de1b4 --- /dev/null +++ b/cocos2dx/support/user_default/CCUserDefaultAndroid.cpp @@ -0,0 +1,179 @@ +/**************************************************************************** +Copyright (c) 2010-2012 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 "CCUserDefault.h" +#include "platform/CCPlatformConfig.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) +#include "platform/android/jni/JniHelper.h" + +// root name of xml +#define USERDEFAULT_ROOT_NAME "userDefaultRoot" + +#define XML_FILE_NAME "UserDefault.xml" + +using namespace std; + +NS_CC_BEGIN + +/** + * implements of CCUserDefault + */ + +CCUserDefault* CCUserDefault::m_spUserDefault = 0; +string CCUserDefault::m_sFilePath = string(""); +bool CCUserDefault::m_sbIsFilePathInitialized = false; + +/** + * If the user invoke delete CCUserDefault::sharedUserDefault(), should set m_spUserDefault + * to null to avoid error when he invoke CCUserDefault::sharedUserDefault() later. + */ +CCUserDefault::~CCUserDefault() +{ + CC_SAFE_DELETE(m_spUserDefault); + m_spUserDefault = NULL; +} + +CCUserDefault::CCUserDefault() +{ + m_spUserDefault = NULL; +} + +void CCUserDefault::purgeSharedUserDefault() +{ + m_spUserDefault = NULL; +} + + bool CCUserDefault::getBoolForKey(const char* pKey) + { + return getBoolForKey(pKey, false); + } + +bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue) +{ + +} + +int CCUserDefault::getIntegerForKey(const char* pKey) +{ + return getIntegerForKey(pKey, 0); +} + +int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue) +{ + +} + +float CCUserDefault::getFloatForKey(const char* pKey) +{ + return getFloatForKey(pKey, 0.0f); +} + +float CCUserDefault::getFloatForKey(const char* pKey, float defaultValue) +{ + float ret = (float)getDoubleForKey(pKey, (double)defaultValue); + + return ret; +} + +double CCUserDefault::getDoubleForKey(const char* pKey) +{ + return getDoubleForKey(pKey, 0.0); +} + +double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue) +{ + +} + +std::string CCUserDefault::getStringForKey(const char* pKey) +{ + return getStringForKey(pKey, ""); +} + +string CCUserDefault::getStringForKey(const char* pKey, const std::string & defaultValue) +{ + +} + +void CCUserDefault::setBoolForKey(const char* pKey, bool value) +{ + +} + +void CCUserDefault::setIntegerForKey(const char* pKey, int value) +{ + +} + +void CCUserDefault::setFloatForKey(const char* pKey, float value) +{ + +} + +void CCUserDefault::setDoubleForKey(const char* pKey, double value) +{ + +} + +void CCUserDefault::setStringForKey(const char* pKey, const std::string & value) +{ +} + +CCUserDefault* CCUserDefault::sharedUserDefault() +{ + if (! m_spUserDefault) + { + m_spUserDefault = new CCUserDefault(); + } + + return m_spUserDefault; +} + +bool CCUserDefault::isXMLFileExist() +{ + return false; +} + +void CCUserDefault::initXMLFilePath() +{ +} + +// create new xml file +bool CCUserDefault::createXMLFile() +{ + return false; +} + +const string& CCUserDefault::getXMLFilePath() +{ + return m_sFilePath; +} + +void CCUserDefault::flush() +{ +} + +NS_CC_END + +#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) diff --git a/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp b/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp index 4e5ae910e4..c35aa26044 100644 --- a/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp +++ b/samples/Cpp/TestCpp/Classes/RenderTextureTest/RenderTextureTest.cpp @@ -1,6 +1,6 @@ #include "CCConfiguration.h" #include "RenderTextureTest.h" -#include "testBasic.h" +#include "../testBasic.h" // Test #1 by Jason Booth (slipster216) // Test #3 by David Deaco (ddeaco)