add support for controlling how fast game updates run at
This commit is contained in:
parent
b5fccb3fd4
commit
8cd834c455
10
src/main/java/ca/blarg/gdx/GameLooper.java
Normal file
10
src/main/java/ca/blarg/gdx/GameLooper.java
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
package ca.blarg.gdx;
|
||||||
|
|
||||||
|
public interface GameLooper {
|
||||||
|
void setTiming(int updatesPerSecond, int maxFrameSkip);
|
||||||
|
|
||||||
|
int getUpdateFrequency();
|
||||||
|
|
||||||
|
float getUpdateDelta();
|
||||||
|
float getRenderDelta();
|
||||||
|
}
|
|
@ -2,8 +2,19 @@ package ca.blarg.gdx;
|
||||||
|
|
||||||
import com.badlogic.gdx.ApplicationListener;
|
import com.badlogic.gdx.ApplicationListener;
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.utils.TimeUtils;
|
||||||
|
|
||||||
|
public class GdxGameAppListener implements ApplicationListener, GameLooper {
|
||||||
|
int updatesPerSecond;
|
||||||
|
int updateFrequency;
|
||||||
|
int maxFrameSkip;
|
||||||
|
|
||||||
|
long nextTick = 0;
|
||||||
|
int loops;
|
||||||
|
|
||||||
|
float updateDelta = 0.0f;
|
||||||
|
float renderDelta = 0.0f;
|
||||||
|
|
||||||
public class GdxGameAppListener implements ApplicationListener {
|
|
||||||
Class<? extends GameApp> gameAppType;
|
Class<? extends GameApp> gameAppType;
|
||||||
GameApp gameApp;
|
GameApp gameApp;
|
||||||
|
|
||||||
|
@ -16,6 +27,10 @@ public class GdxGameAppListener implements ApplicationListener {
|
||||||
Gdx.app.debug("GdxGameAppListener", "create");
|
Gdx.app.debug("GdxGameAppListener", "create");
|
||||||
Gdx.app.debug("GdxGameAppListener", String.format("Application type: %s", Gdx.app.getType()));
|
Gdx.app.debug("GdxGameAppListener", String.format("Application type: %s", Gdx.app.getType()));
|
||||||
|
|
||||||
|
Services.register(GameLooper.class, this);
|
||||||
|
|
||||||
|
setTiming(20, 5);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
gameApp = gameAppType.newInstance();
|
gameApp = gameAppType.newInstance();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -30,6 +45,8 @@ public class GdxGameAppListener implements ApplicationListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
gameApp.onCreate();
|
gameApp.onCreate();
|
||||||
|
|
||||||
|
nextTick = TimeUtils.millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -40,10 +57,17 @@ public class GdxGameAppListener implements ApplicationListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render() {
|
public void render() {
|
||||||
// TODO: probably not the best idea to share the same delta with both renders and updates...
|
loops = 0;
|
||||||
float delta = Gdx.graphics.getDeltaTime();
|
while (TimeUtils.millis() > nextTick && loops < maxFrameSkip) {
|
||||||
gameApp.onUpdate(delta);
|
gameApp.onUpdate(updateDelta);
|
||||||
gameApp.onRender(delta);
|
|
||||||
|
nextTick += updateFrequency;
|
||||||
|
++loops;
|
||||||
|
}
|
||||||
|
|
||||||
|
renderDelta = (float)(TimeUtils.millis() + updateFrequency - nextTick) / (float)updateFrequency;
|
||||||
|
|
||||||
|
gameApp.onRender(renderDelta);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -64,4 +88,27 @@ public class GdxGameAppListener implements ApplicationListener {
|
||||||
if (gameApp != null)
|
if (gameApp != null)
|
||||||
gameApp.dispose();
|
gameApp.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setTiming(int updatesPerSecond, int maxFrameSkip) {
|
||||||
|
this.updatesPerSecond = updatesPerSecond;
|
||||||
|
updateFrequency = 1000 / this.updatesPerSecond;
|
||||||
|
this.maxFrameSkip = maxFrameSkip;
|
||||||
|
this.updateDelta = updateFrequency / 1000.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getUpdateFrequency() {
|
||||||
|
return updateFrequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getUpdateDelta() {
|
||||||
|
return updateDelta;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getRenderDelta() {
|
||||||
|
return renderDelta;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue