packer_test/
packer-test.rs

1extern crate image;
2extern crate texture_packer;
3
4use std::{
5    fs::{self, File},
6    path::Path,
7};
8use texture_packer::{
9    exporter::ImageExporter, importer::ImageImporter, texture::Texture, MultiTexturePacker,
10    TexturePacker, TexturePackerConfig,
11};
12
13fn main() {
14    fs::create_dir_all("target/output").unwrap();
15
16    //
17    // Perform texture packing
18    //
19    let config = TexturePackerConfig {
20        max_width: 400,
21        max_height: 400,
22        allow_rotation: false,
23        texture_outlines: true,
24        border_padding: 2,
25        force_max_dimensions: false,
26        ..Default::default()
27    };
28
29    // single atlas
30    {
31        let mut packer = TexturePacker::new_skyline(config);
32
33        for i in 1..11 {
34            let name = format!("{}.png", i);
35            let path = format!("examples/assets/{}", name);
36            let path = Path::new(&path);
37            let texture = ImageImporter::import_from_file(path)
38                .expect("Unable to import file. Run this example with --features=\"png\"");
39
40            packer.pack_own(name, texture).unwrap();
41        }
42
43        //
44        // Print the information
45        //
46        println!("Dimensions : {}x{}", packer.width(), packer.height());
47        for (name, frame) in packer.get_frames() {
48            println!("  {:7} : {:?}", name, frame.frame);
49        }
50
51        //
52        // Save the result
53        //
54        let exporter = ImageExporter::export(&packer, None).unwrap();
55        let mut file = File::create("target/output/skyline-packer-output.png").unwrap();
56        exporter
57            .write_to(&mut file, image::ImageFormat::Png)
58            .unwrap();
59
60        println!("Output texture stored in {:?}", file);
61    }
62
63    // multiple atlases
64    {
65        let mut packer = MultiTexturePacker::new_skyline(config);
66
67        for i in 1..11 {
68            let name = format!("{}.png", i);
69            let path = format!("examples/assets/{}", name);
70            let path = Path::new(&path);
71            let texture = ImageImporter::import_from_file(path).unwrap();
72
73            packer.pack_own(format!("A{}", i), texture.clone()).unwrap();
74            packer.pack_own(format!("B{}", i), texture).unwrap();
75        }
76
77        for (i, page) in packer.get_pages().iter().enumerate() {
78            //
79            // Print the information
80            //
81            println!("#{} | Dimensions : {}x{}", i, page.width(), page.height());
82            for (name, frame) in page.get_frames() {
83                println!("#{} |   {:7} : {:?}", i, name, frame.frame);
84            }
85
86            //
87            // Save the result
88            //
89            let exporter = ImageExporter::export(page, None).unwrap();
90            let mut file = File::create(&format!(
91                "target/output/skyline-multi-packer-output-{}.png",
92                i
93            ))
94            .unwrap();
95            exporter
96                .write_to(&mut file, image::ImageFormat::Png)
97                .unwrap();
98
99            println!("Multi output texture stored in {:?}", file);
100        }
101    }
102}