Compare commits

..

3 Commits

Author SHA1 Message Date
Attila Uygun 9680ca975c PlatformObserver wip 2023-05-25 16:02:00 +02:00
Attila Uygun b1c2e3f0d4 for android 2023-05-25 15:33:05 +02:00
Attila Uygun aca0213b0a crash fix 2023-05-25 10:48:19 +02:00
6 changed files with 72 additions and 25 deletions

View File

@ -21,7 +21,7 @@
#include "engine/shader_source.h"
#include "third_party/texture_compressor/texture_compressor.h"
#define USE_VULKAN_RENDERER 0
#define USE_VULKAN_RENDERER 1
using namespace base;
@ -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>();
@ -50,6 +51,7 @@ Engine::Engine(Platform* platform)
Engine::~Engine() {
LOG << "Shutting down engine.";
game_.reset();
stats_.reset();
textures_.clear();
@ -66,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()) {
@ -506,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

@ -145,7 +145,9 @@ bool Platform::CreateWindow(int width, int height) {
void Platform::DestroyWindow() {
if (display_) {
XDestroyWindow(display_, window_);
#if 0 // TODO: Figure out why XCloseDisplay is crashing
XCloseDisplay(display_);
#endif
display_ = nullptr;
window_ = 0;
}

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