feat(平台): 添加全屏模式配置选项并优化资源路径解析

为窗口配置添加 fullscreenDesktop 选项,支持不同平台的全屏模式
优化资源管理器在 Windows 平台的路径解析逻辑,支持可执行文件相对路径
修复场景退出时空间索引清理问题,确保正确调用 onExit
统一示例项目的平台配置检测方式,使用 is_plat 替代旧方法
This commit is contained in:
ChestnutYueyue 2026-02-10 18:58:43 +08:00
parent 261d8d2135
commit 0f89262498
9 changed files with 71 additions and 26 deletions

View File

@ -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
};
// ============================================================================

View File

@ -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();

View File

@ -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;

View File

@ -7,6 +7,11 @@
#include <extra2d/utils/logger.h>
#include <sys/stat.h>
// Windows 平台需要包含的头文件
#ifdef _WIN32
#include <windows.h>
#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 "";
}

View File

@ -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")

View File

@ -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")

View File

@ -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")

View File

@ -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);

View File

@ -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")