diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libjin/Audio/Audio.h | 25 | ||||
-rw-r--r-- | src/libjin/Audio/SDL/SDLAudio.cpp | 60 | ||||
-rw-r--r-- | src/libjin/Audio/SDL/SDLAudio.h | 11 | ||||
-rw-r--r-- | src/libjin/Audio/SDL/SDLSource.cpp | 94 | ||||
-rw-r--r-- | src/libjin/Audio/SDL/SDLSource.h | 28 | ||||
-rw-r--r-- | src/libjin/Core/Game.cpp | 2 | ||||
-rw-r--r-- | src/libjin/Core/Game.h | 7 | ||||
-rw-r--r-- | src/libjin/Math/Math.h | 6 | ||||
-rw-r--r-- | src/libjin/audio/audio.h | 25 | ||||
-rw-r--r-- | src/libjin/core/game.cpp | 2 | ||||
-rw-r--r-- | src/libjin/core/game.h | 7 | ||||
-rw-r--r-- | src/libjin/math/math.h | 6 | ||||
-rw-r--r-- | src/libjin/modules.h | 6 |
13 files changed, 166 insertions, 113 deletions
diff --git a/src/libjin/Audio/Audio.h b/src/libjin/Audio/Audio.h index 5b43729..6c3468e 100644 --- a/src/libjin/Audio/Audio.h +++ b/src/libjin/Audio/Audio.h @@ -20,23 +20,30 @@ namespace audio public: + enum State + { + PLAY , + STOP , + PAUSE, + }; + virtual void play() = 0; virtual void stop() = 0; - virtual bool pause() = 0; - virtual bool pause(Source* source) = 0; - virtual bool resume() = 0; - virtual bool resume(Source* source) = 0; - virtual void rewind() = 0; + virtual void pause() = 0; + virtual void resume() = 0; virtual void setVolume(float volume) = 0; - virtual float getVolume() = 0; protected: - Audio() {}; + Audio() + : volume(1) + , state(State::PLAY) + {}; virtual ~Audio() {}; - SINGLETON(Audio); - + + float volume; + State state; }; } diff --git a/src/libjin/Audio/SDL/SDLAudio.cpp b/src/libjin/Audio/SDL/SDLAudio.cpp index a41382b..f7ca70d 100644 --- a/src/libjin/Audio/SDL/SDLAudio.cpp +++ b/src/libjin/Audio/SDL/SDLAudio.cpp @@ -18,15 +18,20 @@ namespace audio static void defaultCallback(void *userdata, Uint8 *stream, int size) { static SDLAudio* audio = static_cast<SDLAudio*>(userdata); + if (!audio->goOnProcess()) + return; audio->lock(); audio->processCommands(); audio->processSources(stream, size); + audio->processBuffer(stream, size); audio->unlock(); } onlyonce bool SDLAudio::initSystem(const SettingBase* s) { +#if JIN_DEBUG Loghelper::log(Loglevel::LV_INFO, "Init Audio System"); +#endif if (SDL_Init(SDL_INIT_AUDIO) < 0) return false; @@ -68,6 +73,20 @@ namespace audio SDL_UnlockAudioDevice(audioDevice); } + bool SDLAudio::goOnProcess() + { + if (state == SDLAudio::State::STOP) + { + SDLSourceManager::get()->removeAllSource(); + pause(); + return false; + } + else if (state == SDLAudio::State::PAUSE) + return false; + else + return true; + } + void SDLAudio::processCommands() { SDLSourceManager::get()->processCommands(); @@ -78,45 +97,44 @@ namespace audio SDLSourceManager::get()->processSources(buffer, len); } - void SDLAudio::play() {} - - void SDLAudio::stop() {} - - bool SDLAudio::pause() + void SDLAudio::processBuffer(void* buff, size_t len) { - return false; + short* buffer = (short*)buff; + int samples = (len / SDLAUDIO_BYTEDEPTH) >> 1; // ˫ + const char L = 0, R = 1; + for (int i = 0; i < samples; ++i) + { + short* clip = buffer + (i << 1); + clip[L] *= volume; + clip[R] *= volume; + } } - bool SDLAudio::pause(Source* source) + void SDLAudio::play() { - return false; + state = State::PLAY; } - bool SDLAudio::resume() + void SDLAudio::stop() { - return false; + state = State::STOP; } - bool SDLAudio::resume(Source* source) + void SDLAudio::pause() { - return false; + state = State::PAUSE; } - - void SDLAudio::rewind() + + void SDLAudio::resume() { - + state = State::PLAY; } void SDLAudio::setVolume(float volume) { - + this->volume = clamp(volume, 0.0f, 1.0f); } - float SDLAudio::getVolume() - { - return 0.f; - } - } } diff --git a/src/libjin/Audio/SDL/SDLAudio.h b/src/libjin/Audio/SDL/SDLAudio.h index 6837126..f2a4fab 100644 --- a/src/libjin/Audio/SDL/SDLAudio.h +++ b/src/libjin/Audio/SDL/SDLAudio.h @@ -3,6 +3,7 @@ #include "../../modules.h" #if JIN_MODULES_AUDIO && JIN_AUDIO_SDLAUDIO +#include "SDLSource.h" #include <vector> #include "../audio.h" @@ -30,17 +31,15 @@ namespace audio /* IAudio interface */ void play() override; void stop() override; - bool pause() override; - bool pause(Source* source) override; - bool resume() override; - bool resume(Source* source) override; - void rewind() override; + void pause() override; + void resume() override; void setVolume(float volume) override; - float getVolume() override; /* process functions*/ void processCommands(); void processSources(void* buffer, size_t len); + void processBuffer(void* buffer, size_t len); + bool goOnProcess(); void lock(); void unlock(); diff --git a/src/libjin/Audio/SDL/SDLSource.cpp b/src/libjin/Audio/SDL/SDLSource.cpp index 18ba855..c868df5 100644 --- a/src/libjin/Audio/SDL/SDLSource.cpp +++ b/src/libjin/Audio/SDL/SDLSource.cpp @@ -3,14 +3,13 @@ #include <exception> #include <fstream> - +#include <climits> #include "../../math/math.h" #include "../../utils/macros.h" #include "SDLSource.h" #include "../../3rdparty/wav/wav.h" #define STB_VORBIS_HEADER_ONLY #include "../../3rdparty/stb/stb_vorbis.c" - #include "SDLAudio.h" namespace jin @@ -64,9 +63,9 @@ namespace audio #define Action Command::Action #define Manager SDLSourceManager - shared std::queue<Command*> Manager::commands; - shared std::stack<Command*> Manager::commandsPool; - shared std::vector<SDLSource*> Manager::sources; + //shared std::queue<Command*> Manager::commands; + //shared std::stack<Command*> Manager::commandsPool; + //shared std::vector<SDLSource*> Manager::sources; shared Manager* Manager::manager = nullptr; SDLSource* SDLSource::createSource(const char* file) @@ -116,6 +115,7 @@ namespace audio { memset(&status, 0, sizeof(status)); memset(&raw, 0, sizeof(raw)); + status.volume = 1; } SDLSource::~SDLSource() @@ -131,12 +131,12 @@ namespace audio if (wav_read(&wav, mem, size) == 0) { raw.data = wav.data; - raw.length = wav.length * wav.bitdepth / 8; + raw.length = wav.length * wav.channels * wav.bitdepth / 8; + raw.channels = clamp<int>(wav.channels, CHANNEL::MONO, CHANNEL::STEREO); raw.end = (char*)raw.data + raw.length; raw.samplerate = wav.samplerate; raw.bitdepth = wav.bitdepth; - raw.samples = raw.length / (wav.bitdepth / 8.f) / wav.channels; - raw.channels = clamp<int>(wav.channels, CHANNEL::MONO, CHANNEL::STEREO); + raw.samples = wav.length; } else throw SourceException(); @@ -159,8 +159,8 @@ namespace audio raw.channels = channels; raw.samplerate = samplerate; raw.data = data; - raw.samples = samples; - raw.length = samples * channels * sizeof(short); + raw.samples = samples; // һsample + raw.length = samples * channels * sizeof(short); // һsample raw.bitdepth = bitdepth; raw.end = (char*)data + raw.length; } @@ -228,7 +228,7 @@ Manager::get()->pushCommand(cmd); \ void SDLSource::setVolume(float volume) { - ActionFloat(SetVolume, volume); + ActionFloat(SetVolume, clamp(volume, 0.0f, 1.0f)); } bool SDLSource::setLoop(bool loop) @@ -274,7 +274,7 @@ Manager::get()->pushCommand(cmd); \ status.pos = 0; break; case Command::Action::SetVolume: - //float cmd->parameter._float; + status.volume = cmd->parameter._float; break; case Command::Action::SetLoop: status.loop = cmd->parameter._boolean; @@ -285,39 +285,31 @@ Manager::get()->pushCommand(cmd); \ inline void SDLSource::process(void* buf, size_t size) { short* buffer = (short*)buf; // AUDIO_S16SYS - unsigned int samples = size / SDLAUDIO_BYTEDEPTH; - short* sample; - short origin; - - const char bitdepth = raw.bitdepth; - const char channles = raw.channels; - - int pos = status.pos; - int pitch = status.pitch; - int state = status.state; - bool loop = status.loop; - int volume = status.volume; - short* clip16 = nullptr; - char* clip8 = nullptr; - int clip = 0; - - if (bitdepth == 8) - clip8 = (char*)raw.data; - else if (bitdepth == 16) - clip16 = (short*)raw.data; - - for (int i = 0; i < samples; i+=2 /*˫*/) + int samples = (size / SDLAUDIO_BYTEDEPTH) >> 1; // ˫ + const char L = 0, R = 1; + for (int i = 0; i < samples; ++i) { - /* Ƶļsampleᱻ */ - sample = buffer + i * SDLAUDIO_BYTEDEPTH; - origin = *sample; - if (bitdepth == 8) + char* source = (char*)raw.data + status.pos * (raw.bitdepth / 8) * raw.channels; + short left = 0; + short right = 0; + if (raw.bitdepth == 16) + { + left = ((short*)source)[L] * status.volume; + right = ((short*)source)[L + raw.channels - 1] * status.volume; + } + else if (raw.bitdepth == 8) { - clip = *clip8; + left = source[L] << 8; // << 8 Ŵ16bits + right = source[L + raw.channels - 1] << 8; } - else if (bitdepth == 16) - clip = *clip16; - + short* sample = buffer + (i << 1); + sample[L] = clamp(sample[L] + left, SHRT_MIN, SHRT_MAX); // + sample[R] = clamp(sample[R] + right, SHRT_MIN, SHRT_MAX); // + ++status.pos; + if (status.pos > raw.samples && status.loop) + status.pos = 0; // rewind + else if (status.pos > raw.samples && !status.loop) + break; } } @@ -326,7 +318,7 @@ Manager::get()->pushCommand(cmd); \ return (manager == nullptr ? manager = new Manager() : manager); } - shared void Manager::processCommands() + void Manager::processCommands() { Command* cmd = nullptr; SDLSource* source = nullptr; @@ -344,7 +336,7 @@ Manager::get()->pushCommand(cmd); \ } /* AUDIO_S16SYS[size>>1] buffer */ - shared void Manager::processSources(void* buf, size_t size) + void Manager::processSources(void* buf, size_t size) { /* clear render buffer */ memset(buf, 0, size); @@ -359,7 +351,7 @@ Manager::get()->pushCommand(cmd); \ } } - shared void Manager::removeSource(SDLSource* source) + void Manager::removeSource(SDLSource* source) { std::vector<SDLSource*>::iterator it = sources.begin(); for (it = sources.begin(); it != sources.end(); ) @@ -373,18 +365,23 @@ Manager::get()->pushCommand(cmd); \ } } - shared void Manager::pushSource(SDLSource* source) + void Manager::removeAllSource() + { + sources.clear(); + } + + void Manager::pushSource(SDLSource* source) { if(source != nullptr) sources.push_back(source); } - shared void Manager::pushCommand(SDLSourceCommand* cmd) + void Manager::pushCommand(SDLSourceCommand* cmd) { commands.push(cmd); } - shared Command* Manager::getCommand() + Command* Manager::getCommand() { if (!commandsPool.empty()) { @@ -395,6 +392,7 @@ Manager::get()->pushCommand(cmd); \ return new Command(); } + } } diff --git a/src/libjin/Audio/SDL/SDLSource.h b/src/libjin/Audio/SDL/SDLSource.h index 5c6aefc..9a3dd9b 100644 --- a/src/libjin/Audio/SDL/SDLSource.h +++ b/src/libjin/Audio/SDL/SDLSource.h @@ -72,7 +72,7 @@ namespace audio int pitch; // pitch int state; // ǰ״̬ bool loop; // loop or not - int volume; // + float volume; // } status; }; @@ -85,20 +85,22 @@ namespace audio static SDLSourceManager* get(); /* Process function */ - static void processCommands(); - static void processSources(void* buffer, size_t size); - - static void removeSource(SDLSource* source); - static void pushSource(SDLSource* source); - static SDLSourceCommand* getCommand(); - static void pushCommand(SDLSourceCommand* cmd); - + void processCommands(); + void processSources(void* buffer, size_t size); + + void removeAllSource(); + void removeSource(SDLSource* source); + void pushSource(SDLSource* source); + SDLSourceCommand* getCommand(); + void pushCommand(SDLSourceCommand* cmd); + + private : + + std::queue<SDLSourceCommand*> commands; + std::stack<SDLSourceCommand*> commandsPool; + std::vector<SDLSource*> sources; // processing sources static SDLSourceManager* manager; - static std::queue<SDLSourceCommand*> commands; - static std::stack<SDLSourceCommand*> commandsPool; - static std::vector<SDLSource*> sources; // processing sources - }; class SourceException : public std::exception diff --git a/src/libjin/Core/Game.cpp b/src/libjin/Core/Game.cpp index ffd8015..929cc07 100644 --- a/src/libjin/Core/Game.cpp +++ b/src/libjin/Core/Game.cpp @@ -19,6 +19,7 @@ namespace core void Game::run() { + SAFECALL(_onLoad); Window* wnd = Window::get(); const int FPS = wnd ? wnd->getFPS() : 60; const int MS_PER_UPDATE = 1000.0f / FPS; @@ -59,6 +60,7 @@ namespace core _onEvent = s->eventHandler; _onUpdate = s->updater; _onDraw = s->drawer; + _onLoad = s->loader; return true; } diff --git a/src/libjin/Core/Game.h b/src/libjin/Core/Game.h index 090e7c6..9359487 100644 --- a/src/libjin/Core/Game.h +++ b/src/libjin/Core/Game.h @@ -16,6 +16,7 @@ namespace core { public: + typedef void(*onLoad)(); typedef void(*onEvent)(jin::input::Event* e); typedef void(*onUpdate)(float dt); typedef void(*onDraw)(); @@ -25,6 +26,7 @@ namespace core onEvent eventHandler; onUpdate updater; onDraw drawer; + onLoad loader; }; void run(); @@ -38,9 +40,10 @@ namespace core SINGLETON(Game); - onEvent _onEvent; + onEvent _onEvent; onUpdate _onUpdate; - onDraw _onDraw; + onDraw _onDraw; + onLoad _onLoad; bool _running; diff --git a/src/libjin/Math/Math.h b/src/libjin/Math/Math.h index 5b34f4c..d4bd202 100644 --- a/src/libjin/Math/Math.h +++ b/src/libjin/Math/Math.h @@ -65,6 +65,12 @@ namespace math return a > upper ? upper : a; } + template<typename T> + inline T lerp(T a, T b, float t) + { + return a + t * (b - a); + } + } } diff --git a/src/libjin/audio/audio.h b/src/libjin/audio/audio.h index 5b43729..6c3468e 100644 --- a/src/libjin/audio/audio.h +++ b/src/libjin/audio/audio.h @@ -20,23 +20,30 @@ namespace audio public: + enum State + { + PLAY , + STOP , + PAUSE, + }; + virtual void play() = 0; virtual void stop() = 0; - virtual bool pause() = 0; - virtual bool pause(Source* source) = 0; - virtual bool resume() = 0; - virtual bool resume(Source* source) = 0; - virtual void rewind() = 0; + virtual void pause() = 0; + virtual void resume() = 0; virtual void setVolume(float volume) = 0; - virtual float getVolume() = 0; protected: - Audio() {}; + Audio() + : volume(1) + , state(State::PLAY) + {}; virtual ~Audio() {}; - SINGLETON(Audio); - + + float volume; + State state; }; } diff --git a/src/libjin/core/game.cpp b/src/libjin/core/game.cpp index ffd8015..929cc07 100644 --- a/src/libjin/core/game.cpp +++ b/src/libjin/core/game.cpp @@ -19,6 +19,7 @@ namespace core void Game::run() { + SAFECALL(_onLoad); Window* wnd = Window::get(); const int FPS = wnd ? wnd->getFPS() : 60; const int MS_PER_UPDATE = 1000.0f / FPS; @@ -59,6 +60,7 @@ namespace core _onEvent = s->eventHandler; _onUpdate = s->updater; _onDraw = s->drawer; + _onLoad = s->loader; return true; } diff --git a/src/libjin/core/game.h b/src/libjin/core/game.h index 090e7c6..9359487 100644 --- a/src/libjin/core/game.h +++ b/src/libjin/core/game.h @@ -16,6 +16,7 @@ namespace core { public: + typedef void(*onLoad)(); typedef void(*onEvent)(jin::input::Event* e); typedef void(*onUpdate)(float dt); typedef void(*onDraw)(); @@ -25,6 +26,7 @@ namespace core onEvent eventHandler; onUpdate updater; onDraw drawer; + onLoad loader; }; void run(); @@ -38,9 +40,10 @@ namespace core SINGLETON(Game); - onEvent _onEvent; + onEvent _onEvent; onUpdate _onUpdate; - onDraw _onDraw; + onDraw _onDraw; + onLoad _onLoad; bool _running; diff --git a/src/libjin/math/math.h b/src/libjin/math/math.h index 5b34f4c..d4bd202 100644 --- a/src/libjin/math/math.h +++ b/src/libjin/math/math.h @@ -65,6 +65,12 @@ namespace math return a > upper ? upper : a; } + template<typename T> + inline T lerp(T a, T b, float t) + { + return a + t * (b - a); + } + } } diff --git a/src/libjin/modules.h b/src/libjin/modules.h index 454a2c2..171c691 100644 --- a/src/libjin/modules.h +++ b/src/libjin/modules.h @@ -21,7 +21,7 @@ #define JIN_MODULES_NET 1 -#define JIN_MODULES_PHYSICS 0 +#define JIN_MODULES_PHYSICS 1 #define JIN_PHYSICS_BOX2D 1 #define JIN_PHYSICS_NEWTON 1 @@ -29,7 +29,7 @@ #define JIN_MODULES_UI 1 -#define JIN_MODULES_TOOLS 0 +#define JIN_MODULES_TOOLS 1 #define JIN_TOOLS_COMPONENT 1 #define JIN_TOOLS_EVENTMSGCENTER 1 #define JIN_TOOLS_XML 1 @@ -45,6 +45,6 @@ * Debug */ -#define JIN_DEBUG 1 +#define JIN_DEBUG 1 #endif
\ No newline at end of file |