shithub: orca

Download patch

ref: b2bacf2993e5aef1de9edaa2e6f271cbb6030ad2
parent: 61bd27d3684d1f03cfd3e9c966b1ccff8a53a71b
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Feb 12 07:55:52 EST 2020

plan9: fix field resizing by copying to a separate buffer and back

--- a/plan9.c
+++ b/plan9.c
@@ -27,6 +27,8 @@
 static Image *curbg;
 static int charw, charh;
 static Field field;
+static Mbuf_reusable mbuf;
+static Oevent_list events;
 
 static char *menu3i[] = {
 	"load",
@@ -109,6 +111,19 @@
 }
 
 static void
+orcathread(void *)
+{
+	int w, h;
+
+	w = field.width;
+	h = field.height;
+	mbuffer_clear(mbuf.buffer, h, w);
+	oevent_list_clear(&events);
+	orca_run(field.buffer, mbuf.buffer, h, w, tick, &events, 0);
+	process(&events);
+}
+
+static void
 redraw(void)
 {
 	Point p, top;
@@ -213,10 +228,9 @@
 void
 threadmain(int argc, char **argv)
 {
-	Mbuf_reusable mbuf;
-	Oevent_list events;
 	Mousectl *mctl;
 	Keyboardctl *kctl;
+	Field copyfield;
 	Rune key;
 	Mouse m;
 	char tmp[256];
@@ -250,6 +264,7 @@
 
 	screensize(&w, &h);
 	field_init_fill(&field, h, w, '.');
+	field_init(&copyfield);
 
 	linebuf = malloc(sizeof(Rune)*MAX(w+1, 64));
 	memset(noteoff, 0, sizeof(noteoff));
@@ -258,10 +273,7 @@
 	oevent_list_init(&events);
 
 	for (tick = 0;; tick++) {
-		mbuffer_clear(mbuf.buffer, h, w);
-		oevent_list_clear(&events);
-		orca_run(field.buffer, mbuf.buffer, h, w, tick, &events, 0);
-		process(&events);
+		orcathread(nil);
 		redraw();
 
 		oldw = w = field.width;
@@ -357,9 +369,17 @@
 			}
 
 			if (w != oldw || h != oldh) {
+				field_copy(&field, &copyfield);
 				field_resize_raw(&field, h, w);
-				if (w >= oldw && h >= oldh)
-					memset(field.buffer + oldw*oldh, '.', w*h - oldw*oldh);
+				memset(field.buffer, '.', w*h);
+				gbuffer_copy_subrect(
+					copyfield.buffer,
+					field.buffer,
+					copyfield.height, copyfield.width,
+					field.height, field.width,
+					0, 0, 0, 0,
+					MIN(field.height, copyfield.height), MIN(field.width, copyfield.width)
+				);
 			}
 		}
 
@@ -373,6 +393,7 @@
 	mbuf_reusable_deinit(&mbuf);
 	oevent_list_deinit(&events);
 	field_deinit(&field);
+	field_deinit(&copyfield);
 
 	threadexitsall(nil);
 }