Skip to content

Commit fe44070

Browse files
authored
Merge pull request #16 from dtcristo/bevy-0.10
Update to bevy 0.10
2 parents a9589d8 + b8ac2b7 commit fe44070

File tree

5 files changed

+263
-134
lines changed

5 files changed

+263
-134
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,30 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- Added support support for multiple windows. Made possible by the move from `PixelsResource` to
8+
`PixelsWrapper` described below.
9+
- Added `multiple_windows` example demonstrating support for multiple windows.
10+
- Added `scale_factor` option to control scale factor between logical window size and buffer size
11+
when using `auto_resize_buffer`.
12+
- Added `auto_resize_buffer` option to control automatic resizing of the buffer when the window
13+
changes.
14+
- Added `auto_resize_surface` option to control automatic resizing of the surface when the window
15+
changes.
16+
517
### Changed
618

19+
- Updated `bevy` to 0.10.
720
- Updated `pixels` to 0.12.
21+
- Configuration of buffer size has been moved from `PixelsPlugin` to `PixelsOptions`.
22+
- Primary window buffer is created by providing `Some(PixelsOptions { ... })` to the
23+
`primary_window` when creating `PixelsPlugin`. This works the same was as Bevy's own configuration
24+
of primary window in the `WindowPlugin`.
25+
- Resouce `PixelsResource` has been replaced with `PixelsWrapper` component that is automatically
26+
added to `Window` entities with the `PixelsOptions` component.
27+
- Diagnostic `PixelsPlugin::RENDER_TIME` is now recorded in miliseconds instead of seconds.
28+
- Updated `minimal` example to demonstrate `auto_resize_buffer` feature.
829

930
## [0.8.0] - 2022-12-20
1031

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ wayland = ["bevy/wayland"]
1919
x11 = ["bevy/x11"]
2020

2121
[dependencies]
22-
bevy = { version = "0.9", default_features = false, features = ["bevy_winit"] }
22+
bevy = { version = "0.10", default_features = false, features = ["bevy_winit"] }
2323
pixels = "0.12"
2424

2525
[target.'cfg(target_arch = "wasm32")'.dependencies]

examples/minimal.rs

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use bevy::{
2-
app::AppExit,
32
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
43
prelude::*,
5-
window::WindowResizeConstraints,
4+
window::{WindowResizeConstraints, WindowResolution},
65
};
76
use bevy_pixels::prelude::*;
87
use rand::prelude::*;
98

10-
const WIDTH: u32 = 320;
11-
const HEIGHT: u32 = 240;
9+
const INITIAL_WIDTH: u32 = 320;
10+
const INITIAL_HEIGHT: u32 = 240;
11+
const SCALE_FACTOR: f32 = 2.0;
1212

