playdate-rs 0.0.2

Rust type-safe bindings for the Playdate SDK
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
use alloc::vec::Vec;

use crate::{
    graphics::{so2d_to_lcdrect, Bitmap},
    math::{Point2D, Rect, SideOffsets2D, Vec2D},
    PLAYDATE,
};

pub use sys::SpriteCollisionResponseType;

fn rect_to_pdrect(rect: Rect<f32>) -> sys::PDRect {
    sys::PDRect {
        x: rect.origin.x,
        y: rect.origin.y,
        width: rect.size.width,
        height: rect.size.height,
    }
}

fn pdrect_to_rect(rect: sys::PDRect) -> Rect<f32> {
    Rect {
        origin: (rect.x, rect.y).into(),
        size: (rect.width, rect.height).into(),
    }
}

fn collision_point_to_point(point: sys::CollisionPoint) -> Point2D<f32> {
    Point2D::new(point.x, point.y)
}

fn collision_vec_to_vec(vec: sys::CollisionVector) -> Vec2D<i32> {
    Vec2D::new(vec.x, vec.y)
}

pub struct _Sprite {
    handle: *const sys::playdate_sprite,
}

impl _Sprite {
    pub(crate) fn new(handle: *const sys::playdate_sprite) -> Self {
        Self { handle }
    }

    /// When flag is set to 1, the given sprite will always redraw.
    pub fn set_always_redraw(&self, flag: bool) {
        unsafe {
            (*self.handle).setAlwaysRedraw.unwrap()(flag as i32);
        }
    }

    /// Marks the given dirtyRect (in screen coordinates) as needing a redraw. Graphics drawing functions now call this automatically, adding their drawn areas to the sprite’s dirty list, so there’s usually no need to call this manually.
    pub fn add_dirty_rect(&self, dirty_rect: SideOffsets2D<i32>) {
        unsafe {
            (*self.handle).addDirtyRect.unwrap()(so2d_to_lcdrect(dirty_rect));
        }
    }

    /// Draws every sprite in the display list.
    pub fn draw_sprites(&self) {
        unsafe {
            (*self.handle).drawSprites.unwrap()();
        }
    }

    /// Updates and draws every sprite in the display list.
    pub fn update_and_draw_sprites(&self) {
        unsafe {
            (*self.handle).updateAndDrawSprites.unwrap()();
        }
    }

    /// Allocates and returns a new LCDSprite.
    pub fn new_sprite(&self) -> Sprite {
        Sprite::from(unsafe { (*self.handle).newSprite.unwrap()() })
    }

    /// Frees the given sprite.
    pub(crate) fn free_sprite(&self, sprite: *mut sys::LCDSprite) {
        unsafe {
            (*self.handle).freeSprite.unwrap()(sprite);
        }
    }

    /// Allocates and returns a copy of the given sprite.
    pub(crate) fn copy(&self, sprite: *mut sys::LCDSprite) -> *mut sys::LCDSprite {
        unsafe { (*self.handle).copy.unwrap()(sprite) }
    }

    /// Adds the given sprite to the display list, so that it is drawn in the current scene.
    pub fn add_sprite(&self, sprite: &Sprite) {
        unsafe {
            (*self.handle).addSprite.unwrap()(sprite.handle);
        }
    }

    /// Removes the given sprite from the display list.
    pub fn remove_sprite(&self, sprite: &Sprite) {
        unsafe {
            (*self.handle).removeSprite.unwrap()(sprite.handle);
        }
    }

    /// Removes the given count sized array of sprites from the display list.
    pub fn remove_sprites(&self, sprites: &[&Sprite]) {
        let mut sprites: Vec<_> = sprites.iter().map(|s| s.handle).collect();
        unsafe {
            (*self.handle).removeSprites.unwrap()(sprites.as_mut_ptr(), sprites.len() as i32);
        }
    }

    /// Removes all sprites from the display list.
    pub fn remove_all_sprites(&self) {
        unsafe {
            (*self.handle).removeAllSprites.unwrap()();
        }
    }

    /// Returns the total number of sprites in the display list.
    pub fn get_sprite_count(&self) -> i32 {
        unsafe { (*self.handle).getSpriteCount.unwrap()() }
    }

