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);
}
public void preRenderUpdate(int delta) {
state.update(delta);
}
public void render(Graphics g) {
state.render(g);
}

View File

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

View File

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

View File

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

View File

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

View File

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