Calling the setup method starts the timers that i have, the one I'm having problems with is render, which takes upwards of 50 milliseconds to complete. far too long for only a double buffer.
public void setup() {
hB = new Rectangle(screenSize.width, screenSize.width);
player.setRect(new Rectangle(player.getX(), player.getY(), 10, 10));
this.createBufferStrategy(2);
strategy = this.getBufferStrategy();
start();
startShooting();
startRender();
}
This method starts the timer task and its sole purpose is to call render and about 60 fps
public void startRender() {
startRen = new TimerTask() {
public void run() {
render();
}
};
timer.schedule(startRen, 0, 16);
}
This is the troublesome method.
public void render() {
g = (Graphics2D) strategy.getDrawGraphics();
g.setPaint(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setPaint(Color.black);
g.drawString("Using " + player.getWeapon().getName(), 10, 20);
g.drawString("Magazine " + mag, 10, 50);
g.drawString("Number of AI " + AI.ai.size(), 10, 80);
g.drawString("Bullets " + Bullet.bullets.size(), 10, 110);
if (reloading) {
g.setFont(new Font("Calibri", Font.BOLD, 100));
g.setColor(Color.RED);
g.drawString("RELOADING", screenSize.width / 2 - 250, screenSize.height / 2);
g.setColor(Color.BLACK);
}
for (Bullet b : Bullet.bullets) {
g.fillOval((int) b.getX(), (int) b.getY(), 4, 4);
}
for (AI a : AI.ai) {
g.fill(a.getRect());
}
g.fill(player.getRect());
g.dispose();
strategy.show();
Toolkit.getDefaultToolkit().sync();
}
And the only part that takes time is
strategy.show()
and it takes 50+ milliseconds to just create two buffers, is this common? Will i have to restructure my game to avoid timer task and not use BufferStrategy?