How can you draw ~50 objects without reducing frameRate?

I have roughly 54 objects that have a function to draw rect() in draw(). So I have loop from 0 to 54 that do calculation to call the object function to draw rectangle for every frame. This caused the frameRate for this sketch dropped to 5.

Is there anyway so that I can update 54 objects or more that draw itself without reducing frameRate. I have been thinking to use multithreading but it always return null pointer exception in rect() and then the sketch runs but behaves very weird. What to do?

Answers

  • There might be a way to optimize performance, 54 rectangles should really not have a great impact. But without seeing your code, that's just a guess.

  • Can you post some code? It's a bit hard to tell why it's so slow.

  • Drawing 54 rectangles should not reduce the framerate, certainly not to 5fps. There is probably something wrong with your code, I suggest you post it here.

    I have been thinking to use multithreading

    It is most unlikely this would help because the 'drawing' has to be done in the main event thread. That might explain why you are getting NPEs

  • edited November 2015

    An example using Processing 3's "only" new feature: FXD2 renderer.
    Plus Comparable + sort() in order to achieve high performance: :ar!

    https://forum.Processing.org/two/discussion/comment/52645/#Comment_52645

  • edited November 2015

    I have been thinking to use multithreading but...

    • Processing's canvas got 2 distinct states: "drawing" & "rendering".
    • After draw() finishes and all other events are emptied/dequeued, sketch's "Animation" Thread enters its "rendering" state and the canvas' content shouldn't be changed at that moment!
    • However, since other threads can't know the exact time that crucial rendering period is taking place, they shouldn't directly modify sketch's canvas at all!
    • Most other threads can do is "drawing" to other PGraphics objects.
    • Then leave for draw() the task of image() them into the canvas.
  • edited November 2015

    I think the biggest problem, that you do not only draw your MuseumObjects. You create new instances of Panel every frame. you should only create the panel once and store it. Then update it, if it changes. But keep the drawing seperate from this.
    Another problem is, that you create fonts all the time in Panel.drawVoid(). You could do that once in the constructor instead.

    With some minor changes, your program runs with 60fps.

  • Another problem is, that you create fonts all the time in Panel.drawVoid(). You could do that once in the constructor instead.

    this

  • Well isn't that the most OCD formatted code I've ever seen! :O)

Sign In or Register to comment.