From 70514416c2ade2abe628efbd0a629a66febdeb13 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 6 Dec 2022 19:30:03 +0100 Subject: minor stuff, store translation as i8 --- evc/src/block.rs | 6 +++--- evc/src/debug.rs | 11 +++++++---- evc/src/frame.rs | 5 +++++ evc/src/ser.rs | 12 ++++++++++++ evc/src/vec2.rs | 15 +++++++++++++++ 5 files changed, 42 insertions(+), 7 deletions(-) (limited to 'evc/src') diff --git a/evc/src/block.rs b/evc/src/block.rs index 024adb6..929793d 100644 --- a/evc/src/block.rs +++ b/evc/src/block.rs @@ -3,7 +3,7 @@ use anyhow::bail; use crate::{ pixel::Pixel, ser::{Ser, Sink, Source}, - vec2::Vec2, + vec2::{Small, Vec2}, }; #[derive(Clone, Debug)] @@ -27,7 +27,7 @@ impl Block { } Block::Reference { translation } => { sink.put(2u8)?; - sink.put(*translation)?; + sink.put(Small(*translation))?; } } Ok(()) @@ -54,7 +54,7 @@ impl Block { [a, b] })), 2 => Block::Reference { - translation: source.get()?, + translation: source.get::>()?.0, }, x => bail!("corrupt block type ({})", x), }) diff --git a/evc/src/debug.rs b/evc/src/debug.rs index cbc598e..0858fe4 100644 --- a/evc/src/debug.rs +++ b/evc/src/debug.rs @@ -25,10 +25,13 @@ impl Frame { let (mut cx, mut cy) = (sx, sy); let mut lc = 0.0; while lc < len { - self[Vec2 { - x: cx as isize, - y: cy as isize, - }] = color; + self.set( + Vec2 { + x: cx as isize, + y: cy as isize, + }, + color, + ); lc += 0.5; cx += nx * 0.5; cy += ny * 0.5; diff --git a/evc/src/frame.rs b/evc/src/frame.rs index 81390a2..3c40a3d 100644 --- a/evc/src/frame.rs +++ b/evc/src/frame.rs @@ -49,6 +49,11 @@ impl Frame { pub fn view_area_mut<'a>(&'a mut self, offset: Vec2, size: Vec2) -> View<&'a mut Frame> { View::new(self, offset, size) } + pub fn set(&mut self, pos: Vec2, color: Pixel) { + if pos.x >= 0 && pos.y >= 0 && pos.x < self.size.x && pos.y < self.size.y { + self[pos] = color + } + } } impl Index for Frame { diff --git a/evc/src/ser.rs b/evc/src/ser.rs index 61ca684..98d6706 100644 --- a/evc/src/ser.rs +++ b/evc/src/ser.rs @@ -93,6 +93,18 @@ impl Ser for u8 { Ok(buf[0]) } } +impl Ser for i8 { + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { + Ok(sink + .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 1]>(self) }) + .context("write i8")?) + } + fn read(source: &mut impl Read) -> anyhow::Result { + let mut buf = [0u8; 1]; + source.read_exact(&mut buf)?; + Ok(unsafe { std::mem::transmute_copy(&buf) }) + } +} impl Ser for u16 { fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { Ok(sink diff --git a/evc/src/vec2.rs b/evc/src/vec2.rs index b1dd1b4..8b01fdb 100644 --- a/evc/src/vec2.rs +++ b/evc/src/vec2.rs @@ -26,6 +26,21 @@ impl Ser for Vec2 { } } +pub struct Small(pub T); +impl Ser for Small { + fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { + sink.put((self.0.x as i8, self.0.y as i8)) + } + + fn read(source: &mut impl std::io::Read) -> anyhow::Result { + let (x, y): (i8, i8) = source.get()?; + Ok(Small(Vec2 { + x: x as isize, + y: y as isize, + })) + } +} + impl std::ops::Add for Vec2 { type Output = Vec2; #[inline] -- cgit v1.2.3-70-g09d2