Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
wze::speaker Class Referencefinal
Inheritance diagram for wze::speaker:
wze::component

Public Member Functions

std::shared_ptr< wze::sound > const & sound () const
 
void set_sound (std::shared_ptr< wze::sound > const &sound)
 
int8_t volume () const
 
void set_volume (int8_t volume)
 
float range () const
 
void set_range (float range)
 
bool auto_panning () const
 
void set_auto_panning (bool auto_panning)
 
float x () const
 
void set_x (float x) final
 
float y () const
 
void set_y (float y) final
 
float z () const
 
void set_z (float z)
 
bool spatial () const
 
void set_spatial (bool spatial)
 
float x_offset () const final
 
void set_x_offset (float x_offset)
 
float y_offset () const final
 
void set_y_offset (float y_offset)
 
bool attach_x () const final
 
void set_attach_x (bool attach_x)
 
bool attach_y () const final
 
void set_attach_y (bool attach_y)
 
bool x_angle_lock () const final
 
void set_x_angle_lock (bool x_angle_lock)
 
bool y_angle_lock () const final
 
void set_y_angle_lock (bool y_angle_lock)
 
bool playing () const
 
bool paused () const
 
 speaker (std::shared_ptr< wze::sound > const &sound={}, int8_t volume=std::numeric_limits< int8_t >::max(), float range=std::numeric_limits< int16_t >::max(), bool auto_panning=false, float x=0, float y=0, float z=0, bool spatial=false, float x_offset=0, float y_offset=0, bool attach_x=true, bool attach_y=true, bool x_angle_lock=true, bool y_angle_lock=true)
 
 speaker (speaker const &other)
 
 speaker (speaker &&other) noexcept(false)
 
speakeroperator= (speaker const &other)
 
speakeroperator= (speaker &&other) noexcept(false)
 
void play (uint16_t fade_in=0, uint16_t loops=0)
 
void pause ()
 
void resume ()
 
void stop (uint16_t fade_out=0)
 
void align_panning ()
 
- Public Member Functions inherited from wze::component

Detailed Description

Definition at line 35 of file speaker.hpp.

Constructor & Destructor Documentation

◆ speaker() [1/3]

wze::speaker::speaker ( std::shared_ptr< wze::sound > const & sound = {},
int8_t volume = std::numeric_limits<int8_t>::max(),
float range = std::numeric_limits<int16_t>::max(),
bool auto_panning = false,
float x = 0,
float y = 0,
float z = 0,
bool spatial = false,
float x_offset = 0,
float y_offset = 0,
bool attach_x = true,
bool attach_y = true,
bool x_angle_lock = true,
bool y_angle_lock = true )
explicit

Definition at line 168 of file speaker.cpp.

172 {
173 _channel = audio::request_channel();
174 set_sound(sound);
175 set_volume(volume);
176 set_range(range);
177 set_auto_panning(auto_panning);
178 set_x(x);
179 set_y(y);
180 set_z(z);
181 set_spatial(spatial);
182 set_x_offset(x_offset);
183 set_y_offset(y_offset);
184 set_attach_x(attach_x);
185 set_attach_y(attach_y);
186 set_x_angle_lock(x_angle_lock);
187 set_y_angle_lock(y_angle_lock);
188 _instances.push_back(this);
189}

◆ speaker() [2/3]

wze::speaker::speaker ( speaker const & other)

Definition at line 191 of file speaker.cpp.

191 {
192 _channel = audio::request_channel();
193 *this = other;
194 _instances.push_back(this);
195}

◆ speaker() [3/3]

wze::speaker::speaker ( speaker && other)

Definition at line 197 of file speaker.cpp.

197 {
198 _channel = audio::request_channel();
199 *this = std::move(other);
200 _instances.push_back(this);
201}

◆ ~speaker()

wze::speaker::~speaker ( )
final

Definition at line 203 of file speaker.cpp.

203 {
204 _instances.erase(std::find(instances().begin(), instances().end(), this));
205 audio::drop_channel(_channel);
206}

Member Function Documentation

◆ sound()

std::shared_ptr< wze::sound > const & wze::speaker::sound ( ) const
nodiscard

Definition at line 47 of file speaker.cpp.

47 {
48 return _sound;
49}

◆ set_sound()

void wze::speaker::set_sound ( std::shared_ptr< wze::sound > const & sound)

Definition at line 51 of file speaker.cpp.

51 {
52 _sound = sound;
53}

