Skip to content

Commit 2586129

Browse files
authored
Merge pull request #42 from CleanCut/collider-fixups
Sprite loading improvements
2 parents 4a5b9b8 + 697e0a5 commit 2586129

File tree

6 files changed

+38
-26
lines changed

6 files changed

+38
-26
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<!-- next-header -->
22
## [Unreleased] - ReleaseDate
33

4+
### New
5+
6+
- The `collider` example can be installed globally with `cargo install rusty_engine --example collider` and run in the root of your own project with `collider assets/some-image.png`.
7+
8+
### Fixed
9+
10+
- The `collider` example can now load sprites from anywhere inside `assets/`, instead of only from inside `assets/sprite/`.
11+
412
## [5.0.1] - 2022-04-11
513

614
### Improved

examples/collider.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! cargo run --release --example collider path/to/some_image.png
44
//!
5-
//! ...where that image is somewhere under assets/sprite
5+
//! ...where that image is somewhere under assets/
66
77
use std::path::PathBuf;
88

@@ -21,27 +21,32 @@ impl Default for GameState {
2121
}
2222

2323
fn main() {
24+
// Some trickiness to make assets load relative to the current working directory, which
25+
// makes using it from `cargo install rusty_engine --example collider` possible.
26+
// This takes advantage of bevy's hard-coded asset loading behavior, and may break in future
27+
// bevy versions.
28+
std::env::set_var(
29+
"CARGO_MANIFEST_DIR",
30+
std::env::var("PWD").unwrap_or_default(),
31+
);
32+
// Make engine logging a bit quieter since we've got console instructions we want folks to see.
33+
std::env::set_var("RUST_LOG", "error");
2434
// We need an image file to work with, so the user must pass in the path of an image
2535
let args = std::env::args().skip(1).collect::<Vec<_>>();
2636
if args.len() != 1 {
2737
println!(
28-
"Please pass in the path of an image inside the `assets/sprite` directory! For example:\n\
38+
"Please pass in the path of an image inside the `assets/` directory! For example:\n\
2939
cargo run --release --example collider assets/sprite/racing/car_green.png"
3040
);
3141
std::process::exit(1);
3242
}
3343

34-
// If the user passed in `assets/sprite/something...` then we need to strip `assets/` (the asset loader will prepend `assets/`)
44+
// If the user passed in `assets/something...` then we need to strip `assets/` (the asset loader will prepend `assets/`)
3545
let mut path = PathBuf::from(args[0].clone());
36-
if path.starts_with("assets/sprite") {
37-
path = path
38-
.strip_prefix("assets")
39-
.unwrap()
40-
.strip_prefix("sprite")
41-
.unwrap()
42-
.to_path_buf();
43-
}
44-
if !(PathBuf::from("assets/sprite").join(&path)).exists() {
46+
if path.starts_with("assets") {
47+
path = path.strip_prefix("assets").unwrap().to_path_buf();
48+
}
49+
if !(PathBuf::from("assets").join(&path)).exists() {
4550
println!("Couldn't find the file {}", path.to_string_lossy());
4651
std::process::exit(1);
4752
}
@@ -57,18 +62,18 @@ fn main() {
5762

5863
// Print instructions to the console
5964
println!("\n\
60-
Instructions:\n\
65+
Collider Instructions:\n\
6166
\n\
6267
1-9: Set Zoom level (sprite scale) to this amount.\n\
6368
Del/Backspace: Delete existing collider.*\n\
6469
Mouse Click: Add a collider point. Add points in a CLOCKWISE direction. Must be a CONVEX polygon to work correctly!\n\
6570
- Hold SHIFT while clicking the mouse to change the LAST point added.\n\
66-
c: Generate a circle collider at the current radius (radius starts at 16.0)*\n\
71+
c: Generate a circle collider at the current radius (radius defaults to 16.0)*\n\
6772
+: Increase the radius by 0.5 and generate a circle collider*\n\
6873
-: Decrease the radius by 0.5 and generate a circle collider*\n\
6974
w: Write the collider file. NOTE: This will overwrite the existing collider file (if any), so make a backup if you need the old one!\n\
7075
\n\
71-
*These all delete the current collider in memory. Only writing the collider file will affect the on-disk collider.\n");
76+
*This command deletes the current collider in memory, but only writing the collider file will affect the collider file on disk.");
7277

7378
// Tell the user to look to the console for the instructions
7479
let msg = game.add_text("msg", "See console output for instructions.");

src/game.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub fn add_sprites(commands: &mut Commands, asset_server: &Res<AssetServer>, eng
187187
for (_, sprite) in engine.sprites.drain() {
188188
// Create the sprite
189189
let transform = sprite.bevy_transform();
190-
let texture_path = PathBuf::from("sprite").join(&sprite.filepath);
190+
let texture_path = sprite.filepath.clone();
191191
commands.spawn().insert(sprite).insert_bundle(SpriteBundle {
192192
texture: asset_server.load(texture_path),
193193
transform,

src/sprite.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Sprite {
6363
let filepath = file_or_preset.into();
6464
let mut collider_filepath = filepath.clone();
6565
collider_filepath.set_extension("collider");
66-
let actual_collider_filepath = PathBuf::from("assets/sprite").join(&collider_filepath);
66+
let actual_collider_filepath = PathBuf::from("assets").join(&collider_filepath);
6767
let collider = if actual_collider_filepath.exists() {
6868
read_collider_from_file(actual_collider_filepath.as_path())
6969
} else {
@@ -103,7 +103,7 @@ impl Sprite {
103103
return false;
104104
}
105105
// Bevy's asset system is relative from the assets/ subdirectory, so we must be too
106-
let filepath = PathBuf::from("assets/sprite").join(self.collider_filepath.clone());
106+
let filepath = PathBuf::from("assets").join(self.collider_filepath.clone());
107107
let mut fh = match File::create(filepath) {
108108
Ok(fh) => fh,
109109
Err(e) => {

tutorial/src/10-assets.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ assets
2727
└── rolling
2828
```
2929

30-
All audio files should be stored in `assets/audio`*. The asset pack divides sounds into `music` and `sfx` subdirectories.
30+
You can organize your own custom files wherever you like, but the asset pack will always be organized like this:
3131

32-
All font files should be stored in `assets/font`*.
32+
- Audio files in `assets/audio`. The asset pack divides sounds into `music` and `sfx` subdirectories.
33+
- Font files in `assets/font`.
34+
- Sprites (images and colliders) in `assets/sprite`.
3335

34-
All sprites should be stored in `assets/sprite`*.
35-
36-
*Additional subdirectories may be used for organization inside of these directories, as the asset pack does in some cases.

tutorial/src/65-sprite-collider.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ All of the sprite presets in the game already have colliders, so you only have t
3434

3535
If you create a new sprite using your own image, and you want it to produce `CollisionEvent`s, then you need to create a collider for that sprite.
3636

37-
Creating colliders from scratch is quite tedius, so there is an "example" program called `collider` that you can use to create a collider! To run `collider`, clone the [`rusty_engine`](https://github.com/CleanCut/rusty_engine/) repository, place your image file in the `assets/sprite` directory (let's call it `db.png`), and then run:
37+
Creating colliders from scratch is quite tedius, so there is an "example" program called `collider` that you can use to create a collider! To run `collider`, clone the [`rusty_engine`](https://github.com/CleanCut/rusty_engine/) repository, place your image file in the `assets` directory (let's call it `db.png`), and then run:
3838

3939
```text
40-
$ cargo run --release --example collider assets/sprite/db.png
40+
$ cargo run --release --example collider assets/db.png
4141
```
4242

4343
Then follow the directions to create (or re-create) a collider and write it to a file.
4444

4545
<img width="1392" alt="Screen Shot 2021-12-26 at 10 45 40 PM" src="https://user-images.githubusercontent.com/5838512/147438683-c8af2db7-66dd-463c-a269-d03f37869496.png">
4646

47-
Once you have a good collider created, copy (or move) both your image and `.collider` file to your own project, under the `assets/sprite` directory.
47+
Once you have a good collider created, copy (or move) both your image and `.collider` files to your own project, under the `assets/` directory somewhere, and then [add the sprite to your game](55-sprite-creation.md)

0 commit comments

Comments
 (0)