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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
//! Sprite implementations.

use core::ffi::c_int;
use core::ffi::c_void;
use core::ffi::c_float;
use core::marker::PhantomData;
use alloc::boxed::Box;

use sys::traits::AsRaw;

use sys::ffi::SpriteCollisionInfo;
use sys::ffi::LCDRect;
use sys::ffi::LCDSprite;
use sys::ffi::PDRect;

use gfx::bitmap::AnyBitmap;
use gfx::bitmap::BitmapRef;
use gfx::bitmap::BitmapDrawMode;
use gfx::bitmap::BitmapFlip;

use crate::AnySprite;
use crate::SpriteApi;
use crate::TypedSprite;
use crate::api;

pub use crate::ext::*;


pub type OwnedSprite<Userdata, Api> = Sprite<Userdata, Api, true>;
pub type SharedSprite<Userdata, Api> = Sprite<Userdata, Api, false>;


impl<UD, Api: api::Api, const FOD: bool> TypedSprite for Sprite<UD, Api, FOD> {
	type Userdata = UD;
	const FREE_ON_DROP: bool = FOD;
}


impl AnySprite for SpriteRef {}
impl<UD, Api: api::Api, const FOD: bool> AnySprite for Sprite<UD, Api, FOD> {}


impl SpriteApi for SpriteRef {
	type Api = api::Default;

	fn api(&self) -> Self::Api
		where Self::Api: Copy {
		api::Default::default()
	}

	fn api_ref(&self) -> &Self::Api {
		static API: api::Default = api::Default;
		&API
	}
}

impl<UD, Api: api::Api, const FOD: bool> SpriteApi for Sprite<UD, Api, FOD> {
	type Api = Api;
	fn api(&self) -> Api
		where Self::Api: Copy {
		self.1
	}

	fn api_ref(&self) -> &Self::Api { &self.1 }
}


#[repr(transparent)]
#[derive(Copy, Clone, Debug)]
pub struct SpriteRef(*mut LCDSprite);

impl From<*mut LCDSprite> for SpriteRef {
	fn from(ptr: *mut LCDSprite) -> Self { Self(ptr) }
}

impl AsRaw for SpriteRef {
	type Type = LCDSprite;
	unsafe fn as_raw(&self) -> *mut LCDSprite { self.0 }
}

impl SpriteRef {
	pub fn into_sprite<UD>(self) -> Sprite<UD, <Self as SpriteApi>::Api, false> {
		Sprite(unsafe { self.as_raw() }, self.api(), PhantomData)
	}

	pub fn into_sprite_with<UD, Api: api::Api>(self, api: Api) -> Sprite<UD, Api, false> {
		Sprite(unsafe { self.as_raw() }, api, PhantomData)
	}
}


#[derive(Debug)]
pub struct Sprite<Userdata = (), Api: api::Api = api::Default, const FREE_ON_DROP: bool = true>(*mut LCDSprite, Api, PhantomData<Userdata>);

impl<UD, Api: api::Api, const FOD: bool> AsRaw for Sprite<UD, Api, FOD> {
	type Type = LCDSprite;
	unsafe fn as_raw(&self) -> *mut LCDSprite { self.0 }
}

impl<UD, Api: api::Api, const FOD: bool> AsRef<Self> for Sprite<UD, Api, FOD> {
	fn as_ref(&self) -> &Self { self }
}
impl<UD, Api: api::Api, const FOD: bool> AsMut<Self> for Sprite<UD, Api, FOD> {
	fn as_mut(&mut self) -> &mut Self { self }
}

impl<UD, Api, const FOD: bool> From<SpriteRef> for Sprite<UD, Api, FOD> where Api: api::Api + Default {
	fn from(sprite: SpriteRef) -> Self { Self(unsafe { sprite.as_raw() }, Api::default(), PhantomData) }
}

impl<UD, Api: api::Api + Clone, const FOD: bool> Clone for Sprite<UD, Api, FOD> {
	fn clone(&self) -> Self {
		let f = self.1.copy();
		let ptr = unsafe { f(self.0) };
		Self(ptr, self.1.clone(), PhantomData)
	}
}

