Hello! I am working on a Falling Sand + Artillery game.
TL;DR: Why is stretching images over a higher resolution significantly more expensive, even if the original image res stays the same? Is there a smart way to render something on multiple layers and then upscale that without loss or blur?
To keep it brief: I am writing the game in a way that allows it to be scaled to basically any resolution, in addition to it allowing for different scaling of the output window.
For example, you could set an internal resolution of 320 x 240 and a window scale of 2, which results in the game content being rendered in multiple layers at 320 x 240, with all of those layers then getting upscaled to 640 x 480 as images.
This is what the crucial part of my main draw() loop looks like. The drawGame(), drawSand(), etc. methods draw the respective content to the layer, the res of which matches the internal resolution, and then all of the layers are drawn on top of each other, stretched to fit the window size.
case 1:
background(0);
if (init) startAnim(playMode);
heldInputs();
simulation();
drawGame();
drawSand();
drawTrails();
drawEffects();
drawGameUI();
image(gameLayer, 0, 0, WIDTH, GAME_HEIGHT);
image(sandLayer, 0, 0, WIDTH, GAME_HEIGHT);
image(trailLayer, 0 - (cameraX*WINDOW_SCALE), -(2*VSCREEN_GAME_HEIGHT) - (cameraY*WINDOW_SCALE));
image(effectsLayer, 0, 0, WIDTH, GAME_HEIGHT);
image(UILayer, 0, GAME_HEIGHT, WIDTH, HEIGHT);
//etc.
This is what drawGame() looks like, for instance:
void drawGame() {
gameLayer.beginDraw();
for (int y = 0; y < VSCREEN_GAME_HEIGHT; y++)
{
gameLayer.stroke((y+cameraY)/1.5, 15, y+cameraY+150);
gameLayer.line(0, y, VSCREEN_WIDTH, y);
}
for (Tank t : tanks) {
if (t.getAlive()) t.drawTank(tankWidth);
}
gameLayer.endDraw();
}
Of course, not all of these drawSomething() calls are super optimised, but something I find very odd is how changing the output res actually has a very strong effect on performance. I get around 67 FPS when using a res of 640 x 480 at double window scale, but when I reduce the window scale to 1, I get a perfect 120 FPS (the target performance), even though the computing workload for the layers doesn’t change at all. So the culprits are the image() calls, right? but why does simply upscaling an image (and without any AA at that) seemingly cost so many resources? Could someone give me a hint on how to optimise this?
Many thanks for reading until the end and have a great day ![]()
(In case you’re curious, this is what the game currently looks like. This particular shows the game running in 640 x 480 at a window scale of 1. The separate layers are, from drawn first to last: Sky and Tanks, Sand, Trails (the dotted lines in the sky), Effects (includes the coloured name tags), UI)


