Skip to content

Commit

Permalink
Fix/sprite size (#2)
Browse files Browse the repository at this point in the history
* chore: files

* feat: trim sprite

* feat: up version

* feat: update formats

* chore: refactoring and updating dependencies
  • Loading branch information
krchmkn authored Nov 14, 2024
1 parent 004b4a0 commit 72f8f3c
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 141 deletions.
80 changes: 7 additions & 73 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spriterator"
version = "0.1.0-alpha.1"
version = "0.1.0-alpha.3"
edition = "2021"
authors = ["Dmitrii Korchemkin <mail@kda.name>"]
description = "Generates sprite sheets from images in the specified directory."
Expand All @@ -13,9 +13,4 @@ readme = "README.md"

[dependencies]
image = "0.25.5"
once_cell = "1.20.2"
rayon = "1.10.0"
walkdir = "2.5.0"

[dev-dependencies]
tempfile = "3.14.0"
44 changes: 29 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@

# Spriterator

[![build](https://github.com/krchmkn/spriterator/actions/workflows/build.yml/badge.svg)](https://github.com/krchmkn/spriterator/actions/workflows/build.yml)

[Spriterator](https://crates.io/crates/spriterator) is a Rust library that generates compact sprite sheets from images in a specified directory. It arranges images row by row to minimize empty space and avoid gaps, even if the images are of different sizes. If the images exceed a specified maximum height, the library will create multiple sprite sheets.
[Spriterator](https://crates.io/crates/spriterator) is a Rust library that creates sprite sheets by combining multiple images from a specified directory into a compact format.

This library supports common image formats such as PNG, JPEG, GIF, and WebP, and it can use optional parallel processing (via `rayon`) for efficient image loading.
## Example

## Features
The following example demonstrates how to use `Spriterator` to create sprite sheets from images in a directory.

- **Recursive Directory Scanning**: Finds all images within nested directories.
- **Compact Layout with No Spacing**: Arranges images tightly in rows without gaps, regardless of size.
- **Multiple Sheets if Necessary**: Generates multiple sprite sheets when images exceed specified height limits.
- **Optional Parallel Processing**: Speeds up loading and processing of images (requires `rayon` feature).
- **Supported Formats**: Handles common image formats (`png`, `jpg`, `jpeg`, `gif`, `bmp`, `ico`, `tiff`, `webp`).
```rust
use spriterator::Spriterator;
use std::fs;
use std::path::Path;

## Usage
fn prepare_directory(path: &str) -> std::io::Result<()> {
let dir_path = Path::new(path);

Here is an example of using `Spriterator` to generate sprite sheets:
if dir_path.exists() {
fs::remove_dir_all(dir_path)?;
}

```rust
use spriterator::Spriterator;
fs::create_dir_all(dir_path)?;

Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let spriterator = Spriterator::new("path/to/images", 1024, 2048);
let ext = "png";
let output_dir = format!("/parth/to/sprites/{}", ext);

prepare_directory(output_dir.as_str())?;

let size = 1024;
let spriterator = Spriterator::new(
format!("/parth/to/images/{}", ext).as_str(),
size,
size,
);
let sprites = spriterator.generate()?;

// Save each generated sprite sheet
for (index, sprite) in sprites.iter().enumerate() {
sprite.save(format!("sprite_sheet_{}.webp", index))?;
sprite.save(format!("{}/{}.{}", output_dir, index, ext))?;
}

Ok(())
Expand Down
Loading

0 comments on commit 72f8f3c

Please sign in to comment.