HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

General Game Loop

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
loopgamegeneral

Problem

I have two interfaces. Entity for updates. Drawable for rendering. They both have two interface methods. One that is called every frame, one every second.

Entity.java

public interface Entity
{
    public void updatePerFrame ();
    public void updatePerSecond ();
}


Drawable.java

public interface Drawable
{
    public void drawPerFrame ();
    public void drawPerSecond ();
}


The main component class Game has two lists, one for Entity and one for Drawable.

```
public class Game {
// if the game is running, default = true
// if the game is paused, default = false
private boolean isRunning;
private boolean isPaused;

// execution time for the last cycle of game loop
private long lastLoopTime;
private long curLoopTime;
private long deltaLoopTime;
private double deltaTime;

// if a second has passed since last execution
private boolean secondPassed;
private long secondTimer;

// nanoseconds spent per frame
private final int OPTIMAL_FPS = 60;
private final int OPTIMAL_TIME = 1000000000 / OPTIMAL_FPS;

// entities
private List entities;

// drawable
private List drawables;

// fps tracker
FPSViewer fpsViewer;

/**
* Main Function, instantiating Game
*/
public static void main() {
Game g = new Game();
g.gameLoop();
}

/**
*
*/
public Game () {
// the game is on and not paused
isRunning = true;
isPaused = false;

// a second has not yet passed
secondPassed = false;
secondTimer = System.currentTimeMillis();

// init entities and drawables
entities = new ArrayList();
drawables = new ArrayList();

// begin with current time
lastLoopTime = System.nanoTime();

// create the fps viewer
fpsViewer = new FPSViewer();
entities.add(fpsViewer);
drawables.add(fpsViewer);
}

/**
* Game Loop

Solution

I just have a couple of superficial points for now. It's been a while since I created a game loop, so I will let someone else comment on that, but at least your busy-loop (while (isPaused == true)) doesn't seem necessary (it will eat up a lot of resources without actually doing anything).

Boolean Checks

Instead of for example isRunning == true you can just write isRunning, which is more readable.

Comments

A lot of your comments are not needed, as the code is already self-explanatory, for example:

// if the game is running
// if the game is paused
// entities
// drawable
// init entities and drawables
// wait for user to unpause
// render everything
// update game
[etc]


These do not add any information, and thus actually make your code harder to read instead of easier.

Misc

  • you often (but not always) have a whitespace before ().



  • magic numbers: extract numbers in your code to (static) fields. This tells us what they actually represent (eg 1000 and 1000000).



  • always use curly brackets, even for one line statements.

Code Snippets

// if the game is running
// if the game is paused
// entities
// drawable
// init entities and drawables
// wait for user to unpause
// render everything
// update game
[etc]

Context

StackExchange Code Review Q#71581, answer score: 7

Revisions (0)

No revisions yet.