#winit #events #window #loops #target #game #draw

main_game_loop

A tool collection for building a winit game loop

21 releases (4 breaking)

0.6.1 Nov 27, 2022
0.6.0 Nov 7, 2022
0.4.6 Aug 23, 2022
0.4.5 Jul 12, 2022
0.1.1 Apr 20, 2022

#939 in Game dev

Download history 11/week @ 2024-01-08 4/week @ 2024-02-05 8/week @ 2024-02-12 8/week @ 2024-02-19 30/week @ 2024-02-26 19/week @ 2024-03-04 16/week @ 2024-03-11 14/week @ 2024-03-18 20/week @ 2024-03-25 40/week @ 2024-04-01 12/week @ 2024-04-08 9/week @ 2024-04-15

81 downloads per month
Used in 9 crates (via srs2dge-core)

MIT license

33KB
876 lines

main_game_loop

dependency status build status crates.io docs.rs

Example usage with some random Engine

struct App {
    window: Window,
    ws: WindowState,
    update_loop: UpdateLoop,
}

impl App {
    fn init(target: &EventLoopTarget) -> Self {
        let window = WindowBuilder::new().build(target).unwrap();
        let ws = WindowState::new(&window);
        let update_loop = UpdateLoop::new(UpdateRate::PerSecond(60));

        Self {
            window,
            ws,
            update_loop,
        }
    }

    fn event(&mut self, event: Event, _: &EventLoopTarget, control: &mut ControlFlow) {
        self.ws.event(&event);

        if self.ws.should_close {
            *control = ControlFlow::Exit;
        }
    }

    fn draw(&mut self) {
        self.update_loop.update(|| {
            // update();
        });

        // draw();
    }
}

fn main() {
    run_app!(App);
}

Example usage with different init, event and draw functions

use main_game_loop::prelude::*;

struct App {
    // ...
}

async fn init(target: &EventLoopTarget) -> App {
    // init

    App {
        // ..
    }
}

impl App {
    fn draw(&mut self) {
        // draw
    }
}

#[tokio::main]
async fn main() {
    run_app!(
        async init,
        |_, _, _, _| {
            // events
        },
        App::draw
    );
}

Dependencies

~6–19MB
~265K SLoC