refactor: 整理头文件顺序并格式化代码

重构头文件引入顺序以保持一致性,并统一代码格式化风格
This commit is contained in:
ChestnutYueyue 2026-02-17 00:12:32 +08:00
parent 9f83b8fde5
commit 4b1de5e36a
2 changed files with 221 additions and 213 deletions

View File

@ -1,268 +1,273 @@
#include <extra2d/app/application.h> #include <extra2d/app/application.h>
#include <extra2d/core/registry.h> #include <extra2d/core/registry.h>
#include <extra2d/platform/window_module.h>
#include <extra2d/platform/input_module.h>
#include <extra2d/graphics/core/render_module.h>
#include <extra2d/platform/iwindow.h>
#include <extra2d/platform/iinput.h>
#include <extra2d/graphics/core/render_backend.h> #include <extra2d/graphics/core/render_backend.h>
#include <extra2d/graphics/core/render_module.h>
#include <extra2d/graphics/memory/vram_manager.h>
#include <extra2d/platform/iinput.h>
#include <extra2d/platform/input_module.h>
#include <extra2d/platform/iwindow.h>
#include <extra2d/platform/window_module.h>
#include <extra2d/services/camera_service.h>
#include <extra2d/services/event_service.h>
#include <extra2d/services/logger_service.h>
#include <extra2d/services/scene_service.h> #include <extra2d/services/scene_service.h>
#include <extra2d/services/timer_service.h> #include <extra2d/services/timer_service.h>
#include <extra2d/services/event_service.h>
#include <extra2d/services/camera_service.h>
#include <extra2d/services/logger_service.h>
#include <extra2d/graphics/memory/vram_manager.h>
#include <chrono> #include <chrono>
#include <thread>
namespace extra2d { namespace extra2d {
static double getTimeSeconds() { static double getTimeSeconds() {
#ifdef __SWITCH__ #ifdef __SWITCH__
struct timespec ts; struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts); clock_gettime(CLOCK_MONOTONIC, &ts);
return static_cast<double>(ts.tv_sec) + return static_cast<double>(ts.tv_sec) +
static_cast<double>(ts.tv_nsec) / 1000000000.0; static_cast<double>(ts.tv_nsec) / 1000000000.0;
#else #else
using namespace std::chrono; using namespace std::chrono;
auto now = steady_clock::now(); auto now = steady_clock::now();
auto duration = now.time_since_epoch(); auto duration = now.time_since_epoch();
return duration_cast<std::chrono::duration<double>>(duration).count(); return duration_cast<std::chrono::duration<double>>(duration).count();
#endif #endif
} }
Application& Application::get() { Application &Application::get() {
static Application instance; static Application instance;
return instance; return instance;
} }
Application::Application() { Application::Application() { Registry::instance().setApp(this); }
Registry::instance().setApp(this);
}
Application::~Application() { Application::~Application() {
if (initialized_) { if (initialized_) {
shutdown(); shutdown();
} }
} }
bool Application::init() { bool Application::init() {
AppConfig cfg; AppConfig cfg;
return init(cfg); return init(cfg);
} }
bool Application::init(const AppConfig& config) { bool Application::init(const AppConfig &config) {
if (initialized_) { if (initialized_) {
return true;
}
appConfig_ = config;
// 首先注册日志服务(模块初始化可能需要它)
auto& locator = ServiceLocator::instance();
if (!locator.hasService<ILogger>()) {
auto logger = makeShared<ConsoleLogger>();
locator.registerService(std::static_pointer_cast<ILogger>(logger));
}
// 初始化所有模块(拓扑排序)
if (!Registry::instance().init()) {
return false;
}
// 模块初始化完成后,注册其他核心服务
registerCoreServices();
initialized_ = true;
running_ = true;
return true; return true;
}
appConfig_ = config;
// 首先注册日志服务(模块初始化可能需要它)
auto &locator = ServiceLocator::instance();
if (!locator.hasService<ILogger>()) {
auto logger = makeShared<ConsoleLogger>();
locator.registerService(std::static_pointer_cast<ILogger>(logger));
}
// 初始化所有模块(拓扑排序)
if (!Registry::instance().init()) {
return false;
}
// 模块初始化完成后,注册其他核心服务
registerCoreServices();
initialized_ = true;
running_ = true;
return true;
} }
void Application::registerCoreServices() { void Application::registerCoreServices() {
auto& locator = ServiceLocator::instance(); auto &locator = ServiceLocator::instance();
if (!locator.hasService<ISceneService>()) { if (!locator.hasService<ISceneService>()) {
auto service = makeShared<SceneService>(); auto service = makeShared<SceneService>();
locator.registerService(std::static_pointer_cast<ISceneService>(service)); locator.registerService(std::static_pointer_cast<ISceneService>(service));
} }
if (!locator.hasService<ITimerService>()) { if (!locator.hasService<ITimerService>()) {
auto service = makeShared<TimerService>(); auto service = makeShared<TimerService>();
locator.registerService(std::static_pointer_cast<ITimerService>(service)); locator.registerService(std::static_pointer_cast<ITimerService>(service));
} }
if (!locator.hasService<IEventService>()) { if (!locator.hasService<IEventService>()) {
auto service = makeShared<EventService>(); auto service = makeShared<EventService>();
locator.registerService(std::static_pointer_cast<IEventService>(service)); locator.registerService(std::static_pointer_cast<IEventService>(service));
} }
auto* winMod = get<WindowModule>(); auto *winMod = get<WindowModule>();
if (winMod && winMod->win() && !locator.hasService<ICameraService>()) { if (winMod && winMod->win() && !locator.hasService<ICameraService>()) {
auto cameraService = makeShared<CameraService>(); auto cameraService = makeShared<CameraService>();
auto* win = winMod->win(); auto *win = winMod->win();
cameraService->setViewport(0, static_cast<float>(win->width()), cameraService->setViewport(0, static_cast<float>(win->width()),
static_cast<float>(win->height()), 0); static_cast<float>(win->height()), 0);
ViewportConfig vpConfig; ViewportConfig vpConfig;
vpConfig.logicWidth = static_cast<float>(win->width()); vpConfig.logicWidth = static_cast<float>(win->width());
vpConfig.logicHeight = static_cast<float>(win->height()); vpConfig.logicHeight = static_cast<float>(win->height());
vpConfig.mode = ViewportMode::AspectRatio; vpConfig.mode = ViewportMode::AspectRatio;
cameraService->setViewportConfig(vpConfig); cameraService->setViewportConfig(vpConfig);
cameraService->updateViewport(win->width(), win->height()); cameraService->updateViewport(win->width(), win->height());
locator.registerService(std::static_pointer_cast<ICameraService>(cameraService)); locator.registerService(
std::static_pointer_cast<ICameraService>(cameraService));
win->onResize([cameraService](int width, int height) { win->onResize([cameraService](int width, int height) {
cameraService->updateViewport(width, height); cameraService->updateViewport(width, height);
cameraService->applyViewportAdapter(); cameraService->applyViewportAdapter();
}); });
} }
locator.initializeAll(); locator.initializeAll();
} }
void Application::shutdown() { void Application::shutdown() {
if (!initialized_) return; if (!initialized_)
return;
VRAMMgr::get().printStats(); VRAMMgr::get().printStats();
ServiceLocator::instance().clear(); ServiceLocator::instance().clear();
Registry::instance().shutdown(); Registry::instance().shutdown();
Registry::instance().clear(); Registry::instance().clear();
initialized_ = false; initialized_ = false;
running_ = false; running_ = false;
} }
void Application::run() { void Application::run() {
if (!initialized_) return; if (!initialized_)
return;
auto* winMod = get<WindowModule>(); auto *winMod = get<WindowModule>();
if (!winMod || !winMod->win()) return; if (!winMod || !winMod->win())
return;
lastFrameTime_ = getTimeSeconds(); lastFrameTime_ = getTimeSeconds();
while (running_ && !winMod->win()->shouldClose()) { while (running_ && !winMod->win()->shouldClose()) {
mainLoop(); mainLoop();
} }
} }
void Application::quit() { void Application::quit() {
shouldQuit_ = true; shouldQuit_ = true;
running_ = false; running_ = false;
} }
void Application::pause() { void Application::pause() {
if (!paused_) { if (!paused_) {
paused_ = true; paused_ = true;
ServiceLocator::instance().pauseAll(); ServiceLocator::instance().pauseAll();
} }
} }
void Application::resume() { void Application::resume() {
if (paused_) { if (paused_) {
paused_ = false; paused_ = false;
ServiceLocator::instance().resumeAll(); ServiceLocator::instance().resumeAll();
lastFrameTime_ = getTimeSeconds(); lastFrameTime_ = getTimeSeconds();
} }
} }
void Application::mainLoop() { void Application::mainLoop() {
double currentTime = getTimeSeconds(); double currentTime = getTimeSeconds();
deltaTime_ = static_cast<float>(currentTime - lastFrameTime_); deltaTime_ = static_cast<float>(currentTime - lastFrameTime_);
lastFrameTime_ = currentTime; lastFrameTime_ = currentTime;
totalTime_ += deltaTime_; totalTime_ += deltaTime_;
frameCount_++; frameCount_++;
fpsTimer_ += deltaTime_; fpsTimer_ += deltaTime_;
if (fpsTimer_ >= 1.0f) { if (fpsTimer_ >= 1.0f) {
currentFps_ = frameCount_; currentFps_ = frameCount_;
frameCount_ = 0; frameCount_ = 0;
fpsTimer_ -= 1.0f; fpsTimer_ -= 1.0f;
} }
auto* winMod = get<WindowModule>(); auto *winMod = get<WindowModule>();
if (winMod && winMod->win()) { if (winMod && winMod->win()) {
winMod->win()->poll(); winMod->win()->poll();
} }
auto eventService = ServiceLocator::instance().getService<IEventService>(); auto eventService = ServiceLocator::instance().getService<IEventService>();
if (eventService) { if (eventService) {
eventService->processQueue(); eventService->processQueue();
} }
if (!paused_) { if (!paused_) {
update(); update();
} }
render(); render();
// 帧率限制 // 帧率限制
auto* renderMod = get<RenderModule>(); auto *renderMod = get<RenderModule>();
if (renderMod && renderMod->renderer()) { if (renderMod && renderMod->renderer()) {
// 这里可以添加帧率限制逻辑 // 这里可以添加帧率限制逻辑
} }
} }
void Application::update() { void Application::update() {
ServiceLocator::instance().updateAll(deltaTime_); ServiceLocator::instance().updateAll(deltaTime_);
auto* inputMod = get<InputModule>(); auto *inputMod = get<InputModule>();
if (inputMod) { if (inputMod) {
inputMod->update(); inputMod->update();
} }
} }
void Application::render() { void Application::render() {
auto* renderMod = get<RenderModule>(); auto *renderMod = get<RenderModule>();
if (!renderMod || !renderMod->renderer()) return; if (!renderMod || !renderMod->renderer())
return;
auto* renderer = renderMod->renderer(); auto *renderer = renderMod->renderer();
auto* winMod = get<WindowModule>(); auto *winMod = get<WindowModule>();
if (!winMod || !winMod->win()) return; if (!winMod || !winMod->win())
return;
auto cameraService = ServiceLocator::instance().getService<ICameraService>(); auto cameraService = ServiceLocator::instance().getService<ICameraService>();
if (cameraService) { if (cameraService) {
const auto& vp = cameraService->getViewportResult().viewport; const auto &vp = cameraService->getViewportResult().viewport;
renderer->setViewport( renderer->setViewport(
static_cast<int>(vp.origin.x), static_cast<int>(vp.origin.y), static_cast<int>(vp.origin.x), static_cast<int>(vp.origin.y),
static_cast<int>(vp.size.width), static_cast<int>(vp.size.height)); static_cast<int>(vp.size.width), static_cast<int>(vp.size.height));
renderer->setViewProjection(cameraService->getViewProjectionMatrix()); renderer->setViewProjection(cameraService->getViewProjectionMatrix());
} else { } else {
renderer->setViewport(0, 0, winMod->win()->width(), winMod->win()->height()); renderer->setViewport(0, 0, winMod->win()->width(),
} winMod->win()->height());
}
auto sceneService = ServiceLocator::instance().getService<ISceneService>(); auto sceneService = ServiceLocator::instance().getService<ISceneService>();
if (sceneService) { if (sceneService) {
sceneService->render(*renderer); sceneService->render(*renderer);
} }
winMod->win()->swap(); winMod->win()->swap();
} }
IWindow* Application::window() { IWindow *Application::window() {
auto* winMod = get<WindowModule>(); auto *winMod = get<WindowModule>();
return winMod ? winMod->win() : nullptr; return winMod ? winMod->win() : nullptr;
} }
RenderBackend* Application::renderer() { RenderBackend *Application::renderer() {
auto* renderMod = get<RenderModule>(); auto *renderMod = get<RenderModule>();
return renderMod ? renderMod->renderer() : nullptr; return renderMod ? renderMod->renderer() : nullptr;
} }
IInput* Application::input() { IInput *Application::input() {
auto* winMod = get<WindowModule>(); auto *winMod = get<WindowModule>();
return (winMod && winMod->win()) ? winMod->win()->input() : nullptr; return (winMod && winMod->win()) ? winMod->win()->input() : nullptr;
} }
void Application::enterScene(Ptr<Scene> scene) { void Application::enterScene(Ptr<Scene> scene) {
auto sceneService = ServiceLocator::instance().getService<ISceneService>(); auto sceneService = ServiceLocator::instance().getService<ISceneService>();
auto* winMod = get<WindowModule>(); auto *winMod = get<WindowModule>();
if (sceneService && scene && winMod && winMod->win()) { if (sceneService && scene && winMod && winMod->win()) {
scene->setViewportSize(static_cast<float>(winMod->win()->width()), scene->setViewportSize(static_cast<float>(winMod->win()->width()),
static_cast<float>(winMod->win()->height())); static_cast<float>(winMod->win()->height()));
sceneService->enterScene(scene); sceneService->enterScene(scene);
} }
} }
} // namespace extra2d } // namespace extra2d

