Skip to content

Commit

Permalink
Creating methods to control volume of music and sound effects
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorbaraujo committed Jun 28, 2016
1 parent 189e32d commit 2cfe466
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
5 changes: 3 additions & 2 deletions include/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ namespace ijengine {

void play_sound_effect(const string &path);

void set_audio_volume();
void set_sound_effect_volume();
int set_audio_volume(double percentage);
int set_sound_effect_volume(double percentage);

}

namespace event {
Expand Down
3 changes: 3 additions & 0 deletions include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ namespace ijengine {
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<MouseEvent> pending_mouse_events(unsigned now) = 0;
virtual list<SystemEvent> pending_system_events(unsigned now) = 0;
virtual list<KeyboardEvent> pending_keyboard_events(unsigned now) = 0;
Expand Down
24 changes: 23 additions & 1 deletion kernel/sdl2/sdl2kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ SDL2Kernel::stop_audio()
void
SDL2Kernel::play_sound_effect(const string& path)
{
//string sound_effect_path = audio_dir_path + "/effects/" + path;
Mix_Chunk *effect = Mix_LoadWAV(path.c_str());

if(not effect){
Expand All @@ -299,6 +298,29 @@ SDL2Kernel::play_sound_effect(const string& path)
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)
{
Expand Down
2 changes: 2 additions & 0 deletions kernel/sdl2/sdl2kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class SDL2Kernel : public Kernel {
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<MouseEvent> pending_mouse_events(unsigned now);
list<SystemEvent> pending_system_events(unsigned now);
Expand Down
14 changes: 7 additions & 7 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace ijengine
void
stop_audio()
{
kernel->stop_audio();
kernel->stop_audio();
}

void
Expand All @@ -114,16 +114,16 @@ namespace ijengine
kernel->play_sound_effect(path);
}

void
set_audio_volume(int volume)
int
set_audio_volume(double percentage)
{
kernel->set_audio_volume(volume);
return kernel->set_audio_volume(percentage);
}

void
set_sound_effect_volume(int volume)
int
set_sound_effect_volume(double percentage)
{
kernel->set_sound_effect_volume(volume);
return kernel->set_sound_effect_volume(percentage);
}
}

Expand Down

0 comments on commit 2cfe466

Please sign in to comment.