Class making program really slow

Hello forum users,

Today am I working on a file where I have to create mountains using the noise function. I made this finally work but now am I struggeling with the fact that the class is slowing my program down a lot.

Mountains mountains = new Mountains();

void setup(){
 mountains.drawMountain();
size(1200,800);

void draw(){

 mountains.drawMountains();
}


class Mountains {
  float m[] = new float[1200];
  float yoff = 0;
  float yincrement = 0.005;
  float noiseVar = 1;
  color mountaincolor = color(51, 153, 51);


  Mountains() {
  }

  void drawMountain() {

    for (int i=0; i<width; i++) {
      m[i] = noise(yoff)*height;
      yoff += yincrement;
    }
  }

  void drawMountains() {
    for (int i=0; i<width; i++) {
      stroke(mountaincolor);
      fill(mountaincolor);
      line(i, m[i], i, height);
    }
  }
}

Is there a way to get rid of this?

Greetings Niek Boersen

Tagged:

Answers

  • noLoop(). your drawing doesn't change, there is no interaction, you have no background() call, so why redraw it 60 times a second?

    lines 34 and 35 could be outside the loop given that they don't change within the loop.

  • Yes but this is not the complete code, I do have interactions which need some a good framerate to work. Can you help with that ?

  • without seeing the code? no

  • edited June 2017

    Okay I will post the code down below :) Thanks again https://gist.github.com/niekboersen/6a4b1f0efe1df30654fa9e88c85ba64f

  • Duplicate nested type: Snowflake.

    The file "leave.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.

    The file "snowflakepic1.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.

    The file "snowflakepic2.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.

    The file "grass.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.

  • then there are the 24 images in the animation...

  • anyway, i think the main problem is just that you're adding 1200 green lines to your sketch.

    that they never change may help you - draw them once to a pgraphic and use that as a background image. you can combine it with the grass image to save you having to draw that every frame as well.

  • edited June 2017

    @niekboersen --

    I think that @koogs is right -- skip all that rerendering by drawing to a PGraphics and then displaying it with image(). Here is an example based on your short sketch above:

    Mountains mountains;
    
    void setup() {
      size(1200, 800);
      mountains = new Mountains();
    }
    
    void draw() {
      background(192,192,255);
      image(mountains.draw(), 0, 0);
    }
    
    void keyPressed(){
      mountains.update();
    }
    
    class Mountains {
      PGraphics pg = createGraphics(width, height);
      float m[] = new float[width];
      float yoff = 0;
      float yincrement = 0.005;
      float noiseVar = 1;
      color mountaincolor = color(51, 153, 51);
      Mountains() {
        update();
      }
      void update(){
        calcMountains();
        renderMountains();
      }
      void calcMountains(){
        for (int i=0; i<width; i++) {
          m[i] = noise(yoff)*height;
          yoff += yincrement;
        }
      }
      void renderMountains(){
        pg.beginDraw();
        pg.clear();
        for (int i=0; i<width; i++) {
          pg.stroke(mountaincolor);
          pg.fill(mountaincolor);
          pg.line(i, m[i], i, height);
        }
        pg.endDraw();
      }
      PGraphics draw(){
        return pg;
      }
    }
    
Sign In or Register to comment.