David Marec 8 mēneši atpakaļ
vecāks
revīzija
305f70737b
1 mainītis faili ar 205 papildinājumiem un 0 dzēšanām
  1. 205 0
      dummy/pepper2.c

+ 205 - 0
dummy/pepper2.c

@@ -0,0 +1,205 @@
+#include <curses.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <malloc_np.h>
+#include <string.h>
+#include <time.h>
+
+/* cc pepper2.c -lncurses -o p2 -O2 */
+
+typedef struct {
+	size_t len;
+	char *items[10];
+} truc_array;
+
+enum dirs {
+	R, /* right */
+	L, /* left */
+	U, /* up */
+	D /* down */	
+};
+
+
+static WINDOW * winR;
+static void worm(WINDOW *win, int x, int y, char c0, char repl)
+{
+	static int ax, ay;
+	static char ac = '*';
+
+	wattron(win, A_REVERSE | A_BOLD);
+	mvwprintw(win, y + 1, x + 1, "%c", c0);
+	wattroff(win, A_REVERSE | A_BOLD);
+	if (ac != '*')
+		mvwprintw(win, ay + 1, ax + 1, "%c", ac);
+	wrefresh(win);
+	ax = x;
+	ay = y;
+	ac = repl;
+	usleep(150 * 1000);
+}
+
+static __attribute((malloc)) __attribute__((nonnull)) char *get_it(truc_array const *src)
+{
+	char *r = mallocx(src->len, MALLOCX_ZERO);
+	size_t n = 0;
+	enum dirs dir = R;
+	int x = 0, y = 0;
+	char c0 = '<';
+
+	WINDOW * win = newwin(14, 18, 1, 40);
+
+	curs_set(0);
+	box(win, 0, 0);
+
+
+	while (src->len && (n < src->len)) {
+		char c;
+
+		while((c = src->items[y][x]) != ' ') {
+			/* store */
+			if (c != '*') {
+				r[n++] = c;
+				worm(win, x, y, c0, c);
+				mvwprintw(winR, 1, 1, "%s", r);
+				wrefresh(winR);
+			} else
+				worm(win, x, y, c0, '.');
+
+
+			if (n == src->len) {
+				break;
+			}
+
+			/* test next one */
+			int tx = x;
+			int ty = y;
+			switch (dir) {
+				case R:
+					tx++;
+					break;
+				case L:
+					tx--;
+					break;
+				case U:
+					ty--;
+					break;
+				case D:
+					ty++;
+					break;
+				default:
+					fprintf(stderr, "Bug!\n");
+					break;
+			}
+
+			/* ouch */
+			if (tx > 14 || tx < 0 || ty <0 || ty > 9)
+				break;
+			if ((c = src->items[ty][tx]) == ' ') 
+				break;
+			/* valid next */
+			x = tx;
+			y = ty;
+		}
+
+		switch (dir) {
+			char c_up, c_down, c_left, c_right;
+			case R:
+			case L:
+			/* cover up and down */
+			c_up   = ' ';
+			c_down = ' ';
+
+			if (y > 1)
+				c_up   = src->items[y - 1][x];
+			if (y < 9)
+				c_down = src->items[y + 1][x];
+			if (c_up != ' ') {
+				y = y - 1;
+				dir = U;
+				c0 = '^';
+			}
+			else if (c_down != ' ') {
+				y = y + 1;
+				dir = D;
+				c0 = ';';
+			}
+			else
+				goto end;
+			break;
+			case U:
+			case D:
+
+			/* cover left and right */
+			c_left   = ' ';
+			c_right = ' ';
+
+			if (x > 1)
+				c_left  = src->items[y][x - 1];
+			if (y < 14)
+				c_right = src->items[y][x + 1];
+
+			if (c_left != ' ') {
+				x = x - 1;
+				dir = L;
+				c0 = '>';
+			}
+			else if (c_right != ' ') {
+				x = x + 1;
+				dir = R;
+				c0 = '<';
+			} else
+				goto end;
+			break;
+		}
+	}
+
+end:
+	worm(win, x , y , ' ', ' ');
+	delwin(win);
+	return r;
+}
+
+static void print_it(truc_array src)
+{
+	WINDOW * win = newwin(14, 18, 1, 1);
+	box(win, 0, 0);
+	for (size_t j= 0; j < 10; ++j) {
+		mvwprintw(win, j + 1, 1, "%s", src.items[j]);
+	}
+	wrefresh(win);
+	delwin(win);
+}
+
+int main(int argc, char *argv[])
+{
+	truc_array a= {60, };
+	char *r = NULL;
+
+
+	/*            012345789ABCDEF */
+	a.items[0] = "*01**         $";
+	a.items[1] = "edcBA          ";
+	a.items[2] = "*  *       X   ";
+	a.items[3] = "*  !       *   ";
+	a.items[4] = "****F**        ";
+	a.items[5] = "      *  *H*   ";
+	a.items[6] = "      *G** *   ";
+	a.items[7] = "   X       I   ";
+	a.items[8] = "   X **J****   ";
+	a.items[9] = "X    Z        $";
+
+	initscr();
+	nonl();
+	noecho();
+	winR = newwin(3, 65, 20, 1);
+	box(winR, 0, 0);
+	wrefresh(winR);
+
+	print_it(a);
+
+	r = get_it(&a);
+	dallocx(r, 0);
+	delwin(winR);
+	endwin();
+}
+