EDIT: Problem solved! Thanks so much to theagentd for helping me!
It’s been a while!
I’ve been dealing with a minor issue for a few days. I’d been having issues with consistency in screen scrolling and small screen tears, so I rewrote my game loop based on examples from ra4king and Eli. I’m still seeing small issues, though, and was wondering if I could have a few more pairs of eyes check it out.
Here’s a jar edit: updated to current version
Here’s the full source on BitBucket
Here’s the most relevant source
Game loop
@Override
public void run() {
createBufferStrategy(2);
BufferStrategy bs = getBufferStrategy();
init();
while (running) {
final double GAME_HERTZ = 60.0;
final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
final int MAX_UPDATES_BEFORE_RENDER = 1;
double lastUpdateTime = System.nanoTime();
double lastRenderTime = System.nanoTime();
final double TARGET_FPS = 60;
final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
while (running) {
double now = System.nanoTime();
int updateCount = 0;
if (!paused) {
while (now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER) {
tick();
lastUpdateTime += TIME_BETWEEN_UPDATES;
updateCount++;
}
render(bs);
lastRenderTime = now;
while (now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS && now - lastUpdateTime < TIME_BETWEEN_UPDATES) {
Thread.yield();
now = System.nanoTime();
}
}
}
}
System.exit(0);
}
Render function
private void render(BufferStrategy bs) {
do {
do {
Graphics g = image.getGraphics();
g.clearRect(0, 0, WIDTH, HEIGHT);
game.draw(screen);
g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
} while (bs.contentsRestored());
bs.show();
} while (bs.contentsLost());
}
edit: added code tags
Thanks for taking the time to help me!
Nathan