Change fps when the button is pressed

I need to be able to change the fps while the program is running, is it possible? I think the code is right but it doesn’t work :face_with_raised_eyebrow:

import g4p_controls.*;

GImageToggleButton btn1,btn2,btn4,btn8;

public int fps = 30;

public void setup(){
  
 size(1200,700);
 frameRate(fps);
 //G4P.setCursor(ARROW);
   
  btn1 = new GImageToggleButton(this, 1050,400,"imagens/1x.png",1,1);
  btn2 = new GImageToggleButton(this, 1100,400,"imagens/2x.png",1,1);
  btn4 = new GImageToggleButton(this, 1050,450,"imagens/4x.png",1,1);
  btn8 = new GImageToggleButton(this, 1100,450,"imagens/8x.png",1,1);
  
 }

public void draw(){
  background(0);
  println(frameRate);
 }

public void handleToggleButtonEvents(GImageToggleButton button, GEvent event) { 
  //println(button.getState());
  if(button == btn1){
    println("botao 1x pressionado");
    fps = 30;
  }
  if(button == btn2){
  println("botao 2x pressionado");
  fps = 60;
  }
  if(button == btn4){
   println("botao 4x pressionado"); 
   fps = 120;
  }
  if(button == btn8){
   println("botao 8x pressionado"); 
   fps = 240;
  }
}
2 Likes

Hey There!

Yes it possible. But to change FPS in running time you need to call frame rate in draw.

2 Likes

haha, so simple! Thanks!!

1 Like

Note to add to update that you should only change the frame rates when you click each button. Rather than in raw draw. This will just save some resources.

1 Like

I set the frameRate() with each button press.

I have an NVIDIA card and with Vertical sync (On) the frame rate is limited to 60 (my monitor refresh rate). I can always use other settings if I wish; this was my setting at the time.

With Vertical sync (On):

size(1200,700);  //I can set 30, 60, 120, 240

size(1200,700, P2D); //I can set 30, 60 and it is limited to 60

size(1200,700, P3D); //I can set 30, 60 and it is limited to 60

Have fun!

4 Likes