PlatformObserver wip

This commit is contained in:
Attila Uygun 2023-05-25 16:02:00 +02:00
parent b1c2e3f0d4
commit 9680ca975c
5 changed files with 68 additions and 24 deletions

View File

@ -43,6 +43,7 @@ Engine::Engine(Platform* platform)
DCHECK(!singleton); DCHECK(!singleton);
singleton = this; singleton = this;
platform_->SetObserver(this);
renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this)); renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this));
stats_ = std::make_unique<ImageQuad>(); stats_ = std::make_unique<ImageQuad>();
@ -67,14 +68,7 @@ Engine& Engine::Get() {
} }
bool Engine::Initialize() { bool Engine::Initialize() {
bool res; InitializeRenderer();
#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.";
// Normalize viewport. // Normalize viewport.
if (GetScreenWidth() > GetScreenHeight()) { if (GetScreenWidth() > GetScreenHeight()) {
@ -507,6 +501,33 @@ bool Engine::IsMobile() const {
return platform_->mobile_device(); 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() { void Engine::CreateTextureCompressors() {
tex_comp_alpha_.reset(); tex_comp_alpha_.reset();
tex_comp_opaque_.reset(); tex_comp_opaque_.reset();

View File

@ -10,6 +10,7 @@
#include "base/random.h" #include "base/random.h"
#include "base/vecmath.h" #include "base/vecmath.h"
#include "engine/persistent_data.h" #include "engine/persistent_data.h"
#include "engine/platform/platform_observer.h"
class TextureCompressor; class TextureCompressor;
@ -29,7 +30,7 @@ class Shader;
class Texture; class Texture;
class Platform; class Platform;
class Engine { class Engine : public PlatformObserver {
public: public:
using CreateImageCB = std::function<std::unique_ptr<Image>()>; using CreateImageCB = std::function<std::unique_ptr<Image>()>;
@ -220,6 +221,13 @@ class Engine {
base::Randomf random_; base::Randomf random_;
// PlatformObserver implementation
void OnWindowCreated() final;
void OnWindowDestroyed() final;
void OnWindowResized(int width, int height) final;
void InitializeRenderer();
void CreateTextureCompressors(); void CreateTextureCompressors();
void ContextLost(); void ContextLost();

View File

@ -25,6 +25,7 @@ struct ANativeWindow;
namespace eng { namespace eng {
class Engine; class Engine;
class PlatformObserver;
class Platform { class Platform {
public: public:
@ -43,6 +44,8 @@ class Platform {
void Exit(); void Exit();
void SetObserver(PlatformObserver* observer) { observer_ = observer; }
// Renderer* SwitchRenderer(bool vulkan); // Renderer* SwitchRenderer(bool vulkan);
void Vibrate(int duration); void Vibrate(int duration);
@ -84,6 +87,8 @@ class Platform {
bool has_focus_ = false; bool has_focus_ = false;
bool should_exit_ = false; bool should_exit_ = false;
PlatformObserver* observer_ = nullptr;
std::unique_ptr<Engine> engine_; std::unique_ptr<Engine> engine_;
base::ThreadPool thread_pool_; base::ThreadPool thread_pool_;

View File

@ -9,6 +9,7 @@
#include "base/task_runner.h" #include "base/task_runner.h"
#include "engine/engine.h" #include "engine/engine.h"
#include "engine/input_event.h" #include "engine/input_event.h"
#include "engine/platform/platform_observer.h"
using namespace base; using namespace base;
@ -294,30 +295,21 @@ void Platform::HandleCmd(android_app* app, int32_t cmd) {
DLOG << "APP_CMD_INIT_WINDOW"; DLOG << "APP_CMD_INIT_WINDOW";
if (app->window != NULL) { if (app->window != NULL) {
platform->SetFrameRate(60); platform->SetFrameRate(60);
// bool res = platform->InitializeRenderer(); platform->observer_->OnWindowCreated();
// CHECK(res) << "Failed to initialize "
// << platform->renderer_->GetDebugName() << " renderer.";
} }
break; break;
case APP_CMD_TERM_WINDOW: case APP_CMD_TERM_WINDOW:
DLOG << "APP_CMD_TERM_WINDOW"; DLOG << "APP_CMD_TERM_WINDOW";
// platform->renderer_->Shutdown(); platform->observer_->OnWindowDestroyed();
break; break;
case APP_CMD_CONFIG_CHANGED: case APP_CMD_CONFIG_CHANGED:
DLOG << "APP_CMD_CONFIG_CHANGED"; DLOG << "APP_CMD_CONFIG_CHANGED";
// if (platform->app_->window != NULL) { if (platform->app_->window != NULL)
// int width = platform->renderer_->screen_width(); platform->observer_->OnWindowResized(
// int height = platform->renderer_->screen_height(); ANativeWindow_getWidth(app->window),
// if (width != ANativeWindow_getWidth(app->window) || ANativeWindow_getHeight(app->window));
// height != ANativeWindow_getHeight(app->window)) {
// platform->renderer_->Shutdown();
// bool res = platform->InitializeRenderer();
// CHECK(res) << "Failed to initialize "
// << platform->renderer_->GetDebugName() << " renderer.";
// }
// }
break; break;
case APP_CMD_STOP: case APP_CMD_STOP:

View File

@ -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