We are migrating issue tracker of Cocos2d-x Project to Github, please create new issue there. Thanks.
Add Orientation Change Support in 3.0 (includes sample code for iOS and Android)
Add Orientation Change Support in 3.0 (includes sample code for iOS and Android)
Android Code
In Android main.cpp, add:
void onNativeWindowRedrawNeededCallback(ANativeActivity* activity, ANativeWindow* window) { int w = ANativeWindow_getWidth(window); int h = ANativeWindow_getHeight(window); LOGD ("Window New Size %d %d", w, h); AppDelegate::onOrientationChange(w>h, w, h); //this call causes a crash, due to JNI and GL threads mismatch. We need to use runOnGLThread here. } void cocos_android_app_init (struct android_app* app) { LOGD("cocos_android_app_init"); AppDelegate *pAppDelegate = new AppDelegate(); app->activity->callbacks->onNativeWindowRedrawNeeded = &onNativeWindowRedrawNeededCallback; }
// Above Android code gives JNI ERROR: env->self != thread-self, the cocos scene reset function should be called on GL thread using proposed cocos2d::ThreadHelper::runOnGLThread(func); but I couldn’t find it in 3.0 alpha0. Is it already implemented or is there some other workaround to do this on GL thread?
// Also, I didn’t use onNativeWindowResized because is only called ‘sometimes’, and I couldn’t tell why. onNativeWindowRedrawNeeded seems more reliable.
The above code takes care of some issues like: height and width of visible screen area stay the same for full screen apps, but on Android devices like Nexus4 with on-screen buttons or with not-full-screen iOS apps with status bar visible, app window’s visible height and width change on orientation change because of new location of buttons and status bar.
Another issue with IOS is that width and height values are not swapped after orientation change, above code manually checks if it is landscape or portrait, and then swap the height/width values if needed.
Only pending issue is “JNI ERROR: env->self != thread-self” on Android which can be solved by runOnGLThread. If runOnGLThread is not implemented yet, this can be hacked for now using schedule_once or some other better way.
Another alternate way to hook up orientation change callback on Android. This one doesn’t need any thread helper etc… as we use the main loop in native activity which is already our main thread.
In nativeactivity.cpp, in engine_handle_cmd(), add:
case APP_CMD_CONFIG_CHANGED: int w = ANativeWindow_getWidth(app->window); int h = ANativeWindow_getHeight(app->window); LOGI("Window New Size %d %d", w, h); AppDelegate::onOrientationChange(w>h, w, h); break;
Sachin Garg wrote:
Another alternate way to hook up orientation change callback on Android. This one doesn’t need any thread helper etc… as we use the main loop in native activity which is already our main thread.
>
In nativeactivity.cpp, in engine_handle_cmd(), add:
>
[…]
Unfortunately, this way doesn’t work. w and h have old values, before changing. We need to find another way to get them.
I sent pull request to fix this bug
https://github.com/cocos2d/cocos2d-x/pull/3880
Dmitriy Torkhov wrote:
Unfortunately, this way doesn’t work. w and h have old values, before changing. We need to find another way to get them.
I have implemented orientation change in my app using this, and I am getting new width and height value when I use ANativeWindow_getWidth after APP_CMD_CONFIG_CHANGED .
After I saw your comment, I again tested on Nexus7 and it is working as expected. Can you please share more details of how you tested or if you changed anything in code, then I can try that on my side too.
OK, I tested more and can see the problem. When rotating device to left I get new values from APP_CMD_CONFIG_CHANGED but when rotating device to right I get old values. Maybe we need to try APP_CMD_WINDOW_RESIZED, APP_CMD_WINDOW_REDRAW_NEEDED or APP_CMD_CONTENT_RECT_CHANGED.
The android issue was fixed by Dmitriy Torkhov in his pull request (but the pull request was closed as it didn’t implement the changes in samples and for iOS).
https://github.com/cocos2d/cocos2d-x/pull/3917
Another pull request by Dmitriy Torkhov, includes iOS support too.
https://github.com/cocos2d/cocos2d-x/pull/3997
http://www.coachoutletstoreinuk.com/ Coach Outlet Online
http://www.coachfactoryoutletanus.com/ Coach Outlet
http://www.coachoutletonlineunsius.com/ Coach Factory Outlet
http://www.coachoutletonlinetur.com/ Coach Outlet Online
http://www.coachfactoryoutletnbsa.com/ Coach Factory Outlet
http://www.coachfactoryoutletuisa.com/ Coach Outlet Store Online
http://www.coachoutletstorenie.com/ Coach Outlet Store Online
http://www.coachfactorystoreuin.com/ Coach Factory Outlet
http://www.coachfactoryoutletreba.com/ Coach Outlet
http://www.coachfactoryoutletonlinesius.com/ Coach Handbags Outlet
http://www.coachoutletonlinetnse.com/ Coach Outlet
http://www.coachoutletnoia.com/ Coach Outlet
http://www.chanleoutletmodi.com/ Chanel Outlet Online
http://www.louisvuittonoutletina.com/ Louis Vuitton
http://www.louisvuittonoutletine.com/ Louis Vuitton Outlet
http://www.guccibeltsoutletbcus.com/ Gucci Belts
http://www.guccibelststco.com/ Gucci Belt
http://www.louisvuittonoutletsnib.com/ Louis Vuitton Outlet
http://www.oakleysunglassescnc.name/ Oakley Sunglasses Outlet
http://www.louisvuittonoutletmoba.com/ Louis Vuitton Outlet
http://www.oakleyssunglassesmobo.com/ Oakley Sunglasses Outlet
http://www.oakleysunglassesmaba.com/ Oakley Sunglasses
http://www.oakleysunglassinsc.com/ Oakley Sunglasses
http://www.louisvuittonoutletinca.com/ Louis Vuitton Outlet
http://www.louisvuittonoutletpft.com/ Louis Vuitton Outlet
http://www.louisvuittonoutletcoms.com/ Louis Vuitton Outlet
http://www.louisvuittonoutletcsc.com/ Louis Vuitton Handbags
http://www.louisvuittonoutletsnib.com/ Louis Vuitton Outlet Online
http://www.louisvuitton-bagsonlinee.net/ Louis Vuitton Handbags
http://www.louisvuittonoutletori.com/ Louis Vuitton
http://www.louisvuittonoutlettca.com/ Louis Vuitton Handbags
http://www.louisvuittonoutletincn.com/ Louis Vuitton Outlet Online
http://www.chaneloutletmodi.com/ Chanel Outlet
http://www.chaneloutletmodo.com/ Chanel Outlet
http://www.chaneloutletinaus.net/ Chanel Outlet
http://www.chaneloutletinfr.com/ Chanel
http://www.oakleysunglassesmodo.com/ Oakley Sunglasses
http://www.okelysuglassesmodo.net/ Oakley Sunglasses Outlet
http://www.oakelysunglassesmod.net/ Oakley Sunglasses
http://www.oakleyoutletshop.us.com/ Oakley Sunglasses
http://www.oakleyoutletonline.us.com/ Oakley Sunglasses Outlet
http://www.oakleysunglassesviews.com/ Oakley Sunglasses Outlet
http://www.raybanshop.name/ Ray Ban
http://www.raybansunglassesshop.name/ Ray Ban Sunglasses
http://www.raybansunglassesmodo.net/ Ray Ban Sunglasses Outlet
http://www.coachoutletonlineptn.com/ Coach Outlet
http://www.coachoutletstoreinuk.com/ Coach Outlet Online
http://www.coachfactoryoutletanus.com/ Coach Outlet
http://www.coachoutletonlineunsius.com/ Coach Factory Outlet
http://www.coachoutletonlinetur.com/ Coach Outlet Online
http://www.coachfactoryoutletnbsa.com/ Coach Outlet Store
http://www.coachfactoryoutletuisa.com/ Coach Outlet Store Online
http://www.coachoutletstorenie.com/ Coach Outlet
http://www.coachfactorystoreuin.com/ Coach Factory Online
http://www.coachfactoryoutletreba.com/ Coach Factory Outlet
http://www.coachfactoryoutletonlinesius.com/ Coach Factory Outlet
http://www.coachoutletonlinetnse.com/ Coach Outlet
http://www.coachoutletnoia.com/ Coach Outlet Store Online
http://www.chanleoutletmodi.com/ Chanel Outlet Online
http://www.louisvuittonoutletina.com/ Louis Vuitton Outlet
http://www.louisvuittonoutletine.com/ Louis Vuitton Handbags
http://www.guccibeltsoutletbcus.com/ Gucci Belt
http://www.guccibelststco.com/ Gucci Belts
http://www.louisvuittonoutletsnib.com/ Louis Vuitton Outlet Online
http://www.oakleysunglassescnc.name/ Oakley Sunglasses Outlet
http://www.louisvuittonoutletmoba.com/ Louis Vuitton Outlet Online
http://www.oakleyssunglassesmobo.com/ Oakley Sunglasses Outlet
http://www.oakleysunglassesmaba.com/ Cheap Oakley Sunglasses
http://www.oakleysunglassinsc.com/ Oakley Sunglasses
http://www.louisvuittonoutletinca.com/ Louis Vuitton Outlet
http://www.louisvuittonoutletpft.com/ Louis Vuitton Outlet
http://www.louisvuittonoutletcoms.com/ Louis Vuitton Outlet
http://www.louisvuittonoutletcsc.com/ Louis Vuitton Handbags
http://www.louisvuittonoutletsnib.com/ Louis Vuitton Outlet Online
http://www.louisvuitton-bagsonlinee.net/ Louis Vuitton Handbags
http://www.louisvuittonoutletori.com/ Louis Vuitton
http://www.louisvuittonoutlettca.com/ Louis Vuitton Handbags
http://www.louisvuittonoutletincn.com/ Louis Vuitton Outlet Online
http://www.chaneloutletmodi.com/ Chanel Outlet
http://www.chaneloutletmodo.com/ Chanel Outlet
http://www.chaneloutletinaus.net/ Chanel Outlet
http://www.chaneloutletinfr.com/ Chanel
http://www.oakleysunglassesmodo.com/ Oakley Sunglasses
http://www.okelysuglassesmodo.net/ Oakley Sunglasses Outlet
http://www.oakelysunglassesmod.net/ Oakley Sunglasses
http://www.oakleyoutletshop.us.com/ Oakley Sunglasses
http://www.oakleyoutletonline.us.com/ Oakley Sunglasses Outlet
http://www.oakleysunglassesviews.com/ Oakley Sunglasses Outlet
http://www.raybanshop.name/ Ray Ban
http://www.raybansunglassesshop.name/ Ray Ban Sunglasses
http://www.raybansunglassesmodo.net/ Ray Ban Sunglasses Outlet
http://www.coachoutletonlineptn.com/ Coach Outlet
http://www.coachoutletstoreinuk.com/ Coach Outlet Online
http://www.coachfactoryoutletanus.com/ Coach Factory Outlet
http://www.coachoutletonlineunsius.com/ Coach Outlet
http://www.coachoutletonlinetur.com/ Coach Online Outlet
http://www.coachfactoryoutletnbsa.com/ Coach Factory Outlet
http://www.coachfactoryoutletuisa.com/ Coach Outlet Store
http://www.coachoutletstorenie.com/ Coach Outlet Store
http://www.coachfactorystoreuin.com/ Coach Outlet Online
http://www.coachfactoryoutletreba.com/ Coach Factory Outlet
http://www.coachfactoryoutletonlinesius.com/ Coach Factory Online
http://www.coachoutletonlinetnse.com/ Coach Factory Outlet
Status: | Closed | |
---|---|---|
Start date: | 2013-10-06 | |
Priority: | Normal | |
Due date: | ||
Assignee: | - | |
% Done: | 0% |
|
Category: | android, ios | |
Target version: | 3.0-alpha1 |
It is easy to add support for orientation change in cocos2dx if we add the following code:
For IOS
In AppController.mm, in didFinishLaunchingWithOptions, add:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:`selector(handleDidChangeStatusBarOrientationNotification:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
In AppController.mm, add:
Common Code
In AppDelegate.h, add:
void onOrientationChange;
In AppDelegate.cpp, add {
Size sSystem; // size that OS tells us
Size sActual; // size that we need to use in cocos2dx
if {
sActual.width = std::max;
sActual.height = std::min;
} else {
sActual.width = std::min;
sActual.height = std::max;
}
cocos2d::Director* director = cocos2d::CCDirector::getInstance;
EGLView* eglView = director~~>getOpenGLView();
eglView~~>setFrameSize;
eglView~~>setDesignResolutionSize(sActual.width, sActual.height, ResolutionPolicy::SHOW_ALL);
cocos2d::CCDirector::getInstance()->replaceScene(/* getCurrentScene()*/); //reload current scene to redraw it for new screen size, or an app might choose to do something different
}