Draw multiple objects with piston

i'm using piston and trying to draw 2 clock.

i created a struct clock and then i implement a draw function as such :

fn draw(&mut self, e: &Event, window: &mut PistonWindow) {
        window.draw_2d(e, |c, g, _| {...}); //draw a circle and a rotating line 

in main i have the following :

    let mut app = clock::new();
    let mut app2 = clock::new();
    app2.x += 200.0; //translate seconde clock
    let mut now = Instant::now();
    while let Some(e) = window.next() {
        app.draw(&e, &mut window);

        app2.draw(&e, &mut window);

        now = Instant::now();

but there is only 1 clock that appears, here is the entire code if nedeed
What i am doing wrong ?
thanks for your feed back

I don't know why only one clock is drawn (I'm on mobile now, so I can't test).
But I noticed that you get a new value for now each time in the end of the loop. I would recommend to always work with an initial time stamp.
As it is now, the two clocks will diverge (the first one will run slower than the second one), as it requests the elapsed time earlier in the loop.

thank you,
i corrected the timer (fun fact : the angular speed isn't constant anymore).
Still working on drawing 2 time the same struct, i try defining a second struc for the second clock, it doesn't work. I tried using the opengl backend directly and the viewport to draw instead of window.draw_2d. Also doesn't work.
I don't understand the concept of piston where am i drawing ? , the documentation isn't really helpful.

Your code attempt to draw two things into the same window (without coordinating it).
in the draw function the screen is cleared:

            clear([1.0; 4], g);

so nothing from before the last draw call will be visible.

1 Like

Indeed, everythink works perfectly now. Thank you !

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.