    /// Sets the bounds of the given sprite with bounds.
    pub(crate) fn set_bounds(&self, sprite: *mut sys::LCDSprite, bounds: Rect<f32>) {
        unsafe {
            (*self.handle).setBounds.unwrap()(sprite, rect_to_pdrect(bounds));
        }
    }

    /// Returns the bounds of the given sprite as an PDRect;
    pub(crate) fn get_bounds(&self, sprite: *mut sys::LCDSprite) -> Rect<f32> {
        unsafe { pdrect_to_rect((*self.handle).getBounds.unwrap()(sprite)) }
    }

    /// Moves the given sprite to x, y and resets its bounds based on the bitmap dimensions and center.
    pub(crate) fn move_to(&self, sprite: *mut sys::LCDSprite, x: f32, y: f32) {
        unsafe {
            (*self.handle).moveTo.unwrap()(sprite, x, y);
        }
    }

    /// Moves the given sprite to by offsetting its current position by dx, dy.
    pub(crate) fn move_by(&self, sprite: *mut sys::LCDSprite, dx: f32, dy: f32) {
        unsafe {
            (*self.handle).moveBy.unwrap()(sprite, dx, dy);
        }
    }

    /// Sets the given sprite's image to the given bitmap.
    pub(crate) fn set_image(
        &self,
        sprite: *mut sys::LCDSprite,
        image: *mut sys::LCDBitmap,
        flip: sys::LCDBitmapFlip,
    ) {
        unsafe {
            (*self.handle).setImage.unwrap()(sprite, image, flip);
        }
    }

    /// Returns the LCDBitmap currently assigned to the given sprite.
    pub(crate) fn get_image(&self, sprite: *mut sys::LCDSprite) -> *mut sys::LCDBitmap {
        unsafe { (*self.handle).getImage.unwrap()(sprite) }
    }

    /// Sets the size. The size is used to set the sprite’s bounds when calling moveTo().
    pub(crate) fn set_size(&self, sprite: *mut sys::LCDSprite, width: f32, height: f32) {
        unsafe {
            (*self.handle).setSize.unwrap()(sprite, width, height);
        }
    }

    /// Sets the Z order of the given sprite. Higher Z sprites are drawn on top of those with lower Z order.
    pub(crate) fn set_z_index(&self, sprite: *mut sys::LCDSprite, z_index: i16) {
        unsafe {
            (*self.handle).setZIndex.unwrap()(sprite, z_index);
        }
    }

    /// Returns the Z index of the given sprite.
    pub(crate) fn get_z_index(&self, sprite: *mut sys::LCDSprite) -> i16 {
        unsafe { (*self.handle).getZIndex.unwrap()(sprite) }
    }

    /// Sets the mode for drawing the sprite’s bitmap.
    pub(crate) fn set_draw_mode(&self, sprite: *mut sys::LCDSprite, mode: sys::LCDBitmapDrawMode) {
        unsafe {
            (*self.handle).setDrawMode.unwrap()(sprite, mode);
        }
    }

    /// Flips the bitmap.
    pub(crate) fn set_image_flip(&self, sprite: *mut sys::LCDSprite, flip: sys::LCDBitmapFlip) {
        unsafe {
            (*self.handle).setImageFlip.unwrap()(sprite, flip);
        }
    }

    /// Returns the flip setting of the sprite’s bitmap.
    pub(crate) fn get_image_flip(&self, sprite: *mut sys::LCDSprite) -> sys::LCDBitmapFlip {
        unsafe { (*self.handle).getImageFlip.unwrap()(sprite) }
    }

    /// Specifies a stencil image to be set on the frame buffer before the sprite is drawn.
    pub(crate) fn set_stencil(&self, sprite: *mut sys::LCDSprite, stencil: *mut sys::LCDBitmap) {
        unsafe {
            (*self.handle).setStencil.unwrap()(sprite, stencil);
        }
    }

