-
Notifications
You must be signed in to change notification settings - Fork 21
/
bg_manager.cpp
58 lines (47 loc) · 1.09 KB
/
bg_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "game.hpp"
#include <cstdlib>
using namespace std;
namespace Icy
{
void BGManager::init(const vector<Track>& tracks)
{
this->tracks = tracks;
srand(time(nullptr));
first = true;
}
void BGManager::step(Audio::Mixer& mixer)
{
lock_guard<Audio::Mixer> guard(mixer);
if (current && current->valid())
return;
if (!tracks.size())
return;
if (!loader.size())
{
if (first)
{
loader.request_vorbis(tracks[0].path);
last = 0;
}
else
{
unsigned index = rand() % tracks.size();
if (index == last)
index = (index + 1) % tracks.size();
loader.request_vorbis(tracks[index].path);
last = index;
}
first = false;
}
auto ret = loader.flush();
if (ret)
{
current = make_shared<Audio::PCMStream>(ret);
current->volume(tracks[last].gain);
}
else
current.reset();
if (current)
mixer.add_stream(current);
}
}