mindustry logic execution, map- and schematic- parsing and rendering
support draw col <var>
| -rw-r--r-- | lemu/src/instructions/draw.rs | 78 | ||||
| -rw-r--r-- | lemu/src/parser/mod.rs | 18 |
2 files changed, 50 insertions, 46 deletions
diff --git a/lemu/src/instructions/draw.rs b/lemu/src/instructions/draw.rs index 7bcca94..1a3573a 100644 --- a/lemu/src/instructions/draw.rs +++ b/lemu/src/instructions/draw.rs @@ -62,6 +62,21 @@ macro_rules! dinstr { $(impl From<$x> for DrawInstr { fn from(v: $x) -> Self { Self::$x(v) } })+ + + impl Frozen<Drawn> for DrawInstr { + fn freeze(&self, mem: &LRegistry<'_>) -> Option<Drawn> { + Some(match self { + $(Self::$x(i) => Drawn::from(i.freeze(mem)?),)+ + }) + } + } + impl Printable for DrawInstr { + fn print(&self, info: &DebugInfo<'_>, f: &mut impl fmt::Write) -> fmt::Result { + match self { + $(Self::$x(i) => i.print(info, f)),+ + } + } + } } } @@ -72,43 +87,12 @@ dinstr! { Triangle, Clear, SetColor, + SetCol, SetStroke, Poly, LinePoly } -impl Frozen<Drawn> for DrawInstr { - fn freeze(&self, mem: &LRegistry<'_>) -> Option<Drawn> { - Some(match self { - Self::Line(i) => Drawn::from(i.freeze(mem)?), - Self::RectBordered(i) => Drawn::from(i.freeze(mem)?), - Self::RectFilled(i) => Drawn::from(i.freeze(mem)?), - Self::Triangle(i) => Drawn::from(i.freeze(mem)?), - Self::Clear(i) => Drawn::from(i.freeze(mem)?), - Self::SetColor(i) => Drawn::from(i.freeze(mem)?), - Self::SetStroke(i) => Drawn::from(i.freeze(mem)?), - Self::Poly(i) => Drawn::from(i.freeze(mem)?), - Self::LinePoly(i) => Drawn::from(i.freeze(mem)?), - }) - } -} - -impl Printable for DrawInstr { - fn print(&self, info: &DebugInfo<'_>, f: &mut impl fmt::Write) -> fmt::Result { - match self { - Self::Line(i) => i.print(info, f), - Self::RectBordered(i) => i.print(info, f), - Self::RectFilled(i) => i.print(info, f), - Self::Triangle(i) => i.print(info, f), - Self::Clear(i) => i.print(info, f), - Self::SetColor(i) => i.print(info, f), - Self::SetStroke(i) => i.print(info, f), - Self::Poly(i) => i.print(info, f), - Self::LinePoly(i) => i.print(info, f), - } - } -} - #[derive(Debug, Copy, Clone)] pub struct Clear { @@ -200,6 +184,36 @@ impl Printable for SetColor { } } +#[derive(Debug, Copy, Clone)] +pub struct SetCol { + pub col: LAddress, +} + +impl Printable for SetCol { + fn print(&self, info: &DebugInfo<'_>, f: &mut impl fmt::Write) -> fmt::Result { + write!(f, "draw col ")?; + match &info[self.col] { + crate::debug::info::VarData::Variable(v) => write!(f, "{v}"), + crate::debug::info::VarData::Constant(c) => match c { + LVar::Num(n) => write!(f, "0x{:0<6x}", *n as u32), + LVar::String(s) => write!(f, r#""{s}""#), + }, + } + } +} + +impl Frozen<SetColorD> for SetCol { + fn freeze(&self, mem: &LRegistry<'_>) -> Option<SetColorD> { + let col = mem.get(self.col).num()? as u32; + Some(SetColorD(( + (col & 0xff00_0000 >> 24) as u8, + (col & 0x00ff_0000 >> 16) as u8, + (col & 0x0000_ff00 >> 8) as u8, + (col & 0x0000_00ff) as u8, + ))) + } +} + impl Disp for SetColorD { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( diff --git a/lemu/src/parser/mod.rs b/lemu/src/parser/mod.rs index cfc0fc8..70a11f6 100644 --- a/lemu/src/parser/mod.rs +++ b/lemu/src/parser/mod.rs @@ -8,8 +8,8 @@ use super::{ executor::{ExecutorBuilderInternal, Instruction, UPInstr}, instructions::{ draw::{ - Clear, Flush, Line, LinePoly, Poly, RectBordered, RectFilled, SetColor, SetStroke, - Triangle, + Clear, Flush, Line, LinePoly, Poly, RectBordered, RectFilled, SetCol, SetColor, + SetStroke, Triangle, }, io::{Print, Read, Write}, AlwaysJump, ConditionOp, DynJump, End, Instr, Jump, MathOp1, MathOp2, Op1, Op2, Set, Stop, @@ -391,18 +391,8 @@ pub fn parse<'source, W: Wr>( executor.draw(SetColor { r, g, b, a }); } "col" => { - let col = take_int!(tok!()?)?; - let r = col & 0xff00_0000 >> 24; - let g = col & 0x00ff_0000 >> 16; - let b = col & 0x0000_ff00 >> 8; - let a = col & 0x0000_00ff; - let i = SetColor { - r: push!(const r)?, - g: push!(const g)?, - b: push!(const b)?, - a: push!(const a)?, - }; - executor.draw(i); + let col = take_numvar!(tok!()?)?; + executor.draw(SetCol { col }); } "stroke" => { let size = take_numvar!(tok!()?)?; |