sprite: add parameters to pass start position and render target size
This commit is contained in:
+20
-15
@@ -4,26 +4,31 @@ use embedded_graphics::{
|
||||
};
|
||||
use embedded_graphics_core::draw_target::DrawTarget;
|
||||
|
||||
pub fn render_sprite_onto_ili9341<D>(ili9341: &mut D, sprite: &[u8], palette: &Palette)
|
||||
where
|
||||
pub fn render_sprite_onto_ili9341<D>(
|
||||
ili9341: &mut D,
|
||||
sprite: &[u8],
|
||||
palette: &Palette,
|
||||
start_pos: Point,
|
||||
size: usize,
|
||||
) where
|
||||
D: DrawTarget<Color = Rgb565>,
|
||||
{
|
||||
log::info!("rendering sprite...");
|
||||
let pixel_count = sprite.len() * 2;
|
||||
if pixel_count == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut src_width = 1usize;
|
||||
while src_width * src_width < pixel_count {
|
||||
src_width += 1;
|
||||
}
|
||||
let src_size = pixel_count.isqrt();
|
||||
|
||||
let target_width = 240usize;
|
||||
let scale = core::cmp::max(1, target_width / src_width);
|
||||
log::debug!("pixel count: {pixel_count}");
|
||||
log::debug!("src size: {src_size}");
|
||||
let scale = size as f64 / src_size as f64;
|
||||
|
||||
for y in 0..src_width {
|
||||
for x in 0..src_width {
|
||||
let pixel_index = y * src_width + x;
|
||||
log::debug!("scale: {scale}");
|
||||
for y in 0..src_size {
|
||||
for x in 0..src_size {
|
||||
let pixel_index = y * src_size + x;
|
||||
if pixel_index >= pixel_count {
|
||||
break;
|
||||
}
|
||||
@@ -39,10 +44,10 @@ where
|
||||
|
||||
let color = palette[color_index as usize];
|
||||
|
||||
let start_x = (x * scale) as i32;
|
||||
let start_y = (y * scale) as i32;
|
||||
let end_x = core::cmp::min(start_x + scale as i32, target_width as i32);
|
||||
let end_y = core::cmp::min(start_y + scale as i32, target_width as i32);
|
||||
let start_x = start_pos.x + (x as f64 * scale) as i32;
|
||||
let start_y = start_pos.y + (y as f64 * scale) as i32;
|
||||
let end_x = start_x + scale as i32 + 1;
|
||||
let end_y = start_y + scale as i32 + 1;
|
||||
|
||||
let width = u32::try_from(end_x - start_x).unwrap();
|
||||
let height = u32::try_from(end_y - start_y).unwrap();
|
||||
|
||||
@@ -104,7 +104,13 @@ impl Navigable for CardView {
|
||||
.map_err(PrimaryDisplayError::from)?;
|
||||
|
||||
let palette: Palette = self.card.cardtype.clone().into();
|
||||
render_sprite_onto_ili9341(display, self.card.sprite.data.as_slice(), &palette);
|
||||
render_sprite_onto_ili9341(
|
||||
display,
|
||||
self.card.sprite.data.as_slice(),
|
||||
&palette,
|
||||
Point::new(13, 10),
|
||||
215,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user