1
1
use bevy:: {
2
- app:: AppExit ,
3
2
diagnostic:: { FrameTimeDiagnosticsPlugin , LogDiagnosticsPlugin } ,
4
3
prelude:: * ,
5
4
window:: { WindowResizeConstraints , WindowResolution } ,
6
5
} ;
7
6
use bevy_pixels:: prelude:: * ;
8
7
use rand:: prelude:: * ;
9
8
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 ;
12
12
13
13
#[ derive( Bundle , Debug ) ]
14
14
struct ObjectBundle {
@@ -44,10 +44,13 @@ fn main() {
44
44
. add_plugins ( DefaultPlugins . set ( WindowPlugin {
45
45
primary_window : Some ( Window {
46
46
title : "Hello Bevy Pixels" . to_string ( ) ,
47
- resolution : WindowResolution :: new ( WIDTH as f32 , HEIGHT as f32 ) ,
47
+ resolution : WindowResolution :: new (
48
+ INITIAL_WIDTH as f32 * SCALE_FACTOR ,
49
+ INITIAL_HEIGHT as f32 * SCALE_FACTOR ,
50
+ ) ,
48
51
resize_constraints : WindowResizeConstraints {
49
- min_width : WIDTH as f32 ,
50
- min_height : HEIGHT as f32 ,
52
+ min_width : INITIAL_WIDTH as f32 * SCALE_FACTOR ,
53
+ min_height : INITIAL_HEIGHT as f32 * SCALE_FACTOR ,
51
54
..default ( )
52
55
} ,
53
56
fit_canvas_to_parent : true ,
@@ -56,21 +59,27 @@ fn main() {
56
59
..default ( )
57
60
} ) )
58
61
. add_plugin ( PixelsPlugin {
59
- width : WIDTH ,
60
- height : HEIGHT ,
61
- ..default ( )
62
+ primary_window : Some ( PixelsOptions {
63
+ width : INITIAL_WIDTH ,
64
+ height : INITIAL_HEIGHT ,
65
+ scale_factor : SCALE_FACTOR ,
66
+ ..default ( )
67
+ } ) ,
62
68
} )
63
69
. add_plugin ( FrameTimeDiagnosticsPlugin :: default ( ) )
64
70
. add_plugin ( LogDiagnosticsPlugin :: default ( ) )
65
71
. add_startup_system ( setup)
66
- . add_system ( bounce)
67
- . add_system ( movement. after ( bounce) )
68
- . add_system ( exit_on_escape)
69
- . add_system ( draw_background. in_set ( PixelsSet :: Draw ) )
70
- . add_system ( draw_objects. in_set ( PixelsSet :: Draw ) . 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
+ )
71
79
. run ( ) ;
72
80
}
73
81
82
+ /// Spawn object.
74
83
fn setup ( mut commands : Commands ) {
75
84
let box_object = ObjectBundle {
76
85
position : Position { x : 24 , y : 16 } ,
@@ -84,14 +93,28 @@ fn setup(mut commands: Commands) {
84
93
commands. spawn ( box_object) ;
85
94
}
86
95
87
- fn bounce ( mut query : Query < ( & Position , & mut Velocity , & Size , & mut Color ) > ) {
88
- 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 {
89
104
let mut bounce = false ;
90
- 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 {
91
110
velocity. x *= -1 ;
92
111
bounce = true ;
93
112
}
94
- 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 {
95
118
velocity. y *= -1 ;
96
119
bounce = true ;
97
120
}
@@ -103,37 +126,43 @@ fn bounce(mut query: Query<(&Position, &mut Velocity, &Size, &mut Color)>) {
103
126
}
104
127
}
105
128
106
- fn movement ( mut query : Query < ( & mut Position , & Velocity ) > ) {
107
- for ( mut position , velocity ) in query . iter_mut ( ) {
108
- position . x = ( position . x as i16 + velocity . x ) as u32 ;
109
- position . y = ( position . y as i16 + velocity . y ) as u32 ;
110
- }
111
- }
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 } ;
112
135
113
- fn exit_on_escape ( keyboard_input : Res < Input < KeyCode > > , mut app_exit_events : EventWriter < AppExit > ) {
114
- if keyboard_input. just_pressed ( KeyCode :: Escape ) {
115
- 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 ) ;
116
140
}
117
141
}
118
142
119
- fn draw_background ( mut pixels_resource : ResMut < PixelsResource > ) {
120
- 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
+
121
148
frame. copy_from_slice ( & [ 0x48 , 0xb2 , 0xe8 , 0xff ] . repeat ( frame. len ( ) / 4 ) ) ;
122
149
}
123
150
151
+ /// Draw objects to buffer.
124
152
fn draw_objects (
125
- mut pixels_resource : ResMut < PixelsResource > ,
153
+ mut wrapper_query : Query < ( & mut PixelsWrapper , & PixelsOptions ) > ,
126
154
query : Query < ( & Position , & Size , & Color ) > ,
127
155
) {
128
- let frame = pixels_resource. pixels . frame_mut ( ) ;
129
- 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 ;
130
159
131
- for ( position, size, color) in query. iter ( ) {
160
+ for ( position, size, color) in & query {
132
161
let x_offset = ( position. x * 4 ) as usize ;
133
162
let width_bytes = ( size. width * 4 ) as usize ;
134
163
let object_row = & [ color. 0 , color. 1 , color. 2 , color. 3 ] . repeat ( size. width as usize ) ;
135
164
136
- for y in position. y ..( position. y + size. height - 1 ) {
165
+ for y in position. y ..( position. y + size. height ) {
137
166
let y_offset = y as usize * frame_width_bytes;
138
167
let i = y_offset + x_offset;
139
168
let j = i + width_bytes;
0 commit comments