Bevy的一些窗口设置

2023-10-08 18:44
文章标签 设置 窗口 bevy

本文主要是介绍Bevy的一些窗口设置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Bevy的一些窗口设置

    • 0. 运行环境
    • 1. 背景色设置
      • 官网文档
      • 代码示例
    • 2. 分辨率、标题、窗口按钮、锁定窗口尺寸设置
      • 官网文档
      • 代码示例
    • 3. 帧率设置
      • 使用方法
    • 完整代码示例
    • 参考链接

记录了Bevy中窗口背景色、分辨率、标题、是否保留窗口按钮、是否锁定窗口尺寸、帧率的设置。

0. 运行环境

运行环境如下:

  • Rust版本:1.72.0
  • Bevy版本:0.11.3

所用Cargo.toml如下:

[package]
name = "bevy-test"
version = "0.1.0"
edition = "2021"[dependencies]
bevy = "0.11.3"

以下代码基于的基本代码如下:

use bevy::prelude::*;
use bevy::window::PrimaryWindow;fn main() {App::new().add_plugins(DefaultPlugins).add_systems(Startup, spawn_camera).add_systems(Startup, spawn_player).run();
}#[derive(Component)]
pub struct Player {}/* 在屏幕中心创建2D摄像机 */
pub fn spawn_camera(mut commands: Commands, window_query: Query<&Window, With<PrimaryWindow>>) {let window = window_query.get_single().unwrap();commands.spawn(Camera2dBundle {transform: Transform::from_xyz(window.width() / 2.0, window.height() / 2.0, 0.0),..default()});
}/* 在屏幕中心创建玩家(方块) */
pub fn spawn_player(mut commands: Commands,window_query: Query<&Window, With<PrimaryWindow>>
) {let window = window_query.get_single().unwrap();commands.spawn((SpriteBundle {sprite: Sprite {color: Color::rgb(0.0, 1.0, 1.0),custom_size: Some(Vec2::new(50.0, 50.0)),..default()},transform: Transform::from_xyz(window.width() / 2.0, window.height() / 2.0, 0.0),..default()},Player {},));
}/* 玩家移动 */
pub fn player_movement(keyboard_input: Res<Input<KeyCode>>,mut player_query: Query<&mut Transform, With<Player>>,time: Res<Time>,
) {if let Ok(mut transform) = player_query.get_single_mut() {let mut direction = Vec3::ZERO;if keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A) {direction += Vec3::new(-1.0, 0.0, 0.0);}if keyboard_input.pressed(KeyCode::Right) || keyboard_input.pressed(KeyCode::D) {direction += Vec3::new(1.0, 0.0, 0.0);}if keyboard_input.pressed(KeyCode::Up) || keyboard_input.pressed(KeyCode::W) {direction += Vec3::new(0.0, 1.0, 0.0);}if keyboard_input.pressed(KeyCode::Down) || keyboard_input.pressed(KeyCode::S) {direction += Vec3::new(0.0, -1.0, 0.0);}if direction.length() > 0.0 {direction = direction.normalize();}transform.translation += direction * 500.0 * time.delta_seconds();}
}

1. 背景色设置

Bevy中背景色是通过添加bevy::prelude::ClearColor资源的方式设置的。

官网文档

ClearColor文档链接:https://docs.rs/bevy/latest/bevy/prelude/struct.ClearColor.html

pub struct ClearColor(pub Color);

A Resource that stores the color that is used to clear the screen between frames.
This color appears as the “background” color for simple apps, when there are portions of the screen with nothing rendered.

翻译:

一种资源,用于储存在两帧之间填充屏幕的颜色。
当屏幕的某些部分没有任何渲染时,此颜色显示为简单应用程序的“背景”颜色。

代码示例

fn main() {App::new()/* 设置背景色 */.insert_resource(ClearColor(Color::rgb(0.0, 0.8, 0.0))).add_plugins(DefaultPlugins).add_systems(Startup, spawn_camera).add_systems(Startup, spawn_player).run();
}

2. 分辨率、标题、窗口按钮、锁定窗口尺寸设置

