In the default mode, an App with a window utilizes a lot of CPU power. This is because the app redraws its window contents repeatedly. The mode is suitable for game projects. Yet, for the most of desktop applications, we would like the app to redraw only when there are events occurred.
Bevy provides such a desktop mode. We can switch to this mode by replacing a Resource.
use bevy::{app::App, winit::WinitSettings, DefaultPlugins};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(WinitSettings::desktop_app())
.run();
}
The DefaultPlugins contains a WinitPlugin plugin, which inserts WinitSettings as a Resource for the setting of the winit backend. The default WinitSettings is WinitSettings::game(). We replace this setting with WinitSettings::desktop_app(). This makes our window utilizes less CPU power as expected.
The insert_resource method helps us replacing an existing Resource. The methods add the given Resource when the Resource does not exist in the App and replace the existing Resource if there is any.
When running in default setting (WinitSettings::game()), the CPU usage would be:
When running in WinitSettings::desktop_app(), the CPU usage would be:
➡️ Next: Closing The Window On Esc Pressed
📘 Back: Table of contents