diff --git a/include/engine.h b/include/engine.h index c4fa6ec..35a380a 100644 --- a/include/engine.h +++ b/include/engine.h @@ -40,6 +40,12 @@ namespace ijengine { void set_audio_dir(const string& dir_path); void play_audio_from_path(const string& path); void stop_audio(); + + void play_sound_effect(const string &path); + + int set_audio_volume(double percentage); + int set_sound_effect_volume(double percentage); + } namespace event { diff --git a/include/kernel.h b/include/kernel.h index 28e23aa..3949f51 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -32,6 +32,10 @@ namespace ijengine { virtual void set_audio_dir(const string& dir_path) = 0; virtual void play_audio_from_path(const string& title) = 0; virtual void stop_audio() = 0; + virtual void play_sound_effect(const string& path) = 0; + + virtual int set_audio_volume(double percentage) = 0; + virtual int set_sound_effect_volume(double percentage) = 0; virtual list pending_mouse_events(unsigned now) = 0; virtual list pending_system_events(unsigned now) = 0; diff --git a/kernel/sdl2/sdl2kernel.cpp b/kernel/sdl2/sdl2kernel.cpp index 89b3768..ca00d47 100644 --- a/kernel/sdl2/sdl2kernel.cpp +++ b/kernel/sdl2/sdl2kernel.cpp @@ -276,7 +276,7 @@ SDL2Kernel::play_audio_from_path(const string& path) // printf("error: %s\n", Mix_GetError()); } - Mix_PlayMusic(audio, -1); + Mix_FadeInMusic(audio, -1, 2000); } } @@ -286,6 +286,41 @@ SDL2Kernel::stop_audio() Mix_HaltMusic(); } +void +SDL2Kernel::play_sound_effect(const string& path) +{ + Mix_Chunk *effect = Mix_LoadWAV(path.c_str()); + + if(not effect){ + printf("Failed to load sound effect\n"); + } + + Mix_PlayChannel(-1, effect, 0); +} + +int +SDL2Kernel::set_audio_volume(double percentage) +{ + double new_volume = (MIX_MAX_VOLUME * percentage); + + // Mix_VolumeMusic(int volume) + // if volume is -1, returns current volume + int d = Mix_VolumeMusic(new_volume); + + return d; +} + +int +SDL2Kernel::set_sound_effect_volume(double percentage){ + double new_volume = (MIX_MAX_VOLUME * percentage); + + // Mix_Volume(channel, volume) + // if channel is -1, set volume to all channels + int d = Mix_Volume(-1, new_volume); + + return d; +} + void SDL2Kernel::update_pending_events(unsigned now) { diff --git a/kernel/sdl2/sdl2kernel.h b/kernel/sdl2/sdl2kernel.h index 5f6ba4c..ab015df 100644 --- a/kernel/sdl2/sdl2kernel.h +++ b/kernel/sdl2/sdl2kernel.h @@ -22,6 +22,9 @@ class SDL2Kernel : public Kernel { void set_audio_dir(const string& dir_path); void play_audio_from_path(const string& title); void stop_audio(); + void play_sound_effect(const string& path); + int set_audio_volume(double percentage); + int set_sound_effect_volume(double percentage); list pending_mouse_events(unsigned now); list pending_system_events(unsigned now); diff --git a/src/engine.cpp b/src/engine.cpp index 5205978..80c25b8 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -105,7 +105,25 @@ namespace ijengine void stop_audio() { - kernel->stop_audio(); + kernel->stop_audio(); + } + + void + play_sound_effect(const string &path) + { + kernel->play_sound_effect(path); + } + + int + set_audio_volume(double percentage) + { + return kernel->set_audio_volume(percentage); + } + + int + set_sound_effect_volume(double percentage) + { + return kernel->set_sound_effect_volume(percentage); } } diff --git a/test/res/effects/boom.wav b/test/res/effects/boom.wav new file mode 100644 index 0000000..2f1d2ac Binary files /dev/null and b/test/res/effects/boom.wav differ