summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEgor <egor.yusov@gmail.com>2018-02-18 08:23:05 +0000
committerEgor <egor.yusov@gmail.com>2018-02-18 08:23:05 +0000
commitf6ffee9ce5e80ae849bb2d55d9b4dbbcc1dcc532 (patch)
treedc873496bbddeb9bcd2fcc6564604cadebaa21f7
parentFew minor updates to lua test scripts (diff)
downloadDiligentEngine-f6ffee9ce5e80ae849bb2d55d9b4dbbcc1dcc532.tar.gz
DiligentEngine-f6ffee9ce5e80ae849bb2d55d9b4dbbcc1dcc532.zip
Improved device orientation/resize handling on Android
-rw-r--r--Common/NativeApp/include/Android/AndroidAppBase.h24
-rw-r--r--Common/NativeApp/src/Android/AndroidAppBase.cpp34
m---------DiligentSamples0
-rw-r--r--Tests/TestApp/src/Android/TestAppAndroid.cpp1
-rw-r--r--unityplugin/UnityEmulator/src/Android/UnityAppAndroid.cpp1
5 files changed, 43 insertions, 17 deletions
diff --git a/Common/NativeApp/include/Android/AndroidAppBase.h b/Common/NativeApp/include/Android/AndroidAppBase.h
index b291f0a..eeedf75 100644
--- a/Common/NativeApp/include/Android/AndroidAppBase.h
+++ b/Common/NativeApp/include/Android/AndroidAppBase.h
@@ -44,9 +44,25 @@ public:
virtual void TermDisplay() = 0;
static int32_t HandleInput( android_app* app, AInputEvent* event );
static void HandleCmd( struct android_app* app, int32_t cmd );
-
+ bool CheckWindowSizeChanged()
+ {
+ auto new_window_width_ = ANativeWindow_getWidth(app_->window);
+ auto new_window_height_ = ANativeWindow_getHeight(app_->window);
+ if(new_window_width_ != window_width_ || new_window_height_ != window_height_)
+ {
+ window_width_ = new_window_width_;
+ window_height_ = new_window_height_;
+ return true;
+ }
+ else
+ return false;
+ }
protected:
- virtual void Initialize(ANativeWindow* window) = 0;
+ virtual void Initialize(ANativeWindow* window)
+ {
+ CheckWindowSizeChanged();
+ }
+
virtual int Resume(ANativeWindow* window) = 0;
virtual int32_t HandleInput(AInputEvent* event ){return 0;}
@@ -79,7 +95,9 @@ private:
android_app* app_ = nullptr;
bool initialized_resources_ = false;
bool has_focus_ = false;
-
+ int32_t window_width_ = 0;
+ int32_t window_height_ = 0;
+
ASensorManager* sensor_manager_ = nullptr;
const ASensor* accelerometer_sensor_ = nullptr;
ASensorEventQueue* sensor_event_queue_ = nullptr;
diff --git a/Common/NativeApp/src/Android/AndroidAppBase.cpp b/Common/NativeApp/src/Android/AndroidAppBase.cpp
index 2743c14..31e2724 100644
--- a/Common/NativeApp/src/Android/AndroidAppBase.cpp
+++ b/Common/NativeApp/src/Android/AndroidAppBase.cpp
@@ -48,21 +48,8 @@ int AndroidAppBase::InitDisplay()
LoadResources();
}
}
-
- ShowUI();
-
- // auto width = pSwapChain_->GetDesc().Width;
- // auto height = pSwapChain_->GetDesc().Height;
-
- // //Note that screen size might have been changed
- // pDeviceContext_->SetViewports( 1, nullptr, width, height );
- // //renderer_.UpdateViewport();
- // WindowResize(width, height);
-
- // // Send the new window size to AntTweakBar
- // TwWindowSize(width, height);
-
+ ShowUI();
//tap_camera_.SetFlip( 1.f, -1.f, -1.f );
//tap_camera_.SetPinchTransformFactor( 2.f, 2.f, 8.f );
@@ -82,6 +69,12 @@ void AndroidAppBase::InitSensors()
//
void AndroidAppBase::DrawFrame()
{
+ // APP_CMD_CONFIG_CHANGED event is generated seveal frames
+ // before the screen is actually resized. The only robust way
+ // to detect window resize is to check it very frame
+ if(CheckWindowSizeChanged())
+ WindowResize(0,0);
+
float fFPS;
if( monitor_.Update( fFPS ) )
{
@@ -126,6 +119,7 @@ void AndroidAppBase::HandleCmd( struct android_app* app, int32_t cmd )
{
case APP_CMD_SAVE_STATE:
break;
+
case APP_CMD_INIT_WINDOW:
// The window is being shown, get it ready.
if( app->window != NULL )
@@ -134,24 +128,36 @@ void AndroidAppBase::HandleCmd( struct android_app* app, int32_t cmd )
eng->DrawFrame();
}
break;
+
+ case APP_CMD_CONFIG_CHANGED:
+ case APP_CMD_WINDOW_RESIZED:
+ // This does not work as the screen resizes few frames
+ // after the event has been received
+ // eng->WindowResize(0,0);
+ break;
+
case APP_CMD_TERM_WINDOW:
// The window is being hidden or closed, clean it up.
eng->TermDisplay();
eng->has_focus_ = false;
break;
+
case APP_CMD_STOP:
break;
+
case APP_CMD_GAINED_FOCUS:
eng->ResumeSensors();
//Start animation
eng->has_focus_ = true;
break;
+
case APP_CMD_LOST_FOCUS:
eng->SuspendSensors();
// Also stop animating.
eng->has_focus_ = false;
eng->DrawFrame();
break;
+
case APP_CMD_LOW_MEMORY:
//Free up GL resources
eng->TrimMemory();
diff --git a/DiligentSamples b/DiligentSamples
-Subproject bd9b37d09342cdd28e63bf33ca86f5c22d91477
+Subproject 1398b133b8e3993cfaed1f5603e5c7f371cb511
diff --git a/Tests/TestApp/src/Android/TestAppAndroid.cpp b/Tests/TestApp/src/Android/TestAppAndroid.cpp
index 8c91f72..222081a 100644
--- a/Tests/TestApp/src/Android/TestAppAndroid.cpp
+++ b/Tests/TestApp/src/Android/TestAppAndroid.cpp
@@ -36,6 +36,7 @@ public:
virtual void Initialize(ANativeWindow* window)override final
{
+ TestApp::Initialize(window);
InitializeDiligentEngine(window);
m_RenderDeviceGLES = RefCntAutoPtr<IRenderDeviceGLES>(m_pDevice, IID_RenderDeviceGLES);
InitializeRenderers();
diff --git a/unityplugin/UnityEmulator/src/Android/UnityAppAndroid.cpp b/unityplugin/UnityEmulator/src/Android/UnityAppAndroid.cpp
index e37bc0f..47db464 100644
--- a/unityplugin/UnityEmulator/src/Android/UnityAppAndroid.cpp
+++ b/unityplugin/UnityEmulator/src/Android/UnityAppAndroid.cpp
@@ -37,6 +37,7 @@ public:
virtual void Initialize(ANativeWindow* window)override final
{
+ UnityAppBase::Initialize(window);
InitGraphics(window, 0/*Unused*/, 0/*Unused*/);
InitScene();
}