mirror of https://github.com/auygun/kaliber.git
Platform code refactoring.
This commit is contained in:
parent
44aa8b2a33
commit
79d9d294e6
|
@ -76,7 +76,7 @@ add_library(kaliber SHARED
|
|||
../../../src/engine/platform/asset_file_android.cc
|
||||
../../../src/engine/platform/asset_file.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/opengl/render_command.cc
|
||||
../../../src/engine/renderer/opengl/renderer_opengl_android.cc
|
||||
|
|
|
@ -100,7 +100,7 @@ GLTEST_SRC := \
|
|||
$(SRC_ROOT)/engine/persistent_data.cc \
|
||||
$(SRC_ROOT)/engine/platform/asset_file_linux.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/renderer/geometry.cc \
|
||||
$(SRC_ROOT)/engine/renderer/opengl/render_command.cc \
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
#include "../base/vecmath.h"
|
||||
#include "audio/audio_forward.h"
|
||||
#include "persistent_data.h"
|
||||
#include "platform/platform_forward.h"
|
||||
#include "renderer/render_resource.h"
|
||||
|
||||
class TextureCompressor;
|
||||
|
@ -29,6 +28,7 @@ class Renderer;
|
|||
class Geometry;
|
||||
class Shader;
|
||||
class Texture;
|
||||
class Platform;
|
||||
|
||||
class Engine {
|
||||
public:
|
||||
|
|
|
@ -13,13 +13,13 @@ using namespace base;
|
|||
|
||||
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.";
|
||||
|
||||
worker_.Initialize();
|
||||
|
@ -38,16 +38,15 @@ void PlatformBase::Initialize() {
|
|||
#endif
|
||||
}
|
||||
|
||||
void PlatformBase::Shutdown() {
|
||||
void Platform::ShutdownCommon() {
|
||||
LOG << "Shutting down platform.";
|
||||
|
||||
audio_->Shutdown();
|
||||
renderer_->Shutdown();
|
||||
}
|
||||
|
||||
void PlatformBase::RunMainLoop() {
|
||||
engine_ = std::make_unique<Engine>(static_cast<Platform*>(this),
|
||||
renderer_.get(), audio_.get());
|
||||
void Platform::RunMainLoop() {
|
||||
engine_ = std::make_unique<Engine>(this, renderer_.get(), audio_.get());
|
||||
if (!engine_->Initialize()) {
|
||||
LOG << "Failed to initialize the engine.";
|
||||
throw internal_error;
|
||||
|
@ -71,7 +70,7 @@ void PlatformBase::RunMainLoop() {
|
|||
while (accumulator >= time_step) {
|
||||
TaskRunner::GetThreadLocalTaskRunner()->SingleConsumerRun();
|
||||
|
||||
static_cast<Platform*>(this)->Update();
|
||||
Update();
|
||||
engine_->Update(time_step);
|
||||
|
||||
if (should_exit_) {
|
|
@ -1,20 +1,118 @@
|
|||
#ifndef 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__)
|
||||
#include "platform_android.h"
|
||||
|
||||
#include "../../base/vecmath.h"
|
||||
|
||||
struct android_app;
|
||||
struct AInputEvent;
|
||||
|
||||
#elif defined(__linux__)
|
||||
#include "platform_linux.h"
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
|
||||
#endif
|
||||
|
||||
namespace eng {
|
||||
|
||||
class Renderer;
|
||||
class Engine;
|
||||
|
||||
class Platform {
|
||||
public:
|
||||
Platform();
|
||||
~Platform();
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
using Platform = PlatformAndroid;
|
||||
void Initialize(android_app* app);
|
||||
#elif defined(__linux__)
|
||||
using Platform = PlatformLinux;
|
||||
void Initialize();
|
||||
#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
|
||||
|
||||
#endif // PLATFORM_H
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "platform_android.h"
|
||||
#include "platform.h"
|
||||
|
||||
#include <android_native_app_glue.h>
|
||||
#include <jni.h>
|
||||
|
@ -208,11 +208,8 @@ int32_t GetDensityDpi(android_app* app) {
|
|||
|
||||
namespace eng {
|
||||
|
||||
PlatformAndroid::PlatformAndroid() = default;
|
||||
PlatformAndroid::~PlatformAndroid() = default;
|
||||
|
||||
int32_t PlatformAndroid::HandleInput(android_app* app, AInputEvent* event) {
|
||||
PlatformAndroid* platform = reinterpret_cast<PlatformAndroid*>(app->userData);
|
||||
int32_t Platform::HandleInput(android_app* app, AInputEvent* event) {
|
||||
Platform* platform = reinterpret_cast<Platform*>(app->userData);
|
||||
|
||||
if (!platform->engine_)
|
||||
return 0;
|
||||
|
@ -292,8 +289,8 @@ int32_t PlatformAndroid::HandleInput(android_app* app, AInputEvent* event) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
void PlatformAndroid::HandleCmd(android_app* app, int32_t cmd) {
|
||||
PlatformAndroid* platform = reinterpret_cast<PlatformAndroid*>(app->userData);
|
||||
void Platform::HandleCmd(android_app* app, int32_t cmd) {
|
||||
Platform* platform = reinterpret_cast<Platform*>(app->userData);
|
||||
|
||||
switch (cmd) {
|
||||
case APP_CMD_SAVE_STATE:
|
||||
|
@ -356,8 +353,8 @@ void PlatformAndroid::HandleCmd(android_app* app, int32_t cmd) {
|
|||
}
|
||||
}
|
||||
|
||||
void PlatformAndroid::Initialize(android_app* app) {
|
||||
PlatformBase::Initialize();
|
||||
void Platform::Initialize(android_app* app) {
|
||||
Platform::InitializeCommon();
|
||||
|
||||
app_ = app;
|
||||
|
||||
|
@ -376,13 +373,17 @@ void PlatformAndroid::Initialize(android_app* app) {
|
|||
LOG << "Device DPI: " << device_dpi_;
|
||||
|
||||
app->userData = reinterpret_cast<void*>(this);
|
||||
app->onAppCmd = PlatformAndroid::HandleCmd;
|
||||
app->onInputEvent = PlatformAndroid::HandleInput;
|
||||
app->onAppCmd = Platform::HandleCmd;
|
||||
app->onInputEvent = Platform::HandleInput;
|
||||
|
||||
Update();
|
||||
}
|
||||
|
||||
void PlatformAndroid::Update() {
|
||||
void Platform::Shutdown() {
|
||||
Platform::ShutdownCommon();
|
||||
}
|
||||
|
||||
void Platform::Update() {
|
||||
int id;
|
||||
int events;
|
||||
android_poll_source* source;
|
||||
|
@ -401,35 +402,35 @@ void PlatformAndroid::Update() {
|
|||
}
|
||||
}
|
||||
|
||||
void PlatformAndroid::Exit() {
|
||||
void Platform::Exit() {
|
||||
ANativeActivity_finish(app_->activity);
|
||||
}
|
||||
|
||||
void PlatformAndroid::Vibrate(int duration) {
|
||||
void Platform::Vibrate(int duration) {
|
||||
::Vibrate(app_->activity, duration);
|
||||
}
|
||||
|
||||
void PlatformAndroid::ShowInterstitialAd() {
|
||||
void Platform::ShowInterstitialAd() {
|
||||
::ShowInterstitialAd(app_->activity);
|
||||
}
|
||||
|
||||
void PlatformAndroid::ShareFile(const std::string& file_name) {
|
||||
void Platform::ShareFile(const std::string& 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);
|
||||
}
|
||||
|
||||
} // namespace eng
|
||||
|
||||
void android_main(android_app* app) {
|
||||
eng::PlatformAndroid platform;
|
||||
eng::Platform platform;
|
||||
try {
|
||||
platform.Initialize(app);
|
||||
platform.RunMainLoop();
|
||||
platform.Shutdown();
|
||||
} catch (eng::PlatformBase::InternalError& e) {
|
||||
} catch (eng::Platform::InternalError& e) {
|
||||
}
|
||||
_exit(0);
|
||||
}
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -1,4 +1,4 @@
|
|||
#include "platform_linux.h"
|
||||
#include "platform.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
@ -14,11 +14,8 @@ using namespace base;
|
|||
|
||||
namespace eng {
|
||||
|
||||
PlatformLinux::PlatformLinux() = default;
|
||||
PlatformLinux::~PlatformLinux() = default;
|
||||
|
||||
void PlatformLinux::Initialize() {
|
||||
PlatformBase::Initialize();
|
||||
void Platform::Initialize() {
|
||||
Platform::InitializeCommon();
|
||||
|
||||
root_path_ = "../../";
|
||||
LOG << "Root path: " << root_path_.c_str();
|
||||
|
@ -46,13 +43,13 @@ void PlatformLinux::Initialize() {
|
|||
XSetWMProtocols(display_, window_, &WM_DELETE_WINDOW, 1);
|
||||
}
|
||||
|
||||
void PlatformLinux::Shutdown() {
|
||||
PlatformBase::Shutdown();
|
||||
void Platform::Shutdown() {
|
||||
Platform::ShutdownCommon();
|
||||
|
||||
DestroyWindow();
|
||||
}
|
||||
|
||||
void PlatformLinux::Update() {
|
||||
void Platform::Update() {
|
||||
while (XPending(display_)) {
|
||||
XEvent e;
|
||||
XNextEvent(display_, &e);
|
||||
|
@ -113,11 +110,19 @@ void PlatformLinux::Update() {
|
|||
}
|
||||
}
|
||||
|
||||
void PlatformLinux::Exit() {
|
||||
void Platform::Exit() {
|
||||
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.
|
||||
display_ = XOpenDisplay(NULL);
|
||||
if (!display_) {
|
||||
|
@ -149,7 +154,7 @@ bool PlatformLinux::CreateWindow(int width, int height) {
|
|||
return true;
|
||||
}
|
||||
|
||||
void PlatformLinux::DestroyWindow() {
|
||||
void Platform::DestroyWindow() {
|
||||
if (display_) {
|
||||
XDestroyWindow(display_, window_);
|
||||
XCloseDisplay(display_);
|
||||
|
@ -161,12 +166,12 @@ void PlatformLinux::DestroyWindow() {
|
|||
} // namespace eng
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
eng::PlatformLinux platform;
|
||||
eng::Platform platform;
|
||||
try {
|
||||
platform.Initialize();
|
||||
platform.RunMainLoop();
|
||||
platform.Shutdown();
|
||||
} catch (eng::PlatformBase::InternalError& e) {
|
||||
} catch (eng::Platform::InternalError& e) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue