Skip to content

Commit

Permalink
Reverting ability to resize immediately
Browse files Browse the repository at this point in the history
Vulkan seems to not be happy with how the swapchain texture is being
dropped and recreated.
  • Loading branch information
ecton committed Sep 22, 2024
1 parent 9d6b8e7 commit e3a51ae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 66 deletions.
5 changes: 0 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Users who are using Kludgine directly should invoke `Kludgine::resize` to
apply the new size.
- `WindowBehavior::prepare` now returns a `Result<(), Resized>`. This change is
to support applications resizing the window during the prepare function. On
platforms that immediately resize the window, an extra redraw can be avoided
by returning `Err(Resized)`. This allows Kludgine to restart the current
frame's redraw with the new size immediately.

### Added

Expand Down
5 changes: 2 additions & 3 deletions examples/sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use appit::winit::error::EventLoopError;
use appit::winit::keyboard::{Key, NamedKey};
use figures::units::{Px, UPx};
use figures::Zero;
use kludgine::app::{Resized, WindowBehavior};
use kludgine::app::WindowBehavior;
use kludgine::figures::Size;
use kludgine::sprite::{
AnimationMode, Sprite, SpriteAnimation, SpriteAnimations, SpriteFrame, SpriteSheet,
Expand Down Expand Up @@ -139,13 +139,12 @@ impl WindowBehavior for Sprites {
&mut self,
window: kludgine::app::Window<'_, ()>,
graphics: &mut kludgine::Graphics<'_>,
) -> Result<(), Resized> {
) {
self.current_frame = self
.sprite
.get_frame(Some(window.elapsed()))
.ok()
.map(|frame| frame.prepare(Size::squared(Px::new(64)).into(), graphics));
Ok(())
}

fn render<'pass>(
Expand Down
9 changes: 2 additions & 7 deletions examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use appit::winit::error::EventLoopError;
use figures::{Angle, FromComponents, Point};
use kludgine::app::{Resized, Window, WindowBehavior};
use kludgine::app::{Window, WindowBehavior};
use kludgine::cosmic_text::{Attrs, AttrsList, Buffer, Edit, Editor, Metrics};
use kludgine::figures::{FloatConversion, ScreenScale};
use kludgine::text::{PreparedText, TextOrigin};
Expand Down Expand Up @@ -60,11 +60,7 @@ impl WindowBehavior for Test {
}
}

fn prepare(
&mut self,
_window: Window<'_>,
graphics: &mut kludgine::Graphics<'_>,
) -> Result<(), Resized> {
fn prepare(&mut self, _window: Window<'_>, graphics: &mut kludgine::Graphics<'_>) {
let scale = graphics.scale();
let size = graphics.size();
self.text.set_size(
Expand All @@ -79,7 +75,6 @@ impl WindowBehavior for Test {
let mut editor = Editor::new(&mut self.text);
editor.shape_as_needed(graphics.font_system(), true);
self.prepared = graphics.prepare_text(&self.text, Color::WHITE, TextOrigin::Center);
Ok(())
}

fn render<'pass>(
Expand Down
77 changes: 26 additions & 51 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fmt::Display;
use std::marker::PhantomData;
use std::num::NonZeroU32;
use std::path::PathBuf;
Expand Down Expand Up @@ -525,20 +524,8 @@ where
///
/// This is called directly before [`render()`](Self::render()) and is a
/// perfect place to update any prepared graphics as needed.
///
/// # Errors
///
/// If during the preparation of rendering, the window is resized,
/// `Err(Resized)` is returned and Kludgine will immediately resize the
/// graphics context and begin rendering again.
#[allow(unused_variables)]
fn prepare(
&mut self,
window: Window<'_, WindowEvent>,
graphics: &mut Graphics<'_>,
) -> Result<(), Resized> {
Ok(())
}
fn prepare(&mut self, window: Window<'_, WindowEvent>, graphics: &mut Graphics<'_>) {}

/// Render the contents of the window.
// TODO refactor away from bool return.
Expand Down Expand Up @@ -901,16 +888,6 @@ where
}
}

/// The window was resized whlie preparing to render.
#[derive(Default, Debug, Eq, PartialEq, Clone, Copy)]
pub struct Resized;

impl Display for Resized {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("the context was resized before preparing to render")
}
}

/// A Kludgine application event.
pub struct AppEvent<User>(AppEventKind<User>);

Expand Down Expand Up @@ -1137,7 +1114,7 @@ impl<Behavior> KludgineWindow<Behavior> {
surface: wgpu::SurfaceTexture,
render_start: Instant,
window: &mut RunningWindow<AppEvent<User>>,
) -> Result<(), Resized>
) -> bool
where
AppEvent<User>: Message,
Behavior: WindowBehavior<User> + 'static,
Expand All @@ -1146,10 +1123,12 @@ impl<Behavior> KludgineWindow<Behavior> {
let mut frame = self.kludgine.next_frame();
let elapsed = render_start - self.last_render;

let current_size = window.inner_size();
self.behavior.prepare(
Window::new(window, elapsed, self.last_render_duration),
&mut frame.prepare(&self.device, &self.queue),
)?;
);
let resized = window.inner_size() != current_size;

let surface_view = surface
.texture
Expand Down Expand Up @@ -1223,7 +1202,7 @@ impl<Behavior> KludgineWindow<Behavior> {
if close_after_frame {
window.close();
}
Ok(())
resized
}
}

Expand Down Expand Up @@ -1390,30 +1369,27 @@ where
}

let mut render_start = None;
loop {
let Some(surface) = self.current_surface_texture(window) else {
return;
};

let render_start = render_start.unwrap_or_else(|| {
let now = Instant::now();
render_start = Some(now);
now
});

if let Err(Resized) = self.render_to_surface(surface, render_start, window) {
self.kludgine.resize(
window.inner_size().into(),
self.kludgine.scale(),
self.kludgine.zoom(),
&self.queue,
);
continue;
}
self.last_render_duration = render_start.elapsed();
self.last_render = render_start;
let Some(surface) = self.current_surface_texture(window) else {
return;
};

let render_start = render_start.unwrap_or_else(|| {
let now = Instant::now();
render_start = Some(now);
now
});

if self.render_to_surface(surface, render_start, window) {
self.kludgine.resize(
window.inner_size().into(),
self.kludgine.scale(),
self.kludgine.zoom(),
&self.queue,
);
window.set_needs_redraw();
}
self.last_render_duration = render_start.elapsed();
self.last_render = render_start;
}
}

Expand Down Expand Up @@ -1830,10 +1806,9 @@ where
}
}

fn prepare(&mut self, window: Window<'_>, graphics: &mut Graphics<'_>) -> Result<(), Resized> {
fn prepare(&mut self, window: Window<'_>, graphics: &mut Graphics<'_>) {
let renderer = self.rendering.new_frame(graphics);
self.keep_running = (self.callback)(renderer, window);
Ok(())
}

fn render<'pass>(
Expand Down

0 comments on commit e3a51ae

Please sign in to comment.