实现完整的音频系统,包括: 1. 添加 SDL2_mixer 依赖 2. 创建音频系统核心类 AudioSystem 3. 实现音乐(Music)和音效(Sound)类 4. 在游戏主循环中初始化音频并播放背景音乐 5. 更新构建脚本以支持音频库
76 lines
3.1 KiB
Lua
76 lines
3.1 KiB
Lua
-- MinGW 编译配置
|
|
set_toolchains("mingw")
|
|
|
|
add_requires("libsdl2", {configs = {shared = true}})
|
|
add_requires("libsdl2_image", {configs = {shared = true}})
|
|
add_requires("libsdl2_mixer", {configs = {shared = true}})
|
|
add_requires("glm")
|
|
|
|
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"))
|
|
|
|
add_files(path.join(os.projectdir(), "Game/src/**.cpp"))
|
|
add_includedirs(path.join(os.projectdir(), "Game/include"))
|
|
|
|
add_packages("libsdl2")
|
|
add_packages("libsdl2_image")
|
|
add_packages("libsdl2_mixer")
|
|
add_packages("glm")
|
|
|
|
-- 复制 assets 目录到输出目录
|
|
after_build(function (target)
|
|
-- 复制 assets 目录
|
|
local assets_dir = path.join(os.projectdir(), "Game/assets")
|
|
local output_dir = target:targetdir()
|
|
local target_assets_dir = path.join(output_dir, "assets")
|
|
|
|
if os.isdir(assets_dir) then
|
|
os.rm(target_assets_dir)
|
|
os.cp(assets_dir, output_dir)
|
|
print("Copy assets directory: " .. assets_dir .. " -> " .. target_assets_dir)
|
|
end
|
|
|
|
-- 复制 SDL2 和 SDL2_mixer DLL (Windows 平台)
|
|
if is_plat("mingw") or is_plat("windows") then
|
|
for _, pkg_name in ipairs({"libsdl2", "libsdl2_mixer"}) do
|
|
local pkg = target:pkg(pkg_name)
|
|
if pkg then
|
|
local libfiles = pkg: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
|
|
end
|
|
end
|
|
|
|
-- 尝试从 xmake 包目录复制 SDL2 和 SDL2_mixer DLL
|
|
local 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"),
|
|
path.join(os.getenv("USERPROFILE") or "", ".xmake/packages/l/libsdl2_mixer/**/bin/SDL2_mixer.dll"),
|
|
path.join(os.getenv("USERPROFILE") or "", ".xmake/packages/l/libsdl2_mixer/**/lib/SDL2_mixer.dll"),
|
|
}
|
|
|
|
for _, dll_pattern in ipairs(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, path.filename(dll_file))
|
|
if not os.isfile(target_dll) then
|
|
os.cp(dll_file, target_dll)
|
|
print("Copy DLL from: " .. dll_file)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end)
|
|
target_end()
|