This commit is contained in:
Attila Uygun 2023-05-26 00:40:35 +02:00
parent 62033ab64d
commit 2afe66857a
8 changed files with 67 additions and 125 deletions

View File

@ -74,7 +74,6 @@ 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.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

@ -106,7 +106,6 @@ ENGINE_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.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

@ -1,6 +1,7 @@
#include "engine/engine.h" #include "engine/engine.h"
#include "base/log.h" #include "base/log.h"
#include "base/task_runner.h"
#include "engine/animator.h" #include "engine/animator.h"
#include "engine/audio/audio_mixer.h" #include "engine/audio/audio_mixer.h"
#include "engine/drawable.h" #include "engine/drawable.h"
@ -27,6 +28,11 @@ using namespace base;
namespace eng { namespace eng {
extern void KaliberMain(Platform* platform) {
TaskRunner::CreateThreadLocalTaskRunner();
Engine(platform).Run();
}
Engine* Engine::singleton = nullptr; Engine* Engine::singleton = nullptr;
Engine::Engine(Platform* platform) Engine::Engine(Platform* platform)
@ -67,7 +73,41 @@ Engine& Engine::Get() {
return *singleton; 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() { bool Engine::Initialize() {
thread_pool_.Initialize();
InitializeRenderer(); InitializeRenderer();
// Normalize viewport. // Normalize viewport.
@ -477,6 +517,7 @@ void Engine::LostFocus() {
} }
void Engine::GainedFocus(bool from_interstitial_ad) { void Engine::GainedFocus(bool from_interstitial_ad) {
timer_.Reset();
audio_mixer_->Resume(); audio_mixer_->Resume();
if (game_) if (game_)

View File

@ -8,6 +8,8 @@
#include <unordered_map> #include <unordered_map>
#include "base/random.h" #include "base/random.h"
#include "base/thread_pool.h"
#include "base/timer.h"
#include "base/vecmath.h" #include "base/vecmath.h"
#include "engine/persistent_data.h" #include "engine/persistent_data.h"
#include "engine/platform/platform_observer.h" #include "engine/platform/platform_observer.h"
@ -39,10 +41,7 @@ class Engine : public PlatformObserver {
static Engine& Get(); static Engine& Get();
bool Initialize(); void Run();
void Update(float delta_time);
void Draw(float frame_frac);
void AddDrawable(Drawable* drawable); void AddDrawable(Drawable* drawable);
void RemoveDrawable(Drawable* drawable); void RemoveDrawable(Drawable* drawable);
@ -215,8 +214,15 @@ class Engine : public PlatformObserver {
bool replaying_ = false; bool replaying_ = false;
unsigned int replay_index_ = 0; unsigned int replay_index_ = 0;
base::ThreadPool thread_pool_;
base::Timer timer_;
base::Randomf random_; base::Randomf random_;
bool Initialize();
void Update(float delta_time);
void Draw(float frame_frac);
// PlatformObserver implementation // PlatformObserver implementation
void OnWindowCreated() final; void OnWindowCreated() final;
void OnWindowDestroyed() final; void OnWindowDestroyed() final;

View File

@ -1,81 +0,0 @@
#include "engine/platform/platform.h"
#include <memory>
#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<RendererVulkan*>(renderer_.get()) && vulkan) ||
// (dynamic_cast<RendererOpenGL*>(renderer_.get()) && !vulkan))
// return renderer_.get();
// if (vulkan)
// renderer_ = std::make_unique<RendererVulkan>();
// else
// renderer_ = std::make_unique<RendererOpenGL>();
// 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> engine = std::make_unique<Engine>(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

View File

@ -3,9 +3,6 @@
#include <string> #include <string>
#include "base/thread_pool.h"
#include "base/timer.h"
#if defined(__ANDROID__) #if defined(__ANDROID__)
#include "../../base/vecmath.h" #include "../../base/vecmath.h"
@ -27,16 +24,12 @@ class PlatformObserver;
class Platform { class Platform {
public: public:
Platform();
~Platform();
#if defined(__ANDROID__) #if defined(__ANDROID__)
void Initialize(android_app* app); Platform(android_app* app);
#elif defined(__linux__) #elif defined(__linux__)
void Initialize(); Platform();
#endif #endif
~Platform();
void Shutdown();
void Update(); void Update();
@ -44,8 +37,6 @@ class Platform {
void SetObserver(PlatformObserver* observer) { observer_ = observer; } void SetObserver(PlatformObserver* observer) { observer_ = observer; }
// Renderer* SwitchRenderer(bool vulkan);
void Vibrate(int duration); void Vibrate(int duration);
void ShowInterstitialAd(); void ShowInterstitialAd();
@ -54,8 +45,6 @@ class Platform {
void SetKeepScreenOn(bool keep_screen_on); void SetKeepScreenOn(bool keep_screen_on);
void RunMainLoop();
int GetDeviceDpi() const { return device_dpi_; } int GetDeviceDpi() const { return device_dpi_; }
const std::string& GetRootPath() const { return root_path_; } const std::string& GetRootPath() const { return root_path_; }
@ -66,6 +55,8 @@ class Platform {
bool mobile_device() const { return mobile_device_; } bool mobile_device() const { return mobile_device_; }
bool should_exit() const { return should_exit_; }
#if defined(__ANDROID__) #if defined(__ANDROID__)
ANativeWindow* GetWindow(); ANativeWindow* GetWindow();
#elif defined(__linux__) #elif defined(__linux__)
@ -74,8 +65,6 @@ class Platform {
#endif #endif
private: private:
base::Timer timer_;
bool mobile_device_ = false; bool mobile_device_ = false;
int device_dpi_ = 100; int device_dpi_ = 100;
std::string root_path_; std::string root_path_;
@ -87,8 +76,6 @@ class Platform {
PlatformObserver* observer_ = nullptr; PlatformObserver* observer_ = nullptr;
base::ThreadPool thread_pool_;
#if defined(__ANDROID__) #if defined(__ANDROID__)
android_app* app_ = nullptr; android_app* app_ = nullptr;
@ -126,8 +113,6 @@ class Platform {
#endif #endif
void InitializeCommon();
Platform(const Platform&) = delete; Platform(const Platform&) = delete;
Platform& operator=(const Platform&) = delete; Platform& operator=(const Platform&) = delete;
}; };

View File

@ -8,7 +8,6 @@
#include <unistd.h> #include <unistd.h>
#include "base/log.h" #include "base/log.h"
#include "base/task_runner.h"
#include "engine/input_event.h" #include "engine/input_event.h"
#include "engine/platform/platform_observer.h" #include "engine/platform/platform_observer.h"
@ -202,6 +201,8 @@ int32_t GetDensityDpi(android_app* app) {
namespace eng { namespace eng {
void KaliberMain(Platform* platform);
int32_t Platform::HandleInput(android_app* app, AInputEvent* event) { int32_t Platform::HandleInput(android_app* app, AInputEvent* event) {
Platform* platform = reinterpret_cast<Platform*>(app->userData); Platform* platform = reinterpret_cast<Platform*>(app->userData);
@ -316,7 +317,7 @@ void Platform::HandleCmd(android_app* app, int32_t cmd) {
case APP_CMD_GAINED_FOCUS: case APP_CMD_GAINED_FOCUS:
DLOG << "APP_CMD_GAINED_FOCUS"; DLOG << "APP_CMD_GAINED_FOCUS";
platform->timer_.Reset(); // platform->timer_.Reset();
platform->has_focus_ = true; platform->has_focus_ = true;
if (platform->observer_) if (platform->observer_)
platform->observer_->GainedFocus(g_showing_interstitial_ad); 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::Platform(android_app* app) {
Platform::InitializeCommon();
app_ = app; app_ = app;
mobile_device_ = true; mobile_device_ = true;
root_path_ = ::GetApkPath(app->activity); root_path_ = ::GetApkPath(app->activity);
@ -374,7 +372,7 @@ void Platform::Initialize(android_app* app) {
Update(); Update();
} }
void Platform::Shutdown() { Platform::~Platform() {
LOG << "Shutting down platform."; LOG << "Shutting down platform.";
} }
@ -435,9 +433,7 @@ ANativeWindow* Platform::GetWindow() {
} // namespace eng } // namespace eng
void android_main(android_app* app) { void android_main(android_app* app) {
eng::Platform platform; eng::Platform platform(app);
platform.Initialize(app); KaliberMain(&platform);
platform.RunMainLoop();
platform.Shutdown();
_exit(0); _exit(0);
} }

View File

@ -3,7 +3,6 @@
#include <memory> #include <memory>
#include "base/log.h" #include "base/log.h"
#include "base/task_runner.h"
#include "base/vecmath.h" #include "base/vecmath.h"
#include "engine/input_event.h" #include "engine/input_event.h"
#include "engine/platform/platform_observer.h" #include "engine/platform/platform_observer.h"
@ -12,9 +11,9 @@ using namespace base;
namespace eng { namespace eng {
void Platform::Initialize() { void KaliberMain(Platform* platform);
Platform::InitializeCommon();
Platform::Platform() {
root_path_ = "../../"; root_path_ = "../../";
LOG << "Root path: " << root_path_.c_str(); LOG << "Root path: " << root_path_.c_str();
@ -34,7 +33,7 @@ void Platform::Initialize() {
XSetWMProtocols(display_, window_, &WM_DELETE_WINDOW, 1); XSetWMProtocols(display_, window_, &WM_DELETE_WINDOW, 1);
} }
void Platform::Shutdown() { Platform::~Platform() {
LOG << "Shutting down platform."; LOG << "Shutting down platform.";
DestroyWindow(); DestroyWindow();
} }
@ -170,8 +169,6 @@ XVisualInfo* Platform::GetXVisualInfo(Display* display) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
eng::Platform platform; eng::Platform platform;
platform.Initialize(); KaliberMain(&platform);
platform.RunMainLoop();
platform.Shutdown();
return 0; return 0;
} }