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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
//! Playdate bitmap API

use core::ffi::c_char;
use core::ffi::c_float;
use core::ffi::c_int;
use core::fmt::Write;
use core::marker::PhantomData;
use alloc::boxed::Box;

use sys::error::OkOrNullFnErr;
use sys::ffi::LCDPattern;
use sys::traits::AsRaw;
use sys::ffi::CString;
use sys::ffi::LCDColor;
use sys::ffi::LCDRect;
use sys::ffi::LCDBitmap;
use fs::Path;

use crate::Graphics;
use crate::error::ApiError;
use crate::error::Error;
use super::api;

pub use color::*;
pub use sys::ffi::LCDBitmapFlip as BitmapFlip;
pub use sys::ffi::LCDBitmapDrawMode as BitmapDrawMode;
pub use crate::{BitmapFlipExt, BitmapDrawModeExt};


pub trait AnyBitmap: AsRaw<Type = LCDBitmap> + BitmapApi {}
impl<T: AnyBitmap> AnyBitmap for &'_ T {}
impl AnyBitmap for BitmapRef<'_> {}
impl<Api: api::Api, const FOD: bool> AnyBitmap for Bitmap<Api, FOD> {}


pub trait BitmapApi {
	type Api: api::Api;
	fn api(&self) -> Self::Api
		where Self::Api: Copy;
	fn api_ref(&self) -> &Self::Api;
}

impl BitmapApi for BitmapRef<'_> {
	type Api = api::Default;

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

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

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

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

impl<T: BitmapApi> BitmapApi for &'_ T {
	type Api = T::Api;

	fn api(&self) -> Self::Api
		where Self::Api: Copy {
		(*self).api()
	}

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


#[cfg_attr(feature = "bindings-derive-debug", derive(Debug))]
pub struct Bitmap<Api: api::Api = api::Default, const FREE_ON_DROP: bool = true>(pub(crate) *mut LCDBitmap,
                                                                                 pub(crate) Api);

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

impl<Api: api::Api + Default, const FOD: bool> From<*mut LCDBitmap> for Bitmap<Api, FOD> {
	fn from(ptr: *mut LCDBitmap) -> Self { Self(ptr, Api::default()) }
}

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


#[repr(transparent)]
pub struct BitmapRef<'owner>(*mut LCDBitmap, api::Default, PhantomData<&'owner ()>);

impl AsRaw for BitmapRef<'_> {
	type Type = LCDBitmap;
	unsafe fn as_raw(&self) -> *mut LCDBitmap { self.0 }
}

impl From<*mut LCDBitmap> for BitmapRef<'_> {
	fn from(ptr: *mut LCDBitmap) -> Self { Self(ptr, Default::default(), PhantomData) }
}

impl<'owner> BitmapRef<'owner> {
	pub fn into_bitmap(self) -> Bitmap<<Self as BitmapApi>::Api, false> {
		Bitmap(unsafe { self.as_raw() }, self.api())
	}

	pub fn into_bitmap_with<Api: api::Api>(self, api: Api) -> Bitmap<Api, false> {
		Bitmap(unsafe { self.as_raw() }, api)
	}
}


impl<Api: api::Api> Bitmap<Api, true> {
	/// Allocates and returns a new `width` by `height` Bitmap filled with `bg` color.
	///
	/// Calls [`sys::ffi::playdate_graphics::newBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::newBitmap")]
	pub fn new(width: c_int, height: c_int, bg: Color) -> Result<Self, Error>
		where Api: Default {
		let api = Api::default();
		Self::new_with(api, width, height, bg)
	}

	/// Allocates and returns a new `width` by `height` Bitmap filled with `bg` color,
	/// using the given `api`.
	///
	/// Calls [`sys::ffi::playdate_graphics::newBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::newBitmap")]
	pub fn new_with(api: Api, width: c_int, height: c_int, bg: Color) -> Result<Self, Error> {
		let f = api.new_bitmap();
		let ptr = unsafe { f(width, height, bg.into()) };
		if ptr.is_null() {
			Err(Error::Alloc)
		} else {
			Ok(Self(ptr, api))
		}
	}


