So I started a project and my goals is to make it run on all computers (Even potatos).
But I already got an (unexpected) problem after I finished my draw method.
I am drawing a grid of 100 tile (each 32*32). And I am getting an fps of 12.
I run this method once when the program starts.
public static void Prepare() {
Main.canvas.createBufferStrategy(2);
//Also tested with 3
}
Then I run this at the begin of each frame.
public static void StartDrawing() {
buffer = Main.canvas.getBufferStrategy();
g = buffer.getDrawGraphics();
g.setColor(new Color(0, 0, 0));
g.fillRect(0, 0, Main.canvas.getWidth(), Main.canvas.getHeight());
}
Then I draw everything with this method.
public static void DrawTex(BufferedImage image, int x, int y, boolean lighted) {
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
int color = image.getRGB(i, j);
int red = (color & 0x00ff0000) >> 16;
int green = (color & 0x0000ff00) >> 8;
int blue = (color & 0x000000ff);
int alpha = (color>>24) & 0xff;
if(lighted){
int lamp = 0;
for(Light l : Main.app.lights){
int xdist = x+i-l.getX();
int ydist = y+j-l.getY();
if(xdist > -l.getRadius() && xdist < l.getRadius()){
if(ydist > -l.getRadius() && ydist < l.getRadius()){
lamp = lamp + l.getBrightness();
}
}
}
red = red - Light.getDayLight() + lamp;
green = green - Light.getDayLight() + lamp;
blue = blue - Light.getDayLight()+ lamp;
if(red < 0){
red = 0;
}
if(green < 0){
green = 0;
}
if(blue < 0){
blue = 0;
}
}
Color c = new Color(red, green, blue, alpha);
g.setColor(c);
g.fillRect((i + x) - Player.getX() + Main.canvas.getWidth() / 2 - 16, (j + y) - Player.getY() + Main.canvas.getHeight() / 2 - 16, 1, 1);
}
}
}
And at the end of the frame Irun this method.
public static void Output(){
g.dispose();
buffer.show();
}
How can I make this more optimal?