shithub: cursedfs

Download patch

ref: 35bac6a836a15c60f6b7b6f26d0f8d8545e8e1a0
parent: 4ae0c34b7ed27db5062153219e7e4b49b9013d75
author: henesy <henesy.dev@gmail.com>
date: Fri Mar 11 11:58:32 EST 2022

fix flickering by triggering a fake resize

--- a/screen.c
+++ b/screen.c
@@ -10,8 +10,8 @@
 Font *ourfont;						// VGA
 Image *brush;						// For drawing the text
 Rune *s = L"☺☹σπß";	// for testing
-int bwidth = 4;						// border width of window
-Rune **sbuf, **ebuf;				// screen buffer, empty buffer
+int bwidth = 4;						// border width of rio win
+Rune ***sbuf, **ebuf;				// screen buffer, empty buffer
 usize sheight = 25, swidth = 80;	// screen height, width
 Lock slock;							// screen buffer lock
 
@@ -19,30 +19,40 @@
 char *buttons[] = {"exit", 0};
 Menu menu = { buttons };
 
+// Free a 2D buffer
+void
+freebuf(Rune **buf, usize height)
+{
+	int i;
+	for(i = 0; i < height; i++)
+		free(buf[i]);
+	free(buf);
+}
+
+
 // Render the active buffer on a timer
 void
-renderbuf(Rune **buf)
+renderbuf(void)
 {
 	int i;
+	Point p;
+
 	lock(&slock);
-	Point out, p;
 	Point at = screen->r.min;
 	for(i = 0; i < sheight; i++){
-		p = runestringsize(ourfont, buf[i]);
-		out = runestring(
+		p = runestringsize(ourfont, (*sbuf)[i]);
+		runestring(
 			screen, at, display->black, ZP,
-		    ourfont, buf[i]
+		    ourfont, (*sbuf)[i]
 		);
 		at.y += p.y;
 	}
 	unlock(&slock);
+
 	flushimage(display, 1);
 }
 
-
-
-/* Graphics library requires this function */
-
+// Handle resize events
 void
 eresized(int new)
 {
@@ -54,8 +64,10 @@
 
 	/* Draw the background DWhite */
 	draw(screen, insetrect(screen->r, bwidth), 
-			allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DWhite), 
-			nil, ZP);
+			allocimage(display, Rect(0, 0, 1, 1), 
+			screen->chan, 1, DWhite), 
+			nil, ZP
+	);
 }
 
 // Initialize the screen buffer
@@ -68,13 +80,14 @@
 	p = runestringsize(ourfont, s);
 
 	lock(&slock);
-	sbuf = calloc(sheight, sizeof (Rune*));
+	sbuf = calloc(sheight, sizeof (Rune**));
+	*sbuf = calloc(sheight, sizeof (Rune*));
 	ebuf = calloc(sheight, sizeof (Rune*));
 	for(y = 0; y < sheight; y++){
-		sbuf[y] = calloc(swidth+1, sizeof (Rune));
+		(*sbuf)[y] = calloc(swidth+1, sizeof (Rune));
 		ebuf[y] = calloc(swidth+1, sizeof (Rune));
 		for(x = 0; x < swidth; x++){
-			sbuf[y][x] = L'☺';
+			(*sbuf)[y][x] = L'☺';
 			ebuf[y][x] = L' ';
 		}
 	}
@@ -81,7 +94,7 @@
 	unlock(&slock);
 }
 
-
+// Initialize the screen, spins forever
 void
 initscreen(void*)
 {
@@ -94,22 +107,17 @@
 
 	einit(Emouse);
 
-	/* Start our timer
-	 * move the ball every 5 milliseconds
-	 * unless there is an Emouse event */
+	// Timer in ms
+	timer = etimer(0, 15);
 
-	timer = etimer(0, 5);
-
 	/* Simulate a resize event to draw the background
 	 * and acquire the screen dimensions */
 
 
 	initbuf();
-	renderbuf(ebuf);
 
 	// Set the screen size (after initbuf)
 	//echo resize -dx 100 -dy 100 > /dev/wctl
-	// TODO - broken
 	int fd;
 	char *str;
 	fd = open("/dev/wctl", OWRITE);
@@ -133,15 +141,17 @@
 		 * pressed and the first menu option selected
 		 * then exit.. */
 
-		if( (e == Emouse) &&
-			(ev.mouse.buttons & 4) && 
-			(emenuhit(3, &ev.mouse, &menu) == 0)) threadexitsall(nil);
+		if(
+			(e == Emouse)
+			&& (ev.mouse.buttons & 4) 
+			&& (emenuhit(3, &ev.mouse, &menu) == 0)
+		)
+			threadexitsall(nil);
 		else 
 			if(e == timer){
 				// Might not want this
-				renderbuf(ebuf);
-				renderbuf(sbuf);
+				eresized(0);
+				renderbuf();
 			}
 	}
 }
-