patternjavaMinor
How is my Game Loop?
Viewed 0 times
loopgamehow
Problem
I wrote a game loop for a game I'm going to be making:
There are two classes, the abstract class and the implementing class.
Any suggestions?
```
public abstract class Game {
private int FRAMES_PER_SECOND;
private boolean running = true;
long targetTime;
private long runningFPS;
protected Game(int fps) {
setTargetFPS(fps);
}
public void setTargetFPS(int fps) {
this.FRAMES_PER_SECOND = fps;
targetTime = 1000 / FRAMES_PER_SECOND;
}
public void run(JPanel panel, BufferedImage image) {
int currentFPS = 0;
long counterstart = System.nanoTime();
long counterelapsed = 0;
long start;
long elapsed;
long wait;
targetTime = 1000 / FRAMES_PER_SECOND;
while (running) {
start = System.nanoTime();
processInput();
update();
// time to update and process input
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
if (hasTimeToDraw(wait)) {
//CREATE AND ANTIALIAS GRAPHICS
Graphics2D g = image.createGraphics();
g.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
//Draw
draw(g);
g.dispose();
panel.repaint();
//Take account for the time it took to draw
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
}
counterelapsed = System.nanoTime() - counterstart;
curren
- Check the current FPS
- Try the keep the FPS to a hardcoded level
- Draw and Update the game.
There are two classes, the abstract class and the implementing class.
Any suggestions?
Game```
public abstract class Game {
private int FRAMES_PER_SECOND;
private boolean running = true;
long targetTime;
private long runningFPS;
protected Game(int fps) {
setTargetFPS(fps);
}
public void setTargetFPS(int fps) {
this.FRAMES_PER_SECOND = fps;
targetTime = 1000 / FRAMES_PER_SECOND;
}
public void run(JPanel panel, BufferedImage image) {
int currentFPS = 0;
long counterstart = System.nanoTime();
long counterelapsed = 0;
long start;
long elapsed;
long wait;
targetTime = 1000 / FRAMES_PER_SECOND;
while (running) {
start = System.nanoTime();
processInput();
update();
// time to update and process input
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
if (hasTimeToDraw(wait)) {
//CREATE AND ANTIALIAS GRAPHICS
Graphics2D g = image.createGraphics();
g.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
//Draw
draw(g);
g.dispose();
panel.repaint();
//Take account for the time it took to draw
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
}
counterelapsed = System.nanoTime() - counterstart;
curren
Solution
long start;
long elapsed;
long wait;This is no Pascal, declare your variable where they get initialized. This is not always possible, maybe 1 such variable per 10 classes is unavoidable.
//dont wanna wait for negative time
if (wait < 0)
wait = 0;
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
e.printStackTrace();
}Better don't change a variable if you don't have to as it makes the code harder to understand. Doing
Thread.sleep(wait<0 ? 0 : wait);is simpler, doing
Thread.sleep(Math.max(0, wait));is even simpler and so is
if (wait>0) Thread.sleep(wait);The main problem is that your method is far too long. Extracting some methods would help a lot, e.g.,
Graphics2D g = newNiceGraphics(image);
sleepUninterruptibly(wait);
Enough for now....
Code Snippets
long start;
long elapsed;
long wait;//dont wanna wait for negative time
if (wait < 0)
wait = 0;
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
e.printStackTrace();
}Thread.sleep(wait<0 ? 0 : wait);Thread.sleep(Math.max(0, wait));if (wait>0) Thread.sleep(wait);Context
StackExchange Code Review Q#60194, answer score: 6
Revisions (0)
No revisions yet.