To detect orientation change (android, c++)

Hello. I create simplest “HelloWorld” pattern app. I succeed to change its orientation from fixed to free, by deleting (android:screenOrientation=“landscape”) in “AndroidManifest.xml”.
Now orientation changes, but how should I catch that event to get new screen size and to redraw/recreate my screens for new sizes?

  1. I display on the screen values like Director::getInstance()->getWinSizeInPixels(); - they do not changes on orientation change :frowning:
  2. But the text in the left-bottom corner (created with Director::getInstance()->setDisplayStats); ) - its position are been updated very nice and properly - I want to do the same.

I believe that Director::getVisibleOrigin() should change with orientation. It describes the offset from design resolution to screen resolution scaled by the current ResolutionPolicy.

Debug text is likely aligned to (0,0) which is at the visible origin, so it will move automatically.

I think you can hook into the orientation events by implementing applicationWillEnterForeground() in your c++ AppDelegate class.

However it must be noted that not all platforms provide a virtual function for this and so the c++11 override keyword may cause compile errors. This should probably be declared in ApplicationProtocol instead of being hidden away in the platform dependant implementations. But there you go. :stuck_out_tongue:

Alas after the orientation change, the next values remain unchanged:

Director::getInstance()->getWinSize();
Director::getInstance()->getWinSizeInPixels();
Director::getInstance()->getVisibleOrigin();
Director::getInstance()->getOpenGLView()->getFrameSize();
Director::getInstance()->getVisibleSize();
this->getContentSize();
this->getNormalizedPosition();
this->getPosition();

Node * p_this_Layer_Parent = this->getParent(); // not null; this==Layer*
p_this_Layer_Parent->getPosition(); // also no change
Node * p_grand_pa= p_this_Layer_Parent->getParent(); // null;

applicationWillEnterForeground() works as it should - been called, when app become available (revoked from background). But it’s not related with orientation change.
So, new question: how could I get new screen resolution=size in pixels, if Director::getInstance()->setDisplayStats gets it properly?

Sorry I meant

applicationScreenSizeChanged(int newWidth, int newHeight)

Not applicationWillEnterForeground()

No idea how I managed to copy paste the wrong function haha :stuck_out_tongue:

1 Like

Hi, this is how I handle orientation change:

void AppDelegate::applicationScreenSizeChanged(int newWidth, int newHeight)
{
	if (newWidth <= 0 || newHeight <= 0)
	{
		return;
	}

	auto director = Director::getInstance();
	auto glview = director->getOpenGLView();
	if (glview)
	{
		auto size = glview->getFrameSize();
		if (size.equals(Size(newWidth, newHeight)))
			return;

		glview->setFrameSize(newWidth, newHeight);

		ResolutionPolicy resolutionPolicy = glview->getResolutionPolicy();
		if (resolutionPolicy == ResolutionPolicy::UNKNOWN)
			resolutionPolicy = ResolutionPolicy::SHOW_ALL;
		glview->setDesignResolutionSize(newWidth, newHeight, resolutionPolicy);
	}
}
1 Like

Thank you all, you have really helped me and saved a lot of my time! :slight_smile:
People of the future: biggest problem I had because of the bug in my code.