shithub: slug

Download patch

ref: 80595a61ea5f6eed2413368c64fa09aa5c6e40cd
parent: c3b6c54fe0c7ad567a5e6843c7339275e88d49fe
author: phil9 <[email protected]>
date: Thu Dec 1 14:53:40 EST 2022

implement frameRate() function and set the default framerate to ~90FPS

	this is not very accurate but still good enough

--- a/a.h
+++ b/a.h
@@ -18,6 +18,7 @@
 Image*	color(int, int, int, int);
 
 extern int		drawing;
+extern int		framerate;
 extern Image	*canvas;
 extern int		width;
 extern int		height;
--- a/api.c
+++ b/api.c
@@ -6,6 +6,7 @@
 	Chsv,
 };
 
+int		framerate;
 Image	*canvas;
 int		width;
 int		height;
@@ -23,6 +24,7 @@
 {
 	Rectangle r;
 
+	framerate = 90;
 	width = 500;
 	height = 500;
 	r = Rect(0, 0, width, height);
@@ -47,6 +49,18 @@
 }
 
 int
+cframerate(lua_State *L)
+{
+	int n;
+
+	n = luaL_checkinteger(L, 1);
+	if(n < 0)
+		return LUA_ERRRUN;
+	framerate = n;
+	return LUA_OK;
+}
+
+int
 csize(lua_State *L)
 {
 	int w, h;
@@ -380,6 +394,7 @@
 	lset(L, "SQUARE", Endsquare);
 	lset(L, "ROUND", Enddisc);
 
+	registerfunc(L, "frameRate", cframerate);
 	registerfunc(L, "size", csize);
 	registerfunc(L, "colorMode", ccolormode);
 	registerfunc(L, "background", cbackground);
--- a/slug.c
+++ b/slug.c
@@ -97,6 +97,8 @@
 threadmain(int argc, char *argv[])
 {
 	lua_State *L;
+	vlong t0, t1;
+	double delta;
 	Mouse m;
 	Rune k;
 	Alt alts[] = {
@@ -124,7 +126,12 @@
 	lcall(L, "setup");
 	drawing = 1;
 	for(;;){
+		t0 = nsec();
 		lcall(L, "draw");
+		t1 = nsec();
+		delta = (t1 - t0) / 1000000.0;
+		if(delta > 0.0)
+			sleep((1000.0 / framerate) - delta);
 		switch(alt(alts)){
 		case 0:
 			emouse(L, m);