nannou-org/nannou

`view()` is called multiple times despite setting `loop_once()`

nebocco opened this issue · 1 comments

Hi, I found the titled issue when I was running my code on WSL Ubuntu:

use nannou::prelude::*;

fn main() {
    nannou::app(model).update(update).run();
}

struct Model(u32);

fn model(app: &App) -> Model {
    app.set_loop_mode(LoopMode::loop_once());
    let _window = app.new_window().view(view).build().unwrap();
    Model(0)
}

fn update(_app: &App, model: &mut Model, _update: Update) {
    model.0 += 1;
}

fn view(app: &App, model: &Model, frame: Frame) {
    println!("{}", model.0);
    let draw = app.draw();
    draw.background().color(WHITE);
    draw.ellipse().radius(100.0).color(BLUE);
    draw.to_frame(app, &frame).unwrap();
}

and the output was:

% cargo run --release
   Compiling loop_test v0.1.0 (/home/user/path/to/dir)
    Finished release [optimized] target(s) in 1.23s
     Running `/home/user/path/to/dir`
1
1
1
1
1

It shows that update() is called only once, as expected, while view() is called multiple times. In this example, view() is very simple, so there is no problem with its behavior. However, if you are placing particles in a full memory, the second call to view() may cause memory depletion. Is this intended behavior?

Environment:

  • OS: Win 10 Home 22H2, 19045.2913
  • WSL: 1.2.5.0
  • WSLg: 1.0.51
  • Ubuntu on WSL: 22.04.2 LTS
  • rust: 1.69.0
  • nannou: 0.18.1

I wasn't able to reproduce the issue. My understanding of Nannou is that on some window events such as Resized, the view function gets called again. Try modifying your code to add logging in the event handler and see what happens between those print statements.

use nannou::prelude::*;

fn main() {
    nannou::app(model)
    .event(event)
    .update(update).run();
}

struct Model(u32);

fn model(app: &App) -> Model {
    app.set_loop_mode(LoopMode::loop_once());
    let _window = app.new_window().view(view).build().unwrap();
    Model(0)
}

fn event(_app:&App, _model: &mut Model, event: Event){
    println!("{:?}", event)
}

fn update(_app: &App, model: &mut Model, _update: Update) {
    model.0 += 1;
}

fn view(app: &App, model: &Model, frame: Frame) {
    println!("{}", model.0);
    let draw = app.draw();
    draw.background().color(WHITE);
    draw.ellipse().radius(100.0).color(BLUE);
    draw.to_frame(app, &frame).unwrap();
}