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
//! All of our macros go in here.

/// Builds a `u32` value from the given RGBA values.
///
/// Each channel is cast into `u32`, and then they're combined into a single
/// `u32` value. If inputs aren't within the range `0..=255` you'll get some
/// unintentional output.
///
/// ```rust
/// #[macro_use]
/// extern crate retro_pixel;
///
/// fn main() {
///   const TRANSPARENT_BLACK: u32 = rgba32!(0, 0, 0, 0);
///   const SOLID_WHITE: u32 = rgba32!(255u8, 255u16, 255u32, 255.0);
///   assert_eq!(TRANSPARENT_BLACK, 0);
///   assert_eq!(SOLID_WHITE, ::std::u32::MAX);
///   // all other outputs depend on what endian the machine is,
///   // currently only little endian is supported.
///   assert_eq!(rgba32!(255, 0, 0, 0), 0x000000FF);
///   assert_eq!(rgba32!(0, 255, 0, 0), 0x0000FF00);
///   assert_eq!(rgba32!(0, 0, 255, 0), 0x00FF0000);
///   assert_eq!(rgba32!(0, 0, 0, 255), 0xFF000000);
/// }
/// ```
#[cfg(target_endian = "little")]
#[macro_export]
macro_rules! rgba32 {
  ($r:expr, $g:expr, $b:expr, $a:expr) => {
    (($a as u32) << 24) | (($b as u32) << 16) | (($g as u32) << 8) | ($r as u32)
  };
}

/// As per [`rgba32`](macro.rgba32.html), but automatically selects alpha = 255.
///
/// ```rust
/// #[macro_use]
/// extern crate retro_pixel;
///
/// fn main() {
///   assert_eq!(rgb32!(255, 0, 0), rgba32!(255, 0, 0, 255));
///   assert_eq!(rgb32!(0, 255, 0), rgba32!(0, 255, 0, 255));
///   assert_eq!(rgb32!(0, 0, 255), rgba32!(0, 0, 255, 255));
/// }
/// ```
#[cfg(target_endian = "little")]
#[macro_export]
macro_rules! rgb32 {
  ($r:expr, $g:expr, $b:expr) => {
    rgba32!($r, $g, $b, 255)
  };
}

/// Builds a `u16` value from the given RGBA values.
///
/// Each _color_ channel is cast into `u16`, and then they're combined into a
/// single `u16` value (`A1_B5_G5_R5`). If color channel aren't within the range
/// `0..=31` you'll get some unintentional output. The alpha value is given as a
/// boolean expression, and flips the highest bit on or off.
///
/// ```rust
/// #[macro_use]
/// extern crate retro_pixel;
///
/// fn main() {
///   const TRANSPARENT_BLACK: u16 = rgba16!(0, 0, 0, false);
///   const SOLID_WHITE: u16 = rgba16!(31, 0x1F, 31.0, true);
///   assert_eq!(TRANSPARENT_BLACK, 0u16);
///   assert_eq!(SOLID_WHITE, ::std::u16::MAX);
///   // all other outputs depend on what endian the machine is,
///   // currently only little endian is supported.
///   assert_eq!(rgba16!(31, 0, 0, false), 0b0000000000011111u16);
///   assert_eq!(rgba16!(0, 31, 0, false), 0b0000001111100000u16);
///   assert_eq!(rgba16!(0, 0, 31, false), 0b0111110000000000u16);
///   assert_eq!(rgba16!(0, 0, 0, true),   0b1000000000000000u16);
/// }
/// ```
#[cfg(target_endian = "little")]
#[macro_export]
macro_rules! rgba16 {
  ($r:expr, $g:expr, $b:expr, $a:expr) => {
    (($a as u16) << 15) | (($b as u16) << 10) | (($g as u16) << 5) | ($r as u16)
  };
}

/// As per [`rgba16`](macro.rgba16.html), but automatically selects alpha=true.
///
/// ```rust
/// #[macro_use]
/// extern crate retro_pixel;
///
/// fn main() {
///   assert_eq!(rgb16!(31, 0, 0), rgba16!(31, 0, 0, true));
///   assert_eq!(rgb16!(0, 31, 0), rgba16!(0, 31, 0, true));
///   assert_eq!(rgb16!(0, 0, 31), rgba16!(0, 0, 31, true));
/// }
/// ```
#[cfg(target_endian = "little")]
#[macro_export]
macro_rules! rgb16 {
  ($r:expr, $g:expr, $b:expr) => {
    rgba16!($r, $g, $b, true)
  };
}

