Extra2D/Extra2D/include/extra2d/app/application.h

257 lines
5.1 KiB
C
Raw Normal View History

2026-02-11 19:40:26 +08:00
#pragma once
#include <extra2d/core/types.h>
#include <extra2d/core/module.h>
#include <extra2d/core/service_locator.h>
#include <extra2d/config/app_config.h>
#include <extra2d/config/config_manager.h>
#include <extra2d/platform/iwindow.h>
#include <string>
#include <vector>
#include <initializer_list>
2026-02-11 19:40:26 +08:00
namespace extra2d {
class IInput;
class RenderBackend;
2026-02-11 19:40:26 +08:00
/**
* @brief
* 使Mock
*/
2026-02-11 19:40:26 +08:00
class Application {
public:
/**
* @brief
* @return
*/
static Application& get();
Application(const Application&) = delete;
Application& operator=(const Application&) = delete;
/**
* @brief
* @param m
*/
void use(Module& m);
/**
* @brief
* @param modules
*/
void use(std::initializer_list<Module*> modules);
/**
* @brief 使
* @return true
*/
bool init();
/**
* @brief 使
* @param config
* @return true
*/
bool init(const AppConfig& config);
/**
* @brief 使
* @param configPath
* @return true
*/
bool init(const std::string& configPath);
/**
* @brief
*/
void shutdown();
/**
* @brief
*/
void run();
/**
* @brief 退
*/
void quit();
/**
* @brief
*/
void pause();
/**
* @brief
*/
void resume();
/**
* @brief
* @return true
*/
bool isPaused() const { return paused_; }
/**
* @brief
* @return true
*/
bool isRunning() const { return running_; }
/**
* @brief
* @return
*/
IWindow& window() { return *window_; }
/**
* @brief
* @return
*/
RenderBackend& renderer();
/**
* @brief
* @return
*/
IInput& input();
/**
* @brief
* @return
*/
SharedPtr<class ISceneService> scenes();
/**
* @brief
* @return
*/
SharedPtr<class ITimerService> timers();
/**
* @brief
* @return
*/
SharedPtr<class IEventService> events();
/**
* @brief
* @return
*/
SharedPtr<class ICameraService> camera();
/**
* @brief
* @param scene
*/
void enterScene(Ptr<class Scene> scene);
/**
* @brief
* @return
*/
float deltaTime() const { return deltaTime_; }
/**
* @brief
* @return
*/
float totalTime() const { return totalTime_; }
/**
* @brief
* @return
*/
int fps() const { return currentFps_; }
/**
* @brief
* @return
*/
ConfigManager& config();
/**
* @brief
* @return
*/
const AppConfig& getConfig() const;
2026-02-11 19:40:26 +08:00
/**
* @brief
* @tparam T
* @param service
*/
template<typename T>
void registerService(SharedPtr<T> service) {
ServiceLocator::instance().registerService(service);
}
/**
* @brief
* @tparam T
* @return
*/
template<typename T>
SharedPtr<T> getService() {
return ServiceLocator::instance().getService<T>();
}
2026-02-11 19:40:26 +08:00
private:
Application() = default;
~Application();
/**
* @brief
* @return true
*/
bool initCoreModules();
/**
* @brief
*/
void setupAllModules();
/**
* @brief
*/
void destroyAllModules();
/**
* @brief
*/
void registerCoreServices();
/**
* @brief
*/
void mainLoop();
/**
* @brief
*/
void update();
/**
* @brief
*/
void render();
std::vector<Module*> modules_;
IWindow* window_ = nullptr;
bool initialized_ = false;
bool running_ = false;
bool paused_ = false;
bool shouldQuit_ = false;
float deltaTime_ = 0.0f;
float totalTime_ = 0.0f;
double lastFrameTime_ = 0.0;
int frameCount_ = 0;
float fpsTimer_ = 0.0f;
int currentFps_ = 0;
2026-02-11 19:40:26 +08:00
};
}