Starshatter_Open
Open source Starshatter engine
Main Page
Classes
Files
File List
File Members
All
Classes
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
MusicTrack.cpp
Go to the documentation of this file.
1
/* Project Starshatter 4.5
2
Destroyer Studios LLC
3
Copyright © 1997-2004. All Rights Reserved.
4
5
SUBSYSTEM: Stars.exe
6
FILE: MusicTrack.cpp
7
AUTHOR: John DiCamillo
8
9
10
OVERVIEW
11
========
12
Music Director class to manage selection, setup, and playback
13
of background music tracks for both menu and game modes
14
*/
15
16
17
#include "
MemDebug.h
"
18
#include "
MusicTrack.h
"
19
#include "
MusicDirector.h
"
20
#include "
Starshatter.h
"
21
#include "
AudioConfig.h
"
22
23
#include "
Game.h
"
24
#include "
Sound.h
"
25
26
// +-------------------------------------------------------------------+
27
28
const
double
FADE_TIME
= 1.5;
29
const
double
SILENCE
= -5000;
30
31
// +-------------------------------------------------------------------+
32
33
MusicTrack::MusicTrack
(
const
Text
& txt,
int
m,
int
n)
34
: name(txt), sound(0), state(NONE), mode(m), index(n),
35
fade(0), fade_time(
FADE_TIME
)
36
{
37
long
max_vol = 0;
38
39
if
(
mode
>=
MusicDirector::FLIGHT
)
40
max_vol =
AudioConfig::GameMusic
();
41
else
42
max_vol =
AudioConfig::MenuMusic
();
43
44
if
(max_vol <=
AudioConfig::Silence
())
45
return
;
46
47
name
.
setSensitive
(
false
);
48
49
if
(
name
.
contains
(
".ogg"
)) {
50
sound
=
Sound::CreateOggStream
(
name
);
51
52
if
(
name
.
contains
(
"-loop"
)) {
53
sound
->
SetFlags
(
Sound::STREAMED
|
54
Sound::OGGVORBIS
|
55
Sound::LOOP
|
56
Sound::LOCKED
);
57
}
58
59
else
{
60
sound
->
SetFlags
(
Sound::STREAMED
|
61
Sound::OGGVORBIS
|
62
Sound::LOCKED
);
63
}
64
65
sound
->
SetVolume
((
long
)
SILENCE
);
66
}
67
}
68
69
MusicTrack::~MusicTrack
()
70
{
71
if
(
sound
) {
72
sound
->
Stop
();
73
sound
->
Release
();
74
}
75
}
76
77
// +--------------------------------------------------------------------+
78
79
void
80
MusicTrack::ExecFrame
()
81
{
82
bool
music_pause =
false
;
83
84
Starshatter
* stars =
Starshatter::GetInstance
();
85
if
(stars) {
86
music_pause = (stars->
GetGameMode
() ==
Starshatter::PLAY_MODE
) &&
87
Game::Paused
();
88
}
89
90
if
(
sound
&& !music_pause) {
91
double
fvol = 1;
92
long
volume = 0;
93
94
switch
(
state
) {
95
case
PLAY
:
96
if
(
sound
->
IsReady
())
97
sound
->
Play
();
98
SetVolume
(volume);
99
break
;
100
101
case
FADE_IN
:
102
if
(
sound
->
IsReady
())
103
sound
->
Play
();
104
105
if
(
fade
> 0) {
106
fvol =
fade
/
fade_time
;
107
volume = (long) (fvol *
SILENCE
);
108
SetVolume
(volume);
109
}
110
111
if
(
fade
< 0.01)
112
state
=
PLAY
;
113
break
;
114
115
case
FADE_OUT
:
116
if
(
sound
->
IsReady
())
117
sound
->
Play
();
118
119
if
(
fade
> 0) {
120
fvol = 1 -
fade
/
fade_time
;
121
volume = (long) (fvol *
SILENCE
);
122
SetVolume
(volume);
123
}
124
125
if
(
fade
< 0.01)
126
state
=
STOP
;
127
break
;
128
129
case
STOP
:
130
if
(
sound
->
IsPlaying
()) {
131
sound
->
Stop
();
132
sound
->
Release
();
133
sound
= 0;
134
}
135
break
;
136
}
137
138
if
(
fade
> 0)
139
fade
-=
Game::GUITime
();
140
141
if
(
fade
< 0)
142
fade
= 0;
143
}
144
}
145
146
// +--------------------------------------------------------------------+
147
148
void
149
MusicTrack::Play
()
150
{
151
state
=
PLAY
;
152
fade
= 0;
153
}
154
155
void
156
MusicTrack::Stop
()
157
{
158
state
=
STOP
;
159
fade
= 0;
160
}
161
162
void
163
MusicTrack::FadeIn
()
164
{
165
if
(
state
!=
FADE_IN
&&
state
!=
PLAY
) {
166
state
=
FADE_IN
;
167
fade
=
fade_time
;
168
}
169
}
170
171
void
172
MusicTrack::FadeOut
()
173
{
174
if
(
state
!=
FADE_OUT
&&
state
!=
STOP
) {
175
state
=
FADE_OUT
;
176
fade
=
fade_time
;
177
}
178
}
179
180
// +--------------------------------------------------------------------+
181
182
int
183
MusicTrack::IsReady
()
const
184
{
185
if
(
sound
)
186
return
sound
->
IsReady
();
187
188
return
false
;
189
}
190
191
int
192
MusicTrack::IsPlaying
()
const
193
{
194
if
(
sound
)
195
return
sound
->
IsPlaying
();
196
197
return
false
;
198
}
199
200
int
201
MusicTrack::IsDone
()
const
202
{
203
if
(
sound
)
204
return
sound
->
IsDone
() ||
sound
->
LoopCount
() >= 5;
205
206
return
true
;
207
}
208
209
int
210
MusicTrack::IsLooped
()
const
211
{
212
if
(
sound
)
213
return
sound
->
IsDone
() ||
sound
->
LoopCount
() >= 4;
214
215
return
true
;
216
}
217
218
// +--------------------------------------------------------------------+
219
220
long
221
MusicTrack::GetVolume
()
const
222
{
223
if
(
sound
)
224
return
sound
->
GetVolume
();
225
226
return
0;
227
}
228
229
void
230
MusicTrack::SetVolume
(
long
v)
231
{
232
if
(
sound
) {
233
long
max_vol = 0;
234
235
if
(
mode
>=
MusicDirector::FLIGHT
)
236
max_vol =
AudioConfig::GameMusic
();
237
else
238
max_vol =
AudioConfig::MenuMusic
();
239
240
if
(v > max_vol)
241
v = max_vol;
242
243
sound
->
SetVolume
(v);
244
}
245
}
246
247
double
248
MusicTrack::GetTotalTime
()
const
249
{
250
if
(
sound
)
251
return
sound
->
GetTotalTime
();
252
253
return
0;
254
}
255
256
double
257
MusicTrack::GetTimeRemaining
()
const
258
{
259
if
(
sound
)
260
return
sound
->
GetTimeRemaining
();
261
262
return
0;
263
}
264
265
double
266
MusicTrack::GetTimeElapsed
()
const
267
{
268
if
(
sound
)
269
return
sound
->
GetTimeElapsed
();
270
271
return
0;
272
}
273
Stars45
MusicTrack.cpp
Generated on Thu May 31 2012 16:31:12 for Starshatter_Open by
1.8.1