-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Points to drawing order wasn't backtracking
The previous approach to collecting points to drawing order didn't account correctly for 'dead ends', i.e. pixels without a neighbor like the antenna in the car sprite included in assets directory. The code had what turned out to be a fragile workaround that really only worked for the car sprite: jumbling the order of the initial points a bit. This commit gets rid of the workaround, and handles backtracking by walking backward when we are at a pixel with no neighbor but we're not out of points.
- Loading branch information
Showing
8 changed files
with
158 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,76 @@ | ||
use bevy::{prelude::Image, render::texture::ImageType}; | ||
use edges::Edges; | ||
|
||
use raqote::*; | ||
// in an actual bevy app, you wouldn't need all this building an Image from scratch logic, | ||
// it'd be something closer to this: | ||
// `let image = image_assets.get(handle).unwrap();` | ||
// let e = Edges::from(image); | ||
fn main() { | ||
// read png as bytes and manually construct a bevy Image | ||
let image = Image::from_buffer( | ||
include_bytes!("../assets/car.png"), | ||
|
||
let boulders = Image::from_buffer( | ||
include_bytes!("../assets/boulders.png"), | ||
ImageType::Extension("png"), | ||
Default::default(), | ||
true, | ||
Default::default(), | ||
Default::default(), | ||
) | ||
.unwrap(); | ||
|
||
let more_lines = Image::from_buffer( | ||
include_bytes!("../assets/more-lines.png"), | ||
ImageType::Extension("png"), | ||
Default::default(), | ||
true, | ||
Default::default(), | ||
Default::default(), | ||
); | ||
) | ||
.unwrap(); | ||
|
||
draw_png(boulders, "boulders.png"); | ||
draw_png(more_lines, "more-lines.png"); | ||
} | ||
|
||
fn draw_png(image: Image, img_path: &str) { | ||
// get the image's edges | ||
let edges = Edges::from(image.unwrap()); | ||
println!("{:#?}", edges.single_image_edge_translated()); | ||
let edges = Edges::from(image.clone()); | ||
let scale = 8; | ||
let (width, height) = (image.width() as i32 * scale, image.height() as i32 * scale); | ||
|
||
// draw the edges to a png | ||
let mut dt = DrawTarget::new(width, height); | ||
|
||
let objects_iter = edges.multi_image_edges_raw().into_iter(); | ||
|
||
for object in objects_iter { | ||
let mut pb = PathBuilder::new(); | ||
let mut edges_iter = object.into_iter(); | ||
|
||
if let Some(first_edge) = edges_iter.next() { | ||
pb.move_to(first_edge.x * scale as f32, first_edge.y * scale as f32); | ||
for edge in edges_iter { | ||
pb.line_to(edge.x * scale as f32, edge.y * scale as f32); | ||
} | ||
} | ||
|
||
let path = pb.finish(); | ||
dt.stroke( | ||
&path, | ||
&Source::Solid(SolidSource { | ||
r: 0xff, | ||
g: 0xff, | ||
b: 0xff, | ||
a: 0xff, | ||
}), | ||
&StrokeStyle { | ||
width: 1., | ||
..StrokeStyle::default() | ||
}, | ||
&DrawOptions::new(), | ||
); | ||
} | ||
|
||
dt.write_png(format!("edges-{}", img_path)).unwrap(); | ||
_ = open::that(format!("edges-{}", img_path)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,46 @@ | ||
use edges::Edges; | ||
use raqote::*; | ||
use std::path::Path; | ||
|
||
fn main() { | ||
let image = image::open(Path::new("assets/car.png")); | ||
let edges = Edges::from(image.unwrap()); | ||
println!("{:#?}", edges.single_image_edge_translated()); | ||
draw_png("car.png"); | ||
draw_png("lines.png"); | ||
draw_png("terrain.png"); | ||
} | ||
|
||
fn draw_png(img_path: &str) { | ||
let image = &image::open(Path::new(&format!("assets/{}", img_path))).unwrap(); | ||
let edges = Edges::from(image); | ||
let scale = 8; | ||
let (width, height) = (image.width() as i32 * scale, image.height() as i32 * scale); | ||
|
||
// draw the edges to a png | ||
let mut dt = DrawTarget::new(width, height); | ||
let mut pb = PathBuilder::new(); | ||
|
||
let mut edges_iter = edges.single_image_edge_raw().into_iter(); | ||
let first_edge = edges_iter.next().unwrap(); | ||
pb.move_to(first_edge.x * scale as f32, first_edge.y * scale as f32); | ||
for edge in edges_iter { | ||
pb.line_to(edge.x * scale as f32, edge.y * scale as f32); | ||
} | ||
|
||
let path = pb.finish(); | ||
dt.stroke( | ||
&path, | ||
&Source::Solid(SolidSource { | ||
r: 0xff, | ||
g: 0xff, | ||
b: 0xff, | ||
a: 0xff, | ||
}), | ||
&StrokeStyle { | ||
width: 1., | ||
..StrokeStyle::default() | ||
}, | ||
&DrawOptions::new(), | ||
); | ||
|
||
dt.write_png(format!("edges-{}", img_path)).unwrap(); | ||
_ = open::that(format!("edges-{}", img_path)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters