From 89387d82fd25771382f26676daf0c4eaf412f99e Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Tue, 25 Oct 2022 00:39:19 +0200 Subject: [PATCH] Use more iterators --- src/support/mod.rs | 108 +++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 63 deletions(-) diff --git a/src/support/mod.rs b/src/support/mod.rs index b575e14..72bbedc 100644 --- a/src/support/mod.rs +++ b/src/support/mod.rs @@ -4,7 +4,6 @@ use glium::glutin::event_loop::{ControlFlow, EventLoop}; use glium::vertex::VertexBufferAny; use glium::{self, Display}; use std::fs; -use std::str::Split; use std::time::{Duration, Instant}; pub mod camera; @@ -77,6 +76,7 @@ fn get_altitude(height_columns: &[&str], columns_index: usize) -> f32 { const SEABED_ALTITUDE: f32 = -100.0; // seabed altitude - biggest drop * tiles in a chunk * (chunks in a row - 1) const ALTITUDE_BETWEEN_CHUNKS: f32 = SEABED_ALTITUDE - 16.17 * 250.0 * 30.0; +const CHUNK_SIZE: u32 = 1_000; /// Returns a vertex buffer that should be rendered as `TrianglesList`. pub fn load_ground(display: &Display) -> VertexBufferAny { @@ -106,24 +106,21 @@ pub fn load_ground(display: &Display) -> VertexBufferAny { println!("{}", file_name); let file_name_parts: Vec<&str> = file_name.split(' ').collect(); - const CHUNK_SIZE: u32 = 1_000; - let chunk = [ - file_name_parts[0].parse::().unwrap() * CHUNK_SIZE, - file_name_parts[1].parse::().unwrap() * CHUNK_SIZE, - ]; + let chunk = [0, 1].map(|file_name_parts_index| { + file_name_parts[file_name_parts_index] + .parse::() + .unwrap() + * CHUNK_SIZE + }); // have deleted a few height files to make it work - let height_contents = fs::read_to_string(&format!( - "Extensions/LemnosLife/Map/Altis/Ground/{}.height", - file_name - )) - .expect("Unable to load ground file!"); - - let biomes_contents = fs::read_to_string(&format!( - "Extensions/LemnosLife/Map/Altis/Biomes/{}.biomes", - file_name - )) - .expect("Unable to load biomes file!"); + let [height_contents, biomes_contents] = + [["Ground", "height"], ["Biomes", "biomes"]].map(|[folder, extension]| { + fs::read_to_string(&format!( + "Extensions/LemnosLife/Map/Altis/{folder}/{file_name}.{extension}" + )) + .expect(&format!("Unable to load {} file!", extension)) + }); let height_lines: Vec<&str> = height_contents.split('\n').rev().collect(); @@ -132,28 +129,22 @@ pub fn load_ground(display: &Display) -> VertexBufferAny { let mut index: u32 = 0; for lines_index in 0..height_lines.len() - 1 { - let height_line = height_lines[lines_index]; - let height_next_line = height_lines[lines_index + 1]; - let height_columns_split: Split = height_line.split(' '); - let height_next_columns_split: Split = height_next_line.split(' '); - let (height_columns, height_next_columns): (Vec<&str>, Vec<&str>) = - if lines_index % 2 == 0 { - ( - height_columns_split.collect(), - height_next_columns_split.collect(), - ) - } else { - ( - height_columns_split.rev().collect(), - height_next_columns_split.rev().collect(), - ) - }; + let [height_columns, height_next_columns]: [Vec<&str>; 2] = + [height_lines[lines_index], height_lines[lines_index + 1]] + .map(|height_line| height_line.split(' ')) + .map(|height_columns_split| { + if lines_index % 2 == 0 { + height_columns_split.collect() + } else { + height_columns_split.rev().collect() + } + }); let biomes_line = biomes_lines[lines_index / 2]; for columns_index in 0..height_columns.len() { - let column = get_altitude(&height_columns, columns_index); - let column_below = get_altitude(&height_next_columns, columns_index); + let [column, column_below]: [f32; 2] = [&height_columns, &height_next_columns] + .map(|height_columns| get_altitude(&height_columns, columns_index)); let biome_index = if lines_index % 2 == 0 { columns_index @@ -163,37 +154,28 @@ pub fn load_ground(display: &Display) -> VertexBufferAny { let biome = get_biome(biomes_line, biome_index); if lines_index == 0 && columns_index == 0 { - // This vertex defines the end of the wall below the ground. - vertex_data.push(Vertex { - altitude: ALTITUDE_BETWEEN_CHUNKS, - index, - biome, - chunk, - }); - // This vertex defines the cursor ready to be used. - vertex_data.push(Vertex { - altitude: column, - index, - biome, - chunk, - }); + // These vertexes respectively define the end of the wall below the ground and make the cursor ready to be used. + [ALTITUDE_BETWEEN_CHUNKS, column] + .into_iter() + .for_each(|altitude| { + vertex_data.push(Vertex { + altitude, + index, + biome, + chunk, + }) + }); } - vertex_data.push(Vertex { - altitude: column, - index, - biome, - chunk, + [column, column_below].into_iter().for_each(|altitude| { + vertex_data.push(Vertex { + altitude, + index, + biome, + chunk, + }); + index += 1 }); - index += 1; - - vertex_data.push(Vertex { - altitude: column_below, - index, - biome, - chunk, - }); - index += 1; } } // This vertex remove the wall across the chunks above the ground.