impl<UD, Api: api::Api, const FOD: bool> Drop for Sprite<UD, Api, FOD> {
	fn drop(&mut self) {
		if FOD && !self.0.is_null() {
			if let Some(ud) = self.take_userdata() {
				drop(ud);
				let f = self.1.set_userdata();
				unsafe { f(self.0, core::ptr::null_mut()) }
			}

			let f = self.1.free_sprite();
			unsafe { f(self.0) }
			self.0 = core::ptr::null_mut();
		}
	}
}

impl<UD, Api: api::Api + Copy> Sprite<UD, Api, true> {
	/// Convert this sprite into the same sprite that will not be freed on drop.
	/// That means that only C-part of the sprite will be freed.
	///
	/// __Safety is guaranteed by the caller.__
	pub fn into_shared(mut self) -> Sprite<UD, Api, false> {
		let res = Sprite(self.0, self.1, self.2);
		self.0 = core::ptr::null_mut();
		res
	}
}


impl<UD, Api: Default + api::Api, const FOD: bool> Sprite<UD, Api, FOD> {
	/// Allocates and returns a new Sprite with [`default`](api::Default) api access-point.
	///
	/// To create a sprite with a custom api access-point, use [`new_with`](Sprite::new_with).
	///
	/// See also [`sys::ffi::playdate_sprite::newSprite`]
	#[doc(alias = "sys::ffi::playdate_sprite::newSprite")]
	pub fn new() -> Self {
		let api = Default::default();
		Self::new_with(api)
	}
}

impl<UD, Api: api::Api, const FOD: bool> Sprite<UD, Api, FOD> {
	/// Allocates and returns a new Sprite with given `api`.
	///
	/// See also [`sys::ffi::playdate_sprite::newSprite`]
	#[doc(alias = "sys::ffi::playdate_sprite::newSprite")]
	pub fn new_with(api: Api) -> Self {
		let f = api.new_sprite();
		let ptr = unsafe { f() };
		Self(ptr, api, PhantomData)
	}
}


impl<Userdata, Api: api::Api, const FOD: bool> Sprite<Userdata, Api, FOD> {
	/// Adds the this sprite to the display list, so that it is drawn in the current scene.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::addSprite`]
	#[doc(alias = "sys::ffi::playdate_sprite::addSprite")]
	pub fn add(&self) {
		let f = self.1.add_sprite();
		unsafe { f(self.0) }
	}

	/// Removes the this sprite from the display list.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::removeSprite`]
	#[doc(alias = "sys::ffi::playdate_sprite::removeSprite")]
	pub fn remove(&self) {
		let f = self.1.remove_sprite();
		unsafe { f(self.0) }
	}


	/// Sets the bounds of the sprite with bounds.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setBounds`]
	#[doc(alias = "sys::ffi::playdate_sprite::setBounds")]
	pub fn set_bounds(&self, bounds: PDRect) {
		let f = self.1.set_bounds();
		unsafe { f(self.0, bounds) }
	}

	/// Returns the bounds of the sprite.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::getBounds`]
	#[doc(alias = "sys::ffi::playdate_sprite::getBounds")]
	pub fn bounds(&self) -> PDRect {
		let f = self.1.get_bounds();
		unsafe { f(self.0) }
	}


	/// Moves the sprite to `x`, `y` and resets its bounds based on the bitmap dimensions and center.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::moveTo`]
	#[doc(alias = "sys::ffi::playdate_sprite::moveTo")]
	pub fn move_to(&self, x: c_float, y: c_float) {
		let f = self.1.move_to();
		unsafe { f(self.0, x, y) }
	}

	/// Moves the sprite to by offsetting its current position by `dx`, `dy`.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::moveBy`]
	#[doc(alias = "sys::ffi::playdate_sprite::moveBy")]
	pub fn move_by(&self, dx: c_float, dy: c_float) {
		let f = self.1.move_by();
		unsafe { f(self.0, dx, dy) }
	}


