Skip to content

Commit

Permalink
Merge pull request #39 from vitorbaraujo/master
Browse files Browse the repository at this point in the history
Implementation of sound effects and volume
  • Loading branch information
edsomjr authored Jul 6, 2016
2 parents e941d4d + 2cfe466 commit ad37352
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
6 changes: 6 additions & 0 deletions include/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MouseEvent> pending_mouse_events(unsigned now) = 0;
virtual list<SystemEvent> pending_system_events(unsigned now) = 0;
Expand Down
37 changes: 36 additions & 1 deletion kernel/sdl2/sdl2kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions kernel/sdl2/sdl2kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MouseEvent> pending_mouse_events(unsigned now);
list<SystemEvent> pending_system_events(unsigned now);
Expand Down
20 changes: 19 additions & 1 deletion src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Binary file added test/res/effects/boom.wav
Binary file not shown.

0 comments on commit ad37352

Please sign in to comment.