diff --git a/build/android/app/CMakeLists.txt b/build/android/app/CMakeLists.txt index 129ee62..e10cd65 100644 --- a/build/android/app/CMakeLists.txt +++ b/build/android/app/CMakeLists.txt @@ -74,7 +74,6 @@ 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.cc ../../../src/engine/renderer/geometry.cc ../../../src/engine/renderer/opengl/render_command.cc ../../../src/engine/renderer/opengl/renderer_opengl_android.cc diff --git a/build/linux/Makefile b/build/linux/Makefile index 022be5a..8da112c 100644 --- a/build/linux/Makefile +++ b/build/linux/Makefile @@ -106,7 +106,6 @@ ENGINE_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.cc \ $(SRC_ROOT)/engine/platform/platform_linux.cc \ $(SRC_ROOT)/engine/renderer/geometry.cc \ $(SRC_ROOT)/engine/renderer/opengl/render_command.cc \ diff --git a/src/engine/engine.cc b/src/engine/engine.cc index dbfb718..7b5a27a 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -1,6 +1,7 @@ #include "engine/engine.h" #include "base/log.h" +#include "base/task_runner.h" #include "engine/animator.h" #include "engine/audio/audio_mixer.h" #include "engine/drawable.h" @@ -27,6 +28,11 @@ using namespace base; namespace eng { +extern void KaliberMain(Platform* platform) { + TaskRunner::CreateThreadLocalTaskRunner(); + Engine(platform).Run(); +} + Engine* Engine::singleton = nullptr; Engine::Engine(Platform* platform) @@ -67,7 +73,41 @@ Engine& Engine::Get() { return *singleton; } +void Engine::Run() { + CHECK(Initialize()) << "Failed to initialize the engine."; + + timer_.Reset(); + float accumulator = 0.0; + float frame_frac = 0.0f; + + for (;;) { + Draw(frame_frac); + + // Accumulate time. + timer_.Update(); + accumulator += timer_.GetSecondsPassed(); + + // Subdivide the frame time using fixed time steps. + while (accumulator >= time_step_) { + TaskRunner::GetThreadLocalTaskRunner()->SingleConsumerRun(); + + platform_->Update(); + Update(time_step_); + + if (platform_->should_exit()) { + return; + } + accumulator -= time_step_; + }; + + // Calculate frame fraction from remainder of the frame time. + frame_frac = accumulator / time_step_; + } +} + bool Engine::Initialize() { + thread_pool_.Initialize(); + InitializeRenderer(); // Normalize viewport. @@ -477,6 +517,7 @@ void Engine::LostFocus() { } void Engine::GainedFocus(bool from_interstitial_ad) { + timer_.Reset(); audio_mixer_->Resume(); if (game_) diff --git a/src/engine/engine.h b/src/engine/engine.h index 0ac97e3..2d7b3f0 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -8,6 +8,8 @@ #include #include "base/random.h" +#include "base/thread_pool.h" +#include "base/timer.h" #include "base/vecmath.h" #include "engine/persistent_data.h" #include "engine/platform/platform_observer.h" @@ -39,10 +41,7 @@ class Engine : public PlatformObserver { static Engine& Get(); - bool Initialize(); - - void Update(float delta_time); - void Draw(float frame_frac); + void Run(); void AddDrawable(Drawable* drawable); void RemoveDrawable(Drawable* drawable); @@ -215,8 +214,15 @@ class Engine : public PlatformObserver { bool replaying_ = false; unsigned int replay_index_ = 0; + base::ThreadPool thread_pool_; + base::Timer timer_; base::Randomf random_; + bool Initialize(); + + void Update(float delta_time); + void Draw(float frame_frac); + // PlatformObserver implementation void OnWindowCreated() final; void OnWindowDestroyed() final; diff --git a/src/engine/platform/platform.cc b/src/engine/platform/platform.cc deleted file mode 100644 index e1a7370..0000000 --- a/src/engine/platform/platform.cc +++ /dev/null @@ -1,81 +0,0 @@ -#include "engine/platform/platform.h" - -#include - -#include "base/log.h" -#include "base/task_runner.h" -#include "engine/engine.h" - -using namespace base; - -namespace eng { - -Platform::Platform() = default; - -Platform::~Platform() = default; - -// Renderer* Platform::SwitchRenderer(bool vulkan) { -// DCHECK(renderer_); - -// if ((dynamic_cast(renderer_.get()) && vulkan) || -// (dynamic_cast(renderer_.get()) && !vulkan)) -// return renderer_.get(); - -// if (vulkan) -// renderer_ = std::make_unique(); -// else -// renderer_ = std::make_unique(); - -// bool result = InitializeRenderer(); -// CHECK(result) << "Failed to initialize " << renderer_->GetDebugName() -// << " renderer."; -// LOG << "Switched to " << renderer_->GetDebugName() << " renderer."; -// return renderer_.get(); -// } - -void Platform::InitializeCommon() { - LOG << "Initializing platform."; - - thread_pool_.Initialize(); - TaskRunner::CreateThreadLocalTaskRunner(); -} - -void Platform::RunMainLoop() { - std::unique_ptr engine = std::make_unique(this); - bool res = engine->Initialize(); - CHECK(res) << "Failed to initialize the engine."; - - // Use fixed time steps. - float time_step = engine->time_step(); - - timer_.Reset(); - float accumulator = 0.0; - float frame_frac = 0.0f; - - for (;;) { - engine->Draw(frame_frac); - - // Accumulate time. - timer_.Update(); - accumulator += timer_.GetSecondsPassed(); - - // Subdivide the frame time. - while (accumulator >= time_step) { - TaskRunner::GetThreadLocalTaskRunner()->SingleConsumerRun(); - - Update(); - engine->Update(time_step); - - if (should_exit_) { - thread_pool_.Shutdown(); - return; - } - accumulator -= time_step; - }; - - // Calculate frame fraction from remainder of the frame time. - frame_frac = accumulator / time_step; - } -} - -} // namespace eng diff --git a/src/engine/platform/platform.h b/src/engine/platform/platform.h index 370f189..37e1805 100644 --- a/src/engine/platform/platform.h +++ b/src/engine/platform/platform.h @@ -3,9 +3,6 @@ #include -#include "base/thread_pool.h" -#include "base/timer.h" - #if defined(__ANDROID__) #include "../../base/vecmath.h" @@ -27,16 +24,12 @@ class PlatformObserver; class Platform { public: - Platform(); - ~Platform(); - #if defined(__ANDROID__) - void Initialize(android_app* app); + Platform(android_app* app); #elif defined(__linux__) - void Initialize(); + Platform(); #endif - - void Shutdown(); + ~Platform(); void Update(); @@ -44,8 +37,6 @@ class Platform { void SetObserver(PlatformObserver* observer) { observer_ = observer; } - // Renderer* SwitchRenderer(bool vulkan); - void Vibrate(int duration); void ShowInterstitialAd(); @@ -54,8 +45,6 @@ class Platform { void SetKeepScreenOn(bool keep_screen_on); - void RunMainLoop(); - int GetDeviceDpi() const { return device_dpi_; } const std::string& GetRootPath() const { return root_path_; } @@ -66,6 +55,8 @@ class Platform { bool mobile_device() const { return mobile_device_; } + bool should_exit() const { return should_exit_; } + #if defined(__ANDROID__) ANativeWindow* GetWindow(); #elif defined(__linux__) @@ -74,8 +65,6 @@ class Platform { #endif private: - base::Timer timer_; - bool mobile_device_ = false; int device_dpi_ = 100; std::string root_path_; @@ -87,8 +76,6 @@ class Platform { PlatformObserver* observer_ = nullptr; - base::ThreadPool thread_pool_; - #if defined(__ANDROID__) android_app* app_ = nullptr; @@ -126,8 +113,6 @@ class Platform { #endif - void InitializeCommon(); - Platform(const Platform&) = delete; Platform& operator=(const Platform&) = delete; }; diff --git a/src/engine/platform/platform_android.cc b/src/engine/platform/platform_android.cc index 934efcf..1e9ef64 100644 --- a/src/engine/platform/platform_android.cc +++ b/src/engine/platform/platform_android.cc @@ -8,7 +8,6 @@ #include #include "base/log.h" -#include "base/task_runner.h" #include "engine/input_event.h" #include "engine/platform/platform_observer.h" @@ -202,6 +201,8 @@ int32_t GetDensityDpi(android_app* app) { namespace eng { +void KaliberMain(Platform* platform); + int32_t Platform::HandleInput(android_app* app, AInputEvent* event) { Platform* platform = reinterpret_cast(app->userData); @@ -316,7 +317,7 @@ void Platform::HandleCmd(android_app* app, int32_t cmd) { case APP_CMD_GAINED_FOCUS: DLOG << "APP_CMD_GAINED_FOCUS"; - platform->timer_.Reset(); + // platform->timer_.Reset(); platform->has_focus_ = true; if (platform->observer_) platform->observer_->GainedFocus(g_showing_interstitial_ad); @@ -336,11 +337,8 @@ void Platform::HandleCmd(android_app* app, int32_t cmd) { } } -void Platform::Initialize(android_app* app) { - Platform::InitializeCommon(); - +Platform::Platform(android_app* app) { app_ = app; - mobile_device_ = true; root_path_ = ::GetApkPath(app->activity); @@ -374,7 +372,7 @@ void Platform::Initialize(android_app* app) { Update(); } -void Platform::Shutdown() { +Platform::~Platform() { LOG << "Shutting down platform."; } @@ -435,9 +433,7 @@ ANativeWindow* Platform::GetWindow() { } // namespace eng void android_main(android_app* app) { - eng::Platform platform; - platform.Initialize(app); - platform.RunMainLoop(); - platform.Shutdown(); + eng::Platform platform(app); + KaliberMain(&platform); _exit(0); } diff --git a/src/engine/platform/platform_linux.cc b/src/engine/platform/platform_linux.cc index dde7862..0b365d3 100644 --- a/src/engine/platform/platform_linux.cc +++ b/src/engine/platform/platform_linux.cc @@ -3,7 +3,6 @@ #include #include "base/log.h" -#include "base/task_runner.h" #include "base/vecmath.h" #include "engine/input_event.h" #include "engine/platform/platform_observer.h" @@ -12,9 +11,9 @@ using namespace base; namespace eng { -void Platform::Initialize() { - Platform::InitializeCommon(); +void KaliberMain(Platform* platform); +Platform::Platform() { root_path_ = "../../"; LOG << "Root path: " << root_path_.c_str(); @@ -34,7 +33,7 @@ void Platform::Initialize() { XSetWMProtocols(display_, window_, &WM_DELETE_WINDOW, 1); } -void Platform::Shutdown() { +Platform::~Platform() { LOG << "Shutting down platform."; DestroyWindow(); } @@ -170,8 +169,6 @@ XVisualInfo* Platform::GetXVisualInfo(Display* display) { int main(int argc, char** argv) { eng::Platform platform; - platform.Initialize(); - platform.RunMainLoop(); - platform.Shutdown(); + KaliberMain(&platform); return 0; }