diff --git a/Extra2D/include/extra2d/platform/window.h b/Extra2D/include/extra2d/platform/window.h index b53b53a..3ba979f 100644 --- a/Extra2D/include/extra2d/platform/window.h +++ b/Extra2D/include/extra2d/platform/window.h @@ -27,6 +27,7 @@ struct WindowConfig { bool centerWindow = true; bool enableCursors = true; bool enableDpiScale = true; + bool fullscreenDesktop = true; // true: SDL_WINDOW_FULLSCREEN_DESKTOP, false: SDL_WINDOW_FULLSCREEN }; // ============================================================================ diff --git a/Extra2D/src/app/application.cpp b/Extra2D/src/app/application.cpp index 4572987..9d99aed 100644 --- a/Extra2D/src/app/application.cpp +++ b/Extra2D/src/app/application.cpp @@ -97,6 +97,7 @@ bool Application::init(const AppConfig &config) { winConfig.height = 720; if (platform == PlatformType::Switch) { winConfig.fullscreen = true; + winConfig.fullscreenDesktop = false; // Switch 使用固定分辨率全屏 winConfig.resizable = false; winConfig.enableCursors = false; winConfig.enableDpiScale = false; @@ -170,6 +171,11 @@ void Application::shutdown() { // 打印 VRAM 统计 VRAMManager::getInstance().printStats(); + // 先结束所有场景,确保 onExit() 被正确调用 + if (sceneManager_) { + sceneManager_->end(); + } + // 清理子系统 sceneManager_.reset(); resourceManager_.reset(); diff --git a/Extra2D/src/platform/window.cpp b/Extra2D/src/platform/window.cpp index e9e375a..afb0b6f 100644 --- a/Extra2D/src/platform/window.cpp +++ b/Extra2D/src/platform/window.cpp @@ -81,7 +81,13 @@ bool Window::initSDL(const WindowConfig &config) { // 根据配置设置窗口模式 if (config.fullscreen) { - windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; + // Switch 平台使用 SDL_WINDOW_FULLSCREEN(固定分辨率) + // PC 平台使用 SDL_WINDOW_FULLSCREEN_DESKTOP(桌面全屏) + if (config.fullscreenDesktop) { + windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; + } else { + windowFlags |= SDL_WINDOW_FULLSCREEN; + } } else { if (config.resizable) { windowFlags |= SDL_WINDOW_RESIZABLE; @@ -252,6 +258,7 @@ void Window::setPosition(int x, int y) { void Window::setFullscreen(bool fullscreen) { if (sdlWindow_) { + // 默认使用桌面全屏模式(PC 平台) Uint32 flags = fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0; SDL_SetWindowFullscreen(sdlWindow_, flags); fullscreen_ = fullscreen; diff --git a/Extra2D/src/resource/resource_manager.cpp b/Extra2D/src/resource/resource_manager.cpp index 5b88f1a..9cfe22f 100644 --- a/Extra2D/src/resource/resource_manager.cpp +++ b/Extra2D/src/resource/resource_manager.cpp @@ -7,6 +7,11 @@ #include #include +// Windows 平台需要包含的头文件 +#ifdef _WIN32 +#include +#endif + namespace extra2d { // 辅助函数:检查文件是否存在 @@ -20,7 +25,23 @@ static bool isRomfsPath(const std::string &path) { return path.find("romfs:/") == 0 || path.find("romfs:\\") == 0; } -// 解析资源路径(优先尝试 romfs:/ 前缀,然后 sdmc:/) +// 辅助函数:获取可执行文件所在目录(Windows 平台) +#ifdef _WIN32 +static std::string getExecutableDirectory() { + char buffer[MAX_PATH]; + DWORD len = GetModuleFileNameA(NULL, buffer, MAX_PATH); + if (len > 0 && len < MAX_PATH) { + std::string exePath(buffer, len); + size_t lastSlash = exePath.find_last_of("\\/"); + if (lastSlash != std::string::npos) { + return exePath.substr(0, lastSlash); + } + } + return ""; +} +#endif + +// 解析资源路径(优先尝试 romfs:/ 前缀,然后 sdmc:/,最后尝试相对于可执行文件的路径) static std::string resolveResourcePath(const std::string &filepath) { // 如果已经是 romfs 或 sdmc 路径,直接返回 if (isRomfsPath(filepath) || filepath.find("sdmc:/") == 0) { @@ -44,6 +65,17 @@ static std::string resolveResourcePath(const std::string &filepath) { return filepath; } + // Windows 平台:尝试相对于可执行文件的路径 +#ifdef _WIN32 + std::string exeDir = getExecutableDirectory(); + if (!exeDir.empty()) { + std::string exeRelativePath = exeDir + "/" + filepath; + if (fileExists(exeRelativePath)) { + return exeRelativePath; + } + } +#endif + return ""; } diff --git a/examples/collision_demo/xmake.lua b/examples/collision_demo/xmake.lua index 9914b7e..b8c0665 100644 --- a/examples/collision_demo/xmake.lua +++ b/examples/collision_demo/xmake.lua @@ -3,10 +3,6 @@ -- 支持平台: MinGW (Windows), Nintendo Switch -- ============================================== --- 获取当前平台 -local host_plat = os.host() -local target_plat = get_config("plat") or host_plat - -- 获取当前脚本所在目录(示例根目录) local example_dir = os.scriptdir() @@ -17,7 +13,8 @@ target("collision_demo") add_includedirs("../../Extra2D/include") add_deps("extra2d") - if target_plat == "switch" then + -- 使用与主项目相同的平台配置 + if is_plat("switch") then set_plat("switch") set_arch("arm64") set_toolchains("switch") @@ -43,7 +40,7 @@ target("collision_demo") end end) - elseif target_plat == "mingw" then + elseif is_plat("mingw") then set_plat("mingw") set_arch("x86_64") set_targetdir("../../build/examples/collision_demo") diff --git a/examples/hello_world/xmake.lua b/examples/hello_world/xmake.lua index 2b665e6..2d7991c 100644 --- a/examples/hello_world/xmake.lua +++ b/examples/hello_world/xmake.lua @@ -3,10 +3,6 @@ -- 支持平台: MinGW (Windows), Nintendo Switch -- ============================================== --- 获取当前平台 -local host_plat = os.host() -local target_plat = get_config("plat") or host_plat - -- 获取当前脚本所在目录(示例根目录) local example_dir = os.scriptdir() @@ -17,7 +13,8 @@ target("hello_world") add_includedirs("../../Extra2D/include") add_deps("extra2d") - if target_plat == "switch" then + -- 使用与主项目相同的平台配置 + if is_plat("switch") then set_plat("switch") set_arch("arm64") set_toolchains("switch") @@ -44,7 +41,7 @@ target("hello_world") end end) - elseif target_plat == "mingw" then + elseif is_plat("mingw") then set_plat("mingw") set_arch("x86_64") set_targetdir("../../build/examples/hello_world") diff --git a/examples/push_box/xmake.lua b/examples/push_box/xmake.lua index a138c36..4e22790 100644 --- a/examples/push_box/xmake.lua +++ b/examples/push_box/xmake.lua @@ -3,10 +3,6 @@ -- 支持平台: MinGW (Windows), Nintendo Switch -- ============================================== --- 获取当前平台 -local host_plat = os.host() -local target_plat = get_config("plat") or host_plat - -- 获取当前脚本所在目录(示例根目录) local example_dir = os.scriptdir() @@ -17,7 +13,8 @@ target("push_box") add_includedirs("../../Extra2D/include") add_deps("extra2d") - if target_plat == "switch" then + -- 使用与主项目相同的平台配置 + if is_plat("switch") then set_plat("switch") set_arch("arm64") set_toolchains("switch") @@ -43,7 +40,7 @@ target("push_box") end end) - elseif target_plat == "mingw" then + elseif is_plat("mingw") then set_plat("mingw") set_arch("x86_64") set_targetdir("../../build/examples/push_box") diff --git a/examples/spatial_index_demo/main.cpp b/examples/spatial_index_demo/main.cpp index f6b51cd..98fa18e 100644 --- a/examples/spatial_index_demo/main.cpp +++ b/examples/spatial_index_demo/main.cpp @@ -115,6 +115,17 @@ public: E2D_LOG_INFO("空间索引已启用: {}", isSpatialIndexingEnabled()); } + void onExit() override { + // 先清理 nodes_ 向量 + nodes_.clear(); + + // 显式移除所有子节点,确保在场景析构前正确清理空间索引 + // 这必须在 Scene::onExit() 之前调用,因为 onExit() 会将 running_ 设为 false + removeAllChildren(); + + Scene::onExit(); + } + void onUpdate(float dt) override { Scene::onUpdate(dt); diff --git a/examples/spatial_index_demo/xmake.lua b/examples/spatial_index_demo/xmake.lua index b7e2719..6916886 100644 --- a/examples/spatial_index_demo/xmake.lua +++ b/examples/spatial_index_demo/xmake.lua @@ -3,10 +3,6 @@ -- 支持平台: MinGW (Windows), Nintendo Switch -- ============================================== --- 获取当前平台 -local host_plat = os.host() -local target_plat = get_config("plat") or host_plat - -- 获取当前脚本所在目录(示例根目录) local example_dir = os.scriptdir() @@ -17,7 +13,8 @@ target("spatial_index_demo") add_includedirs("../../Extra2D/include") add_deps("extra2d") - if target_plat == "switch" then + -- 使用与主项目相同的平台配置 + if is_plat("switch") then set_plat("switch") set_arch("arm64") set_toolchains("switch") @@ -43,7 +40,7 @@ target("spatial_index_demo") end end) - elseif target_plat == "mingw" then + elseif is_plat("mingw") then set_plat("mingw") set_arch("x86_64") set_targetdir("../../build/examples/spatial_index_demo")