    /// Sets the clipping rectangle for sprite drawing.
    pub(crate) fn set_clip_rect(&self, sprite: *mut sys::LCDSprite, clip_rect: SideOffsets2D<i32>) {
        unsafe {
            (*self.handle).setClipRect.unwrap()(sprite, so2d_to_lcdrect(clip_rect));
        }
    }

    /// Clears the sprite’s clipping rectangle.
    pub(crate) fn clear_clip_rect(&self, sprite: *mut sys::LCDSprite) {
        unsafe {
            (*self.handle).clearClipRect.unwrap()(sprite);
        }
    }

    /// Sets the clipping rectangle for all sprites with a Z index within startZ and endZ inclusive.
    pub fn set_clip_rects_in_range(&self, clip_rect: SideOffsets2D<i32>, start_z: i32, end_z: i32) {
        unsafe {
            (*self.handle).setClipRectsInRange.unwrap()(so2d_to_lcdrect(clip_rect), start_z, end_z);
        }
    }

    /// Clears the clipping rectangle for all sprites with a Z index within startZ and endZ inclusive.
    pub fn clear_clip_rects_in_range(&self, start_z: i32, end_z: i32) {
        unsafe {
            (*self.handle).clearClipRectsInRange.unwrap()(start_z, end_z);
        }
    }

    /// Set the updatesEnabled flag of the given sprite (determines whether the sprite has its update function called). One is true, 0 is false.
    pub(crate) fn set_updates_enabled(&self, sprite: *mut sys::LCDSprite, flag: i32) {
        unsafe {
            (*self.handle).setUpdatesEnabled.unwrap()(sprite, flag);
        }
    }

    /// Get the updatesEnabled flag of the given sprite.
    pub(crate) fn updates_enabled(&self, sprite: *mut sys::LCDSprite) -> i32 {
        unsafe { (*self.handle).updatesEnabled.unwrap()(sprite) }
    }

    /// Set the collisionsEnabled flag of the given sprite (along with the collideRect, this determines whether the sprite participates in collisions). One is true, 0 is false. Set to 1 by default.
    pub(crate) fn set_collisions_enabled(&self, sprite: *mut sys::LCDSprite, flag: i32) {
        unsafe {
            (*self.handle).setCollisionsEnabled.unwrap()(sprite, flag);
        }
    }

    /// Get the collisionsEnabled flag of the given sprite.
    pub(crate) fn collisions_enabled(&self, sprite: *mut sys::LCDSprite) -> i32 {
        unsafe { (*self.handle).collisionsEnabled.unwrap()(sprite) }
    }

    /// Set the visible flag of the given sprite (determines whether the sprite has its draw function called). One is true, 0 is false.
    pub(crate) fn set_visible(&self, sprite: *mut sys::LCDSprite, flag: i32) {
        unsafe {
            (*self.handle).setVisible.unwrap()(sprite, flag);
        }
    }

    /// Get the visible flag of the given sprite.
    pub(crate) fn is_visible(&self, sprite: *mut sys::LCDSprite) -> i32 {
        unsafe { (*self.handle).isVisible.unwrap()(sprite) }
    }

    /// 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.
    pub(crate) fn set_opaque(&self, sprite: *mut sys::LCDSprite, flag: i32) {
        unsafe {
            (*self.handle).setOpaque.unwrap()(sprite, flag);
        }
    }

    /// Forces the given sprite to redraw.
    pub(crate) fn mark_dirty(&self, sprite: *mut sys::LCDSprite) {
        unsafe {
            (*self.handle).markDirty.unwrap()(sprite);
        }
    }

    /// Sets the tag of the given sprite. This can be useful for identifying sprites or types of sprites when using the collision API.
    pub(crate) fn set_tag(&self, sprite: *mut sys::LCDSprite, tag: u8) {
        unsafe {
            (*self.handle).setTag.unwrap()(sprite, tag);
        }
    }

    /// Returns the tag of the given sprite.
    pub(crate) fn get_tag(&self, sprite: *mut sys::LCDSprite) -> u8 {
        unsafe { (*self.handle).getTag.unwrap()(sprite) }
    }

    /// When flag is set to 1, the sprite will draw in screen coordinates, ignoring the currently-set drawOffset.
    ///
    /// This only affects drawing, and should not be used on sprites being used for collisions, which will still happen in world-space.
    pub(crate) fn set_ignores_draw_offset(&self, sprite: *mut sys::LCDSprite, flag: i32) {
        unsafe {
            (*self.handle).setIgnoresDrawOffset.unwrap()(sprite, flag);
        }
    }

    /// Sets the update function for the given sprite.
    pub(crate) fn set_update_function(
        &self,
        sprite: *mut sys::LCDSprite,
        func: sys::LCDSpriteUpdateFunction,
    ) {
        unsafe {
            (*self.handle).setUpdateFunction.unwrap()(sprite, func);
        }
    }

    /// Sets the draw function for the given sprite.
    pub(crate) fn set_draw_function(
        &self,
        sprite: *mut sys::LCDSprite,
        func: sys::LCDSpriteDrawFunction,
    ) {
        unsafe {
            (*self.handle).setDrawFunction.unwrap()(sprite, func);
        }
    }

    /// Sets x and y to the current position of sprite.
    pub(crate) fn get_position(&self, sprite: *mut sys::LCDSprite) -> (f32, f32) {
        let mut x = 0.0;
        let mut y = 0.0;
        unsafe {
            (*self.handle).getPosition.unwrap()(sprite, &mut x, &mut y);
        }
        (x, y)
    }

    /// Frees and reallocates internal collision data, resetting everything to its default state.
    pub fn reset_collision_world(&self) {
        unsafe {
            (*self.handle).resetCollisionWorld.unwrap()();
        }
    }

    /// Marks the area of the given sprite, relative to its bounds, to be checked for collisions with other sprites' collide rects.
    pub(crate) fn set_collide_rect(&self, sprite: *mut sys::LCDSprite, collide_rect: Rect<f32>) {
        unsafe {
            (*self.handle).setCollideRect.unwrap()(sprite, rect_to_pdrect(collide_rect));
        }
    }

    /// Returns the given sprite’s collide rect.
    pub(crate) fn get_collide_rect(&self, sprite: *mut sys::LCDSprite) -> Rect<f32> {
        unsafe { pdrect_to_rect((*self.handle).getCollideRect.unwrap()(sprite)) }
    }

    /// Clears the given sprite’s collide rect.
    pub(crate) fn clear_collide_rect(&self, sprite: *mut sys::LCDSprite) {
        unsafe {
            (*self.handle).clearCollideRect.unwrap()(sprite);
        }
    }

    /// Set a callback that returns a SpriteCollisionResponseType for a collision between sprite and other.
    pub(crate) fn set_collision_response_function(
        &self,
        sprite: *mut sys::LCDSprite,
        func: sys::LCDSpriteCollisionFilterProc,
    ) {
        unsafe {
            (*self.handle).setCollisionResponseFunction.unwrap()(sprite, func);
        }
    }

    /// Returns the same values as playdate->sprite->moveWithCollisions() but does not actually move the sprite.
    pub fn check_collisions(
        &self,
        sprite: &Sprite,
        goal_x: f32,
        goal_y: f32,
    ) -> Vec<SpriteCollisionInfo> {
        let mut actual_x = 0.0;
        let mut actual_y = 0.0;
        let mut len = 0;
        let info = unsafe {
            (*self.handle).checkCollisions.unwrap()(
                sprite.handle,
                goal_x,
                goal_y,
                &mut actual_x,
                &mut actual_y,
                &mut len,
            )
        };
        let mut result = Vec::new();
        for i in 0..len {
            let info = unsafe { info.offset(i as isize).as_ref().unwrap() };
            result.push(SpriteCollisionInfo::new(info));
        }
        result
    }