View File

@ -3,15 +3,16 @@
* @brief Extra2D * @brief Extra2D
*/ */
#include <extra2d/extra2d.h>
#include <extra2d/platform/window_module.h>
#include <extra2d/platform/input_module.h>
#include <extra2d/graphics/core/render_module.h>
#include <extra2d/core/service_locator.h> #include <extra2d/core/service_locator.h>
#include <extra2d/services/event_service.h> #include <extra2d/extra2d.h>
#include <extra2d/graphics/core/render_module.h>
#include <extra2d/platform/input_module.h>
#include <extra2d/platform/window_module.h>
#include <extra2d/services/camera_service.h> #include <extra2d/services/camera_service.h>
#include <extra2d/services/event_service.h>
#include <iostream> #include <iostream>
using namespace extra2d; using namespace extra2d;
void createSceneGraph(Scene *scene) { void createSceneGraph(Scene *scene) {
@ -106,6 +107,7 @@ int main(int argc, char *argv[]) {
winCfg.w = 1280; winCfg.w = 1280;
winCfg.h = 720; winCfg.h = 720;
winCfg.priority = 0; winCfg.priority = 0;
winCfg.backend = "glfw";
app.use<WindowModule>(winCfg); app.use<WindowModule>(winCfg);
RenderModule::Cfg renderCfg; RenderModule::Cfg renderCfg;
@ -124,9 +126,10 @@ int main(int argc, char *argv[]) {
std::cout << "Application initialized successfully!" << std::endl; std::cout << "Application initialized successfully!" << std::endl;
auto* win = app.window(); auto *win = app.window();
if (win) { if (win) {
std::cout << "Window: " << win->width() << "x" << win->height() << std::endl; std::cout << "Window: " << win->width() << "x" << win->height()
<< std::endl;
} }
auto eventService = ServiceLocator::instance().getService<IEventService>(); auto eventService = ServiceLocator::instance().getService<IEventService>();