Skip to content

Commit ee7f14c

Browse files
committed
Merge branch 'feat/changes'
2 parents d27eb85 + b2ad130 commit ee7f14c

5 files changed

Lines changed: 73 additions & 12 deletions

File tree

crates/processing_glfw/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ impl GlfwContext {
300300
WindowEvent::Focus(focused) => {
301301
input_set_focus(surface, focused).unwrap();
302302
}
303+
WindowEvent::Size(width, height) => {
304+
processing_render::surface_resize(surface, width as u32, height as u32)
305+
.unwrap();
306+
}
303307
_ => {}
304308
}
305309
}
@@ -355,6 +359,9 @@ impl GlfwContext {
355359
Ok(())
356360
});
357361
self.last_applied.position = frame_pos;
362+
363+
let (w, h) = self.window.get_size();
364+
self.last_applied.size = bevy::math::UVec2::new(w.max(0) as u32, h.max(0) as u32);
358365
}
359366

360367
#[cfg(not(feature = "wayland"))]
@@ -529,8 +536,8 @@ fn read_desired_window(surface: Entity) -> Option<DesiredWindow> {
529536
_ => None,
530537
},
531538
size: bevy::math::UVec2::new(
532-
window.resolution.physical_width(),
533-
window.resolution.physical_height(),
539+
window.resolution.width() as u32,
540+
window.resolution.height() as u32,
534541
),
535542
visible: window.visible,
536543
resizable: window.resizable,

crates/processing_pyo3/src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,12 @@ pub(crate) fn reset_tracked_globals() {
120120
fn sync_globals(module: &Bound<'_, PyModule>, globals: &Bound<'_, PyAny>) -> PyResult<()> {
121121
let graphics =
122122
get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?;
123-
input::sync_globals(
124-
globals,
125-
graphics.surface.entity,
126-
graphics.width,
127-
graphics.height,
128-
)?;
129-
surface::sync_globals(globals, &graphics.surface, graphics.width, graphics.height)?;
123+
let width = ::processing::prelude::surface_width(graphics.surface.entity)
124+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
125+
let height = ::processing::prelude::surface_height(graphics.surface.entity)
126+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
127+
input::sync_globals(globals, graphics.surface.entity, width, height)?;
128+
surface::sync_globals(globals, &graphics.surface, width, height)?;
130129
time::sync_globals(globals)?;
131130
Ok(())
132131
}

crates/processing_render/src/graphics.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use bevy::{
2222
sync_world::MainEntity,
2323
view::ViewTarget,
2424
},
25-
window::WindowRef,
25+
window::{WindowRef, WindowResized},
2626
};
2727

