From 12ad705fce25cf42c4d3860edda918e996786622 Mon Sep 17 00:00:00 2001 From: gered Date: Sat, 4 Jun 2022 16:26:51 -0400 Subject: [PATCH] mini refactor --- libretrogd/src/graphics/bitmap/blit.rs | 29 +++++++++++++++----------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/libretrogd/src/graphics/bitmap/blit.rs b/libretrogd/src/graphics/bitmap/blit.rs index 9332461..f071b38 100644 --- a/libretrogd/src/graphics/bitmap/blit.rs +++ b/libretrogd/src/graphics/bitmap/blit.rs @@ -274,6 +274,8 @@ impl Bitmap { if new_width as i32 <= 0 || new_height as i32 <= 0 { return; } + let half_new_width = new_width * 0.5; + let half_new_height = new_height * 0.5; let angle_cos = angle.cos(); let angle_sin = angle.sin(); @@ -284,8 +286,8 @@ impl Bitmap { let mut src_x = 0.0; let mut src_y = 0.0; - let dest_center_x = dest_x as f32 + (new_width / 2.0); - let dest_center_y = dest_y as f32 + (new_height / 2.0); + let dest_center_x = dest_x as f32 + half_new_width; + let dest_center_y = dest_y as f32 + half_new_height; for point_y in 0..new_height as i32 { let src_pixels = src.pixels_at_unchecked(src_region.x, src_region.y + src_y as i32); @@ -293,11 +295,11 @@ impl Bitmap { for point_x in 0..new_width as i32 { let pixel = src_pixels[src_x as usize]; if transparent_color.is_none() || transparent_color != Some(pixel) { - let draw_x = ((angle_cos * (point_x as f32 - (new_width / 2.0))) - - (angle_sin * (point_y as f32 - (new_height / 2.0))) + let draw_x = ((angle_cos * (point_x as f32 - half_new_width)) + - (angle_sin * (point_y as f32 - half_new_height)) + dest_center_x) as i32; - let draw_y = ((angle_cos * (point_y as f32 - (new_height / 2.0))) - + (angle_sin * (point_x as f32 - (new_width / 2.0))) + let draw_y = ((angle_cos * (point_y as f32 - half_new_height)) + + (angle_sin * (point_x as f32 - half_new_width)) + dest_center_y) as i32; // write the same pixel twice to mask some floating point issues (?) which would @@ -337,6 +339,9 @@ impl Bitmap { return; } + let half_new_width = new_width * 0.5; + let half_new_height = new_height * 0.5; + let angle_cos = angle.cos(); let angle_sin = angle.sin(); @@ -346,8 +351,8 @@ impl Bitmap { let mut src_x = 0.0; let mut src_y = 0.0; - let dest_center_x = dest_x as f32 + (new_width / 2.0); - let dest_center_y = dest_y as f32 + (new_height / 2.0); + let dest_center_x = dest_x as f32 + half_new_width; + let dest_center_y = dest_y as f32 + half_new_height; for point_y in 0..new_height as i32 { let src_pixels = src.pixels_at_unchecked(src_region.x, src_region.y + src_y as i32); @@ -355,11 +360,11 @@ impl Bitmap { for point_x in 0..new_width as i32 { let pixel = src_pixels[src_x as usize]; if transparent_color.is_none() || transparent_color != Some(pixel) { - let draw_x = ((angle_cos * (point_x as f32 - (new_width / 2.0))) - - (angle_sin * (point_y as f32 - (new_height / 2.0))) + let draw_x = ((angle_cos * (point_x as f32 - half_new_width)) + - (angle_sin * (point_y as f32 - half_new_height)) + dest_center_x) as i32; - let draw_y = ((angle_cos * (point_y as f32 - (new_height / 2.0))) - + (angle_sin * (point_x as f32 - (new_width / 2.0))) + let draw_y = ((angle_cos * (point_y as f32 - half_new_height)) + + (angle_sin * (point_x as f32 - half_new_width)) + dest_center_y) as i32; let pixel = pixel.wrapping_add(offset);