feat(平台): 添加全屏模式配置选项并优化资源路径解析
为窗口配置添加 fullscreenDesktop 选项,支持不同平台的全屏模式 优化资源管理器在 Windows 平台的路径解析逻辑,支持可执行文件相对路径 修复场景退出时空间索引清理问题,确保正确调用 onExit 统一示例项目的平台配置检测方式,使用 is_plat 替代旧方法
This commit is contained in:
parent
261d8d2135
commit
0f89262498
|
|
@ -27,6 +27,7 @@ struct WindowConfig {
|
||||||
bool centerWindow = true;
|
bool centerWindow = true;
|
||||||
bool enableCursors = true;
|
bool enableCursors = true;
|
||||||
bool enableDpiScale = true;
|
bool enableDpiScale = true;
|
||||||
|
bool fullscreenDesktop = true; // true: SDL_WINDOW_FULLSCREEN_DESKTOP, false: SDL_WINDOW_FULLSCREEN
|
||||||
};
|
};
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,7 @@ bool Application::init(const AppConfig &config) {
|
||||||
winConfig.height = 720;
|
winConfig.height = 720;
|
||||||
if (platform == PlatformType::Switch) {
|
if (platform == PlatformType::Switch) {
|
||||||
winConfig.fullscreen = true;
|
winConfig.fullscreen = true;
|
||||||
|
winConfig.fullscreenDesktop = false; // Switch 使用固定分辨率全屏
|
||||||
winConfig.resizable = false;
|
winConfig.resizable = false;
|
||||||
winConfig.enableCursors = false;
|
winConfig.enableCursors = false;
|
||||||
winConfig.enableDpiScale = false;
|
winConfig.enableDpiScale = false;
|
||||||
|
|
@ -170,6 +171,11 @@ void Application::shutdown() {
|
||||||
// 打印 VRAM 统计
|
// 打印 VRAM 统计
|
||||||
VRAMManager::getInstance().printStats();
|
VRAMManager::getInstance().printStats();
|
||||||
|
|
||||||
|
// 先结束所有场景,确保 onExit() 被正确调用
|
||||||
|
if (sceneManager_) {
|
||||||
|
sceneManager_->end();
|
||||||
|
}
|
||||||
|
|
||||||
// 清理子系统
|
// 清理子系统
|
||||||
sceneManager_.reset();
|
sceneManager_.reset();
|
||||||
resourceManager_.reset();
|
resourceManager_.reset();
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,13 @@ bool Window::initSDL(const WindowConfig &config) {
|
||||||
|
|
||||||
// 根据配置设置窗口模式
|
// 根据配置设置窗口模式
|
||||||
if (config.fullscreen) {
|
if (config.fullscreen) {
|
||||||
|
// Switch 平台使用 SDL_WINDOW_FULLSCREEN(固定分辨率)
|
||||||
|
// PC 平台使用 SDL_WINDOW_FULLSCREEN_DESKTOP(桌面全屏)
|
||||||
|
if (config.fullscreenDesktop) {
|
||||||
windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
|
windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||||
|
} else {
|
||||||
|
windowFlags |= SDL_WINDOW_FULLSCREEN;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (config.resizable) {
|
if (config.resizable) {
|
||||||
windowFlags |= SDL_WINDOW_RESIZABLE;
|
windowFlags |= SDL_WINDOW_RESIZABLE;
|
||||||
|
|
@ -252,6 +258,7 @@ void Window::setPosition(int x, int y) {
|
||||||
|
|
||||||
void Window::setFullscreen(bool fullscreen) {
|
void Window::setFullscreen(bool fullscreen) {
|
||||||
if (sdlWindow_) {
|
if (sdlWindow_) {
|
||||||
|
// 默认使用桌面全屏模式(PC 平台)
|
||||||
Uint32 flags = fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
|
Uint32 flags = fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
|
||||||
SDL_SetWindowFullscreen(sdlWindow_, flags);
|
SDL_SetWindowFullscreen(sdlWindow_, flags);
|
||||||
fullscreen_ = fullscreen;
|
fullscreen_ = fullscreen;
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,11 @@
|
||||||
#include <extra2d/utils/logger.h>
|
#include <extra2d/utils/logger.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
// Windows 平台需要包含的头文件
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
// 辅助函数:检查文件是否存在
|
// 辅助函数:检查文件是否存在
|
||||||
|
|
@ -20,7 +25,23 @@ static bool isRomfsPath(const std::string &path) {
|
||||||
return path.find("romfs:/") == 0 || path.find("romfs:\\") == 0;
|
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) {
|
static std::string resolveResourcePath(const std::string &filepath) {
|
||||||
// 如果已经是 romfs 或 sdmc 路径,直接返回
|
// 如果已经是 romfs 或 sdmc 路径,直接返回
|
||||||
if (isRomfsPath(filepath) || filepath.find("sdmc:/") == 0) {
|
if (isRomfsPath(filepath) || filepath.find("sdmc:/") == 0) {
|
||||||
|
|
@ -44,6 +65,17 @@ static std::string resolveResourcePath(const std::string &filepath) {
|
||||||
return 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 "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,6 @@
|
||||||
-- 支持平台: MinGW (Windows), Nintendo Switch
|
-- 支持平台: MinGW (Windows), Nintendo Switch
|
||||||
-- ==============================================
|
-- ==============================================
|
||||||
|
|
||||||
-- 获取当前平台
|
|
||||||
local host_plat = os.host()
|
|
||||||
local target_plat = get_config("plat") or host_plat
|
|
||||||
|
|
||||||
-- 获取当前脚本所在目录(示例根目录)
|
-- 获取当前脚本所在目录(示例根目录)
|
||||||
local example_dir = os.scriptdir()
|
local example_dir = os.scriptdir()
|
||||||
|
|
||||||
|
|
@ -17,7 +13,8 @@ target("collision_demo")
|
||||||
add_includedirs("../../Extra2D/include")
|
add_includedirs("../../Extra2D/include")
|
||||||
add_deps("extra2d")
|
add_deps("extra2d")
|
||||||
|
|
||||||
if target_plat == "switch" then
|
-- 使用与主项目相同的平台配置
|
||||||
|
if is_plat("switch") then
|
||||||
set_plat("switch")
|
set_plat("switch")
|
||||||
set_arch("arm64")
|
set_arch("arm64")
|
||||||
set_toolchains("switch")
|
set_toolchains("switch")
|
||||||
|
|
@ -43,7 +40,7 @@ target("collision_demo")
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
elseif target_plat == "mingw" then
|
elseif is_plat("mingw") then
|
||||||
set_plat("mingw")
|
set_plat("mingw")
|
||||||
set_arch("x86_64")
|
set_arch("x86_64")
|
||||||
set_targetdir("../../build/examples/collision_demo")
|
set_targetdir("../../build/examples/collision_demo")
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,6 @@
|
||||||
-- 支持平台: MinGW (Windows), Nintendo Switch
|
-- 支持平台: MinGW (Windows), Nintendo Switch
|
||||||
-- ==============================================
|
-- ==============================================
|
||||||
|
|
||||||
-- 获取当前平台
|
|
||||||
local host_plat = os.host()
|
|
||||||
local target_plat = get_config("plat") or host_plat
|
|
||||||
|
|
||||||
-- 获取当前脚本所在目录(示例根目录)
|
-- 获取当前脚本所在目录(示例根目录)
|
||||||
local example_dir = os.scriptdir()
|
local example_dir = os.scriptdir()
|
||||||
|
|
||||||
|
|
@ -17,7 +13,8 @@ target("hello_world")
|
||||||
add_includedirs("../../Extra2D/include")
|
add_includedirs("../../Extra2D/include")
|
||||||
add_deps("extra2d")
|
add_deps("extra2d")
|
||||||
|
|
||||||
if target_plat == "switch" then
|
-- 使用与主项目相同的平台配置
|
||||||
|
if is_plat("switch") then
|
||||||
set_plat("switch")
|
set_plat("switch")
|
||||||
set_arch("arm64")
|
set_arch("arm64")
|
||||||
set_toolchains("switch")
|
set_toolchains("switch")
|
||||||
|
|
@ -44,7 +41,7 @@ target("hello_world")
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
elseif target_plat == "mingw" then
|
elseif is_plat("mingw") then
|
||||||
set_plat("mingw")
|
set_plat("mingw")
|
||||||
set_arch("x86_64")
|
set_arch("x86_64")
|
||||||
set_targetdir("../../build/examples/hello_world")
|
set_targetdir("../../build/examples/hello_world")
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,6 @@
|
||||||
-- 支持平台: MinGW (Windows), Nintendo Switch
|
-- 支持平台: MinGW (Windows), Nintendo Switch
|
||||||
-- ==============================================
|
-- ==============================================
|
||||||
|
|
||||||
-- 获取当前平台
|
|
||||||
local host_plat = os.host()
|
|
||||||
local target_plat = get_config("plat") or host_plat
|
|
||||||
|
|
||||||
-- 获取当前脚本所在目录(示例根目录)
|
-- 获取当前脚本所在目录(示例根目录)
|
||||||
local example_dir = os.scriptdir()
|
local example_dir = os.scriptdir()
|
||||||
|
|
||||||
|
|
@ -17,7 +13,8 @@ target("push_box")
|
||||||
add_includedirs("../../Extra2D/include")
|
add_includedirs("../../Extra2D/include")
|
||||||
add_deps("extra2d")
|
add_deps("extra2d")
|
||||||
|
|
||||||
if target_plat == "switch" then
|
-- 使用与主项目相同的平台配置
|
||||||
|
if is_plat("switch") then
|
||||||
set_plat("switch")
|
set_plat("switch")
|
||||||
set_arch("arm64")
|
set_arch("arm64")
|
||||||
set_toolchains("switch")
|
set_toolchains("switch")
|
||||||
|
|
@ -43,7 +40,7 @@ target("push_box")
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
elseif target_plat == "mingw" then
|
elseif is_plat("mingw") then
|
||||||
set_plat("mingw")
|
set_plat("mingw")
|
||||||
set_arch("x86_64")
|
set_arch("x86_64")
|
||||||
set_targetdir("../../build/examples/push_box")
|
set_targetdir("../../build/examples/push_box")
|
||||||
|
|
|
||||||
|
|
@ -115,6 +115,17 @@ public:
|
||||||
E2D_LOG_INFO("空间索引已启用: {}", isSpatialIndexingEnabled());
|
E2D_LOG_INFO("空间索引已启用: {}", isSpatialIndexingEnabled());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onExit() override {
|
||||||
|
// 先清理 nodes_ 向量
|
||||||
|
nodes_.clear();
|
||||||
|
|
||||||
|
// 显式移除所有子节点,确保在场景析构前正确清理空间索引
|
||||||
|
// 这必须在 Scene::onExit() 之前调用,因为 onExit() 会将 running_ 设为 false
|
||||||
|
removeAllChildren();
|
||||||
|
|
||||||
|
Scene::onExit();
|
||||||
|
}
|
||||||
|
|
||||||
void onUpdate(float dt) override {
|
void onUpdate(float dt) override {
|
||||||
Scene::onUpdate(dt);
|
Scene::onUpdate(dt);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,6 @@
|
||||||
-- 支持平台: MinGW (Windows), Nintendo Switch
|
-- 支持平台: MinGW (Windows), Nintendo Switch
|
||||||
-- ==============================================
|
-- ==============================================
|
||||||
|
|
||||||
-- 获取当前平台
|
|
||||||
local host_plat = os.host()
|
|
||||||
local target_plat = get_config("plat") or host_plat
|
|
||||||
|
|
||||||
-- 获取当前脚本所在目录(示例根目录)
|
-- 获取当前脚本所在目录(示例根目录)
|
||||||
local example_dir = os.scriptdir()
|
local example_dir = os.scriptdir()
|
||||||
|
|
||||||
|
|
@ -17,7 +13,8 @@ target("spatial_index_demo")
|
||||||
add_includedirs("../../Extra2D/include")
|
add_includedirs("../../Extra2D/include")
|
||||||
add_deps("extra2d")
|
add_deps("extra2d")
|
||||||
|
|
||||||
if target_plat == "switch" then
|
-- 使用与主项目相同的平台配置
|
||||||
|
if is_plat("switch") then
|
||||||
set_plat("switch")
|
set_plat("switch")
|
||||||
set_arch("arm64")
|
set_arch("arm64")
|
||||||
set_toolchains("switch")
|
set_toolchains("switch")
|
||||||
|
|
@ -43,7 +40,7 @@ target("spatial_index_demo")
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
elseif target_plat == "mingw" then
|
elseif is_plat("mingw") then
|
||||||
set_plat("mingw")
|
set_plat("mingw")
|
||||||
set_arch("x86_64")
|
set_arch("x86_64")
|
||||||
set_targetdir("../../build/examples/spatial_index_demo")
|
set_targetdir("../../build/examples/spatial_index_demo")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue