From 3abcdbf10963a1f18233141591435a2c84f6189d Mon Sep 17 00:00:00 2001 From: Avi Weinstock Date: Tue, 11 May 2021 01:08:03 -0400 Subject: [PATCH] Make all the offsets for voxel minimap work properly for arbitrary sizes, and fix the memory leak. --- Cargo.lock | 11 +----- voxygen/Cargo.toml | 2 +- voxygen/src/hud/minimap.rs | 79 ++++++++++++++++---------------------- 3 files changed, 36 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d9e477859a..1ff27e939b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2415,15 +2415,6 @@ dependencies = [ "serde", ] -[[package]] -name = "inline_tweak" -version = "1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7033e97b20277cc0d043226d1940fa7719ff08d2305d1fc7421e53066d00eb4b" -dependencies = [ - "lazy_static", -] - [[package]] name = "inotify" version = "0.7.1" @@ -5653,6 +5644,7 @@ dependencies = [ name = "veloren-i18n" version = "0.9.0" dependencies = [ + "clap", "deunicode", "git2", "hashbrown", @@ -5834,7 +5826,6 @@ dependencies = [ "iced_native", "iced_winit", "image", - "inline_tweak", "itertools 0.10.0", "keyboard-keynames", "lazy_static", diff --git a/voxygen/Cargo.toml b/voxygen/Cargo.toml index 6bc04cbe06..dfd6c62396 100644 --- a/voxygen/Cargo.toml +++ b/voxygen/Cargo.toml @@ -104,7 +104,7 @@ treeculler = "0.2" tokio = { version = "1", default-features = false, features = ["rt-multi-thread"] } num_cpus = "1.0" # vec_map = { version = "0.8.2" } -inline_tweak = "1.0.2" +# inline_tweak = "1.0.2" itertools = "0.10.0" # Tracy diff --git a/voxygen/src/hud/minimap.rs b/voxygen/src/hud/minimap.rs index 3aa3ca502b..8c5afaa3f4 100644 --- a/voxygen/src/hud/minimap.rs +++ b/voxygen/src/hud/minimap.rs @@ -35,16 +35,13 @@ pub struct VoxelMinimap { image_id: img_ids::Rotations, } +const VOXEL_MINIMAP_SIDELENGTH: u32 = 512; impl VoxelMinimap { pub fn new(ui: &mut Ui) -> Self { - let mut composited = RgbaImage::new(96, 96); - for x in 0..96 { - for y in 0..96 { - composited.put_pixel( - x, - y, - image::Rgba([255 - 2 * x as u8, 255 - 2 * y as u8, 0, 64]), - ); + let mut composited = RgbaImage::new(VOXEL_MINIMAP_SIDELENGTH, VOXEL_MINIMAP_SIDELENGTH); + for x in 0..VOXEL_MINIMAP_SIDELENGTH { + for y in 0..VOXEL_MINIMAP_SIDELENGTH { + composited.put_pixel(x, y, image::Rgba([0, 0, 0, 64])); } } Self { @@ -68,7 +65,7 @@ impl VoxelMinimap { .get(Vec3::new(v.x, v.y, z)) .ok() .and_then(|block| block.get_color()) - .map(|rgb| [rgb.r, rgb.g, rgb.b, 128]) + .map(|rgb| [rgb.r, rgb.g, rgb.b, 192]) .unwrap_or([0, 0, 0, 0]) }); layers.insert(z, grid); @@ -79,34 +76,34 @@ impl VoxelMinimap { let player = client.entity(); if let Some(pos) = client.state().ecs().read_storage::().get(player) { let pos = pos.0; - let cpos: Vec2 = (pos.xy() / 32.0).as_(); - for i in -1..=1 { - for j in -1..=1 { - let coff = Vec2::new(i, j); + for x in 0..VOXEL_MINIMAP_SIDELENGTH { + for y in 0..VOXEL_MINIMAP_SIDELENGTH { + let vpos = pos.xy() + Vec2::new(x as f32, y as f32) + - VOXEL_MINIMAP_SIDELENGTH as f32 / 2.0; + let cpos: Vec2 = (vpos / 32.0).as_(); + let cmod: Vec2 = (vpos % 32.0).as_(); if let Some(grid) = self .chunk_minimaps - .get(&(cpos + coff)) + .get(&cpos) .and_then(|l| l.get(&(pos.z as i32))) { - for x in 0..32 { - for y in 0..32 { - self.composited.put_pixel( - (i + 1) as u32 * 32 + x, - (j + 1) as u32 * 32 + y, - grid.get(Vec2::new(x, y).as_()) - .map(|c| image::Rgba(*c)) - .unwrap_or(image::Rgba([0, 0, 0, 0])), - ); - } - } + self.composited.put_pixel( + x, + VOXEL_MINIMAP_SIDELENGTH - y - 1, + grid.get(cmod) + .map(|c| image::Rgba(*c)) + .unwrap_or(image::Rgba([0, 0, 0, 0])), + ); } } } - // TODO: don't leak memory, replace - self.image_id = ui.add_graphic_with_rotations(Graphic::Image( - Arc::new(DynamicImage::ImageRgba8(self.composited.clone())), - Some(Rgba::from([0.0, 0.0, 0.0, 0.0])), - )); + ui.replace_graphic( + self.image_id.none, + Graphic::Image( + Arc::new(DynamicImage::ImageRgba8(self.composited.clone())), + Some(Rgba::from([0.0, 0.0, 0.0, 0.0])), + ), + ); } } } @@ -380,25 +377,17 @@ impl<'a> Widget for MiniMap<'a> { } else { self.voxel_minimap.image_id.source_north }; - use inline_tweak::tweak; - /*let rect_src = position::Rect::from_xy_dim( + let scaling = (VOXEL_MINIMAP_SIDELENGTH as f64 / 32.0) * max_zoom / zoom; + let rect_src = position::Rect::from_xy_dim( [ - player_pos.x as f64 / TerrainChunkSize::RECT_SIZE.x as f64 + tweak!(0.0), - worldsize.y as f64 - - (player_pos.y as f64 / TerrainChunkSize::RECT_SIZE.y as f64) + tweak!(0.0), + VOXEL_MINIMAP_SIDELENGTH as f64 / 2.0, + VOXEL_MINIMAP_SIDELENGTH as f64 / 2.0, ], - [w_src / tweak!(32768.0), h_src / tweak!(32768.0)], - );*/ - let rect_src = position::Rect::from_xy_dim([tweak!(48.0), tweak!(48.0)], [ - tweak!(96.0), - tweak!(96.0), - ]); + [scaling, scaling], + ); Image::new(voxelmap_rotation) .middle_of(state.ids.mmap_frame_bg) - .w_h( - map_size.x * 3.0 * (zoom / max_zoom), - map_size.y * 3.0 * zoom / max_zoom, - ) + .w_h(map_size.x, map_size.y) .parent(state.ids.mmap_frame_bg) .source_rectangle(rect_src) .graphics_for(state.ids.map_layers[0])