diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/components.cpp | 3 | ||||
-rw-r--r-- | src/game.cpp | 2 | ||||
-rw-r--r-- | src/main.cpp | 3 | ||||
-rw-r--r-- | src/muxer.cpp | 111 | ||||
-rw-r--r-- | src/muxer.h | 8 |
5 files changed, 126 insertions, 1 deletions
diff --git a/src/components.cpp b/src/components.cpp index e2eb2ed..941d02e 100644 --- a/src/components.cpp +++ b/src/components.cpp | |||
@@ -1,5 +1,6 @@ | |||
1 | #include "components.h" | 1 | #include "components.h" |
2 | #include "game.h" | 2 | #include "game.h" |
3 | #include "muxer.h" | ||
3 | 4 | ||
4 | // User movement component | 5 | // User movement component |
5 | 6 | ||
@@ -215,6 +216,8 @@ void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) | |||
215 | velocity.second = 0.0; | 216 | velocity.second = 0.0; |
216 | } else if (msg.type == Message::Type::jump) | 217 | } else if (msg.type == Message::Type::jump) |
217 | { | 218 | { |
219 | playSound("../res/Randomize87.wav"); | ||
220 | |||
218 | velocity.second = jump_velocity; | 221 | velocity.second = jump_velocity; |
219 | accel.second = jump_gravity; | 222 | accel.second = jump_gravity; |
220 | } else if (msg.type == Message::Type::stopJump) | 223 | } else if (msg.type == Message::Type::stopJump) |
diff --git a/src/game.cpp b/src/game.cpp index 043c82b..50c01f5 100644 --- a/src/game.cpp +++ b/src/game.cpp | |||
@@ -160,5 +160,5 @@ void Game::loadGame(const Map& curMap) | |||
160 | 160 | ||
161 | void Game::schedule(int frames, std::function<void ()>&& callback) | 161 | void Game::schedule(int frames, std::function<void ()>&& callback) |
162 | { | 162 | { |
163 | scheduled.emplace(begin(scheduled), frames, callback); | 163 | scheduled.emplace_front(frames, callback); |
164 | } | 164 | } |
diff --git a/src/main.cpp b/src/main.cpp index e37c2dd..96eb7da 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -3,12 +3,14 @@ | |||
3 | #include "renderer.h" | 3 | #include "renderer.h" |
4 | #include <cstdlib> | 4 | #include <cstdlib> |
5 | #include "game.h" | 5 | #include "game.h" |
6 | #include "muxer.h" | ||
6 | 7 | ||
7 | int main() | 8 | int main() |
8 | { | 9 | { |
9 | srand(time(NULL)); | 10 | srand(time(NULL)); |
10 | 11 | ||
11 | GLFWwindow* window = initRenderer(); | 12 | GLFWwindow* window = initRenderer(); |
13 | initMuxer(); | ||
12 | 14 | ||
13 | // Put this in a block so game goes out of scope before we destroy the renderer | 15 | // Put this in a block so game goes out of scope before we destroy the renderer |
14 | { | 16 | { |
@@ -16,6 +18,7 @@ int main() | |||
16 | game.execute(window); | 18 | game.execute(window); |
17 | } | 19 | } |
18 | 20 | ||
21 | destroyMuxer(); | ||
19 | destroyRenderer(); | 22 | destroyRenderer(); |
20 | 23 | ||
21 | return 0; | 24 | return 0; |
diff --git a/src/muxer.cpp b/src/muxer.cpp new file mode 100644 index 0000000..0844faa --- /dev/null +++ b/src/muxer.cpp | |||
@@ -0,0 +1,111 @@ | |||
1 | #include "muxer.h" | ||
2 | #include <cstdlib> | ||
3 | #include <sndfile.h> | ||
4 | #include <portaudio.h> | ||
5 | #include <list> | ||
6 | |||
7 | #define SAMPLE_RATE (44100) | ||
8 | |||
9 | class Sound { | ||
10 | public: | ||
11 | Sound(const char* filename); | ||
12 | ~Sound(); | ||
13 | |||
14 | float* ptr; | ||
15 | unsigned long pos; | ||
16 | unsigned long len; | ||
17 | }; | ||
18 | |||
19 | struct Muxer { | ||
20 | std::list<Sound> playing; | ||
21 | PaStream* stream; | ||
22 | }; | ||
23 | |||
24 | inline void dealWithPaError(PaError err) | ||
25 | { | ||
26 | if (err != paNoError) | ||
27 | { | ||
28 | printf("PortAudio error: %s\n", Pa_GetErrorText(err)); | ||
29 | exit(-1); | ||
30 | } | ||
31 | } | ||
32 | |||
33 | int paMuxerCallback(const void*, void* outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void* userData) | ||
34 | { | ||
35 | Muxer* muxer = (Muxer*) userData; | ||
36 | float* out = (float*) outputBuffer; | ||
37 | |||
38 | for (unsigned long i = 0; i<framesPerBuffer; i++) | ||
39 | { | ||
40 | unsigned long curAmount = 0; | ||
41 | *out = 0; | ||
42 | |||
43 | for (auto& sound : muxer->playing) | ||
44 | { | ||
45 | if (sound.pos < sound.len) | ||
46 | { | ||
47 | *out *= curAmount++; | ||
48 | *out += sound.ptr[sound.pos++]; | ||
49 | *out /= (float) curAmount; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | out++; | ||
54 | } | ||
55 | |||
56 | return 0; | ||
57 | } | ||
58 | |||
59 | static Muxer* muxer; | ||
60 | |||
61 | void initMuxer() | ||
62 | { | ||
63 | muxer = new Muxer(); | ||
64 | |||
65 | dealWithPaError(Pa_Initialize()); | ||
66 | dealWithPaError(Pa_OpenDefaultStream(&(muxer->stream), 0, 1, paFloat32, SAMPLE_RATE, paFramesPerBufferUnspecified, paMuxerCallback, muxer)); | ||
67 | dealWithPaError(Pa_StartStream(muxer->stream)); | ||
68 | } | ||
69 | |||
70 | void destroyMuxer() | ||
71 | { | ||
72 | dealWithPaError(Pa_AbortStream(muxer->stream)); | ||
73 | dealWithPaError(Pa_CloseStream(muxer->stream)); | ||
74 | dealWithPaError(Pa_Terminate()); | ||
75 | |||
76 | delete muxer; | ||
77 | muxer = 0; | ||
78 | } | ||
79 | |||
80 | void playSound(const char* filename) | ||
81 | { | ||
82 | // First, clear out any sounds that have finished playing | ||
83 | muxer->playing.remove_if([] (Sound& value) { return value.pos >= value.len; }); | ||
84 | |||
85 | // Then, add the new sound | ||
86 | muxer->playing.emplace_back(filename); | ||
87 | } | ||
88 | |||
89 | Sound::Sound(const char* filename) | ||
90 | { | ||
91 | SF_INFO info; | ||
92 | SNDFILE* file = sf_open(filename, SFM_READ, &info); | ||
93 | if (file == nullptr) | ||
94 | { | ||
95 | printf("LibSndFile error: %s\n", sf_strerror(file)); | ||
96 | exit(-1); | ||
97 | } | ||
98 | |||
99 | ptr = (float*) malloc(info.frames * info.channels * sizeof(float)); | ||
100 | len = info.frames * info.channels; | ||
101 | pos = 0; | ||
102 | |||
103 | sf_readf_float(file, ptr, info.frames); | ||
104 | |||
105 | sf_close(file); | ||
106 | } | ||
107 | |||
108 | Sound::~Sound() | ||
109 | { | ||
110 | free(ptr); | ||
111 | } | ||
diff --git a/src/muxer.h b/src/muxer.h new file mode 100644 index 0000000..b0f5378 --- /dev/null +++ b/src/muxer.h | |||
@@ -0,0 +1,8 @@ | |||
1 | #ifndef MUXER_H | ||
2 | #define MUXER_H | ||
3 | |||
4 | void initMuxer(); | ||
5 | void destroyMuxer(); | ||
6 | void playSound(const char* filename); | ||
7 | |||
8 | #endif | ||