Can anyone here explain why i only get 30FPS when i try to draw my map. The problem is this line:
g.drawImage(image[ (map[y][x][0]) ], (x * 32), (y * 32), null);
If i remove it everything runs fine in around 60FPS wich is what i want. Tile.gif is just a normal 16x16 gif file
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class T extends JFrame{
BufferedImage[] image = new BufferedImage[1];
int[][][] map = new int[100][100][1];
T(boolean full) throws Exception{
super("Game");
BufferedImage bi = ImageIO.read(new File(System.getProperty("java.class.path") + "/tile.gif"));
image[0] = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
image[0].getGraphics().drawImage(
bi.getSubimage(0, 0, 16, 16), 0, 0, 32, 32, null);
setUndecorated(full);
setIgnoreRepaint(true);
if(full){
GraphicsDevice device = getGraphicsConfiguration().getDevice();
device.setFullScreenWindow(this);
device.setDisplayMode(new DisplayMode(640, 480, 16, 0));
}
else{
setSize(640, 480);
show();
}
createBufferStrategy(2);
BufferStrategy strategy = getBufferStrategy();
int[] colors = {0x00ffff, 0x000000};
int fps = 0;
long time = System.currentTimeMillis();
long runTime = System.currentTimeMillis();
long lastFps = 0;
int drFps = 0;
Graphics2D g;
while(System.currentTimeMillis() - runTime < 10000){
long delta = System.currentTimeMillis() - time;
time = System.currentTimeMillis();
lastFps += delta;
fps++;
if(lastFps >= 1000){
drFps = fps;
fps = 0;
lastFps = 0;
}
g = (Graphics2D)strategy.getDrawGraphics();
g.setColor(new Color(0x0000ff));
g.fillRect(0, 0, 640, 480);
int startx = 0;
int maxx = startx + (640 / 32) + 1;
int starty = 0;
int maxy = starty + (480 / 32) + 1;
for(int y = 0; y < maxy; y++){
for(int x = startx; x < maxx; x++){
g.drawImage(image[ (map[y][x][0]) ], (x * 32), (y * 32), null);
}
}
g.setColor(new Color(0xffffff));
g.drawString("FPS: " + drFps, 2, 40);
g.dispose();
strategy.show();
Thread.yield();
}
System.exit(0);
}
public static void main(String[] args) throws Exception{
new T(true);
}
}