	/// Load a bitmap from a file.
	///
	/// Calls [`sys::ffi::playdate_graphics::loadBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::loadBitmap")]
	pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, ApiError>
		where Api: Default {
		let api = Api::default();
		Self::load_with(api, path)
	}

	/// Load a bitmap from a file,
	/// create new bitmap with given `api`.
	///
	/// Calls [`sys::ffi::playdate_graphics::loadBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::loadBitmap")]
	pub fn load_with<P: AsRef<Path>>(api: Api, path: P) -> Result<Self, ApiError> {
		let mut err = Box::new(core::ptr::null() as *const c_char);
		let out_err = Box::into_raw(err);

		let path = CString::new(path.as_ref())?;

		let f = api.load_bitmap();
		let ptr = unsafe { f(path.as_ptr() as *mut c_char, out_err as _) };
		if ptr.is_null() {
			err = unsafe { Box::from_raw(out_err) };
			if let Some(err) = fs::error::Error::from_ptr(*err) {
				Err(Error::Fs(err).into())
			} else {
				Err(Error::Alloc.into())
			}
		} else {
			Ok(Self(ptr, api))
		}
	}
}


impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
	/// Load a bitmap from a file into `self`.
	///
	/// Calls [`sys::ffi::playdate_graphics::loadIntoBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::loadIntoBitmap")]
	pub fn load_into<P: AsRef<Path>>(&mut self, path: P) -> Result<(), ApiError> {
		let mut err = Box::new(core::ptr::null() as *const c_char);
		let out_err = Box::into_raw(err);

		let path = CString::new(path.as_ref())?;

		let f = self.1.load_into_bitmap();
		unsafe { f(path.as_ptr() as *mut c_char, self.0, out_err as _) };
		err = unsafe { Box::from_raw(out_err) };
		if let Some(err) = fs::error::Error::from_ptr(*err) {
			Err(Error::Fs(err).into())
		} else {
			Ok(())
		}
	}
}


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

impl<Api: api::Api + Clone> Clone for Bitmap<Api, true> {
	/// Allocates and returns a new `Bitmap` that is an exact copy of `self`,
	/// __not a reference__.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::copyBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::copyBitmap")]
	fn clone(&self) -> Self {
		let f = self.1.copy_bitmap();
		let ptr = unsafe { f(self.0) };
		if ptr.is_null() {
			panic!("{}: bitmap clone", Error::Alloc)
		} else {
			Self(ptr, self.1.clone())
		}
	}
}


impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
	/// Clears bitmap, filling with the given `bg` color.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::clearBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::clearBitmap")]
	pub fn clear(&self, bg: Color) {
		let f = self.1.clear_bitmap();
		unsafe { f(self.0, bg.into()) };
	}


	/// Returns `(width, height)` of the bitmap.
	///
	/// Can return error if there is no bitmap-data or any internal error occurred.
	///
	/// Calls [`sys::ffi::playdate_graphics::getBitmapData`].
	#[doc(alias = "sys::ffi::playdate_graphics::getBitmapData")]
	pub fn size(&self) -> (c_int, c_int) {
		let mut width: c_int = 0;
		let mut height: c_int = 0;
		let mut row_bytes: c_int = 0;

		let f = self.1.get_bitmap_data();
		unsafe {
			f(
			  self.0,
			  &mut width,
			  &mut height,
			  &mut row_bytes,
			  core::ptr::null_mut(),
			  core::ptr::null_mut(),
			)
		};

		(width, height)
	}

	/// Returns mutable borrow of bitmap-data by this bitmap.
	///
	/// Calls [`sys::ffi::playdate_graphics::getBitmapData`].
	#[doc(alias = "sys::ffi::playdate_graphics::getBitmapData")]
	pub fn bitmap_data<'bitmap>(&'bitmap mut self) -> BitmapData<'bitmap> {
		let mut width: c_int = 0;
		let mut height: c_int = 0;
		let mut row_bytes: c_int = 0;


		let mut boxed_data = Box::new(core::ptr::null_mut());
		let data = Box::into_raw(boxed_data);
		let mut boxed_mask = Box::new(core::ptr::null_mut());
		let mask = Box::into_raw(boxed_mask);

		let f = self.1.get_bitmap_data();
		unsafe { f(self.0, &mut width, &mut height, &mut row_bytes, mask, data) };

		let len = row_bytes * height;

		boxed_data = unsafe { Box::from_raw(data) };
		boxed_mask = unsafe { Box::from_raw(mask) };

		// get mask:
		let mask = {
			if !boxed_mask.is_null() && !(*boxed_mask).is_null() {
				let mask = unsafe { core::slice::from_raw_parts_mut::<u8>(*boxed_mask, len as usize) };
				Some(mask)
			} else {
				None
			}
		};

		// get data:
		let data = unsafe { core::slice::from_raw_parts_mut::<u8>(*boxed_data, len as usize) };

		BitmapData { width,
		             height,
		             row_bytes,
		             mask,
		             data }
	}


	/// Sets a mask image for the bitmap.
	/// The set mask must be the same size as the `self` bitmap.
	///
	/// Calls [`sys::ffi::playdate_graphics::setBitmapMask`].
	#[doc(alias = "sys::ffi::playdate_graphics::setBitmapMask")]
	pub fn set_mask<Api2: api::Api, const FREE: bool>(&self, mask: &mut Bitmap<Api2, FREE>) -> Result<(), Error> {
		// TODO: investigate is it correct "res == 0 => Ok"
		let f = self.1.set_bitmap_mask();
		let res = unsafe { f(self.0, mask.0) };
		if res == 0 { Ok(()) } else { Err(Error::InvalidMask) }
	}

	/// Gets a mask image for the given bitmap.
	/// If the image doesn’t have a mask, returns None.
	///
	/// Clones inner api-access.
	///
	/// Calls [`sys::ffi::playdate_graphics::getBitmapMask`].
	#[doc(alias = "sys::ffi::playdate_graphics::getBitmapMask")]
	#[inline(always)]
	pub fn mask(&self) -> Option<Bitmap<Api, false>>
		where Api: Clone {
		self.mask_with(self.1.clone())
	}

	/// Gets a mask image for the given bitmap.
	/// If the image doesn’t have a mask, returns None.
	///
	/// Produced `Bitmap` uses passed `api` api-access.
	///
	/// Calls [`sys::ffi::playdate_graphics::getBitmapMask`].
	#[doc(alias = "sys::ffi::playdate_graphics::getBitmapMask")]
	// XXX: investigate is it should be free-on-drop?
	pub fn mask_with<NewApi: api::Api>(&self, api: NewApi) -> Option<Bitmap<NewApi, false>> {
		let f = self.1.get_bitmap_mask();
		let ptr = unsafe { f(self.0) };
		if !ptr.is_null() {
			Some(Bitmap(ptr, api))
		} else {
			None
		}
	}

	/// Returns a new, rotated and scaled Bitmap based on the bitmap.
	///
	/// Calls [`sys::ffi::playdate_graphics::rotatedBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::rotatedBitmap")]
	#[inline(always)]
	pub fn rotated_clone(&self,
	                     rotation: c_float,
	                     x_scale: c_float,
	                     y_scale: c_float)
	                     -> Result<Bitmap<Api, true>, Error>
		where Api: Clone
	{
		self.rotated_clone_with(self.1.clone(), rotation, x_scale, y_scale)
	}

	/// Returns a new, rotated and scaled Bitmap based on the bitmap using given `api`.
	///
	/// Calls [`sys::ffi::playdate_graphics::rotatedBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::rotatedBitmap")]
	pub fn rotated_clone_with<NewApi: api::Api>(&self,
	                                            api: NewApi,
	                                            rotation: c_float,
	                                            x_scale: c_float,
	                                            y_scale: c_float)
	                                            -> Result<Bitmap<NewApi, true>, Error>
		where Api: Clone
	{
		let mut alloced_size: c_int = 0;
		let alloced_size_ref = &mut alloced_size;
		let f = self.1.rotated_bitmap();
		let ptr = unsafe { f(self.0, rotation, x_scale, y_scale, alloced_size_ref) };

		if alloced_size == 0 || ptr.is_null() {
			Err(Error::Alloc)
		} else {
			Ok(Bitmap(ptr, api))
		}
	}


	/// Draws `self` with its upper-left corner at location `x`, `y`,
	/// using the given `flip` orientation.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::drawBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::drawBitmap")]
	#[inline(always)]
	pub fn draw(&self, x: c_int, y: c_int, flip: BitmapFlip) {
		let f = self.1.draw_bitmap();
		unsafe { f(self.0, x, y, flip) }
	}

	/// Draws `self` with its upper-left corner at location `x`, `y`
	/// __tiled inside a `width` by `height` rectangle__.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::tileBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::tileBitmap")]
	#[inline(always)]
	pub fn draw_tiled(&self, x: c_int, y: c_int, width: c_int, height: c_int, flip: BitmapFlip) {
		let f = self.1.tile_bitmap();
		unsafe { f(self.0, x, y, width, height, flip) }
	}

	/// Draws the *bitmap* scaled to `x_scale` and `y_scale`
	/// then rotated by `degrees` with its center as given by proportions `center_x` and `center_y` at `x`, `y`;
	///
	/// that is:
	/// * if `center_x` and `center_y` are both 0.5 the center of the image is at (`x`,`y`),
	/// * if `center_x` and `center_y` are both 0 the top left corner of the image (before rotation) is at (`x`,`y`), etc.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::drawRotatedBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::drawRotatedBitmap")]
	#[inline(always)]
	pub fn draw_rotated(&self,
	                    x: c_int,
	                    y: c_int,
	                    degrees: c_float,
	                    center_x: c_float,
	                    center_y: c_float,
	                    x_scale: c_float,
	                    y_scale: c_float) {
		let f = self.1.draw_rotated_bitmap();
		unsafe { f(self.0, x, y, degrees, center_x, center_y, x_scale, y_scale) }
	}

	/// Draws this bitmap scaled to `x_scale` and `y_scale` with its upper-left corner at location `x`, `y`.
	///
	/// Note that flip is not available when drawing scaled bitmaps but negative scale values will achieve the same effect.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::drawScaledBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::drawScaledBitmap")]
	#[inline(always)]
	pub fn draw_scaled(&self, x: c_int, y: c_int, x_scale: c_float, y_scale: c_float) {
		let f = self.1.draw_scaled_bitmap();
		unsafe { f(self.0, x, y, x_scale, y_scale) }
	}


	/// Returns `true` if any of the opaque pixels in this bitmap when positioned at `x, y` with `flip`
	/// overlap any of the opaque pixels in `other` bitmap at `x_other`, `y_other` with `flip_other`
	/// within the non-empty `rect`,
	/// or `false` if no pixels overlap or if one or both fall completely outside of `rect`.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::checkMaskCollision`].
	#[doc(alias = "sys::ffi::playdate_graphics::checkMaskCollision")]
	#[inline(always)]
	pub fn check_mask_collision<OApi: api::Api, const OFOD: bool>(&self,
	                                                              x: c_int,
	                                                              y: c_int,
	                                                              flip: BitmapFlip,
	                                                              other: Bitmap<OApi, OFOD>,
	                                                              x_other: c_int,
	                                                              y_other: c_int,
	                                                              flip_other: BitmapFlip,
	                                                              rect: LCDRect)
	                                                              -> bool {
		let f = self.1.check_mask_collision();
		unsafe { f(self.0, x, y, flip, other.0, x_other, y_other, flip_other, rect) == 1 }
	}


	/// Returns pattern `8 x 8` from this bitmap.
	///
	/// `x, y` indicates the top left corner of the 8 x 8 pattern in bitmap's coordinates.
	///
	/// Returned pattern is owned by rust and can be dropped freely.
	///
	/// Uses [`sys::ffi::playdate_graphics::setColorToPattern`].
	#[doc(alias = "sys::ffi::playdate_graphics::setColorToPattern")]
	pub fn pattern_at(&self, x: c_int, y: c_int) -> LCDPattern {
		let mut color = LCDColor::default();
		let f = self.1.set_color_to_pattern();

		unsafe {
			f(core::ptr::addr_of_mut!(color), self.0, x, y);
			*(color as *mut u8 as *mut LCDPattern)
		}
	}

	/// Sets `color` to an `8 x 8` pattern using this bitmap.
	///
	/// `x, y` indicates the top left corner of the 8 x 8 pattern.
	///
	/// After this operation inner pointer is owned by the system.
	/// To get owned pattern use [`Bitmap::pattern_at`].
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::setColorToPattern`].
	#[doc(alias = "sys::ffi::playdate_graphics::setColorToPattern")]
	pub fn set_color_to_pattern(&self, color: &mut LCDColor, x: c_int, y: c_int) {
		let f = self.1.set_color_to_pattern();
		unsafe { f(color as _, self.0, x, y) }
	}
}


/// The data is 1 bit per pixel packed format, in MSB order; in other words,
/// the high bit of the first byte in data is the top left pixel of the image.
///
/// The `mask` data is in same format but means transparency.
pub struct BitmapData<'bitmap> {
	pub width: c_int,
	pub height: c_int,
	pub row_bytes: c_int,
	mask: Option<&'bitmap mut [u8]>,
	data: &'bitmap mut [u8],
}

impl<'bitmap> BitmapData<'bitmap> {
	pub const fn width(&self) -> c_int { self.width }
	pub const fn height(&self) -> c_int { self.height }
	pub const fn row_bytes(&self) -> c_int { self.row_bytes }
	pub fn mask(&self) -> Option<&[u8]> { self.mask.as_deref() }
	pub fn mask_mut(&mut self) -> Option<&mut [u8]> { self.mask.as_deref_mut() }
	pub const fn data(&self) -> &[u8] { self.data }
	pub fn data_mut(&mut self) -> &mut [u8] { self.data }
}

impl core::fmt::Display for BitmapData<'_> {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		write!(f, "BitmapData({}, {}", self.width(), self.height())?;
		if self.mask.is_some() {
			write!(f, ", masked)")
		} else {
			write!(f, ")")
		}
	}
}

impl core::fmt::Debug for BitmapData<'_> {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		let alternate = f.alternate();
		if alternate {
			let fmt_bd = |f: &mut core::fmt::Formatter<'_>, data: &[u8], row_len: c_int| {
				for (i, b) in data.iter().enumerate() {
					if i % row_len as usize == 0 {
						f.write_char('\n')?;
						f.write_char('\t')?;
					}
					f.write_fmt(format_args!("{b:08b} "))?;
				}
				Ok(())
			};

			write!(f, "BitmapData({}, {}", self.width(), self.height())?;
			if self.mask.is_some() {
				write!(f, ", masked")?;
			}
			write!(f, ", data:")?;
			fmt_bd(f, self.data, self.row_bytes)?;
			write!(f, ")")
		} else {
			let mut res = f.debug_struct("BitmapData");
			res.field("width", &self.width)
			   .field("height", &self.height)
			   .field("row_bytes", &self.row_bytes);
			res.field("data", &self.data).field("mask", &self.mask).finish()
		}
	}
}


//
// Global Bitmap-related methods
//

/// Only valid in the Simulator,
/// returns the debug framebuffer as a bitmap.
///
/// Returns error on device.
///
/// This function is shorthand for [`Graphics::debug_bitmap`],
/// using default ZST end-point.
///
/// Equivalent to [`sys::ffi::playdate_graphics::getDebugBitmap`].
#[doc(alias = "sys::ffi::playdate_graphics::getDebugBitmap")]
#[inline(always)]
pub fn debug_bitmap() -> Result<Bitmap<api::Default, false>, ApiError> { Graphics::Default().debug_bitmap() }

/// Returns a bitmap containing the contents of the display buffer.
///
/// __The system owns this bitmap—​do not free it.__
///
/// This function is shorthand for [`Graphics::display_buffer_bitmap`],
/// using default ZST end-point.
///
/// Equivalent to [`sys::ffi::playdate_graphics::getDisplayBufferBitmap`].
#[doc(alias = "sys::ffi::playdate_graphics::getDisplayBufferBitmap")]
#[inline(always)]
pub fn display_buffer_bitmap() -> Result<Bitmap<api::Default, false>, Error> {
	Graphics::Default().display_buffer_bitmap()
}

/// Returns a copy the contents of the working frame buffer as a bitmap.
///
/// The caller is responsible for freeing the returned bitmap, it will automatically on drop.
///
/// This function is shorthand for [`Graphics::frame_buffer_bitmap`],
/// using default ZST end-point.
///
/// Equivalent to [`sys::ffi::playdate_graphics::copyFrameBufferBitmap`].
#[doc(alias = "sys::ffi::playdate_graphics::copyFrameBufferBitmap")]
#[inline(always)]
pub fn copy_frame_buffer_bitmap() -> Result<Bitmap<api::Default, true>, Error> {
	Graphics::Default().frame_buffer_bitmap()
}


/// Sets the stencil used for drawing.
///
/// If the `tile` is `true` the stencil image will be tiled.
///
/// Tiled stencils must have width equal to a multiple of 32 pixels.
///
/// This function is shorthand for [`Graphics::set_stencil_tiled`],
/// using default ZST end-point.
///
/// Equivalent to [`sys::ffi::playdate_graphics::setStencilImage`].
#[doc(alias = "sys::ffi::playdate_graphics::setStencilImage")]
#[inline(always)]
pub fn set_stencil_tiled(image: &impl AnyBitmap, tile: bool) {
	Graphics::Default().set_stencil_tiled(image, tile)
}

/// Sets the stencil used for drawing.
/// For a tiled stencil, use [`set_stencil_tiled`] instead.
///
/// NOTE: Officially deprecated in favor of [`set_stencil_tiled`], which adds a `tile` flag
///
/// This function is shorthand for [`Graphics::set_stencil`],
/// using default ZST end-point.
///
/// Equivalent to [`sys::ffi::playdate_graphics::setStencil`].
#[doc(alias = "sys::ffi::playdate_graphics::setStencil")]
#[inline(always)]
pub fn set_stencil(image: &impl AnyBitmap) { Graphics::Default().set_stencil(image) }

/// Sets the mode used for drawing bitmaps.
///
/// Note that text drawing uses bitmaps, so this affects how fonts are displayed as well.
///
/// This function is shorthand for [`Graphics::set_draw_mode`],
/// using default ZST end-point.
///
/// Equivalent to [`sys::ffi::playdate_graphics::setDrawMode`].
#[doc(alias = "sys::ffi::playdate_graphics::setDrawMode")]
#[inline(always)]
pub fn set_draw_mode(mode: BitmapDrawMode) { Graphics::Default().set_draw_mode(mode) }

/// Push a new drawing context for drawing into the given bitmap.
///
/// If underlying ptr in the `target` is `null`, the drawing functions will use the display framebuffer.
/// This mostly should not happen, just for note.
///
/// To clear entire context use [`clear_context`].
///
/// This function is shorthand for [`Graphics::push_context`],
/// using default ZST end-point.
///
/// Equivalent to [`sys::ffi::playdate_graphics::pushContext`].
#[doc(alias = "sys::ffi::playdate_graphics::pushContext")]
#[inline(always)]
pub fn push_context(target: &impl AnyBitmap) { Graphics::Default().push_context(target) }

/// Resets drawing context for drawing into the system display framebuffer.
///
/// So drawing functions will use the display framebuffer.
///
/// This function is shorthand for [`Graphics::clear_context`],
/// using default ZST end-point.
///
/// Equivalent to [`sys::ffi::playdate_graphics::pushContext`].
#[doc(alias = "sys::ffi::playdate_graphics::pushContext")]
#[inline(always)]
pub fn clear_context() { Graphics::Default().clear_context() }

/// Pops a context off the stack (if any are left),
/// restoring the drawing settings from before the context was pushed.
///
/// This function is shorthand for [`Graphics::pop_context`],
/// using default ZST end-point.
///
/// Equivalent to [`sys::ffi::playdate_graphics::popContext`].
#[doc(alias = "sys::ffi::playdate_graphics::popContext")]
#[inline(always)]
pub fn pop_context() { Graphics::Default().pop_context() }


impl<Api: crate::api::Api> Graphics<Api> {
	/// Only valid in the Simulator,
	/// returns the debug framebuffer as a bitmap.
	///
	/// Returns error on device.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::getDebugBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::getDebugBitmap")]
	pub fn debug_bitmap(&self) -> Result<Bitmap<api::Default, false>, ApiError> {
		let f = self.0.get_debug_bitmap().ok_or_null()?;
		let ptr = unsafe { f() };
		if ptr.is_null() {
			Err(Error::Alloc.into())
		} else {
			Ok(Bitmap(ptr, Default::default()))
		}
	}

	/// Returns a bitmap containing the contents of the display buffer.
	///
	/// __The system owns this bitmap—​do not free it.__
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::getDisplayBufferBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::getDisplayBufferBitmap")]
	pub fn display_buffer_bitmap(&self) -> Result<Bitmap<api::Default, false>, Error> {
		let f = self.0.get_display_buffer_bitmap();
		let ptr = unsafe { f() };
		if ptr.is_null() {
			Err(Error::Alloc)
		} else {
			Ok(Bitmap(ptr, Default::default()))
		}
	}

	/// Returns a __copy__ the contents of the working frame buffer as a bitmap.
	///
	/// The caller is responsible for freeing the returned bitmap, it will automatically on drop.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::copyFrameBufferBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::copyFrameBufferBitmap")]
	pub fn frame_buffer_bitmap(&self) -> Result<Bitmap<api::Default, true>, Error> {
		let f = self.0.copy_frame_buffer_bitmap();
		let ptr = unsafe { f() };
		if ptr.is_null() {
			Err(Error::Alloc)
		} else {
			Ok(Bitmap(ptr, Default::default()))
		}
	}


	/// Sets the stencil used for drawing.
	///
	/// If the `tile` is `true` the stencil image will be tiled.
	///
	/// Tiled stencils must have width equal to a multiple of 32 pixels.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::setStencilImage`].
	#[doc(alias = "sys::ffi::playdate_graphics::setStencilImage")]
	pub fn set_stencil_tiled(&self, image: &impl AnyBitmap, tile: bool) {
		let f = self.0.set_stencil_image();
		unsafe { f(image.as_raw(), tile as _) };
	}

	/// Sets the stencil used for drawing.
	/// For a tiled stencil, use [`set_stencil_tiled`] instead.
	///
	/// NOTE: Officially deprecated in favor of [`set_stencil_tiled`], which adds a `tile` flag
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::setStencil`].
	#[doc(alias = "sys::ffi::playdate_graphics::setStencil")]
	pub fn set_stencil(&self, image: &impl AnyBitmap) {
		let f = self.0.set_stencil();
		unsafe { f(image.as_raw()) };
	}

	/// Sets the mode used for drawing bitmaps.
	///
	/// Note that text drawing uses bitmaps, so this affects how fonts are displayed as well.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::setDrawMode`].
	#[doc(alias = "sys::ffi::playdate_graphics::setDrawMode")]
	pub fn set_draw_mode(&self, mode: BitmapDrawMode) {
		let f = self.0.set_draw_mode();
		unsafe { f(mode) };
	}

	/// Push a new drawing context for drawing into the given bitmap.
	///
	/// If underlying ptr in the `target` is `null`, the drawing functions will use the display framebuffer.
	/// This mostly should not happen, just for note.
	///
	/// To clear entire context use [`clear_context`].
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::pushContext`].
	#[doc(alias = "sys::ffi::playdate_graphics::pushContext")]
	pub fn push_context(&self, target: &impl AnyBitmap) {
		let f = self.0.push_context();
		unsafe { f(target.as_raw()) };
	}

	/// Resets drawing context for drawing into the system display framebuffer.
	///
	/// So drawing functions will use the display framebuffer.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::pushContext`].
	#[doc(alias = "sys::ffi::playdate_graphics::pushContext")]
	pub fn clear_context(&self) {
		let f = self.0.push_context();
		unsafe { f(core::ptr::null_mut()) };
	}

	/// Pops a context off the stack (if any are left),
	/// restoring the drawing settings from before the context was pushed.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::popContext`].
	#[doc(alias = "sys::ffi::playdate_graphics::popContext")]
	pub fn pop_context(&self) {
		let f = self.0.pop_context();
		unsafe { f() };
	}
}


