parent
9f83b8fde5
commit
4b1de5e36a
|
|
@ -1,20 +1,20 @@
|
||||||
#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 {
|
||||||
|
|
||||||
|
|
@ -32,14 +32,12 @@ static double getTimeSeconds() {
|
||||||
#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_) {
|
||||||
|
|
@ -52,7 +50,7 @@ bool Application::init() {
|
||||||
return init(cfg);
|
return init(cfg);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Application::init(const AppConfig& config) {
|
bool Application::init(const AppConfig &config) {
|
||||||
if (initialized_) {
|
if (initialized_) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -60,7 +58,7 @@ bool Application::init(const AppConfig& config) {
|
||||||
appConfig_ = config;
|
appConfig_ = config;
|
||||||
|
|
||||||
// 首先注册日志服务(模块初始化可能需要它)
|
// 首先注册日志服务(模块初始化可能需要它)
|
||||||
auto& locator = ServiceLocator::instance();
|
auto &locator = ServiceLocator::instance();
|
||||||
if (!locator.hasService<ILogger>()) {
|
if (!locator.hasService<ILogger>()) {
|
||||||
auto logger = makeShared<ConsoleLogger>();
|
auto logger = makeShared<ConsoleLogger>();
|
||||||
locator.registerService(std::static_pointer_cast<ILogger>(logger));
|
locator.registerService(std::static_pointer_cast<ILogger>(logger));
|
||||||
|
|
@ -80,7 +78,7 @@ bool Application::init(const AppConfig& config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
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>();
|
||||||
|
|
@ -97,10 +95,10 @@ void Application::registerCoreServices() {
|
||||||
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;
|
||||||
|
|
@ -109,7 +107,8 @@ void Application::registerCoreServices() {
|
||||||
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);
|
||||||
|
|
@ -121,7 +120,8 @@ void Application::registerCoreServices() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::shutdown() {
|
void Application::shutdown() {
|
||||||
if (!initialized_) return;
|
if (!initialized_)
|
||||||
|
return;
|
||||||
|
|
||||||
VRAMMgr::get().printStats();
|
VRAMMgr::get().printStats();
|
||||||
|
|
||||||
|
|
@ -134,10 +134,12 @@ void Application::shutdown() {
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
|
|
@ -181,7 +183,7 @@ void Application::mainLoop() {
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
@ -198,7 +200,7 @@ void Application::mainLoop() {
|
||||||
render();
|
render();
|
||||||
|
|
||||||
// 帧率限制
|
// 帧率限制
|
||||||
auto* renderMod = get<RenderModule>();
|
auto *renderMod = get<RenderModule>();
|
||||||
if (renderMod && renderMod->renderer()) {
|
if (renderMod && renderMod->renderer()) {
|
||||||
// 这里可以添加帧率限制逻辑
|
// 这里可以添加帧率限制逻辑
|
||||||
}
|
}
|
||||||
|
|
@ -207,29 +209,32 @@ void Application::mainLoop() {
|
||||||
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>();
|
||||||
|
|
@ -240,24 +245,24 @@ void Application::render() {
|
||||||
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()));
|
||||||
|
|
|
||||||
|
|
@ -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>();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue