Function pixel_game_lib::window::window

source ·
pub fn window<G, U, R>(
    game_state: G,
    window_config: WindowConfig,
    update: U,
    render: R
) -> Result<()>
where G: 'static, U: FnMut(&mut G, &WinitInputHelper, Option<Vec2<usize>>, f32) -> bool + 'static, R: FnMut(&mut G, &mut Canvas<'_>, f32) + 'static,
Expand description

Manually create a new window with an event loop and run the game.

For a more integrated and easier use it’s recommended to use crate::PixelGame.

If the audio feature is enabled this will also start a new audio backend.

§Arguments

  • game_state - Global state passed around in the render and update functions.
  • window_config - Configuration options for the window.
  • update - Function called every update tick, arguments are the state, window input event that can be used to handle input events, mouse position in pixels and the time between this and the previous tick. When true is returned the window will be closed.
  • render - Function called every render tick, arguments are the state and the time between this and the previous tick.

§Errors

  • When the audio manager could not find a device to play audio on.
Examples found in repository?
examples/font.rs (lines 29-51)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
fn main() {
    // Window configuration with default pixel size and scaling
    let window_config = WindowConfig {
        ..Default::default()
    };

    // Load a font for the widgets
    let font = font();

    // Open the window and start the game-loop
    pixel_game_lib::window(
        // We don't use any state so we pass a zero-sized type
        (),
        window_config.clone(),
        // Update loop exposing input events we can handle, this is where you would handle the game logic
        |_state, input, _mouse, _dt| {
            // Exit when escape is pressed
            input.key_pressed(KeyCode::Escape)
        },
        // Render loop exposing the pixel buffer we can mutate
        move |_state, canvas, _dt| {
            // Draw the text at the center of the screen
            font.render_centered(
                "pixel-game-lib font example",
                Vec2::new(
                    window_config.buffer_size.w / 2,
                    window_config.buffer_size.h / 2,
                )
                .as_(),
                canvas,
            );
        },
    )
    .expect("Error opening window");
}
More examples
Hide additional examples
examples/draw.rs (lines 17-70)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
fn main() {
    // Window configuration with huge pixels
    let window_config = WindowConfig {
        buffer_size: Extent2::new(64, 64),
        scaling: 8,
        ..Default::default()
    };

    // Open the window and start the game-loop
    pixel_game_lib::window(
        // Keep track of the mouse as our "game state"
        Vec2::zero(),
        window_config.clone(),
        // Update loop exposing input events we can handle, this is where you would handle the game logic
        |state, input, mouse, _dt| {
            // Set the mouse position as the game state
            if let Some(mouse) = mouse {
                *state = mouse;
            }

            // Exit when escape is pressed
            input.key_pressed(KeyCode::Escape)
        },
        // Render loop exposing the pixel buffer we can mutate
        move |mouse, canvas, _dt| {
            // Reset the canvas with a white color
            canvas.fill(0xFFFFFFFF);

            // Draw a gray circle with the radius being the distance of the center to the mouse
            let circle_center = Vec2::new(50.0, 50.0);
            let dist_from_mouse = mouse.as_().distance(circle_center);
            canvas.draw_circle(Disk::new(circle_center, dist_from_mouse), 0xFF999999);
            // Draw a darker gray circle outline on top of the circle
            canvas.draw_circle_outline(Disk::new(circle_center, dist_from_mouse), 0xFF333333);

            // Draw a light green blue triangle with one corner being snapped to the mouse
            canvas.draw_triangle(
                [Vec2::new(45.0, 5.0), Vec2::new(60.0, 8.0), mouse.as_()],
                0xFF99FF99,
            );

            // Draw a light blue quadrilateral with one corner being snapped to the mouse
            canvas.draw_quad(
                [
                    Vec2::new(5.0, 5.0),
                    Vec2::new(20.0, 8.0),
                    Vec2::new(8.0, 30.0),
                    mouse.as_(),
                ],
                0xFF9999FF,
            );

            // Draw a black line from the center of the canvas to our mouse
            canvas.draw_line(
                (window_config.buffer_size.as_() / 2.0).into(),
                mouse.as_(),
                0xFF000000,
            );

            // Draw a red pixel under the mouse
            canvas.set_pixel(mouse.as_(), 0xFFFF0000);
        },
    )
    .expect("Error opening window");
}