shithub: oledsaver

Download patch

ref: e0043ee088f227d5ce3cbf567ca8ddde53001c9f
parent: f89c25789fc5e3ea793d417b8a6cf70f5a5c7740
author: glenda <glenda@narshaddaa>
date: Mon May 1 03:39:37 EDT 2023

iter abstraction

--- a/bitart.c
+++ b/bitart.c
@@ -1,0 +1,45 @@
+#include <u.h>
+#include <libc.h>
+#include "bitart.h"
+
+int
+ob0(int x, int y)
+{
+	return ((-((-y) ^ (-x))) % (-((x - y) / (x / x)))) % 12;
+}
+
+int
+ob1(int x, int y)
+{
+	return ((-((y^23)|(x%2)))-(((~y)*(~y))%((-x)+(y*x))))%7;
+}
+
+int
+ob2(int x, int y)
+{
+	return ((-((x+23)-(-x)))%(~(~(14-y))))%6;
+}
+
+int
+ob3(int x, int y)
+{
+	return ((-(-(x|x)))%(((-x)^(y+x))/((y/x)-(x^x))))%3;
+}
+
+int
+ob4(int x, int y)
+{
+	return (((~(x/y))*(-(-y)))-(-((y-y)|(-x))))%11;
+}
+
+int
+ob5(int x, int y)
+{
+	return (((-(y|y))/((-y)+(y&x)))/(((y*y)&(~x))+((x/y)&(13&x))))%4;
+}
+
+int
+drand(int, int)
+{
+	return ntruerand(2);
+}
\ No newline at end of file
--- a/bitart.h
+++ b/bitart.h
@@ -1,0 +1,20 @@
+int
+ob0(int x, int y);
+
+int
+ob1(int x, int y);
+
+int
+ob2(int x, int y);
+
+int
+ob3(int x, int y);
+
+int
+ob4(int x, int y);
+
+int
+ob5(int x, int y);
+
+int
+drand(int, int);
--- a/main.c
+++ b/main.c
@@ -10,7 +10,11 @@
 #define NDRAWFUNCS 7
 
 typedef int drawfunc(int x, int x);
+typedef Memimage* iterfunc(drawfunc, Memimage*);
+
 int h, w;
+char *path;
+long ms;
 
 void
 usage(void)
@@ -59,46 +63,48 @@
 	return out;
 }
 
-int
-ob0(int x, int y)
+Memimage*
+topdown(drawfunc df, Memimage *img)
 {
-	return ((-((-y) ^ (-x))) % (-((x - y) / (x / x)))) % 12;
-}
+	Rectangle r;
+	
+	r.min.x = 0;
+	r.min.y = 0;
+	r.max.x = 1;
+	r.max.y = 1;	
 
-int
-ob1(int x, int y)
-{
-	return ((-((y^23)|(x%2)))-(((~y)*(~y))%((-x)+(y*x))))%7;
-}
+	int y, x, fd;
+	for(y = 0; y < h; y++){
+		// successive writes will fail if do not close/open
+		fd = open(path, OWRITE);
+		if(fd < 0){
+			sysfatal("could not open kbdoled file → %r");
+		}
 
-int
-ob2(int x, int y)
-{
-	return ((-((x+23)-(-x)))%(~(~(14-y))))%6;
-}
 
-int
-ob3(int x, int y)
-{
-	return ((-(-(x|x)))%(((-x)^(y+x))/((y/x)-(x^x))))%3;
-}
+		for(x = 0; x < w; x++){
+			Memimage *todraw = memblack;
+			if(df(x, y))
+				todraw = memwhite;
+		
+			r.min.x = x;
+			r.min.y = y;
 
-int
-ob4(int x, int y)
-{
-	return (((~(x/y))*(-(-y)))-(-((y-y)|(-x))))%11;
-}
+			r.max.x = r.min.x+1;
+			r.max.y = r.min.y+1;
 
-int
-ob5(int x, int y)
-{
-	return (((-(y|y))/((-y)+(y&x)))/(((y*y)&(~x))+((x/y)&(13&x))))%4;
-}
+			memimagedraw(img, r, todraw, ZP, nil, ZP, SoverD);
 
-int
-drand(int, int)
-{
-	return ntruerand(2);
+		}
+		img = writeuncompressed(fd, img);
+		
+		// flush
+		fd = close(fd);
+		if(fd < 0)
+			sysfatal("close failed → %r");
+		sleep(ms);
+	}
+	return img;
 }
 
 
@@ -110,7 +116,6 @@
 	int i;
 	h = 32;
 	w = 126;
-	long ms;
 	ms = 100;
 	//Point topleft;
 	//double angle;
@@ -120,12 +125,12 @@
 	pixel.min.y = 1;
 	pixel.max.y = 2;
 	pixel.max.x = 2;
-	char *path;
 	path = "/mnt/reform/kbdoled";
 	//u8int fb[H][W];	// framebuffer
-	drawfunc *tab[NDRAWFUNCS] = {
+	
+	drawfunc *drawtab[NDRAWFUNCS] = {
 		ob0,
-		ob1,//general kenobi! *growls* << "hello there"
+		ob1,//general kenobi! *growls* « "hello there"
 		ob2,
 		ob3,
 		ob4,
@@ -133,9 +138,6 @@
 		drand,
 	};
 
-	Rectangle r;
-	int fd;
-
 	ARGBEGIN{
 	default:
 		usage();
@@ -144,17 +146,13 @@
 	if (memimageinit())
 		sysfatal("memimageinit failed: %r");
 
+	
 	for(i = 0;; i++){
 		if(i >= NDRAWFUNCS)
 			i = 0;
-		print("i=%d\n", i);
+		//print("i=%d\n", i);
 
-		
 		// clear rectangle brush position
-		r.min.x = 0;
-		r.min.y = 0;
-		r.max.x = 1;
-		r.max.y = 1;	
 
 		// clear the screen
 		img = allocmemimage(Rect(0, 0, w, h), GREY1);
@@ -163,44 +161,16 @@
 		memfillcolor(img, DBlack);
 
 		/* call the independent drawing routine of choice */
+		topdown(drawtab[i], img);
 
 		
 		//angle = truerand();
 
-		int y, x;
-		for(y = 0; y < h; y++){
-			fd = open(path, OWRITE);
-			if(fd < 0){
-				sysfatal("could not open kbdoled file → %r");
-			}
+		
 
-
-			for(x = 0; x < w; x++){
-				Memimage *todraw = memblack;
-				if(tab[i](x, y))
-					todraw = memwhite;
-			
-				r.min.x = x;
-				r.min.y = y;
-
-				r.max.x = r.min.x+1;
-				r.max.y = r.min.y+1;
-	
-				memimagedraw(img, r, todraw, ZP, nil, ZP, SoverD);
-
-			}
-			img = writeuncompressed(fd, img);
-			
-			// flush
-			fd = close(fd);
-			if(fd < 0)
-				sysfatal("close failed → %r");
-			sleep(ms);
-		}
-
 		freememimage(img);
 		
-		sleep(ms);
+		//sleep(ms);
 	}
 	
 }
\ No newline at end of file