feat(窗口图标): 添加窗口图标支持并完善相关资源处理

添加窗口图标功能,包括:
1. 支持通过配置文件设置窗口图标
2. 添加Windows资源文件处理
3. 更新各平台构建脚本以处理图标资源
4. 添加图标使用文档说明
5. 优化Application类结构

同时修复了Switch平台构建脚本中的团队名称错误
This commit is contained in:
2026-02-26 09:16:25 +08:00
parent c94e43089b
commit ab3dd829b8
12 changed files with 579 additions and 108 deletions

View File

@@ -32,37 +32,20 @@ target("Frostbite2D")
end
end
-- 复制 SDL2 DLL
local sdl2_lib = target:pkg("libsdl2")
if sdl2_lib then
local libfiles = sdl2_lib:get("libfiles")
if libfiles then
for _, libfile in ipairs(libfiles) do
-- 查找 DLL 文件
if libfile:endswith(".dll") then
local target_dll = path.join(output_dir, path.filename(libfile))
os.cp(libfile, target_dll)
print("Copy DLL: " .. path.filename(libfile))
end
end
end
-- 复制图标文件到输出目录
local icons_dir = path.join(os.projectdir(), "assets/icons")
local target_icons_dir = path.join(output_dir, "assets/icons")
if os.isdir(icons_dir) then
if not os.isdir(target_icons_dir) then
os.mkdir(target_icons_dir)
end
-- 尝试从 xmake 包目录复制 SDL2.dll
local sdl2_dll_paths = {
path.join(os.getenv("USERPROFILE") or "", ".xmake/packages/l/libsdl2/**/bin/SDL2.dll"),
path.join(os.getenv("USERPROFILE") or "", ".xmake/packages/l/libsdl2/**/lib/SDL2.dll"),
}
for _, dll_pattern in ipairs(sdl2_dll_paths) do
local dll_files = os.files(dll_pattern)
for _, dll_file in ipairs(dll_files) do
local target_dll = path.join(output_dir, "SDL2.dll")
if not os.isfile(target_dll) then
os.cp(dll_file, target_dll)
print("Copy SDL2.dll from: " .. dll_file)
end
end
for _, file in ipairs(os.files(path.join(icons_dir, "*.*"))) do
local filename = path.filename(file)
local target_file = path.join(target_icons_dir, filename)
os.cp(file, target_file)
print("Copy icon: " .. filename)
end
end
end)
target_end()

View File

@@ -50,14 +50,16 @@ target("Frostbite2D")
local nro_file = path.join(output_dir, "hello_world.nro")
local nacptool = path.join(devkitPro, "tools/bin/nacptool.exe")
local elf2nro = path.join(devkitPro, "tools/bin/elf2nro.exe")
local icon_file = path.join(os.projectdir(), "assets/icons/icon.bmp")
if os.isfile(nacptool) and os.isfile(elf2nro) then
os.vrunv(nacptool, {"--create", "Hello World", "Extra2D Team", "1.0.0", nacp_file})
os.vrunv(nacptool, {"--create", "Hello World", "Frostbite2D Team", "1.0.0", nacp_file})
local romfs = path.join(example_dir, "romfs")
local icon_param = os.isfile(icon_file) and ("--icon=" .. icon_file) or ""
if os.isdir(romfs) then
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file, "--romfsdir=" .. romfs})
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file, icon_param, "--romfsdir=" .. romfs})
else
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file})
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file, icon_param})
end
print("Generated NRO: " .. nro_file)
end

View File

@@ -4,12 +4,33 @@ set_toolchains("mingw")
add_requires("libsdl2", {configs = {shared = true}})
add_requires("glm")
-- 创建自定义规则来编译 .rc 文件
rule("windres_compile")
set_extensions(".rc")
on_build(function (target)
local windres = "windres"
local rc_file = target:sourcefiles()[1]
local obj_file = target:targetfile()
local arch = is_arch("x86_64") and "pe-x86-64" or "pe-i386"
os.execv(windres, {"-i", rc_file, "-o", obj_file, "-O", "coff", "-F", arch})
end)
rule_end()
target("Frostbite2D")
set_kind("binary")
add_files(path.join(os.projectdir(), "Frostbite2D/src/**.cpp"))
add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c"))
add_includedirs(path.join(os.projectdir(), "Frostbite2D/include"))
-- 添加资源文件(如果存在)
local rc_file = path.join(os.projectdir(), "resources/app.rc")
local ico_file = path.join(os.projectdir(), "assets/icons/app.ico")
if os.isfile(rc_file) and os.isfile(ico_file) then
-- 添加 .rc 文件并应用自定义规则
add_files(rc_file, {rule = "windres_compile"})
end
add_packages("libsdl2")
add_packages("glm")
@@ -34,6 +55,22 @@ target("Frostbite2D")
end
end
-- 复制图标文件到输出目录
local icons_dir = path.join(os.projectdir(), "assets/icons")
local target_icons_dir = path.join(output_dir, "assets/icons")
if os.isdir(icons_dir) then
if not os.isdir(target_icons_dir) then
os.mkdir(target_icons_dir)
end
for _, file in ipairs(os.files(path.join(icons_dir, "*.*"))) do
local filename = path.filename(file)
local target_file = path.join(target_icons_dir, filename)
os.cp(file, target_file)
print("Copy icon: " .. filename)
end
end
-- 复制 SDL2 DLL
local sdl2_lib = target:pkg("libsdl2")
if sdl2_lib then
@@ -67,4 +104,4 @@ target("Frostbite2D")
end
end
end)
target_end()
target_end()