-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
108 lines (91 loc) · 3.12 KB
/
Player.cpp
File metadata and controls
108 lines (91 loc) · 3.12 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "Player.h"
//Checking if any of the values changed and call the functions accordingly
void Player::update() {
if (lData->BossEvent != gameData->BossEvent) {
updateBossEventChange();
lData->BossEvent = gameData->BossEvent;
}
if (lData->Music != gameData->Music) {
updateMusicChange();
lData->Music = gameData->Music;
}
if (lData->OptionsOn != gameData->OptionsOn) {
updateOptionsOnChange();
lData->OptionsOn = gameData->OptionsOn;
}
if (lData->OptionsOff != gameData->OptionsOff) {
updateOptionsOffChange();
lData->OptionsOff = gameData->OptionsOff;
}
if (lData->XAxis != gameData->XAxis) {
updateXAxis();
lData->XAxis = gameData->XAxis;
}
if (lData->YAxis != gameData->YAxis) {
updateYAxis();
lData->YAxis = gameData->YAxis;
}
if (lData->InLevel != gameData->InLevel) {
updateInLevelChange();
lData->InLevel = gameData->InLevel;
}
}
//When the Music is switched off from the Options let the Music Player set the state of Stopped, so the
//stream can be restarted if Player chooses to activate music again
void Player::updateMusicChange() {
if (!gameData->Music && !gameData->BossEvent)
Stop();
}
void Player::updateOptionsOffChange() {
//Making sure that the stream has only been paused instead of stopped to resume
if (gameData->OptionsOff && musPlayer.getStatus() == musPlayer.Paused)
Resume();
//If the stream has been stopped and Music were to be reactivated this will turn it back on
else if (gameData->InLevel && gameData->OptionsOff && gameData->Music && musPlayer.getStatus() == musPlayer.Stopped)
Play();
}
//Immediatly pause the stream. In case of a stopped stream nothing will happen!
void Player::updateOptionsOnChange() {
if (gameData->OptionsOn)
Pause();
}
void Player::updateInLevelChange() {
//Is Rayman currently Playable and the following conditions meet?
if (gameData->InLevel && gameData->Music && !gameData->OptionsOn)
Play();
//Else the following could have happened: Rayman died and became unplayable for a brief,
//a cutscene is playing, Rayman is back on overworld. Fade out the music and stop the stream here to be restarted later!
else
Fade();
}
void Player::Play()
{
try {
//Before start playing a new track, I need to make sure there is a track to play
if (!GetSoundtrack())
return;
Stop();
//This will fail in case the soundtrackData is a nullptr! Making sure above line catches before I come here
if (soundtrackData->FileName == "")
return;
//Getting the subtrack from the container as a sub filestream
seeker.setData(soundtrackData->FileName.c_str(), soundtrackData->Offset, soundtrackData->Length);
//Loading the stream into SMFL's music player
if (!musPlayer.openFromStream(seeker)) {
return;
}
//Inject loop attributes from SoundtrackList
if (soundtrackData->Loop) {
span.offset = microseconds(soundtrackData->LoopOffset);
span.length = microseconds(soundtrackData->LoopLength);
musPlayer.setLoopPoints(span);
}
//Start playing it if everything checked out above
musPlayer.setLoop(true);
musPlayer.play();
}
catch (...) {
//Just in case, so the program doesn just crash. Needs proper error handling!
return;
}
}