◆ volume()

int8_t wze::speaker::volume ( ) const
nodiscard

Definition at line 55 of file speaker.cpp.

55 {
56 return (int8_t)Mix_Volume(_channel, -1);
57}

◆ set_volume()

void wze::speaker::set_volume ( int8_t volume)

Definition at line 60 of file speaker.cpp.

60 {
61 Mix_Volume(_channel, volume);
62}

◆ range()

float wze::speaker::range ( ) const
nodiscard

Definition at line 64 of file speaker.cpp.

64 {
65 return _range;
66}

◆ set_range()

void wze::speaker::set_range ( float range)

Definition at line 68 of file speaker.cpp.

68 {
69 _range = range;
70}

◆ auto_panning()

bool wze::speaker::auto_panning ( ) const
nodiscard

Definition at line 72 of file speaker.cpp.

72 {
73 return _auto_panning;
74}

◆ set_auto_panning()

void wze::speaker::set_auto_panning ( bool auto_panning)

Definition at line 76 of file speaker.cpp.

76 {
77 _auto_panning = auto_panning;
78}

◆ x()

float wze::speaker::x ( ) const
nodiscard

Definition at line 80 of file speaker.cpp.

80 {
81 return _x;
82}

◆ set_x()

void wze::speaker::set_x ( float x)
finalvirtual

Implements wze::component.

Definition at line 84 of file speaker.cpp.

84 {
85 _x = x;
86}

◆ y()

float wze::speaker::y ( ) const
nodiscard

Definition at line 88 of file speaker.cpp.

88 {
89 return _y;
90}

◆ set_y()

void wze::speaker::set_y ( float y)
finalvirtual

Implements wze::component.

Definition at line 92 of file speaker.cpp.

92 {
93 _y = y;
94}

◆ z()

float wze::speaker::z ( ) const
nodiscard

Definition at line 96 of file speaker.cpp.

96 {
97 return _z;
98}

◆ set_z()

void wze::speaker::set_z ( float z)

Definition at line 100 of file speaker.cpp.

100 {
101 _z = z;
102}

◆ spatial()

bool wze::speaker::spatial ( ) const
nodiscard

Definition at line 104 of file speaker.cpp.

104 {
105 return _spatial;
106}

◆ set_spatial()

void wze::speaker::set_spatial ( bool spatial)

Definition at line 108 of file speaker.cpp.

108 {
109 _spatial = spatial;
110}

◆ x_offset()

float wze::speaker::x_offset ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 112 of file speaker.cpp.

112 {
113 return _x_offset;
114}

◆ set_x_offset()

void wze::speaker::set_x_offset ( float x_offset)

Definition at line 116 of file speaker.cpp.

116 {
117 _x_offset = x_offset;
118}

◆ y_offset()

float wze::speaker::y_offset ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 120 of file speaker.cpp.

120 {
121 return _y_offset;
122}

◆ set_y_offset()

void wze::speaker::set_y_offset ( float y_offset)

Definition at line 124 of file speaker.cpp.

124 {
125 _y_offset = y_offset;
126}

◆ attach_x()

bool wze::speaker::attach_x ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 128 of file speaker.cpp.

128 {
129 return _attach_x;
130}

◆ set_attach_x()

void wze::speaker::set_attach_x ( bool attach_x)

Definition at line 132 of file speaker.cpp.

132 {
133 _attach_x = attach_x;
134}

◆ attach_y()

bool wze::speaker::attach_y ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 136 of file speaker.cpp.

136 {
137 return _attach_y;
138}

◆ set_attach_y()

void wze::speaker::set_attach_y ( bool attach_y)

Definition at line 140 of file speaker.cpp.

140 {
141 _attach_y = attach_y;
142}

◆ x_angle_lock()

bool wze::speaker::x_angle_lock ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 144 of file speaker.cpp.

144 {
145 return _x_angle_lock;
146}

◆ set_x_angle_lock()

void wze::speaker::set_x_angle_lock ( bool x_angle_lock)

Definition at line 148 of file speaker.cpp.

148 {
149 _x_angle_lock = x_angle_lock;
150}

◆ y_angle_lock()

bool wze::speaker::y_angle_lock ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 152 of file speaker.cpp.

152 {
153 return _y_angle_lock;
154}

◆ set_y_angle_lock()

void wze::speaker::set_y_angle_lock ( bool y_angle_lock)

Definition at line 156 of file speaker.cpp.

