-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathskeleton_binary.rs
112 lines (105 loc) · 3.91 KB
/
skeleton_binary.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
use std::{
ffi::{CStr, CString},
path::Path,
sync::Arc,
};
use crate::{
c::{
c_uchar, spSkeletonBinary, spSkeletonBinary_create, spSkeletonBinary_dispose,
spSkeletonBinary_readSkeletonData, spSkeletonBinary_readSkeletonDataFile,
},
c_interface::SyncPtr,
error::SpineError,
skeleton_data::SkeletonData,
Atlas,
};
/// A loader for Spine binary files.
///
/// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#SkeletonBinary)
#[derive(Debug)]
pub struct SkeletonBinary {
c_skeleton_binary: SyncPtr<spSkeletonBinary>,
owns_memory: bool,
atlas: Option<Arc<Atlas>>,
}
impl SkeletonBinary {
/// Create a new Binary loader using the default atlas attachment loader.
///
/// ```
/// use std::sync::Arc;
/// use rusty_spine::{AnimationState, AnimationStateData, Atlas, SpineError, Skeleton, SkeletonBinary};
///
/// fn load_skeleton() -> Result<(Skeleton, AnimationState), SpineError> {
/// let atlas = Arc::new(Atlas::new_from_file("spineboy.atlas")?);
/// let skeleton_binary = SkeletonBinary::new(atlas);
/// let skeleton_data = Arc::new(skeleton_binary.read_skeleton_data_file("spineboy.skel")?);
/// let animation_state_data = AnimationStateData::new(skeleton_data.clone());
/// // ... prepare animation state (see AnimationStateData docs) ...
/// let skeleton = Skeleton::new(skeleton_data);
/// let animation_state = AnimationState::new(Arc::new(animation_state_data));
/// Ok((skeleton, animation_state))
/// }
/// ```
pub fn new(atlas: Arc<Atlas>) -> Self {
let c_skeleton_binary = unsafe { spSkeletonBinary_create(atlas.c_ptr()) };
Self {
c_skeleton_binary: SyncPtr(c_skeleton_binary),
owns_memory: true,
atlas: Some(atlas),
}
}
/// Read the Spine skeleton binary data in-memory. See [`SkeletonBinary::new`] for a full
/// example.
///
/// # Errors
///
/// Returns [`SpineError::ParsingFailed`] if parsing of the binary data failed.
pub fn read_skeleton_data(&self, data: &[u8]) -> Result<SkeletonData, SpineError> {
let c_skeleton_data = unsafe {
spSkeletonBinary_readSkeletonData(
self.c_skeleton_binary.0,
data.as_ptr() as *const c_uchar,
data.len() as i32,
)
};
if !c_skeleton_data.is_null() {
Ok(SkeletonData::new(c_skeleton_data, self.atlas.clone()))
} else {
let c_error = unsafe { CStr::from_ptr((*self.c_skeleton_binary.0).error) };
Err(SpineError::new_from_spine(c_error.to_str().unwrap()))
}
}
/// Read the Spine skeleton binary data from a file. See [`SkeletonBinary::new`] for a full
/// example.
///
/// # Errors
///
/// Returns [`SpineError::ParsingFailed`] if parsing of the binary data failed. Returns
/// [`SpineError::NulError`] if `path` contains an internal 0 byte.
pub fn read_skeleton_data_file<P: AsRef<Path>>(
&self,
path: P,
) -> Result<SkeletonData, SpineError> {
let c_path = CString::new(path.as_ref().to_str().unwrap())?;
let c_skeleton_data = unsafe {
spSkeletonBinary_readSkeletonDataFile(self.c_skeleton_binary.0, c_path.as_ptr())
};
if !c_skeleton_data.is_null() {
Ok(SkeletonData::new(c_skeleton_data, self.atlas.clone()))
} else {
let c_error = unsafe { CStr::from_ptr((*self.c_skeleton_binary.0).error) };
Err(SpineError::new_from_spine(c_error.to_str().unwrap()))
}
}
c_accessor_mut!(scale, set_scale, scale, f32);
c_ptr!(c_skeleton_binary, spSkeletonBinary);
}
impl Drop for SkeletonBinary {
fn drop(&mut self) {
if self.owns_memory {
unsafe {
spSkeletonBinary_dispose(self.c_skeleton_binary.0);
}
}
}
}