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

@@ -0,0 +1,46 @@
#pragma once
#include <frostbite2D/base/RefObject.h>
#include <frostbite2D/types/type_alias.h>
#include <string>
#include <SDL2/SDL_mixer.h>
namespace frostbite2D {
/**
* @brief 音乐类
*
* 用于加载和播放长音频文件(背景音乐),支持从文件或内存加载。
* 支持 WAV、OGG、MP3 等格式。
*/
class Music : public RefObject {
public:
static Ptr<Music> loadFromFile(const std::string& path);
static Ptr<Music> loadFromMemory(const uint8* data, size_t size);
~Music();
bool play(int loops = -1);
bool fadeIn(int ms, int loops = -1);
void pause();
void resume();
void stop();
bool fadeOut(int ms);
void setVolume(float volume);
float getVolume() const;
bool isPlaying() const;
bool isPaused() const;
const std::string& getPath() const { return path_; }
private:
Music(Mix_Music* music, const std::string& path = "");
Mix_Music* music_ = nullptr;
std::string path_;
float volume_ = 1.0f;
};
}