    /// Moves the given sprite towards goalX, goalY taking collisions into account and returns an array of SpriteCollisionInfo. len is set to the size of the array and actualX, actualY are set to the sprite’s position after collisions. If no collisions occurred, this will be the same as goalX, goalY.
    pub fn move_with_collisions(
        &self,
        sprite: &Sprite,
        goal_x: f32,
        goal_y: f32,
    ) -> Vec<SpriteCollisionInfo> {
        let mut actual_x = 0.0;
        let mut actual_y = 0.0;
        let mut len = 0;
        let info = unsafe {
            (*self.handle).moveWithCollisions.unwrap()(
                sprite.handle,
                goal_x,
                goal_y,
                &mut actual_x,
                &mut actual_y,
                &mut len,
            )
        };
        let mut result = Vec::new();
        for i in 0..len {
            let info = unsafe { info.offset(i as isize).as_ref().unwrap() };
            result.push(SpriteCollisionInfo::new(info));
        }
        result
    }

    pub fn query_sprites_at_point(&self, x: f32, y: f32) -> Vec<Sprite> {
        let mut len = 0;
        let sprites = unsafe { (*self.handle).querySpritesAtPoint.unwrap()(x, y, &mut len) };
        let mut result = Vec::new();
        for i in 0..len {
            let sprite = unsafe { sprites.offset(i as isize).as_ref().unwrap() };
            result.push(Sprite::from_ref(*sprite));
        }
        result
    }

    pub fn query_sprites_in_rect(&self, x: f32, y: f32, width: f32, height: f32) -> Vec<Sprite> {
        let mut len = 0;
        let sprites =
            unsafe { (*self.handle).querySpritesInRect.unwrap()(x, y, width, height, &mut len) };
        let mut result = Vec::new();
        for i in 0..len {
            let sprite = unsafe { sprites.offset(i as isize).as_ref().unwrap() };
            result.push(Sprite::from_ref(*sprite));
        }
        result
    }

    pub fn query_sprites_along_line(&self, x1: f32, y1: f32, x2: f32, y2: f32) -> Vec<Sprite> {
        let mut len = 0;
        let sprites =
            unsafe { (*self.handle).querySpritesAlongLine.unwrap()(x1, y1, x2, y2, &mut len) };
        let mut result = Vec::new();
        for i in 0..len {
            let sprite = unsafe { sprites.offset(i as isize).as_ref().unwrap() };
            result.push(Sprite::from_ref(*sprite));
        }
        result
    }

    pub fn query_sprite_info_along_line(
        &self,
        x1: f32,
        y1: f32,
        x2: f32,
        y2: f32,
    ) -> Vec<SpriteQueryInfo> {
        let mut len = 0;
        let info =
            unsafe { (*self.handle).querySpriteInfoAlongLine.unwrap()(x1, y1, x2, y2, &mut len) };
        let mut result = Vec::new();
        for i in 0..len {
            let info = unsafe { info.offset(i as isize).as_ref().unwrap() };
            result.push(SpriteQueryInfo::new(info));
        }
        result
    }

    /// Returns an array of sprites that have collide rects that are currently overlapping the given sprite’s collide rect.
    pub fn overlapping_sprites(&self, sprite: &Sprite) -> Vec<Sprite> {
        let mut len = 0;
        let sprites =
            unsafe { (*self.handle).overlappingSprites.unwrap()(sprite.handle, &mut len) };
        let mut result = Vec::new();
        for i in 0..len {
            let sprite = unsafe { sprites.offset(i as isize).as_ref().unwrap() };
            result.push(Sprite::from_ref(*sprite));
        }
        result
    }

    /// Returns an array of all sprites that have collide rects that are currently overlapping. Each consecutive pair of sprites is overlapping (eg. 0 & 1 overlap, 2 & 3 overlap, etc).
    pub fn all_overlapping_sprites(&self) -> Vec<Sprite> {
        let mut len = 0;
        let sprites = unsafe { (*self.handle).allOverlappingSprites.unwrap()(&mut len) };
        let mut result = Vec::new();
        for i in 0..len {
            let sprite = unsafe { sprites.offset(i as isize).as_ref().unwrap() };
            result.push(Sprite::from_ref(*sprite));
        }
        result
    }

    /// Sets the sprite’s stencil to the given pattern.
    pub(crate) fn set_stencil_pattern(&self, sprite: *mut sys::LCDSprite, pattern: *mut u8) {
        unsafe {
            (*self.handle).setStencilPattern.unwrap()(sprite, pattern);
        }
    }

