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);
singleton = this;
platform_->SetObserver(this);
renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this));
stats_ = std::make_unique<ImageQuad>();
@ -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();

View File

@ -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<std::unique_ptr<Image>()>;
@ -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();

View File

@ -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> engine_;
base::ThreadPool thread_pool_;

View File

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

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