From c772a3e5d8d9507b898813cdfb597c14b07cdc61 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 2 Feb 2021 20:18:41 -0500 Subject: Added "bumping into something while running" sfx --- src/main.cpp | 8 ++++--- src/mixer.cpp | 16 ++++++++++++++ src/mixer.h | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/party.cpp | 5 +++-- src/party.h | 4 +++- src/renderer.h | 2 +- 6 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 src/mixer.cpp create mode 100644 src/mixer.h (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 5e5953b..1d1583f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,8 +5,9 @@ #include "party.h" #include "timer.h" #include "map.h" +#include "mixer.h" -void loop(Renderer& renderer) { +void loop(Renderer& renderer, Mixer& mixer) { Game game; Input keystate; @@ -61,7 +62,7 @@ void loop(Renderer& renderer) { inputTimer.accumulate(frameTime); while (inputTimer.step()) { - party.move(game, keystate); + party.move(game, mixer, keystate); } animTimer.accumulate(frameTime); @@ -79,8 +80,9 @@ int main(int, char**) { try { Renderer renderer; + Mixer mixer; - loop(renderer); + loop(renderer, mixer); } catch (const sdl_error& ex) { std::cout << "SDL error (" << ex.what() << ")" << std::endl; diff --git a/src/mixer.cpp b/src/mixer.cpp new file mode 100644 index 0000000..bfbedcd --- /dev/null +++ b/src/mixer.cpp @@ -0,0 +1,16 @@ +#include "mixer.h" + +void Mixer::playSound(std::string filename) { + if (!sounds_.count(filename)) { + Mix_Chunk* sample = Mix_LoadWAV(filename.c_str()); + if (!sample) { + throw mix_error(); + } + + sounds_[filename] = chunk_ptr(sample); + } + + if (Mix_PlayChannel(-1, sounds_[filename].get(), 0) == -1) { + throw mix_error(); + } +} diff --git a/src/mixer.h b/src/mixer.h new file mode 100644 index 0000000..010bb09 --- /dev/null +++ b/src/mixer.h @@ -0,0 +1,67 @@ +#ifndef MIXER_H_6DF82000 +#define MIXER_H_6DF82000 + +#include +#include +#include +#include +#include + +class mix_error : public std::logic_error { +public: + + mix_error() : std::logic_error(Mix_GetError()) + { + } +}; + +class mix_wrapper { +public: + + mix_wrapper() + { + if (Mix_Init(0) != 0) { + mix_error ex; + Mix_Quit(); + + throw ex; + } + + if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024) != 0) { + mix_error ex; + Mix_Quit(); + + throw ex; + } + } + + ~mix_wrapper() + { + Mix_CloseAudio(); + Mix_Quit(); + } +}; + +class chunk_deleter { +public: + + void operator()(Mix_Chunk* chunk) { + Mix_FreeChunk(chunk); + } +}; + +using chunk_ptr = std::unique_ptr; + +// MUST create the Renderer first! +class Mixer { +public: + + void playSound(std::string filename); + +private: + + mix_wrapper mix_; + std::map sounds_; +}; + +#endif /* end of include guard: MIXER_H_6DF82000 */ diff --git a/src/party.cpp b/src/party.cpp index 307bca8..b7284c7 100644 --- a/src/party.cpp +++ b/src/party.cpp @@ -1,6 +1,6 @@ #include "party.h" #include "consts.h" -#include +#include "mixer.h" void Party::addMember(Game& game, int spriteId) { int index = members_.size(); @@ -19,7 +19,7 @@ void Party::addMember(Game& game, int spriteId) { game.setSpriteState(spriteId, "still"); } -void Party::move(Game& game, const Input& keystate) { +void Party::move(Game& game, Mixer& mixer, const Input& keystate) { if (members_.empty()) { return; } @@ -151,6 +151,7 @@ void Party::move(Game& game, const Input& keystate) { if (blocked && state_ == State::Running) { stopRunning(game); + mixer.playSound("../res/bump.wav"); } // Move everything diff --git a/src/party.h b/src/party.h index 2864073..59330c8 100644 --- a/src/party.h +++ b/src/party.h @@ -5,12 +5,14 @@ #include #include "game.h" +class Mixer; + class Party { public: void addMember(Game& game, int spriteId); - void move(Game& game, const Input& keystate); + void move(Game& game, Mixer& mixer, const Input& keystate); void beginCrouch(Game& game); diff --git a/src/renderer.h b/src/renderer.h index f952ab3..71c4ade 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -32,7 +32,7 @@ public: sdl_wrapper() { - if (SDL_Init(SDL_INIT_VIDEO) != 0) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) { sdl_error ex; SDL_Quit(); -- cgit 1.4.1