    /// Clears the sprite’s stencil.
    pub(crate) fn clear_stencil(&self, sprite: *mut sys::LCDSprite) {
        unsafe {
            (*self.handle).clearStencil.unwrap()(sprite);
        }
    }

    /// Sets the sprite’s userdata, an arbitrary pointer used for associating the sprite with other data.
    pub(crate) fn set_userdata(
        &self,
        sprite: *mut sys::LCDSprite,
        userdata: *mut core::ffi::c_void,
    ) {
        unsafe {
            (*self.handle).setUserdata.unwrap()(sprite, userdata);
        }
    }

    /// Gets the sprite’s userdata, an arbitrary pointer used for associating the sprite with other data.
    pub(crate) fn get_userdata(&self, sprite: *mut sys::LCDSprite) -> *mut core::ffi::c_void {
        unsafe { (*self.handle).getUserdata.unwrap()(sprite) }
    }

    /// 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.
    pub(crate) fn set_stencil_image(
        &self,
        sprite: *mut sys::LCDSprite,
        stencil: *mut sys::LCDBitmap,
        tile: i32,
    ) {
        unsafe {
            (*self.handle).setStencilImage.unwrap()(sprite, stencil, tile);
        }
    }
}

#[derive(Debug)]
pub struct Sprite {
    handle: *mut sys::LCDSprite,
    forget: bool,
}

impl PartialEq for Sprite {
    fn eq(&self, other: &Self) -> bool {
        self.handle == other.handle
    }
}

impl Eq for Sprite {}

unsafe impl Sync for Sprite {}
unsafe impl Send for Sprite {}

impl Default for Sprite {
    fn default() -> Self {
        Self::new()
    }
}

impl Sprite {
    pub(crate) fn from(handle: *mut sys::LCDSprite) -> Self {
        Self {
            handle,
            forget: false,
        }
    }

    pub(crate) fn from_ref(handle: *mut sys::LCDSprite) -> Self {
        Self {
            handle,
            forget: true,
        }
    }

    /// Allocates and returns a new Sprite.
    pub fn new() -> Self {
        PLAYDATE.sprite.new_sprite()
    }

    /// Sets x and y to the current position of sprite.
    pub fn get_position(&self) -> (f32, f32) {
        PLAYDATE.sprite.get_position(self.handle)
    }

    /// Sets the bounds of the given sprite with bounds.
    pub fn set_bounds(&self, bounds: Rect<f32>) {
        PLAYDATE.sprite.set_bounds(self.handle, bounds);
    }

    /// Returns the bounds of the given sprite as an PDRect;
    pub fn get_bounds(&self) -> Rect<f32> {
        PLAYDATE.sprite.get_bounds(self.handle)
    }

    /// Moves the given sprite to x, y and resets its bounds based on the bitmap dimensions and center.
    pub fn move_to(&self, x: f32, y: f32) {
        PLAYDATE.sprite.move_to(self.handle, x, y);
    }

    /// Moves the given sprite to by offsetting its current position by dx, dy.
    pub fn move_by(&self, dx: f32, dy: f32) {
        PLAYDATE.sprite.move_by(self.handle, dx, dy);
    }

    /// Sets the given sprite's image to the given bitmap.
    pub fn set_image(&self, image: &Bitmap, flip: sys::LCDBitmapFlip) {
        PLAYDATE.sprite.set_image(self.handle, image.handle, flip);
    }

    /// Returns the LCDBitmap currently assigned to the given sprite.
    pub fn get_image(&self) -> Bitmap {
        Bitmap::from_ref(PLAYDATE.sprite.get_image(self.handle))
    }

    /// Sets the size. The size is used to set the sprite’s bounds when calling moveTo().
    pub fn set_size(&self, width: f32, height: f32) {
        PLAYDATE.sprite.set_size(self.handle, width, height);
    }

    /// Sets the Z order of the given sprite. Higher Z sprites are drawn on top of those with lower Z order.
    pub fn set_z_index(&self, z_index: i16) {
        PLAYDATE.sprite.set_z_index(self.handle, z_index);
    }

    /// Returns the Z index of the given sprite.
    pub fn get_z_index(&self) -> i16 {
        PLAYDATE.sprite.get_z_index(self.handle)
    }

