Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Bevy] 画面サイズを取得して、利用する

Posted at

自分向けのTipsです。

Rust製ゲームエンジンBevyをさわり始めたので、やりたいことを実現するためのコードをメモしていきたいと思います。

全体像は本記事の最後に記載しています。

リソース

ウィンドウサイズをアプリケーション全体で共有するため、リソースを準備します。

#[derive(Resource)]
struct WindowSize {
    width: f32,
    height: f32,
}

main関数でリソースを追加しておきます。

app.insert_resource(WindowSize {
    width: 0.0,
    height: 0.0,
});

ウィンドウリサイズのイベント

use bevy::window::WindowResized;

上記を追加しておいて、以下をUpdateのシステムへ加えます。
ウィンドウリサイズのイベントを処理して、取得した横幅縦幅をリソースのWindowSizeへ保存します。

fn window_resize_system(
    mut window_size: ResMut<WindowSize>,
    mut resize_reader: EventReader<WindowResized>,
) {
    for e in resize_reader.read() {
        window_size.width = e.width;
        window_size.height = e.height;
    }
}

テキスト表示

テキスト表示はStartup処理でエンティティ?を準備します。

ウィンドウ右下に文字が表示される設定です。

commands.spawn((
    Text::new("".to_string()),
    Node {
        position_type: PositionType::Absolute,
        bottom: Val::Px(5.0),
        right: Val::Px(5.0),
        ..default()
    },
    TestText,
));

最後に表示しているテキストを書き換える処理をUpdateのシステムへ加えます。

fn window_draw(
    window_size: Res<WindowSize>,
    mut query: Query<&mut Text>>,
) {
    for mut text in query.iter_mut() {
        **text = format!(
            "Window size: {} x {}",
            window_size.width, window_size.height
        );
    }
}

以上をあわせると以下のようなウィンドウが表示されます。

image.png

image.png

終わりに

まだ仕組みがわかっていないため、ふわふわとコードを書いています。

でも、見た目にわかりやすいものをつくるのは楽しいなと思いました。

それでは、ここまで読んでいただきありがとうございました。

以下は付録コードです。

付録:全体像

use bevy::prelude::*;
use bevy::window::WindowResized;

#[derive(Resource)]
struct WindowSize {
    width: f32,
    height: f32,
}

fn main() {
    let mut app = App::new();
    app.add_plugins(DefaultPlugins);
    app.insert_resource(WindowSize {
        width: 0.0,
        height: 0.0,
    });
    app.add_systems(Startup, setup);
    app.add_systems(Update, (window_resize_system, window_draw));
    app.run();
}

fn setup(
    mut commands: Commands,
) {
    commands.spawn(Camera2d);

    commands.spawn((
        Text::new("".to_string()),
        Node {
            position_type: PositionType::Absolute,
            bottom: Val::Px(5.0),
            right: Val::Px(5.0),
            ..default()
        },
    ));
}

fn window_resize_system(
    mut window_size: ResMut<WindowSize>,
    mut resize_reader: EventReader<WindowResized>,
) {
    for e in resize_reader.read() {
        window_size.width = e.width;
        window_size.height = e.height;
    }
}

fn window_draw(
    window_size: Res<WindowSize>,
    mut query: Query<&mut Text>>,
) {
    for mut text in query.iter_mut() {
        **text = format!(
            "Window size: {} x {}",
            window_size.width, window_size.height
        );
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?