0

i am creating a game in rust using bevy 0.10.1. I have tried to manually set the window to fit the screen. However, if i am distrubuting this game, i can't set the sizes for all screens. Also, when I try to fullscreen the normal small window, the game objects that use the window width and height still seem to use the old width and height.

is there a way to automatically fit the window to the sreen the moment it is created or force bevy to update the width and hight of the Window struct?.

Shawn
  • 1

1 Answers1

0

These are multiple questions in one. I will provide an answer for how to create a fullscreen window and get its size.

This can be done like demonstated in the example below:

use bevy::{
    prelude::*,
    sprite::MaterialMesh2dBundle,
    window::{WindowMode, WindowResized},
};

fn main() {
    let window_plugin = WindowPlugin {
        primary_window: Some(Window {
            title: "Fullscreen Test".into(),
            mode: WindowMode::Fullscreen,
            ..default()
        }),
        ..default()
    };

    App::new()
        .add_plugins(DefaultPlugins.set(window_plugin))
        .add_startup_system(setup)
        .add_system(window_resize_system)
        .run();
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    commands.spawn(Camera2dBundle::default());
    commands.spawn(MaterialMesh2dBundle {
        mesh: meshes.add(Mesh::from(shape::Quad::default())).into(),
        transform: Transform::default().with_scale(Vec3::splat(128.)),
        material: materials.add(ColorMaterial::from(Color::PURPLE)),
        ..default()
    });
}

fn window_resize_system(resize_event: Res<Events<WindowResized>>) {
    let mut reader = resize_event.get_reader();
    for e in reader.iter(&resize_event) {
        println!("width = {} height = {}", e.width, e.height);
    }
}

Update because of OP comment

Here is a sample for how to create a maximzed window:

use bevy::{
    prelude::*,
    sprite::MaterialMesh2dBundle,
    window::{WindowMode, WindowResized},
};

fn main() {
    let window_plugin = WindowPlugin {
        primary_window: Some(Window {
            title: "Fullscreen Test".into(),
            mode: WindowMode::Windowed,
            ..default()
        }),
        ..default()
    };

    App::new()
        .add_plugins(DefaultPlugins.set(window_plugin))
        .add_startup_system(setup)
        .add_system(window_resize_system)
        .run();
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
    mut windows: Query<&mut Window>,
) {
    let mut window = windows.single_mut();
    window.set_maximized(true);

    commands.spawn(Camera2dBundle::default());
    commands.spawn(MaterialMesh2dBundle {
        mesh: meshes.add(Mesh::from(shape::Quad::default())).into(),
        transform: Transform::default().with_scale(Vec3::splat(128.)),
        material: materials.add(ColorMaterial::from(Color::PURPLE)),
        ..default()
    });
}

fn window_resize_system(resize_event: Res<Events<WindowResized>>) {
    let mut reader = resize_event.get_reader();
    for e in reader.iter(&resize_event) {
        println!("width = {} height = {}", e.width, e.height);
    }
}
frankenapps
  • 5,800
  • 6
  • 28
  • 69