Don't repaint unnecessary frames (when we lag)

This commit is contained in:
theKidOfArcrania 2017-02-27 00:23:02 +00:00
parent bdfc2984ee
commit 87054842de
2 changed files with 58 additions and 38 deletions

View File

@ -115,6 +115,7 @@ $(function() {
frame = data.frame; frame = data.frame;
}); });
var paintFrames = 0;
socket.on('notifyFrame', function(data, fn) { socket.on('notifyFrame', function(data, fn) {
if (timeout != undefined) if (timeout != undefined)
clearTimeout(timeout); clearTimeout(timeout);
@ -127,7 +128,6 @@ $(function() {
} }
frame++; frame++;
requestAnimationFrame(function() {
if (data.newPlayers) if (data.newPlayers)
{ {
data.newPlayers.forEach(function(p) { data.newPlayers.forEach(function(p) {
@ -146,6 +146,8 @@ $(function() {
player.heading = val.heading; player.heading = val.heading;
}); });
paintFrames++;
requestAnimationFrame(function() {
paintLoop(); paintLoop();
}); });
timeout = setTimeout(function() { timeout = setTimeout(function() {
@ -165,8 +167,15 @@ $(function() {
var deadFrames = 0; var deadFrames = 0;
function paintLoop() function paintLoop()
{ {
renderer.paint(); if (paintFrames === 0)
return;
while (paintFrames > 0)
{
renderer.update(); renderer.update();
paintFrames--;
}
renderer.paint();
if (user.dead) if (user.dead)
{ {
if (timeout) if (timeout)
@ -184,6 +193,7 @@ $(function() {
socket.disconnect(); socket.disconnect();
deadFrames++; deadFrames++;
paintFrames++;
requestAnimationFrame(paintLoop); requestAnimationFrame(paintLoop);
} }
} }

View File

@ -401,6 +401,7 @@ $(function() {
frame = data.frame; frame = data.frame;
}); });
var paintFrames = 0;
socket.on('notifyFrame', function(data, fn) { socket.on('notifyFrame', function(data, fn) {
if (timeout != undefined) if (timeout != undefined)
clearTimeout(timeout); clearTimeout(timeout);
@ -413,7 +414,6 @@ $(function() {
} }
frame++; frame++;
requestAnimationFrame(function() {
if (data.newPlayers) if (data.newPlayers)
{ {
data.newPlayers.forEach(function(p) { data.newPlayers.forEach(function(p) {
@ -432,6 +432,8 @@ $(function() {
player.heading = val.heading; player.heading = val.heading;
}); });
paintFrames++;
requestAnimationFrame(function() {
paintLoop(); paintLoop();
}); });
timeout = setTimeout(function() { timeout = setTimeout(function() {
@ -451,8 +453,15 @@ $(function() {
var deadFrames = 0; var deadFrames = 0;
function paintLoop() function paintLoop()
{ {
renderer.paint(); if (paintFrames === 0)
return;
while (paintFrames > 0)
{
renderer.update(); renderer.update();
paintFrames--;
}
renderer.paint();
if (user.dead) if (user.dead)
{ {
if (timeout) if (timeout)
@ -470,6 +479,7 @@ $(function() {
socket.disconnect(); socket.disconnect();
deadFrames++; deadFrames++;
paintFrames++;
requestAnimationFrame(paintLoop); requestAnimationFrame(paintLoop);
} }
} }