nannou-org / nannou

A Creative Coding Framework for Rust.
https://nannou.cc/
6.05k stars 306 forks source link

LoopMode set to loop_once actually loops twice #913

Open dvidbruhm opened 1 year ago

dvidbruhm commented 1 year ago

I am trying to draw a sketch that requires to loop only once, but whatever I try the view function is always called twice.

Here is a minimal example:

use nannou::prelude::*;

fn main() {
    nannou::app(model)
        .loop_mode(LoopMode::loop_once())
        .simple_window(view)
        .run()
}

struct Model {}

fn model(_app: &App) -> Model {
    Model {}
}

fn view(app: &App, _model: &Model, frame: Frame) {
    println!("{}", frame.nth());
    let draw = app.draw();
    draw.ellipse();
    draw.to_frame(app, &frame).unwrap();
}

This prints the frame number twice (0 and 1) instead of once. Is this normal or am I missing something? I also tried using LoopMode::loop_ntimes(1) but it behaves the same.

For now my workaround is to check for the frame number and return at the beginning of the view function:

if frame.nth() > 0 {
    return;
}

I am running nannou 0.18.1 on Fedora (X11) and rust nightly (but the issue is still present on stable).

danwilhelm commented 1 year ago

I tried it on Windows, and it only showed 0. I noticed it does redraw it every time there is a change to the window (e.g. resize, minimize, maximize). Perhaps Fedora is adjusting the window after creation?

dvidbruhm commented 1 year ago

Thanks for trying, good to know that it works on Windows. Must be fedora (or Gnome) that does something with the window.