Java OpenGL 2D Game Tutorial - Episode 14 - Finishing The Game Loop

preview_player
Показать описание
In this episode we finish getting the game loop ready for action!
Don't forget to like, comment, and subscribe for more videos. Thanks for watching!
Рекомендации по теме
Комментарии
Автор

Hey, I just stumbled upon this series and it was really helpful so far. Thanks a lot!

I know, it has been a while, but do you still remember, how you got rid of the fps cap?

TheShitAssistant
Автор

at line 47 how does the compiler know that (1 000 000 000) is in nanosecond and it has to convert it in seconds ? Also why at line 56 do we divide by 1 000 000 ?

Ludoovik
Автор

Here's how I'm writing mine (so far) with a ScheduledExecutorService to take care of updates. Java's Util Concurrent ScheduledExecutorService is generally recommended more highly than manual management of Thread timing & sleeping, and is regarded as a more robust implementation.


public class GameLoop {

private static final double MILLIS_PER_SECOND = 1000.0;
private static final double MICROS_PER_SECOND =
private static final double UPDATE_FREQUENCY = 60.0;
private static final ScheduledExecutorService scheduler =
private static final Runnable scheduledUpdate = new Runnable() {
public void run() {
long currentTimeMillis = System.currentTimeMillis();
double deltaTime = (double) (currentTimeMillis - previousTimeMillis)
/ MILLIS_PER_SECOND;
previousTimeMillis = currentTimeMillis;
updateGame(deltaTime);
}
};
private static ScheduledFuture<?> scheduledUpdateHandle;
private static long previousTimeMillis;

public static void start() {
if (scheduledUpdateHandle == null || {
previousTimeMillis = System.currentTimeMillis();
long updatePeriod = (long) (MICROS_PER_SECOND / UPDATE_FREQUENCY);
scheduledUpdateHandle = scheduler.scheduleAtFixedRate(scheduledUpdate, updatePeriod, updatePeriod,
TimeUnit.MICROSECONDS);
}
}

public static void stop() {
if {

}
}

private static void updateGame(double deltaTime) {
World.update(deltaTime);
Renderer.render();
}

public void onDispose() {
stop();
scheduler.shutdown();
}
}

BingtheLizard