We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › refresh issues with my program
Page Index Toggle Pages: 1
refresh issues with my program (Read 928 times)
refresh issues with my program
May 27th, 2009, 9:07pm
 
Hello,

I'm band new to processing and was wondering if someone could give me some advice on this little program.

I am in the early stages of an animation timeline interface. If you run the code below and click and drag on the back line, you will notice a lag/delay as the position of the frame number tries to keep up with the position of the black line.

I'm wondering if my code is on the right track, or if there is a way to optimise it so there is no percievable lag?

Here is the Code:
Code:
int frame = 0;
int val = 0;
void  setup(){
 size(500,100);
 background(204);
 smooth();
 strokeWeight(4);
 line(0, 0, 0, 100);
 PFont font;
 // The font must be located in the sketch's
 // "data" directory to load successfully
 font = loadFont("ArialMT-48.vlw");
 textFont(font);
 fill(0, 102, 153);
 text("0", 10, 65);
}

void draw()
{

 if (mousePressed == true){
   background(204);
   frameNumText(mouseX, frame + 10, 65);
   line(mouseX, 0, mouseX, 100);
   frame = (mouseX);
 }


 if(keyPressed == true){
   background(204);
   line(frame + val, 0, frame + val, 100);
   frame = frame + val; //update the frame var to the current position
   frameNumText(frame, frame + 10, 65);
   delay(60); // a slight delay to allow for human reflexes
 }


}

void frameNumText(int num, int X, int Y)
{  
 PFont font;
 // The font must be located in the sketch's
 // "data" directory to load successfully
 font = loadFont("ArialMT-48.vlw");
 textFont(font);
 fill(0, 102, 153); // a light grey colour
 text(num, X, Y); //text position based on the input arguments
}


void keyPressed() {
 if (key == CODED) {
   if (keyCode == LEFT) { //if the left key is pressed
     val = (-1); // take one of the current value
   }
   else if (keyCode == RIGHT) { //if the right key is pressed
     val = (+1); //add one of the current value
   }
   else{
     val = 0; //set the val to zero so there is no effect.
   }

 }

}
Re: refresh issues with my program
Reply #1 - May 27th, 2009, 10:10pm
 
One step:
Remove the font stuff from frameNumText()
Smiley

Otherwise it will reload the font as long as a key or mouse is pressed.
Re: refresh issues with my program
Reply #2 - May 27th, 2009, 10:46pm
 
Thanks! That definately helps. I am still noticing a slight lag though.

Is there any way I can stucture the code so that line and the fonts are drawn simultaniously so they are completely locked to each other?

Re: refresh issues with my program
Reply #3 - May 28th, 2009, 1:39am
 
The only time I noticed a lag was with a key pressed, which makes sense. :]

You could use keyPressed() and keyReleased() to toggle your own boolean instead of using the built in keyPressed.

Unless you are having trouble elsewhere...
Re: refresh issues with my program
Reply #4 - May 28th, 2009, 3:08am
 
Yes, I think I am having trouble elsewhere.

Maybe lag is the wrong word for it. I am wanting to keep the relative distance between the number and the black line the same at all times.

If you click and drag the line back and forth very quickly, you can see one object lagging behind the other. It's a minor detail. But for such a simple piece of code, I was hoping that there would be a way to keep the two objects locked to each other in the spacial sence.

Here's the code again with the adjustments you suggested.
Code:
int frame = 0;
int val = 0;
void  setup(){
 size(500,100);
 background(204);
 smooth();
 strokeWeight(4);
 line(0, 0, 0, 100);
 PFont font;
 // The font must be located in the sketch's
 // "data" directory to load successfully
 font = loadFont("ArialMT-48.vlw");
 textFont(font);
 fill(0, 102, 153);
 text("0", 10, 65);
}

void draw()
{

 if (mousePressed == true){
   background(204);
   frameNumText(mouseX, frame + 10, 65);
   line(mouseX, 0, mouseX, 100);
   frame = (mouseX);
 }


 if(keyPressed == true){
   background(204);
   line(frame + val, 0, frame + val, 100);
   frame = frame + val; //update the frame var to the current position
   frameNumText(frame, frame + 10, 65);
   delay(100); // a slight delay to allow for human reflexes
 }


}

void frameNumText(int num, int X, int Y)
{  
// PFont font;
 // The font must be located in the sketch's
 // "data" directory to load successfully
// font = loadFont("ArialMT-48.vlw");
 //textFont(font);
 //fill(0, 102, 153); // a light grey colour
 text(num, X, Y); //text position based on the input arguments
}


void keyPressed() {
 if (key == CODED) {
   if (keyCode == LEFT) { //if the left key is pressed
     val = (-1); // take one of the current value
   }
   else if (keyCode == RIGHT) { //if the right key is pressed
     val = (+1); //add one of the current value
   }
   else{
     val = 0; //set the val to zero so there is no effect.
   }

 }

}




Re: refresh issues with my program
Reply #5 - May 28th, 2009, 9:00am
 
Ah, I see it now.
When you set "frame = (mouseX);" after the call to frameNumText(), the text position will be at the position from the last time draw() was run.

Setting that before frameNumText() should get rid of the space. But then, you could call it directly with mouseX.
Re: refresh issues with my program
Reply #6 - May 28th, 2009, 5:29pm
 
Wow! You have a good eye. It works great now, and you were right about using mouseX directly too.

Thanks NoahBuddy!

Dan. Wink
Page Index Toggle Pages: 1