	/// Sets the sprite's image to the given bitmap.
	///
	/// ⚠️ Caution: Using with draw function, call this method __before__ set callback.
	/// Setting image __after__ setting draw callback is mostly crashes with SIGBUS.
	///
	/// See also [`set_opaque`](Sprite::set_opaque).
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setImage`]
	#[doc(alias = "sys::ffi::playdate_sprite::setImage")]
	pub fn set_image(&self, image: impl AnyBitmap, flip: BitmapFlip) {
		let f = self.1.set_image();
		unsafe { f(self.0, image.as_raw(), flip) }
	}

	/// Returns the bitmap currently assigned to the given sprite.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::getImage`]
	#[doc(alias = "sys::ffi::playdate_sprite::getImage")]
	pub fn image<'t>(&'t self) -> Option<BitmapRef<'t>> {
		let f = self.1.get_image();
		let ptr = unsafe { f(self.0) };
		if ptr.is_null() {
			None
		} else {
			Some(BitmapRef::from(ptr))
		}
	}


	/// Sets the size.
	/// The size is used to set the sprite’s bounds when calling [`move_to`](Sprite::move_to).
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setSize`]
	#[doc(alias = "sys::ffi::playdate_sprite::setSize")]
	pub fn set_size(&self, width: c_float, height: c_float) {
		let f = self.1.set_size();
		unsafe { f(self.0, width, height) }
	}


	/// Sets the Z order of the sprite.
	/// Higher Z sprites are drawn on top of those with lower Z order.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setZIndex`]
	#[doc(alias = "sys::ffi::playdate_sprite::setZIndex")]
	pub fn set_z_index(&self, z_index: i16) {
		let f = self.1.set_z_index();
		unsafe { f(self.0, z_index) }
	}

	/// Returns the Z index of the sprite.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::getZIndex`]
	#[doc(alias = "sys::ffi::playdate_sprite::getZIndex")]
	pub fn z_index(&self) -> i16 {
		let f = self.1.get_z_index();
		unsafe { f(self.0) }
	}


	/// Sets the mode for drawing the sprite’s bitmap.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setDrawMode`]
	#[doc(alias = "sys::ffi::playdate_sprite::setDrawMode")]
	pub fn set_draw_mode(&self, mode: BitmapDrawMode) {
		let f = self.1.set_draw_mode();
		unsafe { f(self.0, mode) }
	}


	/// Flips the sprite's bitmap.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setImageFlip`]
	#[doc(alias = "sys::ffi::playdate_sprite::setImageFlip")]
	pub fn set_image_flip(&self, flip: BitmapFlip) {
		let f = self.1.set_image_flip();
		unsafe { f(self.0, flip) }
	}

	/// Returns the flip setting of the sprite’s bitmap.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::getImageFlip`]
	#[doc(alias = "sys::ffi::playdate_sprite::getImageFlip")]
	pub fn image_flip(&self) -> BitmapFlip {
		let f = self.1.get_image_flip();
		unsafe { f(self.0) }
	}


	/// Specifies a stencil image to be set on the frame buffer before the sprite is drawn.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setStencil`]
	#[doc(alias = "sys::ffi::playdate_sprite::setStencil")]
	pub fn set_stencil(&self, stencil: impl AnyBitmap) {
		let f = self.1.set_stencil();
		unsafe { f(self.0, stencil.as_raw()) }
	}


	/// Sets the clipping rectangle for sprite drawing.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setClipRect`]
	#[doc(alias = "sys::ffi::playdate_sprite::setClipRect")]
	pub fn set_clip_rect(&self, clip: LCDRect) {
		let f = self.1.set_clip_rect();
		unsafe { f(self.0, clip) }
	}

	/// Clears the sprite’s clipping rectangle.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::clearClipRect`]
	#[doc(alias = "sys::ffi::playdate_sprite::clearClipRect")]
	pub fn clear_clip_rect(&self) {
		let f = self.1.clear_clip_rect();
		unsafe { f(self.0) }
	}


	/// Set the `updates_enabled` flag of the sprite
	/// (determines whether the sprite has its update function called).
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setUpdatesEnabled`]
	#[doc(alias = "sys::ffi::playdate_sprite::setUpdatesEnabled")]
	pub fn set_updates_enabled(&self, value: bool) {
		let f = self.1.set_updates_enabled();
		unsafe { f(self.0, value.into()) }
	}

	/// Get the `updates_enabled` flag of the sprite.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::updatesEnabled`]
	#[doc(alias = "sys::ffi::playdate_sprite::updatesEnabled")]
	pub fn updates_enabled(&self) -> bool {
		let f = self.1.updates_enabled();
		unsafe { f(self.0) == 1 }
	}

	/// Set the collisions_enabled flag of the sprite
	/// (along with the `collide_rect`, this determines whether the sprite participates in collisions).
	///
	/// Set to `true` by default.
	///
	/// See also [`collide_rect`](Sprite::collide_rect),
	/// [`set_collide_rect`](Sprite::set_collide_rect),
	/// [`clear_collide_rect`](Sprite::clear_collide_rect).
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setCollisionsEnabled`]
	#[doc(alias = "sys::ffi::playdate_sprite::setCollisionsEnabled")]
	pub fn set_collisions_enabled(&self, value: bool) {
		let f = self.1.set_collisions_enabled();
		unsafe { f(self.0, value.into()) }
	}

	/// Get the `collisions_enabled` flag of the sprite.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::collisionsEnabled`]
	#[doc(alias = "sys::ffi::playdate_sprite::collisionsEnabled")]
	pub fn collisions_enabled(&self) -> bool {
		let f = self.1.collisions_enabled();
		unsafe { f(self.0) == 1 }
	}

	/// Set the visible flag of the given sprite
	/// (determines whether the sprite has its draw function called).
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setVisible`]
	#[doc(alias = "sys::ffi::playdate_sprite::setVisible")]
	pub fn set_visible(&self, value: bool) {
		let f = self.1.set_visible();
		unsafe { f(self.0, value.into()) }
	}

	/// Get the visible flag of the sprite.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::isVisible`]
	#[doc(alias = "sys::ffi::playdate_sprite::isVisible")]
	pub fn is_visible(&self) -> bool {
		let f = self.1.is_visible();
		unsafe { f(self.0) == 1 }
	}

	/// Marking a sprite opaque tells the sprite system that it doesn’t need to draw anything underneath the sprite,
	/// since it will be overdrawn anyway.
	///
	/// If you set an image without a mask/alpha channel on the sprite, it automatically sets the opaque flag.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setOpaque`]
	#[doc(alias = "sys::ffi::playdate_sprite::setOpaque")]
	pub fn set_opaque(&self, value: bool) {
		let f = self.1.set_opaque();
		unsafe { f(self.0, value.into()) }
	}

	/// Forces the sprite to redraw.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::markDirty`]
	#[doc(alias = "sys::ffi::playdate_sprite::markDirty")]
	pub fn mark_dirty(&self) {
		let f = self.1.mark_dirty();
		unsafe { f(self.0) }
	}

	/// Sets the tag of the sprite.
	///
	/// This can be useful for identifying sprites or types of sprites when using the [collision][] API.
	///
	/// [collision]: crate::callback::collision
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setTag`]
	#[doc(alias = "sys::ffi::playdate_sprite::setTag")]
	pub fn set_tag(&self, tag: u8) {
		let f = self.1.set_tag();
		unsafe { f(self.0, tag) }
	}

	/// Returns the tag of the given sprite.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::getTag`]
	#[doc(alias = "sys::ffi::playdate_sprite::getTag")]
	pub fn tag(&self) -> u8 {
		let f = self.1.get_tag();
		unsafe { f(self.0) }
	}

	/// When flag is set to `true`,
	/// the sprite will draw in screen coordinates,
	/// ignoring the currently-set `draw_offset`.
	///
	/// This only affects drawing,
	/// and should not be used on sprites being used for collisions,
	/// which will still happen in world-space.
	///
	/// See also [`gfx::set_draw_offset`].
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setIgnoresDrawOffset`]
	#[doc(alias = "sys::ffi::playdate_sprite::setIgnoresDrawOffset")]
	pub fn set_ignores_draw_offset(&self, value: bool) {
		let f = self.1.set_ignores_draw_offset();
		unsafe { f(self.0, value.into()) }
	}


	/// Sets `x` and `y` to the current position of sprite.
	///
	/// Equivalent to [`get_position_to`](Sprite::position_to) and [`sys::ffi::playdate_sprite::getPosition`]
	///
	#[doc(alias = "sys::ffi::playdate_sprite::getPosition")]
	pub fn position(&self) -> (c_float, c_float) {
		let (mut x, mut y) = Default::default();
		self.position_to(&mut x, &mut y);
		(x, y)
	}

	/// Sets `x` and `y` to the current position of sprite.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::getPosition`]
	#[doc(alias = "sys::ffi::playdate_sprite::getPosition")]
	pub fn position_to(&self, x: &mut c_float, y: &mut c_float) {
		let f = self.1.get_position();
		unsafe { f(self.0, x, y) }
	}


	/// Marks the area of the sprite, relative to its bounds,
	/// to be checked for collisions with other sprites' collide rects.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setCollideRect`]
	#[doc(alias = "sys::ffi::playdate_sprite::setCollideRect")]
	pub fn set_collide_rect(&self, collide: PDRect) {
		let f = self.1.set_collide_rect();
		unsafe { f(self.0, collide) }
	}

	/// Returns the sprite’s collide rect.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::getCollideRect`]
	#[doc(alias = "sys::ffi::playdate_sprite::getCollideRect")]
	pub fn collide_rect(&self) -> PDRect {
		let f = self.1.get_collide_rect();
		unsafe { f(self.0) }
	}

	/// Clears the sprite’s collide rect.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::clearCollideRect`]
	#[doc(alias = "sys::ffi::playdate_sprite::clearCollideRect")]
	pub fn clear_collide_rect(&self) {
		let f = self.1.clear_collide_rect();
		unsafe { f(self.0) }
	}

	/// Returns the same values as [`move_with_collisions`] but does not actually move the sprite.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::checkCollisions`]
	#[doc(alias = "sys::ffi::playdate_sprite::check_collisions")]
	#[must_use = "Result is borrowed by C-API"]
	pub fn check_collisions(&self,
	                        goal_x: c_float,
	                        goal_y: c_float,
	                        actual_x: &mut c_float,
	                        actual_y: &mut c_float)
	                        -> &[SpriteCollisionInfo] {
		let f = self.1.check_collisions();
		let mut len: c_int = 0;
		let ptr = unsafe { f(self.0, goal_x, goal_y, actual_x, actual_y, &mut len) };

		if ptr.is_null() || len == 0 {
			&[]
		} else {
			let slice = unsafe { core::slice::from_raw_parts(ptr, len as _) };
			slice
		}
	}

	/// Moves the sprite towards `goal_x`, `goal_y` taking collisions into account
	/// and returns a slice of [`SpriteCollisionInfo`].
	///
	/// `actual_x`, `actual_y` are set to the sprite’s position after collisions.
	/// If no collisions occurred, this will be the same as `goal_x`, `goal_y`.
	///
	/// Resulting slice with entire content can be freely dropped.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::moveWithCollisions`]
	#[doc(alias = "sys::ffi::playdate_sprite::moveWithCollisions")]
	#[must_use = "Result is borrowed by C-API"]
	pub fn move_with_collisions<'t>(&'t self,
	                                goal_x: c_float,
	                                goal_y: c_float,
	                                actual_x: &mut c_float,
	                                actual_y: &mut c_float)
	                                -> &'t [SpriteCollisionInfo] {
		let f = self.1.move_with_collisions();
		let mut len: c_int = 0;
		let ptr = unsafe { f(self.0, goal_x, goal_y, actual_x, actual_y, &mut len) };

		if ptr.is_null() || len == 0 {
			&[]
		} else {
			let slice = unsafe { core::slice::from_raw_parts(ptr, len as _) };
			slice
		}
	}


	/// Returns an slice of sprites that have collide rects
	/// that are currently overlapping the given sprite’s collide rect.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::overlappingSprites`]
	#[doc(alias = "sys::ffi::playdate_sprite::overlapping_sprites")]
	#[must_use = "Result is borrowed by C-API"]
	pub fn overlapping_sprites(&self) -> &[SpriteRef] {
		let f = self.1.overlapping_sprites();
		let mut len: c_int = 0;
		let ptr = unsafe { f(self.0, &mut len) };
		let slice = unsafe { core::slice::from_raw_parts(ptr, len as _) };
		unsafe { core::mem::transmute(slice) }
	}


	/// Sets the sprite’s stencil to the given pattern.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setStencilPattern`]
	#[doc(alias = "sys::ffi::playdate_sprite::setStencilPattern")]
	pub fn set_stencil_pattern(&self, pattern: &mut [u8; 8]) {
		let f = self.1.set_stencil_pattern();
		unsafe { f(self.0, pattern) }
	}

	/// Specifies a stencil image to be set on the frame buffer before the sprite is drawn.
	///
	/// If tile is set, the stencil will be tiled.
	///
	/// Tiled stencils must have __width__ evenly __divisible by 32__.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setStencilImage`]
	#[doc(alias = "sys::ffi::playdate_sprite::setStencilImage")]
	pub fn set_stencil_image(&self, stencil: impl AnyBitmap, tile: bool) {
		let f = self.1.set_stencil_image();
		unsafe { f(self.0, stencil.as_raw(), tile.into()) }
	}

	/// Clears the sprite’s stencil.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::clearStencil`]
	#[doc(alias = "sys::ffi::playdate_sprite::clearStencil")]
	pub fn clear_stencil(&self) {
		let f = self.1.clear_stencil();
		unsafe { f(self.0) }
	}

	/// Sets the sprite’s drawing center as a fraction (ranging from `0.0` to `1.0`) of the height and width.
	///
	/// Default is `0.5, 0.5` (the center of the sprite).
	///
	/// This means that when you call [`Sprite::move_to`]`(x, y)`,
	/// the center of your sprite will be positioned at `x, y`.
	///
	/// If you want `x` and `y` to represent the upper left corner of your sprite, specify the center as `0, 0`.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setCenter`].
	#[doc(alias = "sys::ffi::playdate_sprite::setCenter")]
	#[inline(always)]
	pub fn set_center(&self, x: c_float, y: c_float) {
		let f = self.1.set_center();
		unsafe { f(self.0, x, y) }
	}

	/// Returns the sprite’s drawing center as a fraction (ranging from `0.0` to `1.0`) of the height and width.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::getCenter`].
	#[doc(alias = "sys::ffi::playdate_sprite::getCenter")]
	#[inline(always)]
	pub fn center(&self) -> (c_float, c_float) {
		let (mut x, mut y) = (0.0, 0.0);
		let f = self.1.get_center();
		unsafe { f(self.0, &mut x, &mut y) };
		(x, y)
	}


	/// Sets custom data to the sprite.
	///
	/// Used for associating the sprite with other data.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::setUserdata`]
	#[doc(alias = "sys::ffi::playdate_sprite::setUserdata")]
	pub fn set_userdata(&self, data: Userdata) {
		let f = self.1.set_userdata();
		let userdata = Box::into_raw(Box::new(data));
		let ptr = userdata as *mut c_void;
		unsafe { f(self.0, ptr) }
	}

	/// Gets the _mutable__ reference to sprite’s userdata.
	///
	/// Used for associating the sprite with other data.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::getUserdata`]
	#[doc(alias = "sys::ffi::playdate_sprite::get_userdata")]
	pub fn userdata(&self) -> Option<&mut Userdata> {
		let f = self.1.get_userdata();
		let ptr = unsafe { f(self.0) };
		if ptr.is_null() {
			None
		} else {
			let ptr = ptr as *mut Userdata;
			// TODO: check ptr is aligned to `UD`
			unsafe { ptr.as_mut() }
		}
	}

	/// Returns __taken__ value the sprite’s userdata.
	///
	/// Equivalent to [`sys::ffi::playdate_sprite::getUserdata`]
	#[doc(alias = "sys::ffi::playdate_sprite::get_userdata")]
	pub(crate) fn take_userdata(&self) -> Option<Box<Userdata>> {
		let f = self.1.get_userdata();
		let ptr = unsafe { f(self.0) };
		if ptr.is_null() {
			None
		} else {
			// TODO: check ptr is aligned to `UD`
			let ud = unsafe { Box::from_raw(ptr as *mut Userdata) };
			Some(ud)
		}
	}
}


#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	/// Ensure that SpriteRef have same size as LCDSprite.
	fn sprite_ref_layout() {
		assert_eq!(
		           core::mem::size_of::<SpriteRef>(),
		           core::mem::size_of::<*mut LCDSprite>()
		);
		assert_eq!(
		           core::mem::size_of::<&[SpriteRef]>(),
		           core::mem::size_of::<&[*mut LCDSprite]>()
		);
	}
}