QuantumBadger / Speedy2D

Rust library for hardware accelerated drawing of 2D shapes, images, and text, with an easy to use API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding an on_update method to the WindowHandler

d0rianb opened this issue · comments

I didn't found any solution to execute a function each tick of the the WindowEventLoop.
Is that possible to add an on_update() method to the WindowHandler trait. Or is it a more simple solution.
For now, the only solution I have found is to call the update function from the on_draw() method but this solution is not optimal because the window have to be redrawn each tick.

Thanks for the request. The Speedy2D event loop only wakes up when something happens (e.g. a mouse movement or a redraw request), so it doesn't tick in the background (it just remains idle).

If you'd like to perform a regular action in your app, one solution would be to spawn a new thread, which periodically sends a user event to the main thread. For example:

fn on_start(&mut self, helper: &mut WindowHelper<String>, _info: WindowStartupInfo)
{
    let event_sender = helper.create_user_event_sender();
    thread::spawn(move || {
        loop {
            // Send an event every 10ms
            event_sender.send_event(MyEvent{}).unwrap();
            thread::sleep(Duration::from_millis(10));
        }
    });
}

The WindowHandler::on_user_event() callback will be invoked every time send_event() is called. See the user_events.rs sample code for an example of this :)

Thanks, it works well.