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
use crate::internal::*;
use crate::texture::*;
use crate::*;
use std::marker::PhantomData;
pub struct SpriteBatch<'a> {
raw: *mut BLZ_SpriteBatch,
_marker: PhantomData<&'a ()>,
options: SpriteBatchOpts,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpriteBatchOpts {
pub max_buckets: u32,
pub max_sprites_per_bucket: u32,
pub flags: InitFlags,
}
bitflags! {
#[cfg_attr(tarpaulin, skip)]
pub struct InitFlags: u32
{
const Default = BLZ_InitFlags_DEFAULT;
const NoBuffering = BLZ_InitFlags_NO_BUFFERING;
}
}
impl<'a> Drop for SpriteBatch<'a> {
fn drop(&mut self) {
unsafe {
BLZ_FreeBatch(self.raw);
}
}
}
impl<'s> SpriteBatch<'s> {
pub fn new(options: SpriteBatchOpts) -> Result<SpriteBatch<'s>, String> {
unsafe {
let ptr = BLZ_CreateBatch(
options.max_buckets as i32,
options.max_sprites_per_bucket as i32,
options.flags.bits(),
);
if ptr.is_null() {
return Err(try_get_err());
} else {
return Ok(SpriteBatch { raw: ptr, _marker: PhantomData, options: options });
}
}
}
pub fn draw<'t: 's>(
&self,
texture: &'t Texture,
position: Vector2,
srcRectangle: Option<Rectangle>,
rotationInRadians: f32,
origin: Option<Vector2>,
scale: Option<Vector2>,
color: Color,
flip: SpriteFlip,
) -> CallResult {
unsafe {
wrap_result(BLZ_Draw(
self.raw,
texture.raw,
position.into(),
srcRectangle.map(|r| r.into()).as_raw(),
rotationInRadians,
origin.map(|v| v.into()).as_raw(),
scale.map(|v| v.into()).as_raw(),
color.into(),
flip as u32,
))
}
}
pub fn lower_draw<'t: 's>(&self, texture: &'t Texture, quad: &SpriteQuad) -> CallResult {
let q: BLZ_SpriteQuad = quad.into();
unsafe { wrap_result(BLZ_LowerDraw(self.raw, texture.id, &q)) }
}
pub fn present(&self) -> CallResult {
unsafe { wrap_result(BLZ_Present(self.raw)) }
}
pub fn get_options(&self) -> &SpriteBatchOpts {
&self.options
}
}