61 lines
1.6 KiB
C++
61 lines
1.6 KiB
C++
#include "async/thread_pool.h"
|
|
#include "mirage.h"
|
|
|
|
using time_type = decltype(std::chrono::high_resolution_clock::now());
|
|
std::chrono::duration<double> delta_time = {};
|
|
time_type begin_time = {};
|
|
time_type last_time = {};
|
|
|
|
namespace mirage {
|
|
bool init(const init_info& in_info) {
|
|
spdlog::info("初始化 mirage");
|
|
begin_time = std::chrono::high_resolution_clock::now();
|
|
last_time = std::chrono::high_resolution_clock::now();
|
|
return true;
|
|
}
|
|
|
|
void destroy() {
|
|
spdlog::info("mirage 销毁");
|
|
}
|
|
|
|
void update() {
|
|
thread_pool::global().process_main_thread_callbacks();
|
|
|
|
// 更新时间
|
|
const auto& current_time = std::chrono::high_resolution_clock::now();
|
|
delta_time = current_time - last_time;
|
|
last_time = current_time;
|
|
|
|
std::this_thread::yield();
|
|
}
|
|
|
|
bool should_exit() {
|
|
return false;
|
|
}
|
|
|
|
int run(const init_info& in_init_info) {
|
|
try {
|
|
if (!init(in_init_info)) {
|
|
return -1;
|
|
}
|
|
while (!should_exit()) {
|
|
update();
|
|
}
|
|
destroy();
|
|
} catch (const std::exception& e) {
|
|
spdlog::error("运行时异常: {}", e.what());
|
|
return -1;
|
|
} catch (...) {
|
|
spdlog::error("未知异常");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
const std::chrono::duration<double>& get_delta_time() { return delta_time; }
|
|
|
|
std::chrono::duration<double> get_total_time() {
|
|
return std::chrono::high_resolution_clock::now() - begin_time;
|
|
}
|
|
}
|