156 {
157 _y_angle_lock = y_angle_lock;
158}

◆ playing()

bool wze::speaker::playing ( ) const
nodiscard

Definition at line 160 of file speaker.cpp.

160 {
161 return (bool)Mix_Playing(_channel);
162}

◆ paused()

bool wze::speaker::paused ( ) const
nodiscard

Definition at line 164 of file speaker.cpp.

164 {
165 return (bool)Mix_Paused(_channel);
166}

◆ operator=() [1/2]

wze::speaker & wze::speaker::operator= ( speaker const & other)

Definition at line 208 of file speaker.cpp.

208 {
209 if (&other != this) {
210 set_sound(other.sound());
211 set_volume(other.volume());
212 set_range(other.range());
213 set_auto_panning(other.auto_panning());
214 set_x(other.x());
215 set_y(other.y());
216 set_z(other.z());
217 set_spatial(other.spatial());
218 set_x_offset(other.x_offset());
219 set_y_offset(other.y_offset());
220 set_attach_x(other.attach_x());
221 set_attach_y(other.attach_y());
222 set_x_angle_lock(other.x_angle_lock());
223 set_y_angle_lock(other.y_angle_lock());
224 }
225
226 return *this;
227}

◆ operator=() [2/2]

wze::speaker & wze::speaker::operator= ( speaker && other)

Definition at line 229 of file speaker.cpp.

229 {
230 if (&other != this) {
231 *this = other;
232 std::swap(_channel, other._channel);
233 }
234
235 return *this;
236}

◆ play()

void wze::speaker::play ( uint16_t fade_in = 0,
uint16_t loops = 0 )

Definition at line 239 of file speaker.cpp.

239 {
240 if (Mix_FadeInChannel(_channel, sound().get(), loops, fade_in) == -1) {
241 throw exception(Mix_GetError());
242 }
243}

◆ pause()

void wze::speaker::pause ( )

Definition at line 246 of file speaker.cpp.

246 {
247 Mix_Pause(_channel);
248}

◆ resume()

void wze::speaker::resume ( )

Definition at line 251 of file speaker.cpp.

251 {
252 Mix_Resume(_channel);
253}

◆ stop()

void wze::speaker::stop ( uint16_t fade_out = 0)

Definition at line 256 of file speaker.cpp.

256 {
257 Mix_FadeOutChannel(_channel, fade_out);
258}

◆ align_panning()

void wze::speaker::align_panning ( )

Definition at line 261 of file speaker.cpp.

261 {
262 constexpr uint16_t circle = 360;
263 constexpr uint16_t top = 270;
264 constexpr uint16_t bottom = 90;
265
266 float x_distance;
267 float y_distance;
268 float z_ratio;
269 float distance;
270 int32_t angle;
271 float left;
272 float right;
273
274 x_distance = x() - camera::x();
275 y_distance = y() - camera::y();
276 z_ratio = spatial() ? 1 - abs(z() - camera::z()) / range() : 1;
277 distance = math::length(x_distance, y_distance);
278 angle = (int32_t)roundf(math::to_degrees(
279 math::angle(x_distance, y_distance) - camera::angle())) %
280 circle;
281 if (angle < 0) {
282 angle += circle;
283 }
284
285 if (range() <= distance || z_ratio <= 0) {
286 left = 0;
287 right = 0;
288 } else if (bottom < angle && angle < top) {
289 left = 1 - distance / range();
290 right = powf(left, 2);
291 if (spatial()) {
292 left *= z_ratio;
293 right *= z_ratio;
294 }
295 } else if (top < angle || angle < bottom) {
296 right = 1 - distance / range();
297 left = powf(right, 2);
298 if (spatial()) {
299 right *= z_ratio;
300 left *= z_ratio;
301 }
302 } else {
303 left = z_ratio;
304 right = z_ratio;
305 }
306
307 if (!(bool)Mix_SetPanning(
308 _channel,
309 (uint8_t)roundf(std::numeric_limits<uint8_t>::max() * left),
310 (uint8_t)roundf(std::numeric_limits<uint8_t>::max() * right))) {
311 throw exception(Mix_GetError());
312 }
313}
static float angle(float x, float y)
Returns the angle of a vector.
Definition math.cpp:37
static constexpr float to_degrees(float radians)
Converts radians to degrees.
Definition math.hpp:75
static float length(float x, float y)
Returns the length of a vector.
Definition math.cpp:33

The documentation for this class was generated from the following files: