1
- //! Playdate Bitmap API
1
+ //! Playdate bitmap API
2
2
3
3
use core:: ffi:: c_char;
4
4
use core:: ffi:: c_float;
@@ -117,14 +117,22 @@ impl<'owner> BitmapRef<'owner> {
117
117
}
118
118
119
119
120
- // TODO: Properly document methods of `Bitmap`.
121
120
impl < Api : api:: Api > Bitmap < Api , true > {
121
+ /// Allocates and returns a new `width` by `height` Bitmap filled with `bg` color.
122
+ ///
123
+ /// Calls [`sys::ffi::playdate_graphics::newBitmap`].
124
+ #[ doc( alias = "sys::ffi::playdate_graphics::newBitmap" ) ]
122
125
pub fn new ( width : c_int , height : c_int , bg : Color ) -> Result < Self , Error >
123
126
where Api : Default {
124
127
let api = Api :: default ( ) ;
125
128
Self :: new_with ( api, width, height, bg)
126
129
}
127
130
131
+ /// Allocates and returns a new `width` by `height` Bitmap filled with `bg` color,
132
+ /// using the given `api`.
133
+ ///
134
+ /// Calls [`sys::ffi::playdate_graphics::newBitmap`].
135
+ #[ doc( alias = "sys::ffi::playdate_graphics::newBitmap" ) ]
128
136
pub fn new_with ( api : Api , width : c_int , height : c_int , bg : Color ) -> Result < Self , Error > {
129
137
let f = api. new_bitmap ( ) ;
130
138
let ptr = unsafe { f ( width, height, bg. into ( ) ) } ;
@@ -137,14 +145,20 @@ impl<Api: api::Api> Bitmap<Api, true> {
137
145
138
146
139
147
/// Load a bitmap from a file.
148
+ ///
149
+ /// Calls [`sys::ffi::playdate_graphics::loadBitmap`].
150
+ #[ doc( alias = "sys::ffi::playdate_graphics::loadBitmap" ) ]
140
151
pub fn load < P : AsRef < Path > > ( path : P ) -> Result < Self , ApiError >
141
152
where Api : Default {
142
153
let api = Api :: default ( ) ;
143
154
Self :: load_with ( api, path)
144
155
}
145
156
146
157
/// Load a bitmap from a file,
147
- /// create new bitmap with given api-access-point.
158
+ /// create new bitmap with given `api`.
159
+ ///
160
+ /// Calls [`sys::ffi::playdate_graphics::loadBitmap`].
161
+ #[ doc( alias = "sys::ffi::playdate_graphics::loadBitmap" ) ]
148
162
pub fn load_with < P : AsRef < Path > > ( api : Api , path : P ) -> Result < Self , ApiError > {
149
163
let mut err = Box :: new ( core:: ptr:: null ( ) as * const c_char ) ;
150
164
let out_err = Box :: into_raw ( err) ;
@@ -168,6 +182,10 @@ impl<Api: api::Api> Bitmap<Api, true> {
168
182
169
183
170
184
impl < Api : api:: Api , const FOD : bool > Bitmap < Api , FOD > {
185
+ /// Load a bitmap from a file into `self`.
186
+ ///
187
+ /// Calls [`sys::ffi::playdate_graphics::loadIntoBitmap`].
188
+ #[ doc( alias = "sys::ffi::playdate_graphics::loadIntoBitmap" ) ]
171
189
pub fn load_into < P : AsRef < Path > > ( & mut self , path : P ) -> Result < ( ) , ApiError > {
172
190
let mut err = Box :: new ( core:: ptr:: null ( ) as * const c_char ) ;
173
191
let out_err = Box :: into_raw ( err) ;
@@ -197,6 +215,11 @@ impl<Api: api::Api, const FOD: bool> Drop for Bitmap<Api, FOD> {
197
215
}
198
216
199
217
impl < Api : api:: Api + Clone > Clone for Bitmap < Api , true > {
218
+ /// Allocates and returns a new `Bitmap` that is an exact copy of `self`,
219
+ /// __not a reference__.
220
+ ///
221
+ /// Equivalent to [`sys::ffi::playdate_graphics::copyBitmap`].
222
+ #[ doc( alias = "sys::ffi::playdate_graphics::copyBitmap" ) ]
200
223
fn clone ( & self ) -> Self {
201
224
let f = self . 1 . copy_bitmap ( ) ;
202
225
let ptr = unsafe { f ( self . 0 ) } ;
@@ -210,12 +233,20 @@ impl<Api: api::Api + Clone> Clone for Bitmap<Api, true> {
210
233
211
234
212
235
impl < Api : api:: Api , const FOD : bool > Bitmap < Api , FOD > {
236
+ /// Clears bitmap, filling with the given `bg` color.
237
+ ///
238
+ /// Equivalent to [`sys::ffi::playdate_graphics::clearBitmap`].
239
+ #[ doc( alias = "sys::ffi::playdate_graphics::clearBitmap" ) ]
213
240
pub fn clear ( & self , bg : Color ) {
214
241
let f = self . 1 . clear_bitmap ( ) ;
215
242
unsafe { f ( self . 0 , bg. into ( ) ) } ;
216
243
}
217
244
218
245
246
+ /// Returns mutable borrow of bitmap-data by this bitmap.
247
+ ///
248
+ /// Calls [`sys::ffi::playdate_graphics::getBitmapData`].
249
+ #[ doc( alias = "sys::ffi::playdate_graphics::getBitmapData" ) ]
219
250
pub fn bitmap_data < ' bitmap > ( & ' bitmap mut self ) -> Result < BitmapData < ' bitmap > , Error > {
220
251
let mut width: c_int = 0 ;
221
252
let mut height: c_int = 0 ;
@@ -262,7 +293,11 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
262
293
}
263
294
264
295
265
- /// Sets a mask image for the given bitmap. The set mask must be the same size as the target bitmap.
296
+ /// Sets a mask image for the bitmap.
297
+ /// The set mask must be the same size as the `self` bitmap.
298
+ ///
299
+ /// Calls [`sys::ffi::playdate_graphics::setBitmapMask`].
300
+ #[ doc( alias = "sys::ffi::playdate_graphics::setBitmapMask" ) ]
266
301
pub fn set_mask < Api2 : api:: Api , const FREE : bool > ( & self , mask : & mut Bitmap < Api2 , FREE > ) -> Result < ( ) , Error > {
267
302
// TODO: investigate is it correct "res == 0 => Ok"
268
303
let f = self . 1 . set_bitmap_mask ( ) ;
@@ -278,6 +313,9 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
278
313
/// If the image doesn’t have a mask, returns None.
279
314
///
280
315
/// Clones inner api-access.
316
+ ///
317
+ /// Calls [`sys::ffi::playdate_graphics::getBitmapMask`].
318
+ #[ doc( alias = "sys::ffi::playdate_graphics::getBitmapMask" ) ]
281
319
#[ inline( always) ]
282
320
pub fn mask ( & self ) -> Option < Bitmap < Api , false > >
283
321
where Api : Clone {
@@ -288,6 +326,9 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
288
326
/// If the image doesn’t have a mask, returns None.
289
327
///
290
328
/// Produced `Bitmap` uses passed `api` api-access.
329
+ ///
330
+ /// Calls [`sys::ffi::playdate_graphics::getBitmapMask`].
331
+ #[ doc( alias = "sys::ffi::playdate_graphics::getBitmapMask" ) ]
291
332
// XXX: investigate is it should be free-on-drop?
292
333
pub fn mask_with < NewApi : api:: Api > ( & self , api : NewApi ) -> Option < Bitmap < NewApi , false > > {
293
334
let f = self . 1 . get_bitmap_mask ( ) ;
@@ -299,7 +340,10 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
299
340
}
300
341
}
301
342
302
- /// Returns a new, rotated and scaled Bitmap based on the given bitmap.
343
+ /// Returns a new, rotated and scaled Bitmap based on the bitmap.
344
+ ///
345
+ /// Calls [`sys::ffi::playdate_graphics::rotatedBitmap`].
346
+ #[ doc( alias = "sys::ffi::playdate_graphics::rotatedBitmap" ) ]
303
347
#[ inline( always) ]
304
348
pub fn rotated_clone ( & self ,
305
349
rotation : c_float ,
@@ -311,6 +355,10 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
311
355
self . rotated_clone_with ( self . 1 . clone ( ) , rotation, x_scale, y_scale)
312
356
}
313
357
358
+ /// Returns a new, rotated and scaled Bitmap based on the bitmap using given `api`.
359
+ ///
360
+ /// Calls [`sys::ffi::playdate_graphics::rotatedBitmap`].
361
+ #[ doc( alias = "sys::ffi::playdate_graphics::rotatedBitmap" ) ]
314
362
pub fn rotated_clone_with < NewApi : api:: Api > ( & self ,
315
363
api : NewApi ,
316
364
rotation : c_float ,
@@ -332,12 +380,22 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
332
380
}
333
381
334
382
383
+ /// Draws `self` with its upper-left corner at location `x`, `y`,
384
+ /// using the given `flip` orientation.
385
+ ///
386
+ /// Equivalent to [`sys::ffi::playdate_graphics::drawBitmap`].
387
+ #[ doc( alias = "sys::ffi::playdate_graphics::drawBitmap" ) ]
335
388
#[ inline( always) ]
336
389
pub fn draw ( & self , x : c_int , y : c_int , flip : BitmapFlip ) {
337
390
let f = self . 1 . draw_bitmap ( ) ;
338
391
unsafe { f ( self . 0 , x, y, flip) }
339
392
}
340
393
394
+ /// Draws `self` with its upper-left corner at location `x`, `y`
395
+ /// __tiled inside a `width` by `height` rectangle__.
396
+ ///
397
+ /// Equivalent to [`sys::ffi::playdate_graphics::tileBitmap`].
398
+ #[ doc( alias = "sys::ffi::playdate_graphics::tileBitmap" ) ]
341
399
#[ inline( always) ]
342
400
pub fn draw_tiled ( & self , x : c_int , y : c_int , width : c_int , height : c_int , flip : BitmapFlip ) {
343
401
let f = self . 1 . tile_bitmap ( ) ;
@@ -352,6 +410,7 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
352
410
/// * if `center_x` and `center_y` are both 0 the top left corner of the image (before rotation) is at (`x`,`y`), etc.
353
411
///
354
412
/// Equivalent to [`sys::ffi::playdate_graphics::drawRotatedBitmap`].
413
+ #[ doc( alias = "sys::ffi::playdate_graphics::drawRotatedBitmap" ) ]
355
414
#[ inline( always) ]
356
415
pub fn draw_rotated ( & self ,
357
416
x : c_int ,
@@ -365,15 +424,26 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
365
424
unsafe { f ( self . 0 , x, y, degrees, center_x, center_y, x_scale, y_scale) }
366
425
}
367
426
427
+ /// Draws this bitmap scaled to `x_scale` and `y_scale` with its upper-left corner at location `x`, `y`.
428
+ ///
429
+ /// Note that flip is not available when drawing scaled bitmaps but negative scale values will achieve the same effect.
430
+ ///
431
+ /// Equivalent to [`sys::ffi::playdate_graphics::drawScaledBitmap`].
432
+ #[ doc( alias = "sys::ffi::playdate_graphics::drawScaledBitmap" ) ]
368
433
#[ inline( always) ]
369
434
pub fn draw_scaled ( & self , x : c_int , y : c_int , x_scale : c_float , y_scale : c_float ) {
370
435
let f = self . 1 . draw_scaled_bitmap ( ) ;
371
436
unsafe { f ( self . 0 , x, y, x_scale, y_scale) }
372
437
}
373
438
374
439
375
- /// 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`,
440
+ /// Returns `true` if any of the opaque pixels in this bitmap when positioned at `x, y` with `flip`
441
+ /// overlap any of the opaque pixels in `other` bitmap at `x_other`, `y_other` with `flip_other`
442
+ /// within the non-empty `rect`,
376
443
/// or `false` if no pixels overlap or if one or both fall completely outside of `rect`.
444
+ ///
445
+ /// Equivalent to [`sys::ffi::playdate_graphics::checkMaskCollision`].
446
+ #[ doc( alias = "sys::ffi::playdate_graphics::checkMaskCollision" ) ]
377
447
#[ inline( always) ]
378
448
pub fn check_mask_collision < OApi : api:: Api , const OFOD : bool > ( & self ,
379
449
x : c_int ,
@@ -390,16 +460,21 @@ impl<Api: api::Api, const FOD: bool> Bitmap<Api, FOD> {
390
460
}
391
461
392
462
393
- /// Sets `color` to an 8 x 8 pattern using this bitmap.
463
+ /// Sets `color` to an ` 8 x 8` pattern using this bitmap.
394
464
/// `x, y` indicates the top left corner of the 8 x 8 pattern.
465
+ ///
466
+ /// Equivalent to [`sys::ffi::playdate_graphics::setColorToPattern`].
467
+ #[ doc( alias = "sys::ffi::playdate_graphics::setColorToPattern" ) ]
395
468
pub fn set_color_to_pattern ( & self , color : & mut LCDColor , x : c_int , y : c_int ) {
396
469
let f = self . 1 . set_color_to_pattern ( ) ;
397
470
unsafe { f ( color as _ , self . 0 , x, y) }
398
471
}
399
472
}
400
473
401
474
402
- /// 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.
475
+ /// The data is 1 bit per pixel packed format, in MSB order; in other words,
476
+ /// the high bit of the first byte in data is the top left pixel of the image.
477
+ ///
403
478
/// The `mask` data is in same format but means transparency.
404
479
pub struct BitmapData < ' bitmap > {
405
480
pub width : c_int ,
@@ -441,6 +516,10 @@ pub fn debug_bitmap() -> Result<Bitmap<api::Default, false>, ApiError> {
441
516
}
442
517
}
443
518
519
+ /// Returns a bitmap containing the contents of the display buffer.
520
+ ///
521
+ /// __The system owns this bitmap—do not free it.__
522
+ ///
444
523
/// Equivalent to [`sys::ffi::playdate_graphics::getDisplayBufferBitmap`].
445
524
#[ doc( alias = "sys::ffi::playdate_graphics::getDisplayBufferBitmap" ) ]
446
525
pub fn display_buffer_bitmap ( ) -> Result < Bitmap < api:: Default , false > , Error > {
0 commit comments