Problems with framerate and loading pictures

Hi I am trying to create a game called “Undertale” as a project at my school however my framerate drops every time I load a picture in draw. Here is a part of my code:

PImage img;
PImage img2;

void setup()
{
 fullScreen();
 img=loadImage("1.jpg");
 img2=loadImage("2.jpg");
}

void draw()
{
  background(0);
  println(frameRate);
  
  if (frameCount<420)
  {
    image(img,width/2,300,700,500);
  }
  
  if (frameCount>420)
  {
    if(img2==null) img2=loadImage("2.jpg");
    image(img2,width/2,300,700,500);
  }
}
1 Like

Yes, loading a picture takes a lot of time, because you have to read it from a file, create some space for it in the memory and store it there. It is much faster if you preload your image in setup and then reuse it.

So try to avoid loadImage() in draw() and preload all the images you need in setup.

What is the purpose of loading the img2 again? Because you already loaded it in setup and I can’t see where you set it to null again.

Btw. also println(FrameRate) can be a performance killer. Try to display the framerate on the screen or at least through surface.setTitle(frameRate).

And why did you create a new thread for that, you already had one with the same topic, but different code:
https://discourse.processing.org/t/problems-with-framerate/8998/6

2 Likes

sorry i screwed up there haha… I’m just gonna remove the old one

I have tried to load everything in setup and it has improved the framerate with around 20% however i still do not hit the 60 frames pr. sec as intended

regarding your example, what is much shorter now,
but still not PASTE into

</> code tag

pls. repair that in your above post!


as you removed the text output, how did the frameRate change?
also make more tests ( to get a feeling what is COSTLY )

  • not use fullscreen
  • use image only with 3 parameter , not 5
    the image resize 60 times per sec is the wrong way.
    resize them in setup?? and in draw just show them
image(img,x,y);

here my test with fps log regarding

  • full screen or 800,800
  • image resize or not
// https://discourse.processing.org/t/problems-with-framerate-and-loading-pictures/9670

PImage img1, img2;

void setup() {
  //fullScreen();
  size(800,800);
  img1=loadImage("moonwalk.jpg");
  img2=loadImage("moonwalk.jpg");
}

void draw() {
  background(0);
  if (frameCount< 420) image(img1, width/2, 40, width/2, height/2);  //full: 6 fps  // 12fps
  if (frameCount==420) println("not use image resize");
  if (frameCount> 420) image(img2, width/2, 300);                    //full: 12 fps // 36fps
  fill(255);
  text(nf(frameRate,1,1)+" fps",width-100,20);
}

this test ‘here’ ( on my 40$ computer ) shows extreme values,
but good for learning.

as if we use your example code only,
what shows one picture, and later a other, the FPS

does not make any difference

or you complain?

with the low and varying fps the timing of your code not work as planned?

well, not forget that we show you some time ago better ways to build a timer
( what use millis() and not frameCount )
Help with delay/millis without influencing the framerate - #2 by kll !

1 Like

If you really want to track what takes a lot of time, then use a profiler like VisualVM. I recommend to sample your application and see which methods take a lot of time.

And it is really hard to help you, if we do not have an example which is runnable on our own computer. Just to mention, if you set the framerate to 60 FPS, processing will try to run it with 60 FPS, but usually you will be a bit under these 60 FPS (like 57-59) because it tries to fit in there.

2 Likes