spritesheet_generator/
spritesheet_generator.rs

1use std::fs::File;
2
3use serde_json;
4use image;
5use texture_packer::texture::Texture;
6use texture_packer::{TexturePacker, TexturePackerConfig};
7use texture_packer::exporter::ImageExporter;
8
9
10use file_texture;
11use spritesheet;
12use spritesheet_generator_config;
13
14pub fn generate(config: spritesheet_generator_config::SpritesheetGeneratorConfig) {
15    // Initial setup
16    let input_folder = config.input_folder;
17    let output_folder = config.output_folder;
18    let output_file_name = config.output_file_name;
19
20    // Perform texture packing
21    let config = TexturePackerConfig {
22        max_width: config.max_width,
23        max_height: config.max_height,
24        border_padding: config.border_padding,
25        allow_rotation: config.allow_rotation,
26        texture_outlines: false,
27        ..Default::default()
28    };
29    let mut packer = TexturePacker::new_skyline(config);
30    for file_textures in file_texture::find_all(input_folder) {
31        packer.pack_own(file_textures.file.name, file_textures.texture);
32    }
33
34    // Save Json
35    let atlas = spritesheet::to_atlas(
36        packer.get_frames(),
37        packer.width(),
38        packer.height(),
39    );
40    let json_path = format!("{}{}.json", output_folder, output_file_name);
41    let json_file = File::create(json_path).unwrap();
42    serde_json::to_writer_pretty(json_file, &atlas).unwrap();
43
44    // Save Image
45    let exporter = ImageExporter::export(&packer).unwrap();
46    let image_path = format!("{}{}.png", output_folder, output_file_name);
47    let mut image_file = File::create(image_path).unwrap();
48    exporter.write_to(&mut image_file, image::PNG).unwrap();
49}