Files
2026-06-08 19:58:35 +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;
};
}