@@ -59,7 +59,7 @@ impl<'a> Renderer<'a> {
5959 // Init BF, color = black (0)
6060 let back_buffer = vec ! [ 0 ; buffer_size] ;
6161 Self {
62- framebuffer : framebuffer ,
62+ framebuffer,
6363 back_buffer,
6464 pixel_size,
6565 clear_color : color:: BLACK ,
@@ -87,7 +87,7 @@ impl<'a> Renderer<'a> {
8787 let value: u32 = ( ( color. r as u32 ) << self . framebuffer . red_mask_shift ( ) )
8888 | ( ( color. g as u32 ) << self . framebuffer . green_mask_shift ( ) )
8989 | ( ( color. b as u32 ) << self . framebuffer . blue_mask_shift ( ) ) ;
90- return value;
90+ value
9191 } else if self . bpp == 24 {
9292 color. to_u32 ( false )
9393 } else {
@@ -96,6 +96,11 @@ impl<'a> Renderer<'a> {
9696 }
9797
9898 /// Draw pixel to BF
99+ ///
100+ /// # Safety
101+ ///
102+ /// This function is unsafe because it does not check if the coordinates are within the
103+ /// framebuffer boundaries. The caller must ensure that `x` and `y` are valid.
99104 #[ inline( always) ]
100105 pub unsafe fn set_pixel_raw_unchecked ( & mut self , x : u64 , y : u64 , color : & color:: Color ) {
101106 let offset = self . get_buffer_offset ( x, y) ;
@@ -177,17 +182,15 @@ impl<'a> Renderer<'a> {
177182 pub fn clear ( & mut self ) {
178183 let width = self . framebuffer . width ( ) ;
179184 let height = self . framebuffer . height ( ) ;
180- let color = self . clear_color . clone ( ) ;
185+ let color = self . clear_color ;
181186 // Optimize clear operation
182187 let masked_clear_color = self . mask_color ( & color) ;
183188 let pixel_bytes = masked_clear_color. to_le_bytes ( ) ; // To byte array
184189 let bytes_to_fill = & pixel_bytes[ ..self . pixel_size ] ;
185190 for y in 0 ..height {
186191 for x in 0 ..width {
187192 let offset = self . get_buffer_offset ( x, y) ;
188- for i in 0 ..self . pixel_size {
189- self . back_buffer [ offset + i] = bytes_to_fill[ i] ;
190- }
193+ self . back_buffer [ offset..offset + self . pixel_size ] . copy_from_slice ( bytes_to_fill) ;
191194 }
192195 }
193196
@@ -200,8 +203,8 @@ impl<'a> Renderer<'a> {
200203 /* ======== Drawing Example functions ======== */
201204 /// Draw a line
202205 pub fn draw_line ( & mut self , p1 : Pixel , p2 : Pixel , color : color:: Color ) {
203- let dx_abs = ( ( p2. x as i64 - p1. x as i64 ) . abs ( ) ) as u64 ;
204- let dy_abs = ( ( p2. y as i64 - p1. y as i64 ) . abs ( ) ) as u64 ;
206+ let dx_abs = ( p2. x as i64 - p1. x as i64 ) . unsigned_abs ( ) ;
207+ let dy_abs = ( p2. y as i64 - p1. y as i64 ) . unsigned_abs ( ) ;
205208 let steep = dy_abs > dx_abs;
206209 let ( mut x1, mut y1) = p1. to_coord ( ) ;
207210 let ( mut x2, mut y2) = p2. to_coord ( ) ;
@@ -214,7 +217,7 @@ impl<'a> Renderer<'a> {
214217 core:: mem:: swap ( & mut y1, & mut y2) ;
215218 }
216219 let dx = x2 - x1;
217- let dy = ( y2 as i64 - y1 as i64 ) . abs ( ) as u64 ;
220+ let dy = ( y2 as i64 - y1 as i64 ) . unsigned_abs ( ) ;
218221 let mut error = ( dx / 2 ) as i64 ;
219222 let y_step = if y1 < y2 { 1 } else { -1 } ;
220223 let mut y = y1 as i64 ;
@@ -348,7 +351,7 @@ impl<'a> Renderer<'a> {
348351 }
349352
350353 /// Draw a rectangle
351- pub fn draw_rect ( & mut self , pixel : Pixel , width : u64 , height : u64 , color : color:: Color ) -> ( ) {
354+ pub fn draw_rect ( & mut self , pixel : Pixel , width : u64 , height : u64 , color : color:: Color ) {
352355 let ( x, y) = pixel. to_coord ( ) ;
353356 let x2 = x + width;
354357 let y2 = y + height;
@@ -364,9 +367,9 @@ impl<'a> Renderer<'a> {
364367 let ( x_min, y_min) = pixel. to_coord ( ) ;
365368 let x_max = x_min + width;
366369 let y_max = y_min + height;
367- let x_start = x_min. max ( 0 ) ;
370+ let x_start = x_min;
368371 let x_end = x_max. min ( self . width ( ) - 1 ) ;
369- let y_start = y_min. max ( 0 ) ;
372+ let y_start = y_min;
370373 let y_end = y_max. min ( self . height ( ) - 1 ) ;
371374 for y in y_start..=y_end {
372375 for x in x_start..=x_end {
0 commit comments