From 9680ca975c8d3e01984aaea7de94d5b8fd15e92f Mon Sep 17 00:00:00 2001 From: Attila Uygun Date: Thu, 25 May 2023 16:02:00 +0200 Subject: [PATCH] PlatformObserver wip --- src/engine/engine.cc | 37 +++++++++++++++++++------ src/engine/engine.h | 10 ++++++- src/engine/platform/platform.h | 5 ++++ src/engine/platform/platform_android.cc | 22 +++++---------- src/engine/platform/platform_observer.h | 18 ++++++++++++ 5 files changed, 68 insertions(+), 24 deletions(-) create mode 100644 src/engine/platform/platform_observer.h diff --git a/src/engine/engine.cc b/src/engine/engine.cc index 7380ff4..429e830 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -43,6 +43,7 @@ Engine::Engine(Platform* platform) DCHECK(!singleton); singleton = this; + platform_->SetObserver(this); renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this)); stats_ = std::make_unique(); @@ -67,14 +68,7 @@ Engine& Engine::Get() { } bool Engine::Initialize() { - bool res; -#if defined(__ANDROID__) - res = renderer_->Initialize(platform_->GetWindow()); -#elif defined(__linux__) - res = renderer_->Initialize(platform_->GetDisplay(), platform_->GetWindow()); -#endif - CHECK(res) << "Failed to initialize " << renderer_->GetDebugName() - << " renderer."; + InitializeRenderer(); // Normalize viewport. if (GetScreenWidth() > GetScreenHeight()) { @@ -507,6 +501,33 @@ bool Engine::IsMobile() const { return platform_->mobile_device(); } +void Engine::OnWindowCreated() { + InitializeRenderer(); +} + +void Engine::OnWindowDestroyed() { + renderer_->Shutdown(); +} + +void Engine::OnWindowResized(int width, int height) { + if (width != renderer_->screen_width() || + height != renderer_->screen_height()) { + renderer_->Shutdown(); + InitializeRenderer(); + } +} + +void Engine::InitializeRenderer() { + bool res; +#if defined(__ANDROID__) + res = renderer_->Initialize(platform_->GetWindow()); +#elif defined(__linux__) + res = renderer_->Initialize(platform_->GetDisplay(), platform_->GetWindow()); +#endif + CHECK(res) << "Failed to initialize " << renderer_->GetDebugName() + << " renderer."; +} + void Engine::CreateTextureCompressors() { tex_comp_alpha_.reset(); tex_comp_opaque_.reset(); diff --git a/src/engine/engine.h b/src/engine/engine.h index 7ad79ad..2c472cb 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -10,6 +10,7 @@ #include "base/random.h" #include "base/vecmath.h" #include "engine/persistent_data.h" +#include "engine/platform/platform_observer.h" class TextureCompressor; @@ -29,7 +30,7 @@ class Shader; class Texture; class Platform; -class Engine { +class Engine : public PlatformObserver { public: using CreateImageCB = std::function()>; @@ -220,6 +221,13 @@ class Engine { base::Randomf random_; + // PlatformObserver implementation + void OnWindowCreated() final; + void OnWindowDestroyed() final; + void OnWindowResized(int width, int height) final; + + void InitializeRenderer(); + void CreateTextureCompressors(); void ContextLost(); diff --git a/src/engine/platform/platform.h b/src/engine/platform/platform.h index a027a93..09b4709 100644 --- a/src/engine/platform/platform.h +++ b/src/engine/platform/platform.h @@ -25,6 +25,7 @@ struct ANativeWindow; namespace eng { class Engine; +class PlatformObserver; class Platform { public: @@ -43,6 +44,8 @@ class Platform { void Exit(); + void SetObserver(PlatformObserver* observer) { observer_ = observer; } + // Renderer* SwitchRenderer(bool vulkan); void Vibrate(int duration); @@ -84,6 +87,8 @@ class Platform { bool has_focus_ = false; bool should_exit_ = false; + PlatformObserver* observer_ = nullptr; + std::unique_ptr engine_; base::ThreadPool thread_pool_; diff --git a/src/engine/platform/platform_android.cc b/src/engine/platform/platform_android.cc index acf5ef0..4bf821b 100644 --- a/src/engine/platform/platform_android.cc +++ b/src/engine/platform/platform_android.cc @@ -9,6 +9,7 @@ #include "base/task_runner.h" #include "engine/engine.h" #include "engine/input_event.h" +#include "engine/platform/platform_observer.h" using namespace base; @@ -294,30 +295,21 @@ void Platform::HandleCmd(android_app* app, int32_t cmd) { DLOG << "APP_CMD_INIT_WINDOW"; if (app->window != NULL) { platform->SetFrameRate(60); - // bool res = platform->InitializeRenderer(); - // CHECK(res) << "Failed to initialize " - // << platform->renderer_->GetDebugName() << " renderer."; + platform->observer_->OnWindowCreated(); } break; case APP_CMD_TERM_WINDOW: DLOG << "APP_CMD_TERM_WINDOW"; - // platform->renderer_->Shutdown(); + platform->observer_->OnWindowDestroyed(); break; case APP_CMD_CONFIG_CHANGED: DLOG << "APP_CMD_CONFIG_CHANGED"; - // if (platform->app_->window != NULL) { - // int width = platform->renderer_->screen_width(); - // int height = platform->renderer_->screen_height(); - // if (width != ANativeWindow_getWidth(app->window) || - // height != ANativeWindow_getHeight(app->window)) { - // platform->renderer_->Shutdown(); - // bool res = platform->InitializeRenderer(); - // CHECK(res) << "Failed to initialize " - // << platform->renderer_->GetDebugName() << " renderer."; - // } - // } + if (platform->app_->window != NULL) + platform->observer_->OnWindowResized( + ANativeWindow_getWidth(app->window), + ANativeWindow_getHeight(app->window)); break; case APP_CMD_STOP: diff --git a/src/engine/platform/platform_observer.h b/src/engine/platform/platform_observer.h new file mode 100644 index 0000000..4538b5a --- /dev/null +++ b/src/engine/platform/platform_observer.h @@ -0,0 +1,18 @@ +#ifndef ENGINE_PLATFORM_PLATFORM_OBSERVER_H +#define ENGINE_PLATFORM_PLATFORM_OBSERVER_H + +namespace eng { + +class PlatformObserver { + public: + PlatformObserver() = default; + virtual ~PlatformObserver() = default; + + virtual void OnWindowCreated() = 0; + virtual void OnWindowDestroyed() = 0; + virtual void OnWindowResized(int width, int height) = 0; +}; + +} // namespace eng + +#endif // ENGINE_PLATFORM_PLATFORM_OBSERVER_H