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
use crate::color::Color;
use crate::image::Image;
use crate::text::format::TextFormat;
use crate::text::pos::TextPos;
use crate::text::{chr_to_code, Text, TextSize};
use crate::Graphics;
use graphics_shapes::coord::Coord;
use std::mem::swap;
pub trait Renderable {
fn render(&self, graphics: &mut Graphics);
}
impl Graphics<'_> {
#[inline]
pub fn index(&self, x: usize, y: usize) -> usize {
(x + y * self.width) * 4
}
pub fn width(&self) -> usize {
self.width
}
pub fn height(&self) -> usize {
self.height
}
pub fn is_on_screen(&self, point: Coord) -> bool {
let x = point.x - self.translate.x;
let y = point.y - self.translate.y;
x >= 0 && y >= 0 && x < self.width as isize && y < self.height as isize
}
}
impl Graphics<'_> {
pub fn get_translate(&self) -> Coord {
self.translate
}
pub fn set_translate(&mut self, new_value: Coord) {
self.translate = new_value;
}
pub fn update_translate(&mut self, delta: Coord) {
self.translate.x += delta.x;
self.translate.y += delta.y;
}
pub fn copy_to_image(&mut self) -> Image {
let pixels = self
.buffer
.chunks_exact(4)
.map(|px| Color {
r: px[0],
g: px[1],
b: px[2],
a: px[3],
})
.collect::<Vec<Color>>();
Image::new(pixels, self.width, self.height)
.expect("Copy to image failed, please create github issue for buffer-graphics-lib")
}
pub fn get_px_for_char(x: usize, y: usize, size: TextSize) -> (usize, usize) {
let (width, height) = size.get_size();
let margin = size.get_spacing();
(x * (width + margin), y * (height + margin))
}
pub fn get_text_size(text: &str, width: usize, size: TextSize) -> (usize, usize) {
let len = text.chars().count();
let x = if len < width { len } else { width };
let y = (len as f64 / width as f64).ceil() as usize;
let (width, height) = size.get_size();
let margin = size.get_spacing();
((width + margin) * x, (height + margin) * y)
}
pub fn draw_image<P: Into<Coord>>(&mut self, xy: P, image: &Image) {
let xy = xy.into();
let mut x = 0;
for (y, row) in image.pixels.chunks_exact(image.width()).enumerate() {
for color in row {
if color.a > 0 {
self.update_pixel(xy.x as isize + x, xy.y as isize + y as isize, *color);
}
x += 1;
}
x = 0;
}
}
pub fn draw_line<P1: Into<Coord>, P2: Into<Coord>>(
&mut self,
start: P1,
end: P2,
color: Color,
) {
let mut start = start.into();
let mut end = end.into();
if start.x > end.x || start.y > end.y {
swap(&mut start, &mut end);
}
if start.x == end.x {
for y in start.y as isize..=end.y as isize {
self.update_pixel(start.x as isize, y, color);
}
} else if start.y == end.y {
for x in start.x as isize..=end.x as isize {
self.update_pixel(x, start.y as isize, color);
}
} else {
let mut delta = 0;
let x1 = start.x as isize;
let y1 = start.y as isize;
let x2 = end.x as isize;
let y2 = end.y as isize;
let dx = isize::abs(x2 - x1);
let dy = isize::abs(y2 - y1);
let dx2 = dx * 2;
let dy2 = dy * 2;
let ix: isize = if x1 < x2 { 1 } else { -1 };
let iy: isize = if y1 < y2 { 1 } else { -1 };
let mut x = x1;
let mut y = y1;
if dx >= dy {
loop {
self.update_pixel(x, y, color);
if x == x2 {
break;
}
x += ix;
delta += dy2;
if delta > dx {
y += iy;
delta -= dx2;
}
}
} else {
loop {
self.update_pixel(x, y, color);
if y == y2 {
break;
}
y += iy;
delta += dx2;
if delta > dy {
x += ix;
delta -= dy2;
}
}
}
}
}
pub fn draw_at<P: Into<Coord>>(&mut self, xy: P, shape: &dyn Renderable) {
let xy = xy.into();
self.update_translate(xy);
shape.render(self);
self.update_translate(-xy);
}
pub fn draw(&mut self, shape: &dyn Renderable) {
shape.render(self);
}
#[inline]
fn get_pixel(&mut self, x: isize, y: isize, use_translate: bool) -> Option<Color> {
let (x, y) = if use_translate {
(
x as isize + self.translate.x as isize,
y as isize + self.translate.y as isize,
)
} else {
(x as isize, y as isize)
};
if x >= 0 && y >= 0 && x < self.width as isize {
let idx = self.index(x as usize, y as usize);
if idx < self.buffer.len() {
return Some(Color::rgb(
self.buffer[idx],
self.buffer[idx + 1],
self.buffer[idx + 2],
));
}
}
None
}
#[inline]
pub fn update_pixel(&mut self, x: isize, y: isize, color: Color) {
if color.a == 255 {
self.set_pixel(x, y, color);
} else {
self.blend_pixel(x, y, color);
}
}
pub fn clear(&mut self, color: Color) {
self.buffer.chunks_exact_mut(4).for_each(|px| {
px[0] = color.r;
px[1] = color.g;
px[2] = color.b;
px[3] = color.a;
});
}
pub fn draw_letter(&mut self, pos: (isize, isize), chr: char, size: TextSize, color: Color) {
self.draw_ascii_letter(pos, chr_to_code(chr), size, color);
}
pub fn draw_ascii_letter(
&mut self,
pos: (isize, isize),
code: u8,
size: TextSize,
color: Color,
) {
if code == 32 || code == 9 {
return;
}
let (width, height) = size.get_size();
let px = size.get_px_ascii(code);
for x in 0..width {
for y in 0..height {
let i = x + y * width;
if px[i] {
self.update_pixel(x as isize + pos.0, y as isize + pos.1, color);
}
}
}
}
pub fn draw_ascii<P: Into<TextPos>, F: Into<TextFormat>>(
&mut self,
text: &[Vec<u8>],
pos: P,
format: F,
) {
let format = format.into();
let size = format.size();
let color = format.color();
let (start_x, start_y) = pos.into().to_px(size);
let per_x = size.get_size().0 + size.get_spacing();
let per_y = size.get_size().1 + size.get_spacing();
for (y, line) in text.iter().enumerate() {
let y = (y * per_y) as isize;
for (x, char) in line.iter().enumerate() {
let x = (x * per_x) as isize;
self.draw_ascii_letter((start_x + x, start_y + y), *char, size, color);
}
}
}
pub fn draw_text<P: Into<TextPos>, F: Into<TextFormat>>(
&mut self,
text: &str,
pos: P,
format: F,
) {
let text = Text::new(text, pos.into(), format.into());
text.render(self);
}
#[inline]
pub fn blend_pixel(&mut self, x: isize, y: isize, color: Color) {
let x = x + self.translate.x as isize;
let y = y + self.translate.y as isize;
if x >= 0 && y >= 0 && x < self.width as isize {
if let Some(base) = self.get_pixel(x, y, false) {
let new_color = base.blend(color);
let idx = self.index(x as usize, y as usize);
self.buffer[idx] = new_color.r;
self.buffer[idx + 1] = new_color.g;
self.buffer[idx + 2] = new_color.b;
}
}
}
#[inline]
pub fn set_pixel(&mut self, x: isize, y: isize, color: Color) {
let x = x + self.translate.x as isize;
let y = y + self.translate.y as isize;
if x >= 0 && y >= 0 && x < self.width as isize {
let idx = self.index(x as usize, y as usize);
if idx < self.buffer.len() {
self.buffer[idx] = color.r;
self.buffer[idx + 1] = color.g;
self.buffer[idx + 2] = color.b;
self.buffer[idx + 3] = color.a;
}
}
}
}
#[cfg(test)]
mod test {
use crate::Graphics;
use graphics_shapes::coord::Coord;
#[test]
fn is_inside() {
let mut buf = [0; 400];
let mut graphics = Graphics::new(&mut buf, 10, 10).unwrap();
assert!(graphics.is_on_screen(Coord { x: 1, y: 1 }));
assert!(graphics.is_on_screen(Coord { x: 9, y: 9 }));
assert!(graphics.is_on_screen(Coord { x: 0, y: 0 }));
assert!(!graphics.is_on_screen(Coord { x: 10, y: 10 }));
assert!(!graphics.is_on_screen(Coord { x: 4, y: -1 }));
assert!(!graphics.is_on_screen(Coord { x: -1, y: 4 }));
graphics.set_translate(Coord { x: 2, y: -1 });
assert!(graphics.is_on_screen(Coord { x: 4, y: 4 }));
assert!(graphics.is_on_screen(Coord { x: 4, y: 0 }));
assert!(!graphics.is_on_screen(Coord { x: 0, y: 0 }));
assert!(!graphics.is_on_screen(Coord { x: 4, y: 9 }));
}
}