Skip to content

Commit

Permalink
use thread for image/gif saving
Browse files Browse the repository at this point in the history
  • Loading branch information
ScanMountGoat committed Oct 9, 2022
1 parent 2d58e51 commit f6fe934
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added
* Added an option to add and delete constraints to the Hlpb Editor
* Added validation for model.numdlb entries.
* Add basic support for rendering animations to a GIF or image sequence.

### Changed
* Improved alignment and consistenty of UI elements.
Expand Down
54 changes: 32 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,18 @@ fn render_animation_to_gif(
renderer: &mut SsbhRenderer,
file: std::path::PathBuf,
) {
// TODO: Rendering modifies the app, so this needs to be on the UI thread for now.
let rect = app.viewport_rect(size.width, size.height, window.scale_factor() as f32);
let images = render_animation_sequence(renderer, app, size.width, size.height, rect);
let file_out = std::fs::File::create(&file).unwrap();
let mut encoder = image::codecs::gif::GifEncoder::new(file_out);
if let Err(e) = encoder.encode_frames(images.into_iter().map(image::Frame::new)) {
error!("Error saving GIF to {:?}: {}", file, e);
}

// TODO: Add progress indication.
std::thread::spawn(move || {
let file_out = std::fs::File::create(&file).unwrap();
let mut encoder = image::codecs::gif::GifEncoder::new(file_out);
if let Err(e) = encoder.encode_frames(images.into_iter().map(image::Frame::new)) {
error!("Error saving GIF to {:?}: {}", file, e);
}
});
}

fn render_animation_to_image_sequence(
Expand All @@ -661,26 +666,31 @@ fn render_animation_to_image_sequence(
renderer: &mut SsbhRenderer,
file: std::path::PathBuf,
) {
// TODO: Rendering modifies the app, so this needs to be on the UI thread for now.
let rect = app.viewport_rect(size.width, size.height, window.scale_factor() as f32);
let images = render_animation_sequence(renderer, app, size.width, size.height, rect);
for (i, image) in images.iter().enumerate() {
// TODO: Find a simpler way to do this.
let file_name = file
.with_extension("")
.file_name()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or("img".to_owned());
let extension = file
.extension()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or("png".to_owned());
let output = file
.with_file_name(file_name + &i.to_string())
.with_extension(extension);
if let Err(e) = image.save(output) {
error!("Error saving image to {:?}: {}", file, e);

// TODO: Add progress indication.
std::thread::spawn(move || {
for (i, image) in images.iter().enumerate() {
// TODO: Find a simpler way to do this.
let file_name = file
.with_extension("")
.file_name()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or("img".to_owned());
let extension = file
.extension()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or("png".to_owned());
let output = file
.with_file_name(file_name + &i.to_string())
.with_extension(extension);
if let Err(e) = image.save(output) {
error!("Error saving image to {:?}: {}", file, e);
}
}
}
});
}

fn update_lighting(renderer: &mut SsbhRenderer, app: &mut SsbhApp) {
Expand Down

0 comments on commit f6fe934

Please sign in to comment.