Platform code refactoring.

This commit is contained in:
Attila Uygun 2021-10-03 00:43:07 +02:00
parent 44aa8b2a33
commit 79d9d294e6
11 changed files with 153 additions and 216 deletions

View File

@ -76,7 +76,7 @@ add_library(kaliber SHARED
../../../src/engine/platform/asset_file_android.cc ../../../src/engine/platform/asset_file_android.cc
../../../src/engine/platform/asset_file.cc ../../../src/engine/platform/asset_file.cc
../../../src/engine/platform/platform_android.cc ../../../src/engine/platform/platform_android.cc
../../../src/engine/platform/platform_base.cc ../../../src/engine/platform/platform.cc
../../../src/engine/renderer/geometry.cc ../../../src/engine/renderer/geometry.cc
../../../src/engine/renderer/opengl/render_command.cc ../../../src/engine/renderer/opengl/render_command.cc
../../../src/engine/renderer/opengl/renderer_opengl_android.cc ../../../src/engine/renderer/opengl/renderer_opengl_android.cc

View File

@ -100,7 +100,7 @@ GLTEST_SRC := \
$(SRC_ROOT)/engine/persistent_data.cc \ $(SRC_ROOT)/engine/persistent_data.cc \
$(SRC_ROOT)/engine/platform/asset_file_linux.cc \ $(SRC_ROOT)/engine/platform/asset_file_linux.cc \
$(SRC_ROOT)/engine/platform/asset_file.cc \ $(SRC_ROOT)/engine/platform/asset_file.cc \
$(SRC_ROOT)/engine/platform/platform_base.cc \ $(SRC_ROOT)/engine/platform/platform.cc \
$(SRC_ROOT)/engine/platform/platform_linux.cc \ $(SRC_ROOT)/engine/platform/platform_linux.cc \
$(SRC_ROOT)/engine/renderer/geometry.cc \ $(SRC_ROOT)/engine/renderer/geometry.cc \
$(SRC_ROOT)/engine/renderer/opengl/render_command.cc \ $(SRC_ROOT)/engine/renderer/opengl/render_command.cc \

View File

@ -11,7 +11,6 @@
#include "../base/vecmath.h" #include "../base/vecmath.h"
#include "audio/audio_forward.h" #include "audio/audio_forward.h"
#include "persistent_data.h" #include "persistent_data.h"
#include "platform/platform_forward.h"
#include "renderer/render_resource.h" #include "renderer/render_resource.h"
class TextureCompressor; class TextureCompressor;
@ -29,6 +28,7 @@ class Renderer;
class Geometry; class Geometry;
class Shader; class Shader;
class Texture; class Texture;
class Platform;
class Engine { class Engine {
public: public:

View File

@ -13,13 +13,13 @@ using namespace base;
namespace eng { namespace eng {
PlatformBase::InternalError PlatformBase::internal_error; Platform::InternalError Platform::internal_error;
PlatformBase::PlatformBase() = default; Platform::Platform() = default;
PlatformBase::~PlatformBase() = default; Platform::~Platform() = default;
void PlatformBase::Initialize() { void Platform::InitializeCommon() {
LOG << "Initializing platform."; LOG << "Initializing platform.";
worker_.Initialize(); worker_.Initialize();
@ -38,16 +38,15 @@ void PlatformBase::Initialize() {
#endif #endif
} }
void PlatformBase::Shutdown() { void Platform::ShutdownCommon() {
LOG << "Shutting down platform."; LOG << "Shutting down platform.";
audio_->Shutdown(); audio_->Shutdown();
renderer_->Shutdown(); renderer_->Shutdown();
} }
void PlatformBase::RunMainLoop() { void Platform::RunMainLoop() {
engine_ = std::make_unique<Engine>(static_cast<Platform*>(this), engine_ = std::make_unique<Engine>(this, renderer_.get(), audio_.get());
renderer_.get(), audio_.get());
if (!engine_->Initialize()) { if (!engine_->Initialize()) {
LOG << "Failed to initialize the engine."; LOG << "Failed to initialize the engine.";
throw internal_error; throw internal_error;
@ -71,7 +70,7 @@ void PlatformBase::RunMainLoop() {
while (accumulator >= time_step) { while (accumulator >= time_step) {
TaskRunner::GetThreadLocalTaskRunner()->SingleConsumerRun(); TaskRunner::GetThreadLocalTaskRunner()->SingleConsumerRun();
static_cast<Platform*>(this)->Update(); Update();
engine_->Update(time_step); engine_->Update(time_step);
if (should_exit_) { if (should_exit_) {

View File

@ -1,20 +1,118 @@
#ifndef PLATFORM_H #ifndef PLATFORM_H
#define PLATFORM_H #define PLATFORM_H
#include <exception>
#include <memory>
#include <string>
#include "../../base/timer.h"
#include "../../base/worker.h"
#include "../audio/audio_forward.h"
#if defined(__ANDROID__) #if defined(__ANDROID__)
#include "platform_android.h"
#include "../../base/vecmath.h"
struct android_app;
struct AInputEvent;
#elif defined(__linux__) #elif defined(__linux__)
#include "platform_linux.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif #endif
namespace eng { namespace eng {
class Renderer;
class Engine;
class Platform {
public:
Platform();
~Platform();
#if defined(__ANDROID__) #if defined(__ANDROID__)
using Platform = PlatformAndroid; void Initialize(android_app* app);
#elif defined(__linux__) #elif defined(__linux__)
using Platform = PlatformLinux; void Initialize();
#endif #endif
void Shutdown();
void Update();
void Exit();
void Vibrate(int duration);
void ShowInterstitialAd();
void ShareFile(const std::string& file_name);
void SetKeepScreenOn(bool keep_screen_on);
void RunMainLoop();
int GetDeviceDpi() const { return device_dpi_; }
const std::string& GetRootPath() const { return root_path_; }
const std::string& GetDataPath() const { return data_path_; }
const std::string& GetSharedDataPath() const { return shared_data_path_; }
bool mobile_device() const { return mobile_device_; }
static class InternalError : public std::exception {
} internal_error;
protected:
base::Timer timer_;
bool mobile_device_ = false;
int device_dpi_ = 100;
std::string root_path_;
std::string data_path_;
std::string shared_data_path_;
bool has_focus_ = false;
bool should_exit_ = false;
std::unique_ptr<Audio> audio_;
std::unique_ptr<Renderer> renderer_;
std::unique_ptr<Engine> engine_;
base::Worker worker_;
#if defined(__ANDROID__)
android_app* app_ = nullptr;
base::Vector2f pointer_pos_[2] = {{0, 0}, {0, 0}};
bool pointer_down_[2] = {false, false};
static int32_t HandleInput(android_app* app, AInputEvent* event);
static void HandleCmd(android_app* app, int32_t cmd);
#elif defined(__linux__)
Display* display_ = nullptr;
Window window_ = 0;
bool CreateWindow(int width, int height);
void DestroyWindow();
#endif
void InitializeCommon();
void ShutdownCommon();
Platform(const Platform&) = delete;
Platform& operator=(const Platform&) = delete;
};
} // namespace eng } // namespace eng
#endif // PLATFORM_H #endif // PLATFORM_H

View File

@ -1,4 +1,4 @@
#include "platform_android.h" #include "platform.h"
#include <android_native_app_glue.h> #include <android_native_app_glue.h>
#include <jni.h> #include <jni.h>
@ -208,11 +208,8 @@ int32_t GetDensityDpi(android_app* app) {
namespace eng { namespace eng {
PlatformAndroid::PlatformAndroid() = default; int32_t Platform::HandleInput(android_app* app, AInputEvent* event) {
PlatformAndroid::~PlatformAndroid() = default; Platform* platform = reinterpret_cast<Platform*>(app->userData);
int32_t PlatformAndroid::HandleInput(android_app* app, AInputEvent* event) {
PlatformAndroid* platform = reinterpret_cast<PlatformAndroid*>(app->userData);
if (!platform->engine_) if (!platform->engine_)
return 0; return 0;
@ -292,8 +289,8 @@ int32_t PlatformAndroid::HandleInput(android_app* app, AInputEvent* event) {
return 0; return 0;
} }
void PlatformAndroid::HandleCmd(android_app* app, int32_t cmd) { void Platform::HandleCmd(android_app* app, int32_t cmd) {
PlatformAndroid* platform = reinterpret_cast<PlatformAndroid*>(app->userData); Platform* platform = reinterpret_cast<Platform*>(app->userData);
switch (cmd) { switch (cmd) {
case APP_CMD_SAVE_STATE: case APP_CMD_SAVE_STATE:
@ -356,8 +353,8 @@ void PlatformAndroid::HandleCmd(android_app* app, int32_t cmd) {
} }
} }
void PlatformAndroid::Initialize(android_app* app) { void Platform::Initialize(android_app* app) {
PlatformBase::Initialize(); Platform::InitializeCommon();
app_ = app; app_ = app;
@ -376,13 +373,17 @@ void PlatformAndroid::Initialize(android_app* app) {
LOG << "Device DPI: " << device_dpi_; LOG << "Device DPI: " << device_dpi_;
app->userData = reinterpret_cast<void*>(this); app->userData = reinterpret_cast<void*>(this);
app->onAppCmd = PlatformAndroid::HandleCmd; app->onAppCmd = Platform::HandleCmd;
app->onInputEvent = PlatformAndroid::HandleInput; app->onInputEvent = Platform::HandleInput;
Update(); Update();
} }
void PlatformAndroid::Update() { void Platform::Shutdown() {
Platform::ShutdownCommon();
}
void Platform::Update() {
int id; int id;
int events; int events;
android_poll_source* source; android_poll_source* source;
@ -401,35 +402,35 @@ void PlatformAndroid::Update() {
} }
} }
void PlatformAndroid::Exit() { void Platform::Exit() {
ANativeActivity_finish(app_->activity); ANativeActivity_finish(app_->activity);
} }
void PlatformAndroid::Vibrate(int duration) { void Platform::Vibrate(int duration) {
::Vibrate(app_->activity, duration); ::Vibrate(app_->activity, duration);
} }
void PlatformAndroid::ShowInterstitialAd() { void Platform::ShowInterstitialAd() {
::ShowInterstitialAd(app_->activity); ::ShowInterstitialAd(app_->activity);
} }
void PlatformAndroid::ShareFile(const std::string& file_name) { void Platform::ShareFile(const std::string& file_name) {
::ShareFile(app_->activity, file_name); ::ShareFile(app_->activity, file_name);
} }
void PlatformAndroid::SetKeepScreenOn(bool keep_screen_on) { void Platform::SetKeepScreenOn(bool keep_screen_on) {
::SetKeepScreenOn(app_->activity, keep_screen_on); ::SetKeepScreenOn(app_->activity, keep_screen_on);
} }
} // namespace eng } // namespace eng
void android_main(android_app* app) { void android_main(android_app* app) {
eng::PlatformAndroid platform; eng::Platform platform;
try { try {
platform.Initialize(app); platform.Initialize(app);
platform.RunMainLoop(); platform.RunMainLoop();
platform.Shutdown(); platform.Shutdown();
} catch (eng::PlatformBase::InternalError& e) { } catch (eng::Platform::InternalError& e) {
} }
_exit(0); _exit(0);
} }

View File

@ -1,43 +0,0 @@
#ifndef PLATFORM_ANDROID_H
#define PLATFORM_ANDROID_H
#include "../../base/vecmath.h"
#include "platform_base.h"
struct android_app;
struct AInputEvent;
namespace eng {
class PlatformAndroid : public PlatformBase {
public:
PlatformAndroid();
~PlatformAndroid();
void Initialize(android_app* app);
void Update();
void Exit();
void Vibrate(int duration);
void ShowInterstitialAd();
void ShareFile(const std::string& file_name);
void SetKeepScreenOn(bool keep_screen_on);
private:
android_app* app_ = nullptr;
base::Vector2f pointer_pos_[2] = {{0, 0}, {0, 0}};
bool pointer_down_[2] = {false, false};
static int32_t HandleInput(android_app* app, AInputEvent* event);
static void HandleCmd(android_app* app, int32_t cmd);
};
} // namespace eng
#endif // PLATFORM_ANDROID_H

View File

@ -1,65 +0,0 @@
#ifndef PLATFORM_BASE_H
#define PLATFORM_BASE_H
#include <exception>
#include <memory>
#include <string>
#include "../../base/timer.h"
#include "../../base/worker.h"
#include "../audio/audio_forward.h"
namespace eng {
class Renderer;
class Engine;
class PlatformBase {
public:
void Initialize();
void Shutdown();
void RunMainLoop();
int GetDeviceDpi() const { return device_dpi_; }
const std::string& GetRootPath() const { return root_path_; }
const std::string& GetDataPath() const { return data_path_; }
const std::string& GetSharedDataPath() const { return shared_data_path_; }
bool mobile_device() const { return mobile_device_; }
static class InternalError : public std::exception {
} internal_error;
protected:
base::Timer timer_;
bool mobile_device_ = false;
int device_dpi_ = 100;
std::string root_path_;
std::string data_path_;
std::string shared_data_path_;
bool has_focus_ = false;
bool should_exit_ = false;
std::unique_ptr<Audio> audio_;
std::unique_ptr<Renderer> renderer_;
std::unique_ptr<Engine> engine_;
base::Worker worker_;
PlatformBase();
~PlatformBase();
PlatformBase(const PlatformBase&) = delete;
PlatformBase& operator=(const PlatformBase&) = delete;
};
} // namespace eng
#endif // PLATFORM_BASE_H

View File

@ -1,16 +0,0 @@
#ifndef PLATFORM_FORWARD_H
#define PLATFORM_FORWARD_H
namespace eng {
#if defined(__ANDROID__)
class PlatformAndroid;
using Platform = PlatformAndroid;
#elif defined(__linux__)
class PlatformLinux;
using Platform = PlatformLinux;
#endif
} // namespace eng
#endif // PLATFORM_FORWARD_H

View File

@ -1,4 +1,4 @@
#include "platform_linux.h" #include "platform.h"
#include <memory> #include <memory>
@ -14,11 +14,8 @@ using namespace base;
namespace eng { namespace eng {
PlatformLinux::PlatformLinux() = default; void Platform::Initialize() {
PlatformLinux::~PlatformLinux() = default; Platform::InitializeCommon();
void PlatformLinux::Initialize() {
PlatformBase::Initialize();
root_path_ = "../../"; root_path_ = "../../";
LOG << "Root path: " << root_path_.c_str(); LOG << "Root path: " << root_path_.c_str();
@ -46,13 +43,13 @@ void PlatformLinux::Initialize() {
XSetWMProtocols(display_, window_, &WM_DELETE_WINDOW, 1); XSetWMProtocols(display_, window_, &WM_DELETE_WINDOW, 1);
} }
void PlatformLinux::Shutdown() { void Platform::Shutdown() {
PlatformBase::Shutdown(); Platform::ShutdownCommon();
DestroyWindow(); DestroyWindow();
} }
void PlatformLinux::Update() { void Platform::Update() {
while (XPending(display_)) { while (XPending(display_)) {
XEvent e; XEvent e;
XNextEvent(display_, &e); XNextEvent(display_, &e);
@ -113,11 +110,19 @@ void PlatformLinux::Update() {
} }
} }
void PlatformLinux::Exit() { void Platform::Exit() {
should_exit_ = true; should_exit_ = true;
} }
bool PlatformLinux::CreateWindow(int width, int height) { void Platform::Vibrate(int duration) {}
void Platform::ShowInterstitialAd() {}
void Platform::ShareFile(const std::string& file_name) {}
void Platform::SetKeepScreenOn(bool keep_screen_on) {}
bool Platform::CreateWindow(int width, int height) {
// Try to open the local display. // Try to open the local display.
display_ = XOpenDisplay(NULL); display_ = XOpenDisplay(NULL);
if (!display_) { if (!display_) {
@ -149,7 +154,7 @@ bool PlatformLinux::CreateWindow(int width, int height) {
return true; return true;
} }
void PlatformLinux::DestroyWindow() { void Platform::DestroyWindow() {
if (display_) { if (display_) {
XDestroyWindow(display_, window_); XDestroyWindow(display_, window_);
XCloseDisplay(display_); XCloseDisplay(display_);
@ -161,12 +166,12 @@ void PlatformLinux::DestroyWindow() {
} // namespace eng } // namespace eng
int main(int argc, char** argv) { int main(int argc, char** argv) {
eng::PlatformLinux platform; eng::Platform platform;
try { try {
platform.Initialize(); platform.Initialize();
platform.RunMainLoop(); platform.RunMainLoop();
platform.Shutdown(); platform.Shutdown();
} catch (eng::PlatformBase::InternalError& e) { } catch (eng::Platform::InternalError& e) {
return -1; return -1;
} }
return 0; return 0;

View File

@ -1,42 +0,0 @@
#ifndef PLATFORM_LINUX_H
#define PLATFORM_LINUX_H
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "platform_base.h"
namespace eng {
class PlatformLinux : public PlatformBase {
public:
PlatformLinux();
~PlatformLinux();
void Initialize();
void Shutdown();
void Update();
void Exit();
void Vibrate(int duration) {}
void ShowInterstitialAd() {}
void ShareFile(const std::string& file_name) {}
void SetKeepScreenOn(bool keep_screen_on) {}
private:
Display* display_ = nullptr;
Window window_ = 0;
bool CreateWindow(int width, int height);
void DestroyWindow();
};
} // namespace eng
#endif // PLATFORM_LINUX_H