Bevy中分辨率、标题、窗口按钮、锁定窗口尺寸设置是通过设置默认插件(DefaultPlugins)中的WindowPlugin来实现的。
注意:在Bevy 0.8及以前分辨率等设置是通过WindowDescriptor来实现的,从0.9版本改为通过设置WindowPlugin来实现。

官网文档

Window结构体文档链接:https://docs.rs/bevy/latest/bevy/window/struct.Window.html

pub struct Window {pub cursor: Cursor,pub present_mode: PresentMode,pub mode: WindowMode,pub position: WindowPosition,pub resolution: WindowResolution,pub title: String,pub composite_alpha_mode: CompositeAlphaMode,pub resize_constraints: WindowResizeConstraints,pub resizable: bool,pub decorations: bool,pub transparent: bool,pub focused: bool,pub window_level: WindowLevel,pub canvas: Option<String>,pub fit_canvas_to_parent: bool,pub prevent_default_event_handling: bool,pub internal: InternalWindowState,pub ime_enabled: bool,pub ime_position: Vec2,pub window_theme: Option<WindowTheme>,
}

The defining Component for window entities, storing information about how it should appear and behave.
Each window corresponds to an entity, and is uniquely identified by the value of their Entity. When the Window component is added to an entity, a new window will be opened. When it is removed or the entity is despawned, the window will close.
This component is synchronized with winit through bevy_winit: it will reflect the current state of the window and can be modified to change this state.

代码示例

fn main() {App::new().insert_resource(ClearColor(Color::rgb(0.0, 0.8, 0.0)))/* 窗口设置 */.add_plugins(DefaultPlugins.set(WindowPlugin {primary_window: Some(Window {title: "A Cool Title".into(),resolution: (800., 600.).into(),resizable: true,decorations: true,..default()}),..default()})).add_systems(Startup, spawn_camera).add_systems(Startup, spawn_player).run();
}

3. 帧率设置

目前官网似乎没有提供设置帧率的方法,但有一个名为bevy_framepace的crate可以实现设置帧率的功能。
链接:https://github.com/aevyrie/bevy_framepace

和Bevy的版本对应关系:

bevybevy_framepace
0.110.13
0.100.12
0.90.7, 0.8, 0.9, 0.10, 0.11
0.80.5, 0.6

使用方法

  1. Cargo.toml中加入依赖:
[package]
name = "bevy-test"
version = "0.1.0"
edition = "2021"[dependencies]
bevy = "0.11.3"
bevy_framepace = "0.13.3"
  1. 加入插件
app.add_plugin(bevy_framepace::FramepacePlugin);
  1. 设置帧率
pub fn set_frame_rate(mut settings: ResMut<bevy_framepace::FramepaceSettings>,){use bevy_framepace::Limiter;settings.limiter = Limiter::from_framerate(30.0);
}

完整代码示例

