1

I'm creating a game with processing java. I optimized the game as much as I could making sure the image textures are very small, drawing only certain portions of the map, etc., and the game runs consistently at 60 FPS. However, when I want to draw an image across the entire screen, for an example, as a tinted overlay (as seen from the image below)

enter image description here

the FPS significantly decreases, going from 60 FPS to around 40 FPS. The same happens if I use a fullscreen graphic, like a rect(0, 0, width, height) the FPS will still decrease when the graphic is quite large, spanning the width of the entire screen. Literally something as simple as the code below causes lag.

PImage fullscreenImg;

void setup() {
    size(displayWidth, displayHeight);
    fullscreenImg = loadImage("img.png");
}

void draw() {
    image(fullScreenImg, 0, 0, width, width);
} 

Here's a video of the lag happening when a full width image is displayed (the FPS goes from ~30 to ~20): https://www.youtube.com/watch?v=bjKFIgb2fII

I've tried to solve this problem by using the get() function, or reducing the resolution of the image (which just causes the image to be more pixely), and none of it works; the FPS still stays at around 40. Is there any way to make an image that has a very wide width, in my case, covering the entire screen, without decreasing the FPS? Am I doing something wrong?

Thanks for any help!

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Scollier
  • 575
  • 6
  • 19
  • It's hard to test things without running the game. Have you tried `size(displayWidth, displayHeight, P2D);` just in case ? (Ideally you'd profile your game with a tool like [jvisualvm](https://visualvm.github.io/) to get an exact picture of where the slowdown happens). – George Profenza Jul 28 '22 at 23:27
  • Try P2D or FX2D renderers. – micycle Jul 29 '22 at 08:49

1 Answers1

1

Make sure you are not loading the image during the draw() method. All of your images should load in your setup() function, and should be stored in a variable (memory), before the main loop executes. Otherwise the image will have to be pulled from the disk every time the game loops, which takes much longer than pulling from memory.

Hopefully this helps, I would recommend posting code samples rather than screenshots of the game (although the game looks very nice), otherwise it is a bit hard to diagnose the issue.

  • Hey Raymond, I've made sure to load the image from the `setup()` function which gets called once and there is still lag. As for adding some code, I added some to the post, hopefully that should help. – Scollier Jul 28 '22 at 23:15
  • Try using the 3-argument version of image() in your draw() method. What may be happening is it’s resizing the image every frame, which is more expensive. Instead, use the 3-argument version of loadImage in your setup() to resize it from the beginning. Here’s a thread that goes into it in more detail https://discourse.processing.org/t/image-is-slow-are-there-any-alternatives/20146/11 – Raymond Keating Jul 29 '22 at 13:32
  • Thanks for the response Raymond. This did help, so now instead of the program running at 30 FPS, it runs at around 40 FPS. This still isn't optimal, so I'll continue to search for other ways to make the image performance better. – Scollier Jul 29 '22 at 21:29