diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 8 | ||||
-rw-r--r-- | src/mixer.cpp | 16 | ||||
-rw-r--r-- | src/mixer.h | 67 | ||||
-rw-r--r-- | src/party.cpp | 5 | ||||
-rw-r--r-- | src/party.h | 4 | ||||
-rw-r--r-- | src/renderer.h | 2 |
6 files changed, 95 insertions, 7 deletions
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 @@ | |||
5 | #include "party.h" | 5 | #include "party.h" |
6 | #include "timer.h" | 6 | #include "timer.h" |
7 | #include "map.h" | 7 | #include "map.h" |
8 | #include "mixer.h" | ||
8 | 9 | ||
9 | void loop(Renderer& renderer) { | 10 | void loop(Renderer& renderer, Mixer& mixer) { |
10 | Game game; | 11 | Game game; |
11 | Input keystate; | 12 | Input keystate; |
12 | 13 | ||
@@ -61,7 +62,7 @@ void loop(Renderer& renderer) { | |||
61 | 62 | ||
62 | inputTimer.accumulate(frameTime); | 63 | inputTimer.accumulate(frameTime); |
63 | while (inputTimer.step()) { | 64 | while (inputTimer.step()) { |
64 | party.move(game, keystate); | 65 | party.move(game, mixer, keystate); |
65 | } | 66 | } |
66 | 67 | ||
67 | animTimer.accumulate(frameTime); | 68 | animTimer.accumulate(frameTime); |
@@ -79,8 +80,9 @@ int main(int, char**) { | |||
79 | try | 80 | try |
80 | { | 81 | { |
81 | Renderer renderer; | 82 | Renderer renderer; |
83 | Mixer mixer; | ||
82 | 84 | ||
83 | loop(renderer); | 85 | loop(renderer, mixer); |
84 | } catch (const sdl_error& ex) | 86 | } catch (const sdl_error& ex) |
85 | { | 87 | { |
86 | std::cout << "SDL error (" << ex.what() << ")" << std::endl; | 88 | 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 @@ | |||
1 | #include "mixer.h" | ||
2 | |||
3 | void Mixer::playSound(std::string filename) { | ||
4 | if (!sounds_.count(filename)) { | ||
5 | Mix_Chunk* sample = Mix_LoadWAV(filename.c_str()); | ||
6 | if (!sample) { | ||
7 | throw mix_error(); | ||
8 | } | ||
9 | |||
10 | sounds_[filename] = chunk_ptr(sample); | ||
11 | } | ||
12 | |||
13 | if (Mix_PlayChannel(-1, sounds_[filename].get(), 0) == -1) { | ||
14 | throw mix_error(); | ||
15 | } | ||
16 | } | ||
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 @@ | |||
1 | #ifndef MIXER_H_6DF82000 | ||
2 | #define MIXER_H_6DF82000 | ||
3 | |||
4 | #include <SDL_mixer.h> | ||
5 | #include <memory> | ||
6 | #include <stdexcept> | ||
7 | #include <map> | ||
8 | #include <string> | ||
9 | |||
10 | class mix_error : public std::logic_error { | ||
11 | public: | ||
12 | |||
13 | mix_error() : std::logic_error(Mix_GetError()) | ||
14 | { | ||
15 | } | ||
16 | }; | ||
17 | |||
18 | class mix_wrapper { | ||
19 | public: | ||
20 | |||
21 | mix_wrapper() | ||
22 | { | ||
23 | if (Mix_Init(0) != 0) { | ||
24 | mix_error ex; | ||
25 | Mix_Quit(); | ||
26 | |||
27 | throw ex; | ||
28 | } | ||
29 | |||
30 | if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024) != 0) { | ||
31 | mix_error ex; | ||
32 | Mix_Quit(); | ||
33 | |||
34 | throw ex; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | ~mix_wrapper() | ||
39 | { | ||
40 | Mix_CloseAudio(); | ||
41 | Mix_Quit(); | ||
42 | } | ||
43 | }; | ||
44 | |||
45 | class chunk_deleter { | ||
46 | public: | ||
47 | |||
48 | void operator()(Mix_Chunk* chunk) { | ||
49 | Mix_FreeChunk(chunk); | ||
50 | } | ||
51 | }; | ||
52 | |||
53 | using chunk_ptr = std::unique_ptr<Mix_Chunk, chunk_deleter>; | ||
54 | |||
55 | // MUST create the Renderer first! | ||
56 | class Mixer { | ||
57 | public: | ||
58 | |||
59 | void playSound(std::string filename); | ||
60 | |||
61 | private: | ||
62 | |||
63 | mix_wrapper mix_; | ||
64 | std::map<std::string, chunk_ptr> sounds_; | ||
65 | }; | ||
66 | |||
67 | #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 @@ | |||
1 | #include "party.h" | 1 | #include "party.h" |
2 | #include "consts.h" | 2 | #include "consts.h" |
3 | #include <iostream> | 3 | #include "mixer.h" |
4 | 4 | ||
5 | void Party::addMember(Game& game, int spriteId) { | 5 | void Party::addMember(Game& game, int spriteId) { |
6 | int index = members_.size(); | 6 | int index = members_.size(); |
@@ -19,7 +19,7 @@ void Party::addMember(Game& game, int spriteId) { | |||
19 | game.setSpriteState(spriteId, "still"); | 19 | game.setSpriteState(spriteId, "still"); |
20 | } | 20 | } |
21 | 21 | ||
22 | void Party::move(Game& game, const Input& keystate) { | 22 | void Party::move(Game& game, Mixer& mixer, const Input& keystate) { |
23 | if (members_.empty()) { | 23 | if (members_.empty()) { |
24 | return; | 24 | return; |
25 | } | 25 | } |
@@ -151,6 +151,7 @@ void Party::move(Game& game, const Input& keystate) { | |||
151 | 151 | ||
152 | if (blocked && state_ == State::Running) { | 152 | if (blocked && state_ == State::Running) { |
153 | stopRunning(game); | 153 | stopRunning(game); |
154 | mixer.playSound("../res/bump.wav"); | ||
154 | } | 155 | } |
155 | 156 | ||
156 | // Move everything | 157 | // 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 @@ | |||
5 | #include <vector> | 5 | #include <vector> |
6 | #include "game.h" | 6 | #include "game.h" |
7 | 7 | ||
8 | class Mixer; | ||
9 | |||
8 | class Party { | 10 | class Party { |
9 | public: | 11 | public: |
10 | 12 | ||
11 | void addMember(Game& game, int spriteId); | 13 | void addMember(Game& game, int spriteId); |
12 | 14 | ||
13 | void move(Game& game, const Input& keystate); | 15 | void move(Game& game, Mixer& mixer, const Input& keystate); |
14 | 16 | ||
15 | void beginCrouch(Game& game); | 17 | void beginCrouch(Game& game); |
16 | 18 | ||
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: | |||
32 | 32 | ||
33 | sdl_wrapper() | 33 | sdl_wrapper() |
34 | { | 34 | { |
35 | if (SDL_Init(SDL_INIT_VIDEO) != 0) | 35 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) |
36 | { | 36 | { |
37 | sdl_error ex; | 37 | sdl_error ex; |
38 | SDL_Quit(); | 38 | SDL_Quit(); |