    /// Sets the mode for drawing the sprite’s bitmap.
    pub fn set_draw_mode(&self, mode: sys::LCDBitmapDrawMode) {
        PLAYDATE.sprite.set_draw_mode(self.handle, mode);
    }

    /// Flips the bitmap.
    pub fn set_image_flip(&self, flip: sys::LCDBitmapFlip) {
        PLAYDATE.sprite.set_image_flip(self.handle, flip);
    }

    /// Returns the flip setting of the sprite’s bitmap.
    pub fn get_image_flip(&self) -> sys::LCDBitmapFlip {
        PLAYDATE.sprite.get_image_flip(self.handle)
    }

    /// Specifies a stencil image to be set on the frame buffer before the sprite is drawn.
    pub fn set_stencil(&self, stencil: &Bitmap) {
        PLAYDATE.sprite.set_stencil(self.handle, stencil.handle);
    }

    /// Sets the clipping rectangle for sprite drawing.
    pub fn set_clip_rect(&self, clip_rect: SideOffsets2D<i32>) {
        PLAYDATE.sprite.set_clip_rect(self.handle, clip_rect);
    }

    /// Clears the sprite’s clipping rectangle.
    pub fn clear_clip_rect(&self) {
        PLAYDATE.sprite.clear_clip_rect(self.handle);
    }

    /// Set the updatesEnabled flag of the given sprite (determines whether the sprite has its update function called).
    pub fn set_updates_enabled(&self, flag: bool) {
        PLAYDATE.sprite.set_updates_enabled(self.handle, flag as _);
    }

    /// Get the updatesEnabled flag of the given sprite.
    pub fn updates_enabled(&self) -> bool {
        PLAYDATE.sprite.updates_enabled(self.handle) == 1
    }

    /// Set the collisionsEnabled flag of the given sprite (along with the collideRect, this determines whether the sprite participates in collisions). Set to true by default.
    pub fn set_collisions_enabled(&self, flag: bool) {
        PLAYDATE
            .sprite
            .set_collisions_enabled(self.handle, flag as _);
    }

    /// Get the collisionsEnabled flag of the given sprite.
    pub fn collisions_enabled(&self) -> bool {
        PLAYDATE.sprite.collisions_enabled(self.handle) == 1
    }

    /// Set the visible flag of the given sprite (determines whether the sprite has its draw function called).
    pub fn set_visible(&self, flag: bool) {
        PLAYDATE.sprite.set_visible(self.handle, flag as _);
    }

