From f050fc9f08b9979019df053268dbdddb31fded7c Mon Sep 17 00:00:00 2001 From: Rohan Kuruvilla Date: Fri, 25 Jan 2013 00:29:55 -0800 Subject: [PATCH] Allowing resolution being sent to CocosBuilder to be set by AppDelegate --- .../Javascript/CocosPlayer/Classes/AppDelegate.cpp | 8 +++++++- ...a_org_cocos2dx_cocosplayer_CocosPlayerSocket.cpp | 13 ++++++++++++- ...ava_org_cocos2dx_cocosplayer_CocosPlayerSocket.h | 1 + .../Javascript/CocosPlayer/Classes/PlayerStatus.cpp | 4 ++++ .../Javascript/CocosPlayer/Classes/PlayerStatus.h | 3 ++- .../org/cocos2dx/cocosplayer/CCBStreamHandler.java | 12 +++++++++++- .../src/org/cocos2dx/cocosplayer/CocosPlayer.java | 10 +++++----- 7 files changed, 42 insertions(+), 9 deletions(-) diff --git a/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp b/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp index acbbb5a16a..4dcaf87869 100644 --- a/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp +++ b/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp @@ -65,26 +65,30 @@ bool AppDelegate::applicationDidFinishLaunching() CCSize designSize = CCSizeMake(320, 480); CCSize resourceSize = CCSizeMake(320, 480); - + string res = "xlarge"; if (screenSize.height > 1024) { resourceSize = CCSizeMake(1280, 1920); CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(CCArray::create(CCString::create("resources-xlarge"), CCString::create(""), NULL)); + res = "xlarge"; } else if (screenSize.height > 960) { resourceSize = CCSizeMake(640, 960); CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(CCArray::create(CCString::create("resources-large"), CCString::create(""), NULL)); + res = "large"; } else if (screenSize.height > 480) { resourceSize = CCSizeMake(480, 720); CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(CCArray::create(CCString::create("resources-medium"), CCString::create(""), NULL)); + res = "medium"; } else { resourceSize = CCSizeMake(320, 568); CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(CCArray::create(CCString::create("resources-small"), CCString::create(""), NULL)); + res = "small"; } pDirector->setContentScaleFactor(resourceSize.height/designSize.height); @@ -93,6 +97,8 @@ bool AppDelegate::applicationDidFinishLaunching() CCFileUtils::sharedFileUtils()->setSearchPath(CCArray::create(CCString::create(CCFileUtils::sharedFileUtils()->getWriteablePath()), CCString::create("assets/"), CCString::create(""), NULL)); + + PlayerStatus::setDeviceResolution(res); // turn on display FPS pDirector->setDisplayStats(true); diff --git a/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.cpp b/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.cpp index 0790f4c197..cf6f27dc59 100644 --- a/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.cpp +++ b/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.cpp @@ -10,6 +10,7 @@ #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) #define SOCKET_CLASS_NAME "org/cocos2dx/cocosplayer/CocosPlayerSocket" +#define STREAM_CLASS_NAME "org/cocos2dx/cocosplayer/CCBStreamHandler" using namespace cocos2d; using namespace std; @@ -29,5 +30,15 @@ extern "C" { t.env->CallStaticVoidMethod(t.classID, t.methodID); t.env->DeleteLocalRef(t.classID); } - } + } + + void setDeviceResolutionJNI(const char *res) { + JniMethodInfo t; + if (JniHelper::getStaticMethodInfo(t, STREAM_CLASS_NAME, "setDeviceResolution", "(Ljava/lang/String;)V")) { + jstring stringArg1 = t.env->NewStringUTF(res); + t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1); + t.env->DeleteLocalRef(stringArg1); + t.env->DeleteLocalRef(t.classID); + } + } } diff --git a/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.h b/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.h index a810a3ac68..bb70bc9f39 100644 --- a/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.h +++ b/samples/Javascript/CocosPlayer/Classes/Java_org_cocos2dx_cocosplayer_CocosPlayerSocket.h @@ -29,6 +29,7 @@ THE SOFTWARE. extern "C" { extern void setPairingCodeJNI(int code); extern void cleanCacheDirJNI(); + extern void setDeviceResolutionJNI(const char* res); } #endif diff --git a/samples/Javascript/CocosPlayer/Classes/PlayerStatus.cpp b/samples/Javascript/CocosPlayer/Classes/PlayerStatus.cpp index c37e2067ff..db30ab72dd 100644 --- a/samples/Javascript/CocosPlayer/Classes/PlayerStatus.cpp +++ b/samples/Javascript/CocosPlayer/Classes/PlayerStatus.cpp @@ -46,6 +46,10 @@ SEL_MenuHandler PlayerStatus::onResolveCCBCCMenuItemSelector(CCObject * pTarget, return NULL; } +void PlayerStatus::setDeviceResolution(std::string res) { + setDeviceResolutionJNI(res.c_str()); +} + void editBoxCallbackFunc(const char* pText, void* ctx) { PlayerStatus *thiz = (PlayerStatus *)ctx; diff --git a/samples/Javascript/CocosPlayer/Classes/PlayerStatus.h b/samples/Javascript/CocosPlayer/Classes/PlayerStatus.h index 4219074595..fe701e878c 100644 --- a/samples/Javascript/CocosPlayer/Classes/PlayerStatus.h +++ b/samples/Javascript/CocosPlayer/Classes/PlayerStatus.h @@ -41,7 +41,8 @@ public: void pressedReset(CCObject * pSender, cocos2d::extension::CCControlEvent pCCControlEvent); void pressedRun(CCObject * pSender, cocos2d::extension::CCControlEvent pCCControlEvent); static void loadMainScene(const char *fileName); - + static void setDeviceResolution(std::string res); + cocos2d::CCMenuItemImage* mBtnRun; cocos2d::CCMenuItemImage* mBtnReset; cocos2d::CCMenuItemImage* mBtnPair; diff --git a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBStreamHandler.java b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBStreamHandler.java index e6ec9941bb..e79123977d 100644 --- a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBStreamHandler.java +++ b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBStreamHandler.java @@ -23,6 +23,7 @@ import java.nio.ByteOrder; public class CCBStreamHandler { + private static String resolution = "xlarge"; public static final String TAG = CocosPlayerSocket.class.getSimpleName(); public static NSDictionary parseBinaryPLIST(InputStream is) { @@ -95,7 +96,7 @@ public class CCBStreamHandler { root.put("cmd", "deviceinfo"); root.put("devicename", android.os.Build.MODEL); root.put("devicetype", "Android"); - root.put("preferredresourcetype", "xlarge"); + root.put("preferredresourcetype", resolution); String payload = root.toXMLPropertyList(); // String data = new String(header, 0 , header.length); @@ -104,4 +105,13 @@ public class CCBStreamHandler { } return null; } + + public static void setDeviceResolution(String res) { + CocosPlayerSocket server = new CocosPlayerSocket(); + Log.i(TAG, "Starting with resolution: "+res); + resolution = res; + server.setContext(CocosPlayer.c); + server.createServer(); + } + } \ No newline at end of file diff --git a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayer.java b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayer.java index 13634825a8..94d32cebbb 100644 --- a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayer.java +++ b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayer.java @@ -27,15 +27,15 @@ import org.cocos2dx.lib.Cocos2dxActivity; import android.os.Bundle; import android.content.Intent; +import android.content.ContextWrapper; +import android.content.Context; public class CocosPlayer extends Cocos2dxActivity{ - + + public static Context c; protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); - - CocosPlayerSocket server = new CocosPlayerSocket(); - server.setContext(getApplicationContext()); - server.createServer(); + c = getApplicationContext(); } static {