We are about to switch to a new forum software. Until then we have removed the registration on this forum.
How to GET more FPS ??? I dont know how to make program better :/ can you help me ?
PImage light, screen, bg, lightImage, mask;
PGraphics pg, lightMask;
int up, down, right, left;
float x, y, speed = 1;
color randomColor;
float camX=0, camY=0;
float Ox1, Ox2, Oy1, Oy2;
int globalMX, globalMY;
void setup() {
size(480, 270, P3D);
frameRate(60);
background(0);
bg = loadImage( "https://" + "ichef.bbci.co.uk/images/ic/480xn/p01lckx1.jpg" );
lightImage = loadImage( "https://" + "i.stack.imgur.com/NaD6F.png" );
bg.resize(width, height);
light = bg.copy();
lightMask = createGraphics(width, height);
randomColor = color(random(256), random(256), random(256));
bullets = new ArrayList<Bullet>();
reloadLights();
x = width/2;
y = height/2;
}
void draw() {
background(0);
imageMode(CORNER);
image(bg, 0, 0);
screen = get(0, 0, width, height);
fill(0,125);
noStroke();
rectMode(CENTER);
rect(x, y, width, height);
reloadLights();
screen.mask(lightMask);
image(screen, x-width/2,y-height/2);
fill(255, 255, 0);
textAlign(LEFT, TOP);
textSize(39);
text(int(frameRate) + " FPS", 5, 5);
ellipse(globalMX,globalMY,20,20);
x += (right - left) * speed;
y += (down - up) * speed;
Ox1=camX-width+x;
Ox2=camX+x;
Oy1=camY-y;
Oy2=camY+height-y;
ortho(Ox1, Ox2, Oy1, Oy2);
}
void reloadLights() {
pg = lightMask;
pg.beginDraw();
pg.background(0);
pg.blendMode(ADD);
pg.translate(-x+width/2,-y+height/2);
for (int i = bullets.size()-1; i >= 0; i--) {
Bullet bullet = bullets.get(i);
drawLight(bullet.x, bullet.y, bullet.r);
println(bullet.x + " " + bullet.y);
}
pg.endDraw();
}
void drawLight(int xL, int yL, int rL) {
// pushMatrix();
pg.imageMode(CENTER);
pg.image(lightImage, xL,yL, rL, rL);
//popMatrix();
}
void mousePressed() {
bullets.add( new Bullet(globalMX, globalMY, 50));
reloadLights();
}
ArrayList<Bullet> bullets;
class Bullet {
int x, y, r;
public Bullet(int x_, int y_, int r_) {
x = x_;
y = y_;
r = r_;
}
}
void keyPressed() {
if (key == 'a' || key == 'A') {
left = 1;
}
if (key == 'd' || key == 'D') {
right = 1;
}
if (key == 'w' || key == 'W') {
up = 1;
}
if (key == 's' || key == 'S') {
down = 1;
}
}
void keyReleased() {
if (key == 'a' || key == 'A') {
left = 0;
}
if (key == 'd' || key == 'D') {
right = 0;
}
if (key == 'w' || key=='W') {
up = 0;
}
if (key == 's' || key == 'S') {
down = 0;
}
}
void mouseMoved() {
globalMX=mouseX+int(x)-(width/2);
globalMY=mouseY+int(y)-(height/2);
}
Answers
Here it is images:
I am getting close to 60fps in my machine. Anyhow, test again without using the print call and you can also remove the call to
reloadLights()
in L33 as it is done in your mouse event function.Kf
but when i move its not working well
println
can be a big problem -- make sure to take it out of loops before speed testing.I am not sure where you move the bullets
But if they leave the screen you should remove them - use a backward for loop over your ArrayList and say bullets.remove(i); when bullet i is outside the screen
read kfrajer's answer again.
post the current code.
I am also not sure about these lines:
you seem to draw images that cover the whole screen again and again
^ transparency?
the big problem is redoing the light mask every frame, line 33, when it only changes (and is already recalculated) on mouse press.
but kfrajer already said this over a week ago``