1313
#[derive(Bundle, Debug)]
1414
struct ObjectBundle {
@@ -42,36 +42,44 @@ struct Color(u8, u8, u8, u8);
4242
fn main() {
4343
App::new()
4444
.add_plugins(DefaultPlugins.set(WindowPlugin {
45-
window: WindowDescriptor {
45+
primary_window: Some(Window {
4646
title: "Hello Bevy Pixels".to_string(),
47-
width: WIDTH as f32,
48-
height: HEIGHT as f32,
47+
resolution: WindowResolution::new(
48+
INITIAL_WIDTH as f32 * SCALE_FACTOR,
49+
INITIAL_HEIGHT as f32 * SCALE_FACTOR,
50+
),
4951
resize_constraints: WindowResizeConstraints {
50-
min_width: WIDTH as f32,
51-
min_height: HEIGHT as f32,
52+
min_width: INITIAL_WIDTH as f32 * SCALE_FACTOR,
53+
min_height: INITIAL_HEIGHT as f32 * SCALE_FACTOR,
5254
..default()
5355
},
5456
fit_canvas_to_parent: true,
5557
..default()
56-
},
58+
}),
5759
..default()
5860
}))
5961
.add_plugin(PixelsPlugin {
60-
width: WIDTH,
61-
height: HEIGHT,
62-
..default()
62+
primary_window: Some(PixelsOptions {
63+
width: INITIAL_WIDTH,
64+
height: INITIAL_HEIGHT,
65+
scale_factor: SCALE_FACTOR,
66+
..default()
67+
}),
6368
})
6469
.add_plugin(FrameTimeDiagnosticsPlugin::default())
6570
.add_plugin(LogDiagnosticsPlugin::default())
6671
.add_startup_system(setup)
67-
.add_system(bounce)
68-
.add_system(movement.after(bounce))
69-
.add_system(exit_on_escape)
70-
.add_system_to_stage(PixelsStage::Draw, draw_background)
71-
.add_system_to_stage(PixelsStage::Draw, draw_objects.after(draw_background))
72+
.add_system(bevy::window::close_on_esc)
73+
.add_systems((bounce, movement).chain().in_set(PixelsSet::Update))
74+
.add_systems(
75+
(draw_background, draw_objects)
76+
.chain()
77+
.in_set(PixelsSet::Draw),
78+
)
7279
.run();
7380
}
7481

82+
/// Spawn object.
7583
fn setup(mut commands: Commands) {
7684
let box_object = ObjectBundle {
7785
position: Position { x: 24, y: 16 },
@@ -85,14 +93,28 @@ fn setup(mut commands: Commands) {
8593
commands.spawn(box_object);
8694
}
8795

88-
fn bounce(mut query: Query<(&Position, &mut Velocity, &Size, &mut Color)>) {
89-
for (position, mut velocity, size, mut color) in query.iter_mut() {
96+
/// Bounce object off edges of buffer.
97+
fn bounce(
98+
options_query: Query<&PixelsOptions>,
99+
mut query: Query<(&Position, &mut Velocity, &Size, &mut Color)>,
100+
) {
101+
let Ok(options) = options_query.get_single() else { return };
102+
103+
for (position, mut velocity, size, mut color) in &mut query {
90104
let mut bounce = false;
91-
if position.x == 0 || position.x + size.width > WIDTH {
105+
if position.x == 0 && velocity.x < 0 {
106+
velocity.x *= -1;
107+
bounce = true;
108+
}
109+
if position.x + size.width == options.width && velocity.x > 0 {
92110
velocity.x *= -1;
93111
bounce = true;
94112
}
95-
if position.y == 0 || position.y + size.height > HEIGHT {
113+
if position.y == 0 && velocity.y < 0 {
114+
velocity.y *= -1;
115+
bounce = true;
116+
}
117+
if position.y + size.height == options.height && velocity.y > 0 {
96118
velocity.y *= -1;
97119
bounce = true;
98120
}
@@ -104,37 +126,43 @@ fn bounce(mut query: Query<(&Position, &mut Velocity, &Size, &mut Color)>) {
104126
}
105127
}
106128

107-
fn movement(mut query: Query<(&mut Position, &Velocity)>) {
108-
for (mut position, velocity) in query.iter_mut() {
109-
position.x = (position.x as i16 + velocity.x) as u32;
110-
position.y = (position.y as i16 + velocity.y) as u32;
111-
}
112-
}
129+
/// Move object based on current velocity.
130+
fn movement(
131+
options_query: Query<&PixelsOptions>,
132+
mut query: Query<(&mut Position, &Velocity, &Size)>,
133+
) {
134+
let Ok(options) = options_query.get_single() else { return };
113135

114-
fn exit_on_escape(keyboard_input: Res<Input<KeyCode>>, mut app_exit_events: EventWriter<AppExit>) {
115-
if keyboard_input.just_pressed(KeyCode::Escape) {
116-
app_exit_events.send(AppExit);
136+
for (mut position, velocity, size) in &mut query {
137+
position.x = ((position.x as i16 + velocity.x) as u32).clamp(0, options.width - size.width);
138+
position.y =
139+
((position.y as i16 + velocity.y) as u32).clamp(0, options.height - size.height);
117140
}
118141
}
119142

120-
fn draw_background(mut pixels_resource: ResMut<PixelsResource>) {
121-
let frame = pixels_resource.pixels.frame_mut();
143+
/// Draw solid background to buffer.
144+
fn draw_background(mut wrapper_query: Query<&mut PixelsWrapper>) {
145+
let Ok(mut wrapper) = wrapper_query.get_single_mut() else { return };
146+
let frame = wrapper.pixels.frame_mut();
147+
122148
frame.copy_from_slice(&[0x48, 0xb2, 0xe8, 0xff].repeat(frame.len() / 4));
123149
}
124150

151+
/// Draw objects to buffer.
125152
fn draw_objects(
126-
mut pixels_resource: ResMut<PixelsResource>,
153+
mut wrapper_query: Query<(&mut PixelsWrapper, &PixelsOptions)>,
127154
query: Query<(&Position, &Size, &Color)>,
128155
) {
129-
let frame = pixels_resource.pixels.frame_mut();
130-
let frame_width_bytes = (WIDTH * 4) as usize;
156+
let Ok((mut wrapper, options)) = wrapper_query.get_single_mut() else { return };
157+
let frame = wrapper.pixels.frame_mut();
158+
let frame_width_bytes = (options.width * 4) as usize;
131159

132-
for (position, size, color) in query.iter() {
160+
for (position, size, color) in &query {
133161
let x_offset = (position.x * 4) as usize;
134162
let width_bytes = (size.width * 4) as usize;
135163
let object_row = &[color.0, color.1, color.2, color.3].repeat(size.width as usize);
136164

137-
for y in position.y..(position.y + size.height - 1) {
165+
for y in position.y..(position.y + size.height) {
138166
let y_offset = y as usize * frame_width_bytes;
139167
let i = y_offset + x_offset;
140168
let j = i + width_bytes;

examples/multiple_windows.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use bevy::prelude::*;
2+
use bevy_pixels::prelude::*;
3+
4+
fn main() {
5+
App::new()
6+
.add_plugins(DefaultPlugins)
7+
.add_plugin(PixelsPlugin::default())
8+
.add_startup_system(setup)
9+
.add_system(bevy::window::close_on_esc)
10+
.add_system(draw.in_set(PixelsSet::Draw))
11+
.run();
12+
}
13+
14+
/// Spawn two more windows in addition to the primary window that comes by default.
15+
fn setup(mut commands: Commands) {
16+
commands.spawn((Window::default(), PixelsOptions::default()));
17+
commands.spawn((Window::default(), PixelsOptions::default()));
18+
}
19+
20+
/// Draw solid background to each window's buffer.
21+
fn draw(mut wrapper_query: Query<&mut PixelsWrapper>) {
22+
for mut wrapper in &mut wrapper_query {
23+
let frame = wrapper.pixels.frame_mut();
24+
25+
frame.copy_from_slice(&[0x48, 0xb2, 0xe8, 0xff].repeat(frame.len() / 4));
26+
}
27+
}

0 commit comments

Comments
 (0)