handle input at higher rate than render rate

This commit is contained in:
yugecin 2017-01-10 13:02:40 +01:00
parent 6857393315
commit 7162f8bc7d
6 changed files with 61 additions and 13 deletions

View File

@ -85,6 +85,10 @@ public class Demux implements KeyListener, MouseListener {
state.update(delta); state.update(delta);
} }
public void preRenderUpdate(int delta) {
state.update(delta);
}
public void render(Graphics g) { public void render(Graphics g) {
state.render(g); state.render(g);
} }

View File

@ -54,6 +54,14 @@ public class DisplayContainer {
public int width; public int width;
public int height; public int height;
public int targetRenderInterval;
public int targetBackgroundRenderInterval;
public int realRenderInterval;
public int delta;
public int timeSinceLastRender;
private long lastFrame; private long lastFrame;
@Inject @Inject
@ -61,6 +69,8 @@ public class DisplayContainer {
this.demux = demux; this.demux = demux;
this.nativeDisplayMode = Display.getDisplayMode(); this.nativeDisplayMode = Display.getDisplayMode();
this.resolutionChangeListeners = new LinkedList<>(); this.resolutionChangeListeners = new LinkedList<>();
targetRenderInterval = 16; // ~60 fps
targetBackgroundRenderInterval = 41; // ~24 fps
lastFrame = getTime(); lastFrame = getTime();
} }
@ -81,20 +91,40 @@ public class DisplayContainer {
setup(); setup();
log("GL ready"); log("GL ready");
while(!(Display.isCloseRequested() && demux.onCloseRequest())) { while(!(Display.isCloseRequested() && demux.onCloseRequest())) {
// TODO: lower fps when not visible Display.isVisible delta = getDelta();
int delta = getDelta();
timeSinceLastRender += delta;
input.poll(width, height); input.poll(width, height);
int maxRenderInterval;
if (Display.isVisible() && Display.isActive()) {
maxRenderInterval = targetRenderInterval;
} else {
maxRenderInterval = targetBackgroundRenderInterval;
}
if (timeSinceLastRender >= maxRenderInterval) {
GL.glClear(SGL.GL_COLOR_BUFFER_BIT); GL.glClear(SGL.GL_COLOR_BUFFER_BIT);
/* /*
graphics.resetTransform(); graphics.resetTransform();
graphics.resetFont(); graphics.resetFont();
graphics.resetLineWidth(); graphics.resetLineWidth();
graphics.resetTransform(); graphics.resetTransform();
*/ */
demux.update(delta);
demux.update(timeSinceLastRender);
demux.render(graphics); demux.render(graphics);
Display.update(true);
Display.sync(60); realRenderInterval = timeSinceLastRender;
timeSinceLastRender = 0;
Display.update(false);
}
Display.processMessages();
Display.sync(1000);
} }
teardown(); teardown();
} }

View File

@ -22,6 +22,7 @@ import org.newdawn.slick.Graphics;
public interface OpsuState { public interface OpsuState {
void update(int delta); void update(int delta);
void preRenderUpdate(int delta);
void render(Graphics g); void render(Graphics g);
void enter(); void enter();
void leave(); void leave();

View File

@ -45,6 +45,10 @@ public abstract class TransitionState extends BaseOpsuState {
} }
} }
@Override
public void preRenderUpdate(int delta) {
}
@Override @Override
public void render(Graphics g) { public void render(Graphics g) {
applicableState.render(g); applicableState.render(g);

View File

@ -42,6 +42,10 @@ public class EmptyRedState implements OpsuState {
} }
} }
@Override
public void preRenderUpdate(int delta) {
}
@Override @Override
public void render(Graphics g) { public void render(Graphics g) {
g.setColor(Color.red); g.setColor(Color.red);
@ -69,6 +73,7 @@ public class EmptyRedState implements OpsuState {
@Override @Override
public boolean mouseWheelMoved(int delta) { public boolean mouseWheelMoved(int delta) {
System.out.println("moved");
return false; return false;
} }

View File

@ -42,6 +42,10 @@ public class EmptyState implements OpsuState {
} }
} }
@Override
public void preRenderUpdate(int delta) {
}
@Override @Override
public void render(Graphics g) { public void render(Graphics g) {
g.setColor(Color.green); g.setColor(Color.green);