I’m having trouble drawing the right way it seems, it takes about 500-800 milliseconds to complete a single loop in my render method. Any suggestions or links that might explain what to do. Can’t find anything on the forum, please help!
This is my render code:
public class GamePanel extends Canvas{
private BufferedImage backBuffer;
private int windowHeight;
private int windowWidth;
private ZompocalypseGame game;
private GUI gui;
private Position position;
public GamePanel(int windowWidth, int windowHeight, ZompocalypseGame sGame, GUI gui){
setIgnoreRepaint(true);
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
setVisible(true);
this.game = sGame;
this.gui = gui;
this.position = new Position(0,0);
this.backBuffer = new BufferedImage(this.windowWidth, this.windowHeight, BufferedImage.TYPE_INT_RGB);
}
public void draw(){
Graphics2D g = (Graphics2D) getGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, windowWidth, windowHeight);
g.drawImage(this.backBuffer, 0, 0, this);
g.dispose();
}
public void render() {
Graphics2D bbg = (Graphics2D) this.backBuffer.getGraphics();
BufferedImage image = null;
Player player = this.game.getTileMap().getPlayer();
long foo = System.currentTimeMillis();
for(int y = 0; y < 24; y++){
for(int x = 0; x < 42; x++){
position.setX(x + player.getPosition().getX()-21);
position.setY(y + player.getPosition().getY()-12);
int yOffset = 0;
//Draw tiles
Tile tile = this.game.getTileMap().getTileAt(position);
if(tile != null){
image = tile.getImage();
}else{
tile = new Tile(position, "WATER");
image = tile.getImage();
}
//Draw items
Item item = this.game.getTileMap().getItemAt(position);
if(item != null){
image = item.getImage();
}
//Draw buildings
Building building = this.game.getTileMap().getBuildingAt(position);
if(building != null){
image = building.getImage();
}
//Draw units
Unit unit = this.game.getTileMap().getUnitAt(position);
if(unit != null){
image = unit.getImage();
yOffset = -40; //Unit height
}
bbg.drawImage(image, x*20, y*20+yOffset, this);
bbg.setColor(Color.WHITE);
bbg.fillRect(0, 580, 800, 20);
bbg.setColor(Color.BLACK);
bbg.drawString(String.valueOf(gui.getFps()), 10, 595);
}
}
long bar = System.currentTimeMillis();
System.out.println(bar - foo);
}
}