mindustry logic execution, map- and schematic- parsing and rendering
Diffstat (limited to 'lemu/src/instructions/draw.rs')
-rw-r--r--lemu/src/instructions/draw.rs101
1 files changed, 85 insertions, 16 deletions
diff --git a/lemu/src/instructions/draw.rs b/lemu/src/instructions/draw.rs
index 365b422..2dff764 100644
--- a/lemu/src/instructions/draw.rs
+++ b/lemu/src/instructions/draw.rs
@@ -5,13 +5,14 @@ use crate::{
};
use enum_dispatch::enum_dispatch;
use fimg::Image;
+use std::fmt::{self, Display as Disp, Formatter};
pub const INSTRS: &[&str] = &[
"clear", "color", "col", "stroke", "line", "rect", "lineRect", "triangle",
];
#[enum_dispatch]
-pub trait DrawInstruction<'v> {
+pub trait DrawInstruction<'v>: Disp {
fn draw(
&self,
mem: &mut LRegistry<'v>,
@@ -28,10 +29,22 @@ pub enum DrawInstr<'v> {
DrawRectFilled(RectFilled<'v>),
DrawTriangle(Triangle<'v>),
Clear(Clear<'v>),
- SetColorDyn(SetColorDyn<'v>),
- SetColorConst(SetColorConst),
+ SetColor(SetColor<'v>),
SetStroke(SetStroke<'v>),
}
+impl Disp for DrawInstr<'_> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ match self {
+ Self::DrawLine(i) => write!(f, "{i}"),
+ Self::DrawRectBordered(i) => write!(f, "{i}"),
+ Self::DrawRectFilled(i) => write!(f, "{i}"),
+ Self::DrawTriangle(i) => write!(f, "{i}"),
+ Self::Clear(i) => write!(f, "{i}"),
+ Self::SetColor(i) => write!(f, "{i}"),
+ Self::SetStroke(i) => write!(f, "{i}"),
+ }
+ }
+}
#[derive(Debug)]
pub struct Clear<'v> {
@@ -58,14 +71,20 @@ impl<'v> DrawInstruction<'v> for Clear<'v> {
}
}
+impl Disp for Clear<'_> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ write!(f, "draw clear {} {} {} {}", self.r, self.g, self.b, self.a)
+ }
+}
+
#[derive(Debug)]
-pub struct SetColorDyn<'v> {
+pub struct SetColor<'v> {
pub r: LAddress<'v>,
pub g: LAddress<'v>,
pub b: LAddress<'v>,
pub a: LAddress<'v>,
}
-impl<'v> DrawInstruction<'v> for SetColorDyn<'v> {
+impl<'v> DrawInstruction<'v> for SetColor<'v> {
fn draw(&self, mem: &mut LRegistry<'v>, _: &mut Image<&mut [u8], 4>, state: &mut DisplayState) {
macro_rules! u8 {
($v:ident) => {
@@ -79,16 +98,9 @@ impl<'v> DrawInstruction<'v> for SetColorDyn<'v> {
}
}
-#[derive(Debug)]
-pub struct SetColorConst {
- pub r: u8,
- pub g: u8,
- pub b: u8,
- pub a: u8,
-}
-impl DrawInstruction<'_> for SetColorConst {
- fn draw(&self, _: &mut LRegistry<'_>, _: &mut Image<&mut [u8], 4>, state: &mut DisplayState) {
- state.color = (self.r, self.g, self.b, self.a);
+impl Disp for SetColor<'_> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ write!(f, "draw color {} {} {} {}", self.r, self.g, self.b, self.a)
}
}
@@ -104,6 +116,12 @@ impl<'v> DrawInstruction<'v> for SetStroke<'v> {
}
}
+impl Disp for SetStroke<'_> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ write!(f, "draw stroke {}", self.size)
+ }
+}
+
pub type Point<'v> = (LAddress<'v>, LAddress<'v>);
#[rustfmt::skip]
macro_rules! point {
@@ -125,8 +143,8 @@ pub struct Line<'v> {
pub point_a: Point<'v>,
pub point_b: Point<'v>,
}
+
impl<'v> DrawInstruction<'v> for Line<'v> {
- #[allow(unused_variables)]
fn draw(
&self,
mem: &mut LRegistry<'v>,
@@ -140,12 +158,23 @@ impl<'v> DrawInstruction<'v> for Line<'v> {
}
}
+impl Disp for Line<'_> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "draw line {} {} {} {}",
+ self.point_a.0, self.point_a.1, self.point_b.0, self.point_b.1
+ )
+ }
+}
+
#[derive(Debug)]
pub struct RectFilled<'v> {
pub position: Point<'v>,
pub width: LAddress<'v>,
pub height: LAddress<'v>,
}
+
impl<'v> DrawInstruction<'v> for RectFilled<'v> {
fn draw(
&self,
@@ -160,6 +189,16 @@ impl<'v> DrawInstruction<'v> for RectFilled<'v> {
}
}
+impl Disp for RectFilled<'_> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "draw rect {} {} {} {}",
+ self.position.0, self.position.1, self.width, self.height
+ )
+ }
+}
+
#[derive(Debug)]
pub struct RectBordered<'v> {
pub position: Point<'v>,
@@ -167,6 +206,16 @@ pub struct RectBordered<'v> {
pub height: LAddress<'v>,
}
+impl Disp for RectBordered<'_> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "draw lineRect {} {} {} {}",
+ self.position.0, self.position.1, self.width, self.height
+ )
+ }
+}
+
impl<'v> DrawInstruction<'v> for RectBordered<'v> {
fn draw(
&self,
@@ -197,6 +246,20 @@ impl<'v> DrawInstruction<'v> for Triangle<'v> {
i.tri(a, b, c, state.col());
}
}
+impl Disp for Triangle<'_> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "draw triangle {} {} {} {} {} {}",
+ self.points.0.0,
+ self.points.0.1,
+ self.points.1.0,
+ self.points.1.1,
+ self.points.2.0,
+ self.points.2.1
+ )
+ }
+}
#[derive(Debug, Default)]
pub struct Flush {
@@ -208,3 +271,9 @@ impl LInstruction<'_> for Flush {
Flow::Continue
}
}
+
+impl Disp for Flush {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ write!(f, "drawflush {}", self.display)
+ }
+}