Make all the offsets for voxel minimap work properly for arbitrary sizes, and fix the memory leak.

This commit is contained in:
Avi Weinstock 2021-05-11 01:08:03 -04:00
parent 7980230b7f
commit 937815d8c3
3 changed files with 36 additions and 56 deletions

11
Cargo.lock generated
View File

@ -2415,15 +2415,6 @@ dependencies = [
"serde", "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]] [[package]]
name = "inotify" name = "inotify"
version = "0.7.1" version = "0.7.1"
@ -5659,6 +5650,7 @@ dependencies = [
name = "veloren-i18n" name = "veloren-i18n"
version = "0.9.0" version = "0.9.0"
dependencies = [ dependencies = [
"clap",
"deunicode", "deunicode",
"git2", "git2",
"hashbrown", "hashbrown",
@ -5841,7 +5833,6 @@ dependencies = [
"iced_native", "iced_native",
"iced_winit", "iced_winit",
"image", "image",
"inline_tweak",
"itertools 0.10.0", "itertools 0.10.0",
"keyboard-keynames", "keyboard-keynames",
"lazy_static", "lazy_static",

View File

@ -104,7 +104,7 @@ treeculler = "0.2"
tokio = { version = "1", default-features = false, features = ["rt-multi-thread"] } tokio = { version = "1", default-features = false, features = ["rt-multi-thread"] }
num_cpus = "1.0" num_cpus = "1.0"
# vec_map = { version = "0.8.2" } # vec_map = { version = "0.8.2" }
inline_tweak = "1.0.2" # inline_tweak = "1.0.2"
itertools = "0.10.0" itertools = "0.10.0"
# Tracy # Tracy

View File

@ -35,16 +35,13 @@ pub struct VoxelMinimap {
image_id: img_ids::Rotations, image_id: img_ids::Rotations,
} }
const VOXEL_MINIMAP_SIDELENGTH: u32 = 512;
impl VoxelMinimap { impl VoxelMinimap {
pub fn new(ui: &mut Ui) -> Self { pub fn new(ui: &mut Ui) -> Self {
let mut composited = RgbaImage::new(96, 96); let mut composited = RgbaImage::new(VOXEL_MINIMAP_SIDELENGTH, VOXEL_MINIMAP_SIDELENGTH);
for x in 0..96 { for x in 0..VOXEL_MINIMAP_SIDELENGTH {
for y in 0..96 { for y in 0..VOXEL_MINIMAP_SIDELENGTH {
composited.put_pixel( composited.put_pixel(x, y, image::Rgba([0, 0, 0, 64]));
x,
y,
image::Rgba([255 - 2 * x as u8, 255 - 2 * y as u8, 0, 64]),
);
} }
} }
Self { Self {
@ -68,7 +65,7 @@ impl VoxelMinimap {
.get(Vec3::new(v.x, v.y, z)) .get(Vec3::new(v.x, v.y, z))
.ok() .ok()
.and_then(|block| block.get_color()) .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]) .unwrap_or([0, 0, 0, 0])
}); });
layers.insert(z, grid); layers.insert(z, grid);
@ -79,34 +76,34 @@ impl VoxelMinimap {
let player = client.entity(); let player = client.entity();
if let Some(pos) = client.state().ecs().read_storage::<comp::Pos>().get(player) { if let Some(pos) = client.state().ecs().read_storage::<comp::Pos>().get(player) {
let pos = pos.0; let pos = pos.0;
let cpos: Vec2<i32> = (pos.xy() / 32.0).as_(); for x in 0..VOXEL_MINIMAP_SIDELENGTH {
for i in -1..=1 { for y in 0..VOXEL_MINIMAP_SIDELENGTH {
for j in -1..=1 { let vpos = pos.xy() + Vec2::new(x as f32, y as f32)
let coff = Vec2::new(i, j); - VOXEL_MINIMAP_SIDELENGTH as f32 / 2.0;
let cpos: Vec2<i32> = (vpos / 32.0).as_();
let cmod: Vec2<i32> = (vpos % 32.0).as_();
if let Some(grid) = self if let Some(grid) = self
.chunk_minimaps .chunk_minimaps
.get(&(cpos + coff)) .get(&cpos)
.and_then(|l| l.get(&(pos.z as i32))) .and_then(|l| l.get(&(pos.z as i32)))
{ {
for x in 0..32 {
for y in 0..32 {
self.composited.put_pixel( self.composited.put_pixel(
(i + 1) as u32 * 32 + x, x,
(j + 1) as u32 * 32 + y, VOXEL_MINIMAP_SIDELENGTH - y - 1,
grid.get(Vec2::new(x, y).as_()) grid.get(cmod)
.map(|c| image::Rgba(*c)) .map(|c| image::Rgba(*c))
.unwrap_or(image::Rgba([0, 0, 0, 0])), .unwrap_or(image::Rgba([0, 0, 0, 0])),
); );
} }
} }
} }
} ui.replace_graphic(
} self.image_id.none,
// TODO: don't leak memory, replace Graphic::Image(
self.image_id = ui.add_graphic_with_rotations(Graphic::Image(
Arc::new(DynamicImage::ImageRgba8(self.composited.clone())), Arc::new(DynamicImage::ImageRgba8(self.composited.clone())),
Some(Rgba::from([0.0, 0.0, 0.0, 0.0])), Some(Rgba::from([0.0, 0.0, 0.0, 0.0])),
)); ),
);
} }
} }
} }
@ -380,25 +377,17 @@ impl<'a> Widget for MiniMap<'a> {
} else { } else {
self.voxel_minimap.image_id.source_north self.voxel_minimap.image_id.source_north
}; };
use inline_tweak::tweak; let scaling = (VOXEL_MINIMAP_SIDELENGTH as f64 / 32.0) * max_zoom / zoom;
/*let rect_src = position::Rect::from_xy_dim( let rect_src = position::Rect::from_xy_dim(
[ [
player_pos.x as f64 / TerrainChunkSize::RECT_SIZE.x as f64 + tweak!(0.0), VOXEL_MINIMAP_SIDELENGTH as f64 / 2.0,
worldsize.y as f64 - VOXEL_MINIMAP_SIDELENGTH as f64 / 2.0,
(player_pos.y as f64 / TerrainChunkSize::RECT_SIZE.y as f64) + tweak!(0.0),
], ],
[w_src / tweak!(32768.0), h_src / tweak!(32768.0)], [scaling, scaling],
);*/ );
let rect_src = position::Rect::from_xy_dim([tweak!(48.0), tweak!(48.0)], [
tweak!(96.0),
tweak!(96.0),
]);
Image::new(voxelmap_rotation) Image::new(voxelmap_rotation)
.middle_of(state.ids.mmap_frame_bg) .middle_of(state.ids.mmap_frame_bg)
.w_h( .w_h(map_size.x, map_size.y)
map_size.x * 3.0 * (zoom / max_zoom),
map_size.y * 3.0 * zoom / max_zoom,
)
.parent(state.ids.mmap_frame_bg) .parent(state.ids.mmap_frame_bg)
.source_rectangle(rect_src) .source_rectangle(rect_src)
.graphics_for(state.ids.map_layers[0]) .graphics_for(state.ids.map_layers[0])