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
#[derive(Clone, Copy)]
pub struct Sprite<Bytes: ?Sized = [u8]> {
shape: [u32; 2],
bpp: BitsPerPixel,
pub indices: DrawIndices,
bytes: Bytes,
}
impl<const N: usize> Sprite<[u8; N]> {
pub const fn from_byte_array(
bytes: [u8; N],
shape: [u32; 2],
bpp: BitsPerPixel,
draw_colors: DrawIndices,
) -> Option<Self> {
let resolution = shape[0].checked_mul(shape[1]);
let capacity = N.checked_mul(1 << 3 - bpp as u32);
match (resolution, capacity) {
(Some(resolution), Some(capacity)) if resolution as usize <= capacity => unsafe {
Some(Self::from_bytes_unchecked(bytes, shape, bpp, draw_colors))
},
_ => None,
}
}
}
impl<Bytes> Sprite<Bytes> {
pub const unsafe fn from_bytes_unchecked(
bytes: Bytes,
shape: [u32; 2],
bpp: BitsPerPixel,
draw_colors: DrawIndices,
) -> Self {
Sprite {
shape,
bpp,
indices: draw_colors,
bytes,
}
}
}
impl<Bytes: AsRef<[u8]>> Sprite<Bytes> {
pub fn from_bytes(
bytes: Bytes,
shape: [u32; 2],
bpp: BitsPerPixel,
draw_colors: DrawIndices,
) -> Option<Self> {
let resolution = shape[0].checked_mul(shape[1]);
let capacity = bytes.as_ref().len().checked_mul(1 << 3 - bpp as u32);
match (resolution, capacity) {
(Some(resolution), Some(capacity)) if resolution as usize <= capacity => unsafe {
Some(Self::from_bytes_unchecked(bytes, shape, bpp, draw_colors))
},
_ => None,
}
}
}
impl<Bytes: ?Sized> Sprite<Bytes> {
pub const fn bytes(&self) -> &Bytes {
&self.bytes
}
pub const fn shape(&self) -> [u32; 2] {
self.shape
}
pub const fn width(&self) -> u32 {
self.shape[0]
}
pub const fn height(&self) -> u32 {
self.shape[1]
}
pub const fn bpp(&self) -> BitsPerPixel {
self.bpp
}
}
impl Sprite {
pub const fn view(&self, start: [u32; 2], shape: [u32; 2]) -> Option<SpriteView<'_>> {
let dst_bottom_right = match checked_add_pairs(start, shape) {
Some(it) => it,
None => return None,
};
if lt_pairs(start, self.shape) && le_pairs(dst_bottom_right, self.shape) {
Some(SpriteView {
sprite: self,
start,
shape,
})
} else {
None
}
}
pub const unsafe fn view_unchecked<'a>(
&self,
start: [u32; 2],
shape: [u32; 2],
) -> SpriteView<'_> {
SpriteView {
sprite: self,
start,
shape,
}
}
}
#[repr(u32)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum BitsPerPixel {
One,
Two,
}
#[derive(Clone, Copy)]
pub struct SpriteView<'a> {
sprite: &'a Sprite,
start: [u32; 2],
shape: [u32; 2],
}
impl<'a> SpriteView<'a> {
pub const fn sprite(&self) -> &'a Sprite {
self.sprite
}
pub const fn start(&self) -> [u32; 2] {
self.start
}
pub const fn shape(&self) -> [u32; 2] {
self.shape
}
}
#[derive(Clone, Copy, Default)]
pub struct DrawIndices(u16);
impl DrawIndices {
pub const TRANSPARENT: Self = DrawIndices(0);
pub const fn from_array(array: [DrawIndex; 4]) -> Self {
DrawIndices(
array[0] as u16
| (array[1] as u16) << 4
| (array[2] as u16) << 8
| (array[3] as u16) << 12,
)
}
pub const fn into_array(self) -> [DrawIndex; 4] {
unsafe {
[
DrawIndex::new_unchecked(self.0 & 0xf),
DrawIndex::new_unchecked(self.0 >> 4 & 0xf),
DrawIndex::new_unchecked(self.0 >> 8 & 0xf),
DrawIndex::new_unchecked(self.0 >> 12 & 0xf),
]
}
}
pub const unsafe fn from_u16_unchecked(inner: u16) -> Self {
DrawIndices(inner)
}
pub const fn into_u16(self) -> u16 {
self.0
}
}
impl From<DrawIndices> for u16 {
fn from(v: DrawIndices) -> Self {
v.0
}
}
#[derive(Clone, Copy)]
#[repr(u16)]
pub enum DrawIndex {
Transparent,
First,
Second,
Third,
Fourth,
}
impl DrawIndex {
pub const fn new(index: u16) -> Option<Self> {
if index <= 4 {
Some(unsafe { Self::new_unchecked(index) })
} else {
None
}
}
pub const unsafe fn new_unchecked(index: u16) -> Self {
core::mem::transmute(index)
}
}
impl Default for DrawIndex {
fn default() -> Self {
DrawIndex::Transparent
}
}
pub type Palette = [Color; 4];
#[repr(transparent)]
#[derive(Clone, Copy, Default)]
pub struct Color(pub u32);
impl Color {
pub const BLACK: Self = Color(0);
pub const fn blue(self) -> u8 {
(self.0 & 0xff) as u8
}
pub const fn green(self) -> u8 {
(self.0 >> 8 & 0xff) as u8
}
pub const fn red(self) -> u8 {
(self.0 >> 16 & 0xff) as u8
}
pub const fn with_blue(self, channel: u8) -> Self {
Color((self.0 & !0x0000ff) | (channel as u32))
}
pub const fn with_green(self, channel: u8) -> Self {
Color((self.0 & !0x00ff00) | (channel as u32) << 8)
}
pub const fn with_red(self, channel: u8) -> Self {
Color((self.0 & !0xff0000) | (channel as u32) << 16)
}
}
const fn checked_add_pairs(lhs: [u32; 2], rhs: [u32; 2]) -> Option<[u32; 2]> {
match [lhs[0].checked_add(rhs[0]), lhs[1].checked_add(rhs[1])] {
[Some(first), Some(second)] => Some([first, second]),
_ => None,
}
}
const fn lt_pairs(lhs: [u32; 2], rhs: [u32; 2]) -> bool {
lhs[0] < rhs[0] && lhs[1] < rhs[1]
}
const fn le_pairs(lhs: [u32; 2], rhs: [u32; 2]) -> bool {
lhs[0] <= rhs[0] && lhs[1] <= rhs[1]
}