From f6ffee9ce5e80ae849bb2d55d9b4dbbcc1dcc532 Mon Sep 17 00:00:00 2001 From: Egor Date: Sun, 18 Feb 2018 00:23:05 -0800 Subject: Improved device orientation/resize handling on Android --- Common/NativeApp/include/Android/AndroidAppBase.h | 24 ++++++++++++++-- Common/NativeApp/src/Android/AndroidAppBase.cpp | 34 +++++++++++++---------- 2 files changed, 41 insertions(+), 17 deletions(-) (limited to 'Common') 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(); -- cgit v1.2.3