    /// Get the visible flag of the given sprite.
    pub fn is_visible(&self) -> bool {
        PLAYDATE.sprite.is_visible(self.handle) == 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.
    pub fn set_opaque(&self, flag: bool) {
        PLAYDATE.sprite.set_opaque(self.handle, flag as _);
    }

    /// Forces the given sprite to redraw.
    pub fn mark_dirty(&self) {
        PLAYDATE.sprite.mark_dirty(self.handle);
    }

    /// Sets the tag of the given sprite. This can be useful for identifying sprites or types of sprites when using the collision API.
    pub fn set_tag(&self, tag: u8) {
        PLAYDATE.sprite.set_tag(self.handle, tag);
    }

    /// Returns the tag of the given sprite.
    pub fn get_tag(&self) -> u8 {
        PLAYDATE.sprite.get_tag(self.handle)
    }

    /// When flag is set to 1, the sprite will draw in screen coordinates, ignoring the currently-set drawOffset.
    ///
    /// This only affects drawing, and should not be used on sprites being used for collisions, which will still happen in world-space.
    pub fn set_ignores_draw_offset(&self, flag: i32) {
        PLAYDATE.sprite.set_ignores_draw_offset(self.handle, flag);
    }

    /// Sets the update function for the given sprite.
    pub fn set_update_function(&self, func: sys::LCDSpriteUpdateFunction) {
        PLAYDATE.sprite.set_update_function(self.handle, func);
    }

    /// Sets the draw function for the given sprite.
    pub fn set_draw_function(&self, func: sys::LCDSpriteDrawFunction) {
        PLAYDATE.sprite.set_draw_function(self.handle, func);
    }

    /// Marks the area of the given sprite, relative to its bounds, to be checked for collisions with other sprites' collide rects.
    pub fn set_collide_rect(&self, collide_rect: Rect<f32>) {
        PLAYDATE.sprite.set_collide_rect(self.handle, collide_rect);
    }

    /// Returns the given sprite’s collide rect.
    pub fn get_collide_rect(&self) -> Rect<f32> {
        PLAYDATE.sprite.get_collide_rect(self.handle)
    }

    /// Clears the given sprite’s collide rect.
    pub fn clear_collide_rect(&self) {
        PLAYDATE.sprite.clear_collide_rect(self.handle);
    }

    /// Set a callback that returns a SpriteCollisionResponseType for a collision between sprite and other.
    pub fn set_collision_response_function(&self, func: sys::LCDSpriteCollisionFilterProc) {
        PLAYDATE
            .sprite
            .set_collision_response_function(self.handle, func);
    }

    /// Sets the sprite’s stencil to the given pattern.
    pub fn set_stencil_pattern(&self, pattern: *mut u8) {
        PLAYDATE.sprite.set_stencil_pattern(self.handle, pattern);
    }

    /// Clears the sprite’s stencil.
    pub fn clear_stencil(&self) {
        PLAYDATE.sprite.clear_stencil(self.handle);
    }

    /// Sets the sprite’s userdata, an arbitrary pointer used for associating the sprite with other data.
    pub fn set_userdata(&self, userdata: *mut core::ffi::c_void) {
        PLAYDATE.sprite.set_userdata(self.handle, userdata);
    }

    /// Gets the sprite’s userdata, an arbitrary pointer used for associating the sprite with other data.
    pub fn get_userdata(&self) -> *mut core::ffi::c_void {
        PLAYDATE.sprite.get_userdata(self.handle)
    }

    /// 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.
    pub fn set_stencil_image(&self, stencil: &Bitmap, tile: i32) {
        PLAYDATE
            .sprite
            .set_stencil_image(self.handle, stencil.handle, tile);
    }
}

impl Drop for Sprite {
    fn drop(&mut self) {
        if self.forget {
            return;
        }
        PLAYDATE.sprite.free_sprite(self.handle as *mut _);
    }
}

impl Clone for Sprite {
    fn clone(&self) -> Self {
        let handle = PLAYDATE.sprite.copy(self.handle as *mut _);
        Self {
            handle,
            forget: false,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct SpriteCollisionInfo {
    pub sprite: Sprite,
    pub other: Sprite,
    pub response_type: SpriteCollisionResponseType,
    pub overlaps: u8,
    pub ti: f32,
    pub move_: Point2D<f32>,
    pub normal: Vec2D<i32>,
    pub touch: Point2D<f32>,
    pub sprite_rect: Rect<f32>,
    pub other_rect: Rect<f32>,
}

impl SpriteCollisionInfo {
    fn new(info: &sys::SpriteCollisionInfo) -> Self {
        Self {
            sprite: Sprite::from_ref(info.sprite),
            other: Sprite::from_ref(info.other),
            response_type: info.responseType,
            overlaps: info.overlaps,
            ti: info.ti,
            move_: collision_point_to_point(info.move_),
            normal: collision_vec_to_vec(info.normal),
            touch: collision_point_to_point(info.touch),
            sprite_rect: pdrect_to_rect(info.spriteRect),
            other_rect: pdrect_to_rect(info.otherRect),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct SpriteQueryInfo {
    pub sprite: Sprite,
    pub ti1: f32,
    pub ti2: f32,
    pub entry_point: Point2D<f32>,
    pub exit_point: Point2D<f32>,
}

impl SpriteQueryInfo {
    fn new(info: &sys::SpriteQueryInfo) -> Self {
        Self {
            sprite: Sprite::from_ref(info.sprite),
            ti1: info.ti1,
            ti2: info.ti2,
            entry_point: collision_point_to_point(info.entryPoint),
            exit_point: collision_point_to_point(info.exitPoint),
        }
    }
}