Files
Frostbite2D/Frostbite2D/include/frostbite2D/audio/sound.h
Lenheart d55808d80f feat: 实现游戏地图和城镇系统基础架构
新增GameMap和GameTown类实现游戏地图和城镇的核心功能
添加GameWorld作为游戏世界管理器处理场景切换
完善音频系统支持从路径加载音乐和音效
重构PVF资源系统增加路径规范化功能
添加.gitignore排除游戏资源目录
2026-04-01 09:53:06 +08:00

51 lines
1.2 KiB
C++

#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 等格式。
*/
class Sound : public RefObject {
public:
static Ptr<Sound> loadFromPath(const std::string& path);
static Ptr<Sound> loadFromFile(const std::string& path);
static Ptr<Sound> loadFromMemory(const uint8* data, size_t size);
static Ptr<Sound> loadFromNpk(const std::string& audioPath);
~Sound();
int play(int loops = 0, int channel = -1);
int playLoop(int channel = -1);
void setVolume(float volume);
float getVolume() const;
int getLastChannel() const;
static void stop(int channel);
static void stopAll();
static bool isPlaying(int channel);
static void pause(int channel);
static void resume(int channel);
static void pauseAll();
static void resumeAll();
const std::string& getPath() const { return path_; }
private:
Sound(Mix_Chunk* chunk, const std::string& path = "");
Mix_Chunk* chunk_ = nullptr;
std::string path_;
float volume_ = 1.0f;
int lastChannel_ = -1;
};
}