-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.rs
114 lines (94 loc) · 2.1 KB
/
util.rs
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
use ffi::*;
use Error;
/// A 3D vector.
#[derive(PartialEq, Copy, Clone, Debug)]
#[repr(C, packed)]
pub struct Vector {
/// The x.
pub x: f32,
/// The y.
pub y: f32,
/// The z.
pub z: f32,
}
/// A 3D vector representing position.
#[derive(PartialEq, Copy, Clone, Debug)]
#[repr(C, packed)]
pub struct Position(pub Vector);
/// A 3D vector representing direction.
#[derive(PartialEq, Copy, Clone, Debug)]
#[repr(C, packed)]
pub struct Direction(pub Vector);
/// A 3D vector representing velocity.
#[derive(PartialEq, Copy, Clone, Debug)]
#[repr(C, packed)]
pub struct Velocity(pub Vector);
/// Two 3D vectors representing orientation.
#[derive(PartialEq, Copy, Clone, Debug)]
#[repr(C, packed)]
pub struct Orientation(pub Vector, pub Vector);
/// The doppler.
#[derive(PartialEq, Copy, Clone, Debug)]
pub struct Doppler {
/// The factor.
pub factor: f32,
/// The velocity.
pub velocity: f32,
}
/// A trait for defining a conversion between a type and an OpenAL format.
pub trait Sample {
/// Returns the OpenAL format based on the number of channels.
fn format(channels: u16) -> Result<ALenum, Error>;
}
impl Sample for u8 {
fn format(channels: u16) -> Result<ALenum, Error> {
match channels {
1 => Ok(AL_FORMAT_MONO8),
2 => Ok(AL_FORMAT_STEREO8),
_ => Err(Error::InvalidValue),
}
}
}
impl Sample for i16 {
fn format(channels: u16) -> Result<ALenum, Error> {
match channels {
1 => Ok(AL_FORMAT_MONO16),
2 => Ok(AL_FORMAT_STEREO16),
_ => Err(Error::InvalidValue),
}
}
}
#[doc(hidden)]
macro_rules! al_panic {
() => (
if cfg!(debug_assertions) {
if let Some(error) = ::Error::last() {
panic!("{}", error);
}
}
);
($device:expr) => (
if cfg!(debug_assertions) {
if let Some(error) = ::Error::last_for($device) {
panic!("{}", error);
}
}
);
}
#[doc(hidden)]
macro_rules! al_try {
($body:expr) => ({
let result = { $body };
if let Some(error) = ::Error::last() {
return Err(error);
}
result
});
($device:expr, $body:expr) => ({
let result = { $body };
if let Some(error) = ::Error::last_for($device) {
return Err(error);
}
result
});
}