tilestopng/
tilestopng.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
3
4use anyhow::{Error, Result};
5use clap::Parser;
6use freenukum::tilecache::{FileProperties, TileCache};
7use freenukum::tileprovider::TileProvider;
8use sdl2::image::SaveSurface;
9use std::fs::create_dir_all;
10use std::path::PathBuf;
11
12/// Convert a set of original Duke Nukem 1 tile files to a set of png files.
13#[derive(Parser, Debug)]
14struct Arguments {
15    /// The path to the directory with the files that should be converted.
16    /// The files must be lowercase, such as `anim0.dn1` to `anim5.dn1`,
17    /// `border.dn1`, `font1.dn1`, `font2.dn1`, `numbers.dn1`,
18    /// `object0.dn1` to `object2.dn1` or `solid0.dn1` to `solid3.dn1`.
19    directory: PathBuf,
20
21    /// The directory where the extracted tiles should be placed.
22    /// The tiles are numbered as `tile_000.png` to `tile_102.png`.
23    /// If the directory doesn't exist, the program will attempt to create it.
24    destination: PathBuf,
25}
26
27fn main() -> Result<()> {
28    let args = Arguments::parse();
29
30    let file_properties = FileProperties::get_all();
31    let max_tiles =
32        file_properties.iter().map(|p| p.num_tiles).sum::<usize>();
33
34    let tilecache = TileCache::load_from_path(&args.directory)?;
35
36    create_dir_all(&args.destination)?;
37
38    for i in 0..max_tiles {
39        let tile = tilecache.get_tile(i).unwrap();
40        let filename = format!("tile_{:04}.png", i);
41        tile.save(args.destination.join(filename))
42            .map_err(Error::msg)?;
43    }
44    Ok(())
45}