rust/land_dump/src/main.rs
changeset 14164 1749961647b9
parent 14152 5acfdf49742d
child 15828 44b49f255e31
equal deleted inserted replaced
14163:e337e9920440 14164:1749961647b9
    11     outline_template::OutlineTemplate,
    11     outline_template::OutlineTemplate,
    12     template_based::TemplatedLandGenerator,
    12     template_based::TemplatedLandGenerator,
    13     LandGenerationParameters,
    13     LandGenerationParameters,
    14     LandGenerator
    14     LandGenerator
    15 };
    15 };
    16 use mapgen::MapGenerator;
    16 use mapgen::{
       
    17     MapGenerator,
       
    18     theme::{Theme, slice_u32_to_u8}
       
    19 };
    17 use lfprng::LaggedFibonacciPRNG;
    20 use lfprng::LaggedFibonacciPRNG;
       
    21 use land2d::Land2D;
    18 
    22 
    19 #[derive(StructOpt, Debug)]
    23 #[derive(StructOpt, Debug)]
    20 #[structopt(name = "basic")]
    24 #[structopt(name = "basic")]
    21 struct Opt {
    25 struct Opt {
    22     #[structopt(short = "s", long = "seed", default_value = "TEST_SEED")]
    26     #[structopt(short = "s", long = "seed", default_value = "TEST_SEED")]
    28     #[structopt(short = "f", long = "distance-divisor", default_value = "100")]
    32     #[structopt(short = "f", long = "distance-divisor", default_value = "100")]
    29     distance_divisor: u32,
    33     distance_divisor: u32,
    30     #[structopt(short = "i", long = "templates-file")]
    34     #[structopt(short = "i", long = "templates-file")]
    31     templates_file: Option<String>,
    35     templates_file: Option<String>,
    32     #[structopt(short = "t", long = "template-type")]
    36     #[structopt(short = "t", long = "template-type")]
    33     template_type: Option<String>
    37     template_type: Option<String>,
       
    38     #[structopt(short = "z", long = "theme-dir")]
       
    39     theme_dir: Option<String>
    34 }
    40 }
    35 
    41 
    36 fn template() -> OutlineTemplate {
    42 fn template() -> OutlineTemplate {
    37     let mut template = OutlineTemplate::new(Size::new(4096, 2048));
    43     let mut template = OutlineTemplate::new(Size::new(4096, 2048));
    38     template.islands = vec![vec![
    44     template.islands = vec![vec![
    51     seed: &[u8],
    57     seed: &[u8],
    52     distance_divisor: u32,
    58     distance_divisor: u32,
    53     skip_distort: bool,
    59     skip_distort: bool,
    54     skip_bezier: bool,
    60     skip_bezier: bool,
    55     file_name: &Path,
    61     file_name: &Path,
    56 ) -> std::io::Result<()> {
    62 ) -> std::io::Result<Land2D<u8>> {
    57     let params = LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier);
    63     let params = LandGenerationParameters::new(0 as u8, 255, distance_divisor, skip_distort, skip_bezier);
    58     let landgen = TemplatedLandGenerator::new(template.clone());
    64     let landgen = TemplatedLandGenerator::new(template.clone());
    59     let mut prng = LaggedFibonacciPRNG::new(seed);
    65     let mut prng = LaggedFibonacciPRNG::new(seed);
    60     let land = landgen.generate_land(&params, &mut prng);
    66     let land = landgen.generate_land(&params, &mut prng);
    61 
    67 
    68         .set(png::BitDepth::Eight);
    74         .set(png::BitDepth::Eight);
    69     let mut writer = encoder.write_header()?;
    75     let mut writer = encoder.write_header()?;
    70 
    76 
    71     writer.write_image_data(land.raw_pixels()).unwrap();
    77     writer.write_image_data(land.raw_pixels()).unwrap();
    72 
    78 
    73     Ok(())
    79     Ok(land)
       
    80 }
       
    81 
       
    82 fn texturize(theme_dir: &Path, land: &Land2D<u8>, output_filename: &Path) {
       
    83     let theme = Theme::load(theme_dir).unwrap();
       
    84     let texture = MapGenerator::new().make_texture(land, &theme);
       
    85 
       
    86     let file = File::create(output_filename).unwrap();
       
    87     let ref mut w = BufWriter::new(file);
       
    88 
       
    89     let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1.
       
    90     encoder
       
    91         .set(png::ColorType::RGBA)
       
    92         .set(png::BitDepth::Eight);
       
    93 
       
    94     let mut writer = encoder.write_header().unwrap();
       
    95 
       
    96     writer.write_image_data(slice_u32_to_u8(texture.as_slice())).unwrap();
    74 }
    97 }
    75 
    98 
    76 fn main() {
    99 fn main() {
    77     let opt = Opt::from_args();
   100     let opt = Opt::from_args();
    78     println!("{:?}", opt);
   101     println!("{:?}", opt);
   119             true,
   142             true,
   120             Path::new("out.before_bezier.png"),
   143             Path::new("out.before_bezier.png"),
   121         )
   144         )
   122         .unwrap();
   145         .unwrap();
   123     }
   146     }
   124     dump(
   147     let land = dump(
   125         &template,
   148         &template,
   126         opt.seed.as_str().as_bytes(),
   149         opt.seed.as_str().as_bytes(),
   127         opt.distance_divisor,
   150         opt.distance_divisor,
   128         false,
   151         false,
   129         false,
   152         false,
   130         Path::new("out.full.png"),
   153         Path::new("out.full.png"),
   131     )
   154     )
   132     .unwrap();
   155     .unwrap();
       
   156 
       
   157     if let Some(dir) = opt.theme_dir {
       
   158         texturize(
       
   159             &Path::new(&dir),
       
   160             &land,
       
   161             &Path::new("out.texture.png")
       
   162         );
       
   163     }
   133 }
   164 }