I am trying to create an app that can be resized depending on the user’s preference.
The app for now is going to be a grid that can be moved around. At the default (and small) size, it runs pretty well and fast. But when I enlarge the window to fullscreen or just larger, it begins to slow down, and the framerate drops.
Can I fix this code so it can appear fast on all sizes? I hope to get this working on processing otherwise i’ll have to learn pure JavaFX or perhaps OpenFrameworks…
sketch (pde) file:
Grid grid;
float x, y, dx, dy, ox, oy, cx, cy;
void setup()
{
size(800, 600, FX2D);
surface.setResizable(true);
frameRate(60);
smooth(1);
grid = new Grid();
x = 0;
y = 0;
cx = 0;
cy = 0;
ox = 0;
oy = 0;
}
void draw()
{
background(40, 48, 50);
translate(ox + x, oy + y);
grid.ox = floor(ox + x);
grid.oy = floor(oy + y);
grid.draw();
fill(255);
ellipse(width / 2, height / 3, 20, 20);
}
void mousePressed()
{
cx = mouseX;
cy = mouseY;
ox = 0;
oy = 0;
}
void mouseDragged()
{
ox = mouseX - cx;
oy = mouseY - cy;
}
void mouseReleased()
{
x += mouseX - cx;
y += mouseY - cy;
ox = 0;
oy = 0;
}
grid (pde) file:
class Grid
{
float size;
float weight;
color c;
int startX, startY, ox, oy;
Grid()
{
size = 40;
weight = 4;
c = color(60, 68, 70);
startX = 0;
startY = 0;
ox = 0;
oy = 0;
}
void draw()
{
startX = floor((ox % size) - size);
startY = floor((oy % size) - size);
loadPixels();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int index = x + y * width;
if ((y - startY) % size < weight)
{
pixels[index] = c;
}
else if ((x - startX) % size < weight)
{
pixels[index] = c;
}
}
}
updatePixels();
}
}
Note: I used pixels because I thought it would work faster but it doesn’t seem like it anymore…
I have tried using each of these renderers: default, P2D, P3D, OPENGL, and FX2D and all yield the same laggy result with a larger resolution.
My operating system is a Windows 10 Pro, and I have NVidia graphics card with an i7 8th gen and so I see no reason why it should be laggy. Maybe there is a better way to write this or optimize it?
My goal is to have hundreds of objects drawn on top of the grid, so I need it to work fast right now.
Do you have any input or feedback? Anything will be greatly appreciated!