feat(audio): 添加音频系统支持背景音乐和音效播放

实现完整的音频系统,包括:
1. 添加 SDL2_mixer 依赖
2. 创建音频系统核心类 AudioSystem
3. 实现音乐(Music)和音效(Sound)类
4. 在游戏主循环中初始化音频并播放背景音乐
5. 更新构建脚本以支持音频库
This commit is contained in:
2026-03-19 03:13:18 +08:00
parent fc81c2634c
commit 9ce47cc501
10 changed files with 507 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
add_requires("libsdl2", {configs = {shared = true,wayland = true}})
add_requires("libsdl2_image")
add_requires("libsdl2_mixer")
add_requires("glm")
target("Frostbite2D")
@@ -14,6 +15,7 @@ target("Frostbite2D")
add_packages("libsdl2")
add_packages("libsdl2_image")
add_packages("libsdl2_mixer")
add_packages("glm")
-- 复制着色器文件到输出目录

View File

@@ -3,6 +3,7 @@ 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")
@@ -16,6 +17,7 @@ target("Frostbite2D")
add_packages("libsdl2")
add_packages("libsdl2_image")
add_packages("libsdl2_mixer")
add_packages("glm")
-- 复制 assets 目录到输出目录
@@ -31,36 +33,40 @@ target("Frostbite2D")
print("Copy assets directory: " .. assets_dir .. " -> " .. target_assets_dir)
end
-- 复制 SDL2 DLL (Windows 平台)
-- 复制 SDL2 和 SDL2_mixer DLL (Windows 平台)
if is_plat("mingw") or is_plat("windows") then
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))
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.dll
local sdl2_dll_paths = {
-- 尝试从 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(sdl2_dll_paths) do
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, "SDL2.dll")
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 SDL2.dll from: " .. dll_file)
print("Copy DLL from: " .. dll_file)
end
end
end