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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use crate::{
ffi::graphics as ffi,
graphics::{
Color, Drawable, FloatRect, IntRect, RcTexture, RenderStates, RenderTarget, Texture,
Transform, Transformable,
},
sf_box::SfBox,
system::Vector2f,
};
use std::{cell::RefCell, ptr::NonNull, rc::Weak};
const ERROR_MSG: &str = "Sprite does not hold a texture. Ignoring transformation!";
const RETURN_ERROR_MSG: &str = "Sprite does not hold a texture. Returning default value!";
const PANIC_ERROR_MSG: &str = "Sprite does not hold a texture! Return value cannot be discerned!";
/// Drawable representation of a texture
///
/// Sprite is a drawable type that allows to easily
/// display a [`RcTexture`] (or a part of it) on a render target.
///
/// __Note:__
/// This is an improvement upon the Sprite module which dissallows seperation from the texture
/// lifetime. The `RcSprite` allows from complete seperation from the `RcTexture` while still
/// referencing it. Underneath, it uses reference counting to ensure that the `RcTexture` is alive and
/// well, and will throw errors messages if you try to perform function on the sprite while the
/// `RcTexture` is no longer alive.
#[derive(Debug)]
pub struct RcSprite {
sprite: NonNull<ffi::sfSprite>,
texture: Weak<RefCell<SfBox<Texture>>>,
}
impl RcSprite {
/// Create a new sprite
#[must_use]
pub fn new() -> Self {
let sp = unsafe { ffi::sfSprite_create() };
Self {
sprite: NonNull::new(sp).expect("Failed to create Sprite"),
texture: Weak::new(),
}
}
fn texture_exists(&self) -> bool {
self.texture.strong_count() != 0
}
fn set_rc_texture(&mut self, texture: &RcTexture) {
self.texture = texture.downgrade()
}
/// Create a new sprite with a texture
#[must_use]
pub fn with_texture(texture: &RcTexture) -> RcSprite {
let mut sprite = Self::new();
sprite.set_texture(texture, true);
sprite
}
/// Create a new sprite with a texture and a source rectangle
#[must_use]
pub fn with_texture_and_rect(texture: &RcTexture, rect: IntRect) -> RcSprite {
let mut sprite = Self::with_texture(texture);
sprite.set_texture_rect(rect);
sprite
}
/// Change the source texture of a sprite
///
/// The texture argument refers to a texture that must
/// exist as long as the sprite uses it. Indeed, the sprite
/// doesn't store its own copy of the texture, but rather keeps
/// a pointer to the one that you passed to this function.
/// If the source texture is destroyed and the sprite tries to
/// use it, the behaviour is undefined.
/// If `reset_rect` is true, the [`texture_rect`] property of
/// the sprite is automatically adjusted to the size of the new
/// texture. If it is false, the texture rect is left unchanged.
///
/// [`texture_rect`]: Sprite::texture_rect
///
/// # Arguments
/// * `texture` - New texture
/// * `reset_rect` - Should the texture rect be reset to the size
/// of the new texture?
pub fn set_texture(&mut self, texture: &RcTexture, reset_rect: bool) {
self.set_rc_texture(texture);
let raw_texture = unsafe {
(*self.texture.upgrade().unwrap().as_ptr())
.0
.as_ptr()
.cast_const()
};
unsafe { ffi::sfSprite_setTexture(self.sprite.as_ptr(), raw_texture, reset_rect) };
}
/// Set the global color of a sprite
///
/// This color is modulated (multiplied) with the sprite's
/// texture. It can be used to colorize the sprite, or change
/// its global opacity.
/// By default, the sprite's color is opaque white.
///
/// # Arguments
/// * color - New color of the sprite
pub fn set_color(&mut self, color: Color) {
if !self.texture_exists() {
eprintln!("{ERROR_MSG}");
return;
}
unsafe { ffi::sfSprite_setColor(self.sprite.as_ptr(), color) }
}
/// Get the source texture of a sprite
///
/// If the sprite has no source texture, None is returned.
/// You can't
/// modify the texture when you retrieve it with this function.
///
/// Return an Option to the sprite's texture
#[must_use]
pub fn texture(&self) -> Option<&Texture> {
if self.texture_exists() {
Some(unsafe { &*(*(self.texture.as_ptr())).as_ptr() })
} else {
None
}
}
/// Get the global color of a sprite
///
/// Return the global color of the sprite
#[must_use]
pub fn color(&self) -> Color {
if !self.texture_exists() {
eprintln!("{RETURN_ERROR_MSG}");
return Default::default();
}
unsafe { ffi::sfSprite_getColor(self.sprite.as_ptr()) }
}
/// Get the local bounding rectangle of a sprite
///
/// The returned rectangle is in local coordinates, which means
/// that it ignores the transformations (translation, rotation,
/// scale, ...) that are applied to the entity.
/// In other words, this function returns the bounds of the
/// entity in the entity's coordinate system.
///
/// Return the local bounding rectangle of the entity
#[must_use]
pub fn local_bounds(&self) -> FloatRect {
if !self.texture_exists() {
eprintln!("{RETURN_ERROR_MSG}");
return Default::default();
}
unsafe { ffi::sfSprite_getLocalBounds(self.sprite.as_ptr()) }
}
/// Get the global bounding rectangle of a sprite
///
/// The returned rectangle is in global coordinates, which means
/// that it takes in account the transformations (translation,
/// rotation, scale, ...) that are applied to the entity.
/// In other words, this function returns the bounds of the
/// sprite in the global 2D world's coordinate system.
///
/// Return the global bounding rectangle of the entity
#[must_use]
pub fn global_bounds(&self) -> FloatRect {
if !self.texture_exists() {
eprintln!("{RETURN_ERROR_MSG}");
return Default::default();
}
unsafe { ffi::sfSprite_getGlobalBounds(self.sprite.as_ptr()) }
}
/// Get the sub-rectangle of the texture displayed by a sprite
///
/// Return the texture rectangle of the sprite
#[must_use]
pub fn texture_rect(&self) -> IntRect {
if !self.texture_exists() {
eprintln!("{RETURN_ERROR_MSG}");
return Default::default();
}
unsafe { ffi::sfSprite_getTextureRect(self.sprite.as_ptr()) }
}
/// Set the sub-rectangle of the texture that a sprite will display
///
/// The texture rect is useful when you don't want to display
/// the whole texture, but rather a part of it.
/// By default, the texture rect covers the entire texture.
///
/// # Arguments
/// * rectangle - Rectangle defining the region of the texture to display
pub fn set_texture_rect(&mut self, rect: IntRect) {
if !self.texture_exists() {
eprintln!("{ERROR_MSG}");
return;
}
unsafe { ffi::sfSprite_setTextureRect(self.sprite.as_ptr(), rect) }
}
pub(super) fn raw(&self) -> *const ffi::sfSprite {
self.sprite.as_ptr()
}
}
impl Default for RcSprite {
fn default() -> Self {
Self::new()
}
}
impl Clone for RcSprite {
/// Return a new Sprite or panic! if there is not enough memory
fn clone(&self) -> RcSprite {
let sp = unsafe { ffi::sfSprite_copy(self.sprite.as_ptr()) };
RcSprite {
sprite: NonNull::new(sp).expect("Failed to copy Sprite"),
texture: self.texture.clone(),
}
}
}
impl Drawable for RcSprite {
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
&'a self,
target: &mut dyn RenderTarget,
states: &RenderStates<'texture, 'shader, 'shader_texture>,
) {
if self.texture_exists() {
target.draw_rc_sprite(self, states)
}
}
}
impl Transformable for RcSprite {
fn set_position<P: Into<Vector2f>>(&mut self, position: P) {
if !self.texture_exists() {
eprintln!("{ERROR_MSG}");
return;
}
unsafe { ffi::sfSprite_setPosition(self.sprite.as_ptr(), position.into()) }
}
fn set_rotation(&mut self, angle: f32) {
if !self.texture_exists() {
eprintln!("{ERROR_MSG}");
return;
}
unsafe { ffi::sfSprite_setRotation(self.sprite.as_ptr(), angle) }
}
fn set_scale<S: Into<Vector2f>>(&mut self, scale: S) {
if !self.texture_exists() {
eprintln!("{ERROR_MSG}");
return;
}
unsafe { ffi::sfSprite_setScale(self.sprite.as_ptr(), scale.into()) }
}
fn set_origin<O: Into<Vector2f>>(&mut self, origin: O) {
if !self.texture_exists() {
eprintln!("{ERROR_MSG}");
return;
}
unsafe { ffi::sfSprite_setOrigin(self.sprite.as_ptr(), origin.into()) }
}
fn position(&self) -> Vector2f {
if !self.texture_exists() {
eprintln!("{RETURN_ERROR_MSG}");
return Default::default();
}
unsafe { ffi::sfSprite_getPosition(self.sprite.as_ptr()) }
}
fn rotation(&self) -> f32 {
if !self.texture_exists() {
eprintln!("{RETURN_ERROR_MSG}");
return Default::default();
}
unsafe { ffi::sfSprite_getRotation(self.sprite.as_ptr()) }
}
fn get_scale(&self) -> Vector2f {
if !self.texture_exists() {
eprintln!("{RETURN_ERROR_MSG}");
return Default::default();
}
unsafe { ffi::sfSprite_getScale(self.sprite.as_ptr()) }
}
fn origin(&self) -> Vector2f {
if !self.texture_exists() {
eprintln!("{RETURN_ERROR_MSG}");
return Default::default();
}
unsafe { ffi::sfSprite_getOrigin(self.sprite.as_ptr()) }
}
fn move_<O: Into<Vector2f>>(&mut self, offset: O) {
if !self.texture_exists() {
eprintln!("{ERROR_MSG}");
return;
}
unsafe { ffi::sfSprite_move(self.sprite.as_ptr(), offset.into()) }
}
fn rotate(&mut self, angle: f32) {
if !self.texture_exists() {
eprintln!("{ERROR_MSG}");
return;
}
unsafe { ffi::sfSprite_rotate(self.sprite.as_ptr(), angle) }
}
fn scale<F: Into<Vector2f>>(&mut self, factors: F) {
if !self.texture_exists() {
eprintln!("{ERROR_MSG}");
return;
}
unsafe { ffi::sfSprite_scale(self.sprite.as_ptr(), factors.into()) }
}
fn transform(&self) -> &Transform {
if !self.texture_exists() {
panic!("{}", PANIC_ERROR_MSG);
}
unsafe { &*ffi::sfSprite_getTransform(self.sprite.as_ptr()) }
}
fn inverse_transform(&self) -> &Transform {
if !self.texture_exists() {
panic!("{}", PANIC_ERROR_MSG);
}
unsafe { &*ffi::sfSprite_getInverseTransform(self.sprite.as_ptr()) }
}
}
impl Drop for RcSprite {
fn drop(&mut self) {
unsafe { ffi::sfSprite_destroy(self.sprite.as_ptr()) }
}
}