1- use core:: ops:: { Range , RangeInclusive } ;
2- use crate :: vm:: memory:: machine_state:: FillPatternFlags ;
1+ use core:: ops:: { Not , Range , RangeInclusive } ;
2+
3+ use crate :: vm:: memory:: machine_state:: { FillPatternFlags , Palette } ;
34use crate :: vm:: memory:: Memory ;
45
56#[ derive( Debug ) ]
7+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
68pub struct Painter < ' a > {
79 memory : & ' a mut Memory ,
810 fg : u8 ,
9- _bg : u8 ,
11+ bg : Option < u8 > ,
1012 clip_x : Range < i16 > ,
1113 clip_y : Range < i16 > ,
1214 camera : Vector < i16 > ,
13- _fill : [ [ bool ; 4 ] ; 4 ] ,
14- _fill_flags : FillPatternFlags ,
15+ fill : Option < [ [ bool ; 4 ] ; 4 ] > ,
1516}
1617
1718impl Painter < ' _ > {
@@ -21,27 +22,46 @@ impl Painter<'_> {
2122 let camera = memory. machine_state ( ) . get_camera_position ( ) ;
2223 let fill = memory. machine_state ( ) . fill_pattern ( ) . expand ( ) ;
2324 let fill_flags = * memory. machine_state ( ) . fill_pattern ( ) . flags ( ) ;
24- let fg = pen_color & 0xF ;
25- let bg = pen_color >> 4 ;
25+
26+ let ( fg, bg) = if fill_flags. contains ( FillPatternFlags :: REMAP_ALL ) {
27+ let pal = * memory. machine_state ( ) . palette ( Palette :: Secondary ) ;
28+ let color = pal[ ( pen_color & 0xF ) as usize ] ;
29+
30+ ( color & 0x0F , color >> 4 )
31+ } else {
32+ let pal = * memory. machine_state ( ) . palette ( Palette :: Draw ) ;
33+ let fg = pal[ ( pen_color & 0xF ) as usize ] ;
34+ let bg = pal[ ( pen_color >> 4 ) as usize ] ;
35+
36+ ( fg, bg)
37+ } ;
38+
39+ let bg = fill_flags. contains ( FillPatternFlags :: TRANSPARENT )
40+ . not ( )
41+ . then_some ( bg) ;
2642
2743 Painter {
2844 memory,
2945 fg,
30- _bg : bg,
46+ bg,
3147 clip_x : clip[ 0 ] as i16 .. clip[ 2 ] . min ( 128 ) as i16 ,
3248 clip_y : clip[ 1 ] as i16 .. clip[ 3 ] . min ( 128 ) as i16 ,
3349 camera : Vector :: from ( camera) ,
34- _fill : fill,
35- _fill_flags : fill_flags,
50+ fill,
3651 }
3752 }
3853
3954 pub fn paint ( & mut self , x : i16 , y : i16 ) {
40- let x = x. saturating_add ( self . camera . x ) ;
41- let y = y. saturating_add ( self . camera . y ) ;
55+ let x = x. saturating_sub ( self . camera . x ) ;
56+ let y = y. saturating_sub ( self . camera . y ) ;
57+
58+ self . paint_abs_impl ( x, y) ;
59+ }
60+
61+ pub fn paint_abs ( mut self , x : i16 , y : i16 ) {
4262 if !self . clip_x . contains ( & x) || !self . clip_y . contains ( & y) { return }
4363
44- self . paint_abs ( x, y) ;
64+ self . paint_abs_impl ( x, y) ;
4565 }
4666
4767 pub fn paint_range ( & mut self , x : impl IntoClip , y : impl IntoClip ) {
@@ -51,13 +71,23 @@ impl Painter<'_> {
5171
5272 for y in y {
5373 for x in x. clone ( ) {
54- self . paint_abs ( x, y) ;
74+ self . paint_abs_impl ( x, y) ;
5575 }
5676 }
5777 }
5878
59- fn paint_abs ( & mut self , x : i16 , y : i16 ) {
60- self . memory . screen ( ) . set_pixel ( x, y, self . fg ) ;
79+ fn paint_abs_impl ( & mut self , x : i16 , y : i16 ) {
80+ let col = if let Some ( fill) = self . fill && fill[ y as usize % 4 ] [ x as usize % 4 ] {
81+ if let Some ( bg) = self . bg {
82+ bg
83+ } else {
84+ return ;
85+ }
86+ } else {
87+ self . fg
88+ } ;
89+
90+ self . memory . screen ( ) . set_pixel ( x, y, col) ;
6191 }
6292}
6393
@@ -86,17 +116,17 @@ macro_rules! impl_clip {
86116 ( $typ: ty $( , $rest: ty ) * ) => {
87117 impl IntoClip for Range <$typ> {
88118 fn into_clip( self , camera: i16 ) -> Range <i16 > {
89- let start = ( self . start as i16 ) . saturating_add ( camera) ;
90- let end = ( self . end as i16 ) . saturating_add ( camera) ;
119+ let start = ( self . start as i16 ) . saturating_sub ( camera) ;
120+ let end = ( self . end as i16 ) . saturating_sub ( camera) ;
91121
92122 start..end
93123 }
94124 }
95125
96126 impl IntoClip for RangeInclusive <$typ> {
97127 fn into_clip( self , camera: i16 ) -> Range <i16 > {
98- let start = ( * self . start( ) as i16 ) . saturating_add ( camera) ;
99- let end = ( * self . end( ) as i16 ) . saturating_add ( camera) . saturating_add( 1 ) ;
128+ let start = ( * self . start( ) as i16 ) . saturating_sub ( camera) ;
129+ let end = ( * self . end( ) as i16 ) . saturating_sub ( camera) . saturating_add( 1 ) ;
100130
101131 start..end
102132 }
0 commit comments