Here is what I do to switch between portrait and landscape mode.
- by default set resolution as 320 x 480 (portrait)
- create two buttons “portrait” and “landscape” , by clicking the button, it will call the android binding method setRequestedOrientation respectively.
- AppDelegate::applicationScreenSizeChanged, it should detect screen change and setDesignResolutionSize to portrait or landscape accordingly… however it seems not working well.
anyone can guide me to archive this ?
source code and testing result as below :
AppDelegate.cpp
#include "AppDelegate.h"
#include "HelloWorldScene.h"
USING_NS_CC;
static cocos2d::Size designResolutionSize = cocos2d::Size(320, 480);
static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320);
static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768);
static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536);
AppDelegate::AppDelegate()
{
}
AppDelegate::~AppDelegate()
{
}
// if you want a different context, modify the value of glContextAttrs
// it will affect all platforms
void AppDelegate::initGLContextAttrs()
{
// set OpenGL context attributes: red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
// if you want to use the package manager to install more packages,
// don't modify or remove this function
static int register_all_packages()
{
return 0; //flag for packages manager
}
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect("card2", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
#else
glview = GLViewImpl::create("card2");
#endif
director->setOpenGLView(glview);
}
// turn on display FPS
director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0f / 60);
// Set the design resolution
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::SHOW_ALL);
auto frameSize = glview->getFrameSize();
// if the frame's height is larger than the height of medium size.
if (frameSize.height > mediumResolutionSize.height)
{
director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
}
// if the frame's height is larger than the height of small size.
else if (frameSize.height > smallResolutionSize.height)
{
director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
}
// if the frame's height is smaller than the height of medium size.
else
{
director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
}
register_all_packages();
// create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();
// run
director->runWithScene(scene);
return true;
}
// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();
// if you use SimpleAudioEngine, it must be paused
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
void AppDelegate::applicationScreenSizeChanged(int newWidth, int newHeight) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
auto director = cocos2d::Director::getInstance();
auto glview = director->getOpenGLView();
if (glview != NULL)
{
auto size = glview->getFrameSize();
if (size.equals(Size(newWidth, newHeight)))
return;
glview->setFrameSize(newWidth, newHeight);
if (newWidth < newHeight)
{
CCLOG("%s", "protrait");
CCLOG("%d", newWidth);
CCLOG("%d", newHeight);
glview->setDesignResolutionSize(320, 480, ResolutionPolicy::EXACT_FIT);
}
else
{
CCLOG("%s", "landscape");
CCLOG("%d", newWidth);
CCLOG("%d", newHeight);
glview->setDesignResolutionSize(480, 320, ResolutionPolicy::EXACT_FIT);
}
}
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#endif
}
HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label, 1);
MenuItemFont::setFontSize(28);
MenuItemFont::setFontName("fonts/Marker Felt.ttf");
// Bugs Item
auto protrait = MenuItemFont::create("Protrait", CC_CALLBACK_1(HelloWorld::switchProtrait, this));
auto landscape = MenuItemFont::create("Landscape", CC_CALLBACK_1(HelloWorld::switchLandscape, this));
landscape->setPosition(Vec2(0, 0));
protrait->setPosition(Vec2(100, 100));
auto smenu = Menu::create(protrait, landscape, nullptr);
smenu->setPosition(Vec2(100, 100));
this->addChild(smenu, 1);
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
return true;
}
void HelloWorld::switchLandscape(Ref* sender) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
int value = 0;
std::string msg = "abc";
JniMethodInfo jniMethodInfo;
bool isHave = JniHelper::getStaticMethodInfo(
jniMethodInfo,
"org/cocos2dx/cpp/AppActivity",
"requestAction",
"(ILjava/lang/String;)V");
if (isHave)
{
jstring strArg = jniMethodInfo.env->NewStringUTF(msg.c_str());
jniMethodInfo.env->CallStaticVoidMethod(jniMethodInfo.classID, jniMethodInfo.methodID, value, strArg);
jniMethodInfo.env->DeleteLocalRef(jniMethodInfo.classID);
jniMethodInfo.env->DeleteLocalRef(strArg);
}
#endif
}
void HelloWorld::switchProtrait(Ref* sender) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
int value = 1;
std::string msg = "abc";
JniMethodInfo jniMethodInfo;
bool isHave = JniHelper::getStaticMethodInfo(
jniMethodInfo,
"org/cocos2dx/cpp/AppActivity",
"requestAction",
"(ILjava/lang/String;)V");
if (isHave)
{
jstring strArg = jniMethodInfo.env->NewStringUTF(msg.c_str());
jniMethodInfo.env->CallStaticVoidMethod(jniMethodInfo.classID, jniMethodInfo.methodID, value, strArg);
jniMethodInfo.env->DeleteLocalRef(jniMethodInfo.classID);
jniMethodInfo.env->DeleteLocalRef(strArg);
}
#endif
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
//Close the cocos2d-x game scene and quit the application
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
/*To navigate back to native iOS screen(if present) without quitting the application ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/
//EventCustom customEndEvent("game_scene_close_event");
//_eventDispatcher->dispatchEvent(&customEndEvent);
}
AppActivity.java
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
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.
****************************************************************************/
package org.cocos2dx.cpp;
import android.os.*;
import android.app.Activity;
import android.util.Log;
import android.content.pm.ActivityInfo;
import android.content.Context;
import org.cocos2dx.lib.Cocos2dxActivity;
public class AppActivity extends Cocos2dxActivity {
private static final String TAG = "TAG";
private static Activity activity;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
activity = this;
}
private static Handler actionHandler = new Handler(new Handler.Callback()
{
@Override
public boolean handleMessage(Message msg)
{
if(msg.what == 0)
{
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else if(msg.what == 1)
{
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
return false;
}
});
/**
* @param actionType
* @param str
*/
public static void requestAction(int actionType,String str)
{
Log.i(TAG, "receive command ---" + actionType);
Message msg = actionHandler.obtainMessage();
msg.what = actionType;
msg.obj = str;
actionHandler.sendMessage(msg);
}
}
1, android:screenOrientation=“portrait”
After clicking “landscape” button, it displays like below :
- android:screenOrientation=“landscape”
click “portrait”
click “landscape”