Add runnable examples for reading & writing

This commit is contained in:
Omar Shehata 2019-08-18 17:44:32 -04:00
parent 418b46ebbc
commit 27a0368897
4 changed files with 38 additions and 1 deletions

View File

@ -20,9 +20,11 @@ use std::fs::File;
let file = File::open("file.swf").unwrap();
let reader = BufReader::new(file);
let swf = swf::read_swf(reader).unwrap();
println!("The SWF has {} frames", swf.num_frames);
println!("The SWF has {} frames", swf.header.num_frames);
```
Try `cargo run --example reading` in this repository to run this example.
## Writing
```rust,no_run
@ -48,6 +50,8 @@ let writer = std::io::BufWriter::new(file);
swf::write_swf(&swf, writer).unwrap();
```
Try `cargo run --example writing` in this repository to run this example.
## License
Licensed under either of

10
examples/reading.rs Normal file
View File

@ -0,0 +1,10 @@
use std::io::BufReader;
use std::fs::File;
fn main() {
let file = File::open("tests/swfs/SimpleRedBackground.swf").unwrap();
let reader = BufReader::new(file);
let swf = swf::read_swf(reader).unwrap();
println!("The SWF has {} frame(s).", swf.header.num_frames);
println!("The SWF has {} tag(s).", swf.tags.len());
}

23
examples/writing.rs Normal file
View File

@ -0,0 +1,23 @@
use swf::*;
fn main() {
let swf = Swf {
header: Header {
version: 6,
compression: Compression::Zlib,
stage_size: Rectangle {
x_min: Twips::from_pixels(0.0), x_max: Twips::from_pixels(400.0),
y_min: Twips::from_pixels(0.0), y_max: Twips::from_pixels(400.0)
},
frame_rate: 60.0,
num_frames: 1,
},
tags: vec![
Tag::SetBackgroundColor(Color { r: 255, g: 0, b: 0, a: 255 }),
Tag::ShowFrame
]
};
let file = std::fs::File::create("tests/swfs/SimpleRedBackground.swf").unwrap();
let writer = std::io::BufWriter::new(file);
swf::write_swf(&swf, writer).unwrap();
}

Binary file not shown.