Java Game Programming - Episode 3 - Notch's Game Loop!

preview_player
Показать описание
In this video, we will be looking at creating a simple FPS counter that will essentially make the game run at the same speed on every type of computer.

Рекомендации по теме
Комментарии
Автор

I would recommend that you take a look at LibGDX. It uses LWGJL, which is the primary library in Minecraft, as well as its own specific additions. It keeps you from reinventing the wheel with game loops and creating your basic engine. (It also allows you to easily port to android, iphone, html, and many other platforms). 

syszee
Автор

So the link to the code isn't working, so i'll post it here:



public void run() {
int frames = 0;
int ticks = 0;
long lastTime = System.nanoTime();
double unprocessed = 0;
double nsPerSec =
long timer = System.currentTimeMillis();
while(running) {
long now = System.nanoTime();
unprocessed += (now - lastTime)/nsPerSec;
lastTime = now;

if(unprocessed >= 1) {
ticks++;
tick();
unprocessed -= 1;
}
try {
thread.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
frames++;
render();

- timer > 1000) {
System.out.println(ticks + ": ticks " + frames + ": frames");
ticks = 0;
frames = 0;
timer += 1000;
}
}
}

DougHigashi
Автор

Shouldn't nsPerSec just be (instead of because there are a billion nanoseconds in a second?

arnoio