-
Notifications
You must be signed in to change notification settings - Fork 1
/
EventInstance.hpp
150 lines (121 loc) · 3.62 KB
/
EventInstance.hpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#pragma once
#include "EventHandler.hpp"
#include "FmodInclude.hpp"
namespace lite
{
class EventInstance
{
private: // data
EventDescription* description = nullptr;
fmod::EventInstance* instance = nullptr;
EventHandler onAudioShutdown = { "AudioShutdown", this, &EventInstance::OnAudioShutdown };
EventHandler onAudioUpdate = { "AudioUpdate", this, &EventInstance::OnAudioUpdate };
public: // data
EventDescription* Description() const { return description; }
// 3D attributes
FMOD_VECTOR Forward = FMOD_VECTOR{ 0, 0, 1 };
FMOD_VECTOR Position = FMOD_VECTOR{ 0, 0, 0 };
FMOD_VECTOR Up = FMOD_VECTOR{ 0, 1, 0 };
FMOD_VECTOR Velocity = FMOD_VECTOR{ 0, 0, 0 };
public: // methods
EventInstance()
{}
EventInstance(EventInstance&& b) :
description(b.description),
instance(b.instance)
{
b.description = nullptr;
b.instance = nullptr;
}
explicit EventInstance(fmod::EventInstance* instance_, EventDescription* description_) :
description(description_),
instance(instance_)
{
}
~EventInstance()
{
Release();
}
EventInstance& operator=(EventInstance&& b)
{
Release();
description = b.description;
instance = b.instance;
b.description = nullptr;
b.instance = nullptr;
return *this;
}
float GetParameter(const string& name) const
{
// Get the instance.
fmod::ParameterInstance* paramInstance = nullptr;
FmodCall(instance->getParameter(name.c_str(), ¶mInstance), false);
// Get the value.
float value = 0;
FmodCall(paramInstance->getValue(&value), 0);
return value;
}
// Returns whether the playback is playing, idle,
// sustaining, stopped, starting, or stopping.
FMOD_STUDIO_PLAYBACK_STATE GetPlaybackState() const
{
FMOD_STUDIO_PLAYBACK_STATE state = FMOD_STUDIO_PLAYBACK_STOPPED;
if (!IsValid()) return state;
FmodCall(instance->getPlaybackState(&state), state);
return state;
}
// Returns whether this handle is valid.
bool IsValid() const
{
return instance != nullptr;
}
// Releases the handle. The sound may continue
// playing if Stop isn't called!
void Release()
{
if (instance)
{
FmodCall(instance->release());
instance = nullptr;
}
}
bool SetParameter(const string& name, float value)
{
if (!IsValid()) return false;
// Get the instance.
fmod::ParameterInstance* paramInstance = nullptr;
FmodCall(instance->getParameter(name.c_str(), ¶mInstance), false);
// Set the value.
FmodCall(paramInstance->setValue(value), false);
return true;
}
// Starts playing the sound.
void Start()
{
if (!IsValid()) return;
FmodCall(instance->start());
}
// Stops playing the sound (with option to
// allow a gradual fadeout).
void Stop(bool allowFadeout = false)
{
if (!IsValid()) return;
FmodCall(instance->stop(allowFadeout ? FMOD_STUDIO_STOP_ALLOWFADEOUT : FMOD_STUDIO_STOP_IMMEDIATE));
}
private: // methods
// Releases EventInstance handles in case the user has
// any lying around. Good-guy game engine :)
void OnAudioShutdown(EventData&)
{
if (!IsValid()) return;
Release();
}
// Updates 3D attributes for this instance.
void OnAudioUpdate(EventData&)
{
if (!IsValid()) return;
FMOD_3D_ATTRIBUTES attributes = { Position, Velocity, Forward, Up };
FmodCall(instance->set3DAttributes(&attributes));
}
};
} // namespace lite