What i´m trying to do here is rendering all graphics in my game as fast as possible.
The game is tile-based and needs a lot of images to be rendered.
The problem i´m having is that it takes to long to draw all the images.
I don´t know if i´m supposed to draw the tiles in a special way but right now i´m rendering all tiles that are inside of the view port one by one.
Here is my drawing method:
public void draw(Graphics2D g){
boolean drawn=false; // if player is drawn
int px=vpPosX, py=vpPosY; // Viewport Position
for(int y=Math.round(py/64); y<Math.round(py/64)+core.blcHeight; y++){
for(int x=Math.round(px/64); x<Math.round(px/64)+core.blcWidth; x++){
if(y<height && x<width && y>-1 && x>-1 && worldData[currentLayer][x][y] != 0){ // if tile is inside map
BufferedImage img = tileset.tiles[worldData[currentLayer][x][y]]; //tile image
if(-py+player.PosY+64-15 < (y*64)-py-(img.getHeight())+64 && drawn==false){player.draw(g);drawn=true;} // if player should be drawn
if(worldBackground[currentLayer][x][y]!=0){ // if tile has background
BufferedImage imgBack = tileset.tiles[worldBackground[currentLayer][x][y]]; // background tile
g.drawImage(imgBack, (x*64)-px-(((imgBack.getWidth())-64)/2), (y*64)-py-(imgBack.getHeight())+64, null); //draw background
}
g.drawImage(img, (x*64)-px-(((img.getWidth())-64)/2), (y*64)-py-(img.getHeight())+64, null); // draw tile
}
}
}
if(!drawn){player.draw(g);} // if drawn equals false draw player
}
All of the images are loaded in the tileset class.
The games fps/ups is locked at 60.
This method is called from the core class which has the gameloop.
The thing i want to know is what am i doing wrong?
Do i need to change the draw method?
Do i need to load the images in a special way?
Do i need to change the way i´m painting?
My goal is to make a lag free 2d tile game.
The things i noticed myself was that my bufferedimages that hold the tile images was in RGBA format. But when i made it RGB it took a lot less time to render. I know the extra info is taking more time to draw. But what i didn’t know was how much it really was.
I managed to find one thing that made the performance better.
If i loaded all my images and copied them over to a image created in this way:
BufferedImage bi = ImageIO.read(ims[i]);
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage b = gc.createCompatibleImage(bi.getWidth()*4, bi.getHeight()*4, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = b.createGraphics();
g.drawImage(bi, 0, 0, bi.getWidth()*4, bi.getHeight()*4, null);
tiles[id]=b;
g.dispose();
Then the it took a lot less time to draw all the tiles.
But it could still be better.
And when i´m going to make the light engine(for example underground).
Then i would have to use transparent rectangles over the tiles.
Or if someone could suggest a better way of making tiles darker without using transparent rectangles. But the thing is that the lag comes when trying to draw transparent rectangles too. I don´t know if i´m supposed to avoid using transparent images and rectangles or what.
Hope someone can point me in the right direction.
Some tricks you might find helpful:
TYPE_INT_RGBrather thanTYPE_INT_ARGBIf you really want high performance drawing/animation then you may want to consider switching to an OpenGL-based rendering engine – Slick2D or LWJGL for example.