bevy/crates
ickshonpe b90329aef5
update_text2d_layout creates new font atlases when the primary window is closed (#7849)
# Objective

Necessary conditions:
* Scale factor != 1
* Text is being displayed with Text2d
* The primary window is closed on a frame where the text or text's
bounds are modified.

Then when `update_text2d_layout` runs, it finds no primary window and
assumes a scale factor of 1.
The previous scale_factor was not equal to 1 and the text pipeline's old
font atlases were created for a non-1 scale factor, so it creates new
font atlases even though the app is closing.

The bug was first identified in #6666

## Minimal Example

```rust
use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(WindowPlugin {
            primary_window: Some(Window {
                present_mode: bevy:🪟:PresentMode::Immediate,
                ..Default::default()
            }),
            ..default()
        }))
        .insert_resource(UiScale { scale: std::f64::consts::PI })
        .add_startup_system(setup)
        .add_system(update)
        .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(Camera2dBundle::default());
    commands.spawn(Text2dBundle {
        text: Text {
            sections: (0..10).map(|i| TextSection {
                value: i.to_string(),
                style: TextStyle {
                    font: asset_server.load("fonts/FiraSans-Bold.ttf"),
                    font_size: (10 + i) as f32,
                    color: Color::WHITE,
                }
            }).collect(),
            ..Default::default()            
        },
        ..Default::default()
    });
}

fn update(mut text: Query<&mut Text>) {
    for mut text in text.iter_mut() {
        text.set_changed();
    }
}
```
## Output
On closing the window you'll see the warning (if you don't, increase the
number of text sections):
```
WARN bevy_text::glyph_brush: warning[B0005]: Number of font atlases has exceeded the maximum of 16. Performance and memory usage may suffer.
```
The app should only create font atlases on startup, but it doesn't
display this warning until after you close the window

## Solution

Skip `update_text_layout` when there is no primary window.

## Changelog
* If no primary window is found, skip `update_text2d_layout`.
* Added a `Local` flag `skipped` to `update_text2d_layout`. This should
ensure there are no edge cases where text might not get drawn at all.

---------

Co-authored-by: François Mockers <mockersf@gmail.com>
2025-01-14 01:01:31 +00:00
..
bevy_a11y Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_animation Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_app Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_asset Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_audio Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_color Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_core_pipeline Key render phases off the main world view entity, not the render world view entity. (#16942) 2025-01-12 20:24:17 +00:00
bevy_derive Bump Version after Release (#17176) 2025-01-06 00:04:44 +00:00
bevy_dev_tools Allow users to customize history length in FrameTimeDiagnosticsPlugin (#17259) 2025-01-12 18:18:14 +00:00
bevy_diagnostic Allow users to customize history length in FrameTimeDiagnosticsPlugin (#17259) 2025-01-12 18:18:14 +00:00
bevy_dylib Bump Version after Release (#17176) 2025-01-06 00:04:44 +00:00
bevy_ecs Add std derives to SystemParam types (#16785) 2025-01-14 00:30:51 +00:00
bevy_encase_derive Bump Version after Release (#17176) 2025-01-06 00:04:44 +00:00
bevy_gilrs Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_gizmos Key render phases off the main world view entity, not the render world view entity. (#16942) 2025-01-12 20:24:17 +00:00
bevy_gltf Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_hierarchy Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_image Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_input Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_input_focus Add a simple directional UI navigation example (#17224) 2025-01-09 21:15:28 +00:00
bevy_internal Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_log Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_macro_utils Bump Version after Release (#17176) 2025-01-06 00:04:44 +00:00
bevy_math Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_mesh Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_mikktspace Bump Version after Release (#17176) 2025-01-06 00:04:44 +00:00
bevy_pbr Key render phases off the main world view entity, not the render world view entity. (#16942) 2025-01-12 20:24:17 +00:00
bevy_picking Rename PickingBehavior to Pickable (#17266) 2025-01-12 05:36:52 +00:00
bevy_ptr Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_reflect Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_remote Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_render Key render phases off the main world view entity, not the render world view entity. (#16942) 2025-01-12 20:24:17 +00:00
bevy_scene Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_sprite Key render phases off the main world view entity, not the render world view entity. (#16942) 2025-01-12 20:24:17 +00:00
bevy_state Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_tasks Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_text update_text2d_layout creates new font atlases when the primary window is closed (#7849) 2025-01-14 01:01:31 +00:00
bevy_time Fix bevy_time tests occasionally failing on optimised Windows builds (#17349) 2025-01-14 00:30:07 +00:00
bevy_transform Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_ui Key render phases off the main world view entity, not the render world view entity. (#16942) 2025-01-12 20:24:17 +00:00
bevy_utils Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_window Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_winit Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00