shithub: wl3d

Download patch

ref: 35a6b626209bdb216d0c0df0237c4d9a4af3eae9
parent: efc660ab1ac0f887e06fc3c20ca51244f27ae87c
author: Konstantinn Bonnet <qu7uux@gmail.com>
date: Fri Apr 15 09:18:28 EDT 2016

don't cargo cult timing shit

- "it's pretty and probably correct, lgtm ship it" → wrong.
- if we are doomed to fail, we should at least time our undoing accurately
- still not sure if this is enough/correct, but it seems to be (ε ≤ 1 ms). but
  i've been wrong before!

--- a/wl3d.c
+++ b/wl3d.c
@@ -16,8 +16,9 @@
 Channel *csc, *kbc, *msc;
 
 enum{
-	BILLION = 1000000000,
-	MILLION = 1000000
+	Te9 = 1000000000,
+	Te6 = 1000000,
+	Td = Te9/Tb
 };
 static Point p0;
 static Rectangle fbr, grabr;
@@ -170,7 +171,6 @@
 	return p;
 }
 
-/* use only for shortening buffers, no zeroing done */
 void *
 erealloc(void *p, ulong n)
 {
@@ -234,7 +234,7 @@
 void
 threadmain(int argc, char **argv)
 {
-	vlong t0, t, dt, Δ;
+	vlong t0, t, dt;
 	char *datdir = "/sys/games/lib/wl3d/";
 
 	step = mstep;
@@ -266,7 +266,7 @@
 		sysfatal("proccreate: %r");
 
 	init();
-	t0 = Δ = 0;
+	t0 = nsec();
 	for(;;){
 		if(nbrecv(reszc, nil) != 0){
 			if(getwindow(display, Refnone) < 0)
@@ -274,17 +274,12 @@
 			resetfb();
 		}
 		step();
+		t0 += Td;
 		t = nsec();
-		dt = 0;
-		if(t0 != 0){
-			dt = BILLION/Tb - (t - t0) - Δ;
-			if(dt >= MILLION)
-				sleep(dt/MILLION);
-		}
-		t0 = nsec();
-		if(dt != 0){
-			dt = (t0 - t) - (dt / MILLION) * MILLION;
-			Δ += (dt - Δ) / 100;
-		}
+		dt = (t0 - t) / Te6;
+		if(dt > 0)
+			sleep(dt);
+		else
+			t0 = t;
 	}
 }