2828
use crate::{
@@ -118,6 +118,8 @@ impl CameraProjection for ProcessingProjection {
118118
// this gets called with the render target's physical dimensions (i.e. accounting for
119119
// scale factor), but our projection is in logical pixel units
120120
// TODO: handle resizes?
121+
self.width = _width;
122+
self.height = _height;
121123
}
122124

123125
fn far(&self) -> f32 {
@@ -258,6 +260,7 @@ pub fn sync_to_surface(
258260
mut graphics_query: Query<(&mut Graphics, &RenderTarget)>,
259261
windows: Query<&Window, (With<Surface>, Changed<Window>)>,
260262
render_device: Res<RenderDevice>,
263+
mut resize_messages: MessageWriter<WindowResized>,
261264
) {
262265
for (mut graphics, target) in graphics_query.iter_mut() {
263266
let RenderTarget::Window(WindowRef::Entity(surface_entity)) = *target else {
@@ -271,6 +274,12 @@ pub fn sync_to_surface(
271274
if graphics.size.width == physical_w && graphics.size.height == physical_h {
272275
continue;
273276
}
277+
//winit plugin disabled, so WindowResized is never triggered automatically
278+
resize_messages.write(WindowResized {
279+
window: surface_entity,
280+
width: window.width(),
281+
height: window.height(),
282+
});
274283
graphics.size = Extent3d {
275284
width: physical_w,
276285
height: physical_h,

crates/processing_render/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,24 @@ pub fn surface_physical_height(entity: Entity) -> error::Result<u32> {
17021702
})
17031703
}
17041704

1705+
pub fn surface_width(entity: Entity) -> error::Result<u32> {
1706+
app_mut(|app| {
1707+
Ok(app
1708+
.world_mut()
1709+
.run_system_cached_with(surface::width, entity)
1710+
.unwrap())
1711+
})
1712+
}
1713+
1714+
pub fn surface_height(entity: Entity) -> error::Result<u32> {
1715+
app_mut(|app| {
1716+
Ok(app
1717+
.world_mut()
1718+
.run_system_cached_with(surface::height, entity)
1719+
.unwrap())
1720+
})
1721+
}
1722+
17051723
pub fn monitor_list() -> error::Result<Vec<Entity>> {
17061724
app_mut(|app| Ok(app.world_mut().run_system_cached(monitor::list).unwrap()))
17071725
}

crates/processing_render/src/surface.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
use bevy::{
2222
app::{App, Plugin},
2323
asset::Assets,
24+
camera::RenderTarget,
2425
ecs::query::QueryEntityError,
2526
math::{IRect, IVec2},
2627
prelude::{Commands, Component, Entity, In, Query, ResMut, Window, With, default},
2728
render::render_resource::{Extent3d, TextureFormat},
2829
window::{
2930
CompositeAlphaMode, Monitor, RawHandleWrapper, WindowLevel, WindowMode, WindowPosition,
30-
WindowResolution, WindowWrapper,
31+
WindowRef, WindowResolution, WindowWrapper,
3132
},
3233
};
3334
use raw_window_handle::{
@@ -39,7 +40,7 @@ use processing_core::error::{self, ProcessingError, Result};
3940
#[cfg(not(target_os = "windows"))]
4041
use std::ptr::NonNull;
4142

42-
use crate::image::Image;
43+
use crate::{graphics::SurfaceSize, image::Image};
4344

4445
#[derive(Component, Debug, Clone)]
4546
pub struct Surface;
@@ -394,7 +395,11 @@ pub fn destroy(
394395
pub fn resize(
395396
In((window_entity, width, height)): In<(Entity, u32, u32)>,
396397
mut windows: Query<&mut Window>,
398+
mut graphics_query: Query<(&RenderTarget, &mut SurfaceSize)>,
397399
) -> Result<()> {
400+
let width = width.max(1);
401+
let height = height.max(1);
402+
398403
if let Ok(mut window) = windows.get_mut(window_entity) {
399404
let scale = window.resolution.scale_factor();
400405
let physical_w = (width as f32 * scale) as u32;
@@ -403,6 +408,15 @@ pub fn resize(
403408
.resolution
404409
.set_physical_resolution(physical_w, physical_h);
405410
}
411+
412+
// SurfaceSize changes on resize, if not handled will break APIs dependent on correct SurfaceSize
413+
for (target, mut surface_size) in graphics_query.iter_mut() {
414+
if let RenderTarget::Window(WindowRef::Entity(surface)) = *target {
415+
if surface == window_entity {
416+
*surface_size = SurfaceSize(width, height);
417+
}
418+
}
419+
}
406420
Ok(())
407421
}
408422

@@ -448,6 +462,20 @@ pub fn physical_height(In(entity): In<Entity>, query: Query<&Window>) -> u32 {
448462
.unwrap_or(0)
449463
}
450464

465+
pub fn width(In(entity): In<Entity>, query: Query<&Window>) -> u32 {
466+
query
467+
.get(entity)
468+
.map(|w| w.resolution.width() as u32)
469+
.unwrap_or(0)
470+
}
471+
472+
pub fn height(In(entity): In<Entity>, query: Query<&Window>) -> u32 {
473+
query
474+
.get(entity)
475+
.map(|w| w.resolution.height() as u32)
476+
.unwrap_or(0)
477+
}
478+
451479
pub fn set_title(
452480
In((entity, title)): In<(Entity, String)>,
453481
mut windows: Query<&mut Window>,

0 commit comments

Comments
 (0)