Initial engine repository
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include <frostbite2D/types/type_alias.h>
|
||||
#include <string>
|
||||
|
||||
namespace frostbite2D {
|
||||
|
||||
/**
|
||||
* @brief 音频系统配置
|
||||
*/
|
||||
struct AudioConfig {
|
||||
int frequency = 44100;
|
||||
uint16_t format = 0x8010;
|
||||
int channels = 2;
|
||||
int chunksize = 4096;
|
||||
int maxSoundChannels = 32;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 音频系统(单例)
|
||||
*
|
||||
* 负责初始化和管理整个音频系统,包括音量控制和声道管理。
|
||||
*/
|
||||
class AudioSystem {
|
||||
public:
|
||||
static AudioSystem& get();
|
||||
|
||||
AudioSystem(const AudioSystem&) = delete;
|
||||
AudioSystem& operator=(const AudioSystem&) = delete;
|
||||
|
||||
bool init(const AudioConfig& config = AudioConfig());
|
||||
void shutdown();
|
||||
bool isInitialized() const { return initialized_; }
|
||||
|
||||
void setMasterVolume(float volume);
|
||||
float getMasterVolume() const;
|
||||
|
||||
void setSoundVolume(float volume);
|
||||
float getSoundVolume() const;
|
||||
|
||||
void setMusicVolume(float volume);
|
||||
float getMusicVolume() const;
|
||||
|
||||
void pauseAllSounds();
|
||||
void resumeAllSounds();
|
||||
void stopAllSounds();
|
||||
|
||||
int getMaxChannels() const { return maxChannels_; }
|
||||
|
||||
private:
|
||||
AudioSystem() = default;
|
||||
// ~AudioSystem() 在 shutdown() 中手动调用销毁
|
||||
|
||||
void updateVolumes();
|
||||
|
||||
bool initialized_ = false;
|
||||
float masterVolume_ = 1.0f;
|
||||
float soundVolume_ = 1.0f;
|
||||
float musicVolume_ = 1.0f;
|
||||
int maxChannels_ = 32;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#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> loadFromPath(const std::string& path);
|
||||
static Ptr<Music> loadFromFile(const std::string& path);
|
||||
static Ptr<Music> loadFromMemory(const uint8* data, size_t size);
|
||||
static Ptr<Music> loadFromNpk(const std::string& audioPath);
|
||||
static bool isPathPlaying(const std::string& path);
|
||||
static std::string getCurrentPlayingPath();
|
||||
|
||||
~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 = "");
|
||||
Music(Mix_Music* music, std::vector<uint8> data, const std::string& path = "");
|
||||
|
||||
Mix_Music* music_ = nullptr;
|
||||
std::string path_;
|
||||
std::vector<uint8> data_;
|
||||
float volume_ = 1.0f;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#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;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user