impl<Api: crate::api::Api> Graphics<Api> {
	/// Draws `self` with its upper-left corner at location `x`, `y`,
	/// using the given `flip` orientation.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::drawBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::drawBitmap")]
	#[inline(always)]
	pub fn draw(&self, bitmap: &impl AnyBitmap, x: c_int, y: c_int, flip: BitmapFlip) {
		let f = self.0.draw_bitmap();
		unsafe { f(bitmap.as_raw(), x, y, flip) }
	}

	/// Draws `self` with its upper-left corner at location `x`, `y`
	/// __tiled inside a `width` by `height` rectangle__.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::tileBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::tileBitmap")]
	#[inline(always)]
	pub fn draw_tiled(&self,
	                  bitmap: &impl AnyBitmap,
	                  x: c_int,
	                  y: c_int,
	                  width: c_int,
	                  height: c_int,
	                  flip: BitmapFlip) {
		let f = self.0.tile_bitmap();
		unsafe { f(bitmap.as_raw(), x, y, width, height, flip) }
	}

	/// Draws the *bitmap* scaled to `x_scale` and `y_scale`
	/// then rotated by `degrees` with its center as given by proportions `center_x` and `center_y` at `x`, `y`;
	///
	/// that is:
	/// * if `center_x` and `center_y` are both 0.5 the center of the image is at (`x`,`y`),
	/// * if `center_x` and `center_y` are both 0 the top left corner of the image (before rotation) is at (`x`,`y`), etc.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::drawRotatedBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::drawRotatedBitmap")]
	#[inline(always)]
	pub fn draw_rotated(&self,
	                    bitmap: &impl AnyBitmap,
	                    x: c_int,
	                    y: c_int,
	                    degrees: c_float,
	                    center_x: c_float,
	                    center_y: c_float,
	                    x_scale: c_float,
	                    y_scale: c_float) {
		let f = self.0.draw_rotated_bitmap();
		unsafe {
			f(
			  bitmap.as_raw(),
			  x,
			  y,
			  degrees,
			  center_x,
			  center_y,
			  x_scale,
			  y_scale,
			)
		}
	}

	/// Draws this bitmap scaled to `x_scale` and `y_scale` with its upper-left corner at location `x`, `y`.
	///
	/// Note that flip is not available when drawing scaled bitmaps but negative scale values will achieve the same effect.
	///
	/// Equivalent to [`sys::ffi::playdate_graphics::drawScaledBitmap`].
	#[doc(alias = "sys::ffi::playdate_graphics::drawScaledBitmap")]
	#[inline(always)]
	pub fn draw_scaled(&self, bitmap: &impl AnyBitmap, x: c_int, y: c_int, x_scale: c_float, y_scale: c_float) {
		let f = self.0.draw_scaled_bitmap();
		unsafe { f(bitmap.as_raw(), x, y, x_scale, y_scale) }
	}
}