/// Checks that an address is aligned to a 4 byte bound.
///
/// If the result is non-zero, that's how many bytes _past_ the most recent 4
/// byte bound you are.
///
/// ```rust
/// #[macro_use]
/// extern crate retro_pixel;
///
/// fn main() {
///   for x in 0 .. 100 {
///     assert_eq!(check_misalign4!(x), x % 4);
///   }
/// }
/// ```
#[macro_export]
macro_rules! check_misalign4 {
  ($ptr:expr) => {
    ($ptr as usize) & 3
  };
}

/// Checks that an address is aligned to a 8 byte bound.
///
/// If the result is non-zero, that's how many bytes _past_ the most recent 8
/// byte bound you are.
///
/// ```rust
/// #[macro_use]
/// extern crate retro_pixel;
///
/// fn main() {
///   for x in 0 .. 100 {
///     assert_eq!(check_misalign8!(x), x % 8);
///   }
/// }
/// ```
#[macro_export]
macro_rules! check_misalign8 {
  ($ptr:expr) => {
    ($ptr as usize) & 7
  };
}

/// Checks that an address is aligned to a 16 byte bound.
///
/// If the result is non-zero, that's how many bytes _past_ the most recent 16
/// byte bound you are.
///
/// ```rust
/// #[macro_use]
/// extern crate retro_pixel;
///
/// fn main() {
///   for x in 0 .. 100 {
///     assert_eq!(check_misalign16!(x), x % 16);
///   }
/// }
/// ```
#[macro_export]
macro_rules! check_misalign16 {
  ($ptr:expr) => {
    ($ptr as usize) & 15
  };
}

/// Checks that an address is aligned to a 32 byte bound.
///
/// If the result is non-zero, that's how many bytes _past_ the most recent 32
/// byte bound you are.
///
/// ```rust
/// #[macro_use]
/// extern crate retro_pixel;
///
/// fn main() {
///   for x in 0 .. 100 {
///     assert_eq!(check_misalign32!(x), x % 32);
///   }
/// }
/// ```
#[macro_export]
macro_rules! check_misalign32 {
  ($ptr:expr) => {
    ($ptr as usize) & 31
  };
}

// TODO: doc-tests, docs
#[macro_export]
macro_rules! determine_overlay {
  ($dest:ident, $src:ident, $offset:ident) => {{
    let offset_x = $offset.0;
    let offset_y = $offset.1;
    let dest_width = $dest.width() as isize;
    let dest_height = $dest.height() as isize;
    let src_width = $src.width() as isize;
    let src_height = $src.height() as isize;
    // establish that we should be drawing something at all
    if offset_x < dest_width && offset_y < dest_height && -offset_x < src_width && -offset_y < src_height {
      // determine where we'll be copying
      let dest_start_x = (offset_x).max(0) as usize;
      let dest_start_y = (offset_y).max(0) as usize;
      let src_start_x = (-offset_x).max(0) as usize;
      let src_start_y = (-offset_y).max(0) as usize;
      debug_assert!(dest_width as usize > dest_start_x);
      debug_assert!(dest_width as usize > dest_start_y);
      debug_assert!(src_width as usize > src_start_x);
      debug_assert!(src_height as usize > src_start_y);
      let clip_width = (dest_width as usize - dest_start_x).min(src_width as usize - src_start_x);
      let clip_height = (dest_height as usize - dest_start_y).min(src_height as usize - src_start_y);
      debug_assert!(clip_width > 0);
      debug_assert!(clip_height > 0);
      let clip_width = clip_width as usize;
      let clip_height = clip_height as usize;
      let src_row_start_ptr = $src.as_ptr().offset(src_start_x as isize + src_start_y as isize * $src.pitch());
      let dest_row_start_ptr = $dest.as_mut_ptr().offset(dest_start_x as isize + dest_start_y as isize * $dest.pitch());
      (clip_width, clip_height, src_row_start_ptr, dest_row_start_ptr)
    } else {
      (0, 0, ::core::ptr::null(), ::core::ptr::null_mut())
    }
  }};
}