shithub: slug

Download patch

ref: c5077847e0cdadbbff06fa453ae309a66f25c9eb
parent: 80595a61ea5f6eed2413368c64fa09aa5c6e40cd
author: phil9 <telephil9@gmail.com>
date: Thu Dec 1 15:55:38 EST 2022

implement loop() and noLoop()

	noLoop() disables the draw loop meaning draw() will only be called once
	loop() reactivates the draw loop

--- a/a.h
+++ b/a.h
@@ -18,6 +18,7 @@
 Image*	color(int, int, int, int);
 
 extern int		drawing;
+extern int		looping;
 extern int		framerate;
 extern Image	*canvas;
 extern int		width;
--- a/api.c
+++ b/api.c
@@ -6,6 +6,7 @@
 	Chsv,
 };
 
+int		looping;
 int		framerate;
 Image	*canvas;
 int		width;
@@ -24,6 +25,7 @@
 {
 	Rectangle r;
 
+	looping = 1;
 	framerate = 90;
 	width = 500;
 	height = 500;
@@ -49,6 +51,20 @@
 }
 
 int
+cnoloop(lua_State*)
+{
+	looping = 0;
+	return LUA_OK;
+}
+
+int
+cloop(lua_State*)
+{
+	looping = 1;
+	return LUA_OK;
+}
+
+int
 cframerate(lua_State *L)
 {
 	int n;
@@ -394,6 +410,8 @@
 	lset(L, "SQUARE", Endsquare);
 	lset(L, "ROUND", Enddisc);
 
+	registerfunc(L, "noLoop", cnoloop);
+	registerfunc(L, "loop", cloop);
 	registerfunc(L, "frameRate", cframerate);
 	registerfunc(L, "size", csize);
 	registerfunc(L, "colorMode", ccolormode);
--- a/slug.c
+++ b/slug.c
@@ -126,12 +126,14 @@
 	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);
+		if(looping){
+			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);