Skip to content

Commit fdaf322

Browse files
authored
Try #6408:
2 parents e7719bf + a3d9c9f commit fdaf322

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ description = "Demonstrates transparency in 2d"
262262
category = "2D Rendering"
263263
wasm = true
264264

265+
[[example]]
266+
name = "pixel_perfect"
267+
path = "examples/2d/pixel_perfect.rs"
268+
269+
[package.metadata.example.pixel_perfect]
270+
name = "Pixel Perfect"
271+
description = "Demonstrates pixel perfect in 2d"
272+
category = "2D Rendering"
273+
wasm = true
274+
265275
# 3D Rendering
266276
[[example]]
267277
name = "3d_scene"

assets/pixel/bevy_pixel_dark.png

4.79 KB
Loading

assets/pixel/bevy_pixel_light.png

182 Bytes
Loading

examples/2d/pixel_perfect.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! Renders a 2D scene containing pixelated bevy logo in a pixel perfect style
2+
3+
use bevy::prelude::*;
4+
5+
fn main() {
6+
App::new()
7+
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
8+
.add_startup_system(setup)
9+
.add_system(sprite_movement)
10+
.run();
11+
}
12+
13+
#[derive(Component)]
14+
enum Direction {
15+
Left,
16+
Right,
17+
}
18+
19+
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
20+
commands.spawn(Camera2dBundle::default());
21+
commands.spawn((
22+
SpriteBundle {
23+
texture: asset_server.load("pixel/bevy_pixel_light.png"),
24+
transform: Transform::from_xyz(100., 0., 0.),
25+
..default()
26+
},
27+
Direction::Right,
28+
));
29+
}
30+
31+
fn sprite_movement(time: Res<Time>, mut sprite_position: Query<(&mut Direction, &mut Transform)>) {
32+
for (mut logo, mut transform) in &mut sprite_position {
33+
match *logo {
34+
Direction::Right => transform.translation.x += 30. * time.delta_seconds(),
35+
Direction::Left => transform.translation.x -= 30. * time.delta_seconds(),
36+
}
37+
38+
if transform.translation.x > 200. {
39+
*logo = Direction::Left;
40+
} else if transform.translation.x < -200. {
41+
*logo = Direction::Right;
42+
}
43+
}
44+
}

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Example | Description
9393
[Mesh 2D](../examples/2d/mesh2d.rs) | Renders a 2d mesh
9494
[Mesh 2D With Vertex Colors](../examples/2d/mesh2d_vertex_color_texture.rs) | Renders a 2d mesh with vertex color attributes
9595
[Move Sprite](../examples/2d/move_sprite.rs) | Changes the transform of a sprite
96+
[Pixel Perfect](../examples/2d/pixel_perfect.rs) | Demonstrates pixel perfect in 2d
9697
[Sprite](../examples/2d/sprite.rs) | Renders a sprite
9798
[Sprite Flipping](../examples/2d/sprite_flipping.rs) | Renders a sprite flipped along an axis
9899
[Sprite Sheet](../examples/2d/sprite_sheet.rs) | Renders an animated sprite

0 commit comments

Comments
 (0)