use bevy::prelude::*;
use bevy::window::PrimaryWindow;fn main() {App::new().insert_resource(ClearColor(Color::rgb(0.0, 0.8, 0.0)))/* 窗口设置 */.add_plugins(DefaultPlugins.set(WindowPlugin {primary_window: Some(Window {title: "A Cool Title".into(),resolution: (800., 600.).into(),resizable: true,decorations: true,..default()}),..default()})).add_plugins(bevy_framepace::FramepacePlugin).add_systems(Startup, spawn_camera).add_systems(Startup, spawn_player).add_systems(Startup, set_frame_rate).add_systems(Update, player_movement).run();
}#[derive(Component)]
pub struct Player {}/* 在屏幕中心创建2D摄像机 */
pub fn spawn_camera(mut commands: Commands, window_query: Query<&Window, With<PrimaryWindow>>) {let window = window_query.get_single().unwrap();commands.spawn(Camera2dBundle {transform: Transform::from_xyz(window.width() / 2.0, window.height() / 2.0, 0.0),..default()});
}/* 在屏幕中心创建玩家(方块) */
pub fn spawn_player(mut commands: Commands,window_query: Query<&Window, With<PrimaryWindow>>
) {let window = window_query.get_single().unwrap();commands.spawn((SpriteBundle {sprite: Sprite {color: Color::rgb(0.0, 1.0, 1.0),custom_size: Some(Vec2::new(50.0, 50.0)),..default()},transform: Transform::from_xyz(window.width() / 2.0, window.height() / 2.0, 0.0),..default()},Player {},));
}/* 玩家移动 */
pub fn player_movement(keyboard_input: Res<Input<KeyCode>>,mut player_query: Query<&mut Transform, With<Player>>,time: Res<Time>,
) {if let Ok(mut transform) = player_query.get_single_mut() {let mut direction = Vec3::ZERO;if keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A) {direction += Vec3::new(-1.0, 0.0, 0.0);}if keyboard_input.pressed(KeyCode::Right) || keyboard_input.pressed(KeyCode::D) {direction += Vec3::new(1.0, 0.0, 0.0);}if keyboard_input.pressed(KeyCode::Up) || keyboard_input.pressed(KeyCode::W) {direction += Vec3::new(0.0, 1.0, 0.0);}if keyboard_input.pressed(KeyCode::Down) || keyboard_input.pressed(KeyCode::S) {direction += Vec3::new(0.0, -1.0, 0.0);}if direction.length() > 0.0 {direction = direction.normalize();}transform.translation += direction * 500.0 * time.delta_seconds();}
}/* 设置帧率 */
pub fn set_frame_rate(mut settings: ResMut<bevy_framepace::FramepaceSettings>,){use bevy_framepace::Limiter;settings.limiter = Limiter::from_framerate(30.0);
}

参考链接

  1. 【中字】Bevy 0.8入门教程-基本3D场景_哔哩哔哩_bilibili
  2. 0.8 to 0.9
  3. Window in bevy::window - Rust

这篇关于Bevy的一些窗口设置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/167291

相关文章

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

Windows设置nginx启动端口的方法

《Windows设置nginx启动端口的方法》在服务器配置与开发过程中,nginx作为一款高效的HTTP和反向代理服务器,被广泛应用,而在Windows系统中,合理设置nginx的启动端口,是确保其正... 目录一、为什么要设置 nginx 启动端口二、设置步骤三、常见问题及解决一、为什么要设置 nginx

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

电脑密码怎么设置? 一文读懂电脑密码的详细指南

《电脑密码怎么设置?一文读懂电脑密码的详细指南》为了保护个人隐私和数据安全,设置电脑密码显得尤为重要,那么,如何在电脑上设置密码呢?详细请看下文介绍... 设置电脑密码是保护个人隐私、数据安全以及系统安全的重要措施,下面以Windows 11系统为例,跟大家分享一下设置电脑密码的具体办php法。Windo

如何设置vim永久显示行号

《如何设置vim永久显示行号》在Linux环境下,vim默认不显示行号,这在程序编译出错时定位错误语句非常不便,通过修改vim配置文件vimrc,可以在每次打开vim时永久显示行号... 目录设置vim永久显示行号1.临时显示行号2.永www.chinasem.cn久显示行号总结设置vim永久显示行号在li

Linux:alias如何设置永久生效

《Linux:alias如何设置永久生效》在Linux中设置别名永久生效的步骤包括:在/root/.bashrc文件中配置别名,保存并退出,然后使用source命令(或点命令)使配置立即生效,这样,别... 目录linux:alias设置永久生效步骤保存退出后功能总结Linux:alias设置永久生效步骤

Spring MVC如何设置响应

《SpringMVC如何设置响应》本文介绍了如何在Spring框架中设置响应,并通过不同的注解返回静态页面、HTML片段和JSON数据,此外,还讲解了如何设置响应的状态码和Header... 目录1. 返回静态页面1.1 Spring 默认扫描路径1.2 @RestController2. 返回 html2

四种简单方法 轻松进入电脑主板 BIOS 或 UEFI 固件设置

《四种简单方法轻松进入电脑主板BIOS或UEFI固件设置》设置BIOS/UEFI是计算机维护和管理中的一项重要任务,它允许用户配置计算机的启动选项、硬件设置和其他关键参数,该怎么进入呢?下面... 随着计算机技术的发展,大多数主流 PC 和笔记本已经从传统 BIOS 转向了 UEFI 固件。很多时候,我们也