initial commit
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 7 Apr 2017 12:33:07 +0000 (15:33 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 7 Apr 2017 12:33:07 +0000 (15:33 +0300)
.gitignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
regis.c [new file with mode: 0644]
regis.h [new file with mode: 0644]
rgl.c [new file with mode: 0644]
rgl.h [new file with mode: 0644]
test.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..2918379
--- /dev/null
@@ -0,0 +1,4 @@
+*.o
+*.swp
+*.d
+regis_test
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..33323b6
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+src = $(wildcard *.c)
+obj = $(src:.c=.o)
+bin = regis_test
+
+CFLAGS = -pedantic -Wall -g
+LDFLAGS = -lm
+
+$(bin): $(obj)
+       $(CC) -o $@ $(obj) $(LDFLAGS)
+
+.PHONY: clean
+clean:
+       rm -f $(obj) $(bin)
diff --git a/regis.c b/regis.c
new file mode 100644 (file)
index 0000000..375137a
--- /dev/null
+++ b/regis.c
@@ -0,0 +1,86 @@
+#include "regis.h"
+
+#define OUT    (out ? out : stdout)
+static FILE *out;
+
+void regis_set_output(FILE *fp)
+{
+       out = fp;
+}
+
+void regis_enter(void)
+{
+       fprintf(OUT, "\033P0p\n");
+}
+
+void regis_leave(void)
+{
+       fprintf(OUT, "\033\\");
+       fflush(OUT);
+}
+
+void regis_draw_page(int x)
+{
+       fprintf(OUT, "P(P%d)\n", x ? 1 : 0);
+}
+
+void regis_show_page(int x)
+{
+       fprintf(OUT, "S(P%d)\n", x ? 1 : 0);
+}
+
+void regis_clear(void)
+{
+       fprintf(OUT, "S(E)\n");
+}
+
+void regis_abspos(int x, int y)
+{
+       fputc('P', OUT);
+       regis_absv(x, y);
+}
+
+void regis_relpos(int x, int y)
+{
+       fputc('P', OUT);
+       regis_relv(x, y);
+}
+
+void regis_absv(int x, int y)
+{
+       fprintf(OUT, "[%d,%d]\n", x, y);
+}
+
+void regis_relv(int x, int y)
+{
+       fprintf(OUT, "[%+d,%+d]\n", x, y);
+}
+
+void regis_nullv(void)
+{
+       fprintf(OUT, "[]\n");
+}
+
+static unsigned int cur_mode;
+
+void regis_begin_vector(unsigned int mode)
+{
+       if(mode & REGIS_FILL) {
+               fprintf(OUT, "F(");
+       }
+       fputc('V', OUT);
+       if(mode & REGIS_BOUNDED) {
+               fprintf(OUT, "(B)");
+       }
+       fputc('\n', OUT);
+       cur_mode = mode;
+}
+
+void regis_end_vector(void)
+{
+       fprintf(OUT, "(E)");
+       if(cur_mode & REGIS_FILL) {
+               fputc(')', OUT);
+       }
+       fputc('\n', OUT);
+}
diff --git a/regis.h b/regis.h
new file mode 100644 (file)
index 0000000..0a7e403
--- /dev/null
+++ b/regis.h
@@ -0,0 +1,29 @@
+#ifndef REGIS_H_
+#define REGIS_H_
+
+#include <stdio.h>
+
+enum {
+       REGIS_BOUNDED = 1,
+       REGIS_UNBOUNDED = 0,
+       REGIS_FILL = 2,
+       REGIS_WIRE = 0
+};
+
+void regis_set_output(FILE *fp);
+
+void regis_enter(void);
+void regis_leave(void);
+void regis_draw_page(int x);
+void regis_show_page(int x);
+void regis_clear(void);
+void regis_abspos(int x, int y);
+void regis_relpos(int x, int y);
+void regis_absv(int x, int y);
+void regis_relv(int x, int y);
+void regis_nullv(void);
+
+void regis_begin_vector(unsigned int mode);
+void regis_end_vector(void);
+
+#endif /* REGIS_H_ */
diff --git a/rgl.c b/rgl.c
new file mode 100644 (file)
index 0000000..3bdf725
--- /dev/null
+++ b/rgl.c
@@ -0,0 +1,58 @@
+#include "rgl.h"
+#include "regis.h"
+
+struct vec2 {
+       int x, y;
+};
+
+static void flush_verts(void);
+static void draw_lines(struct vec2 *v, int nverts);
+
+static int cur_mode = -1;
+#define VBUF_SIZE      64
+static struct vec2 vbuf[VBUF_SIZE];
+static int nverts;
+
+void rgl_begin(int mode)
+{
+       cur_mode = mode;
+       nverts = 0;
+}
+
+void rgl_end(void)
+{
+       flush_verts();
+       cur_mode = -1;
+}
+
+void rgl_vertex(int x, int y)
+{
+       vbuf[nverts].x = x;
+       vbuf[nverts].y = y;
+
+       if(++nverts >= VBUF_SIZE) {
+               flush_verts();
+       }
+}
+
+static void flush_verts(void)
+{
+       switch(cur_mode) {
+       case REGIS_LINES:
+               draw_lines(vbuf, nverts);
+               break;
+
+       default:
+               break;
+       }
+       nverts = 0;
+}
+
+static void draw_lines(struct vec2 *v, int nverts)
+{
+       if(nverts <= 0) return;
+
+       regis_enter();
+
+       regis_leave();
+}
diff --git a/rgl.h b/rgl.h
new file mode 100644 (file)
index 0000000..7fe18af
--- /dev/null
+++ b/rgl.h
@@ -0,0 +1,15 @@
+#ifndef RGL_H_
+#define RGL_H_
+
+enum {
+       REGIS_LINES,
+       REGIS_LINE_STRIP,
+       REGIS_LINE_LOOP
+};
+
+void rgl_begin(int mode);
+void rgl_end(void);
+
+void rgl_vertex(int x, int y);
+
+#endif /* RGL_H_ */
diff --git a/test.c b/test.c
new file mode 100644 (file)
index 0000000..c134b3f
--- /dev/null
+++ b/test.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <signal.h>
+#include <math.h>
+#include <unistd.h>
+#include "regis.h"
+
+static void sig(int s);
+
+static volatile int done;
+
+int main(int argc, char **argv)
+{
+       int pg = 1;
+       float tm = 0.0f;
+       signal(SIGINT, sig);
+
+       while(!done) {
+               float dx = cos(tm) * 50.0;
+               float dy = sin(tm * 2.0) * 50.0;
+               tm += 0.01f;
+
+               regis_enter();
+               regis_draw_page(pg);
+               regis_clear();
+               regis_abspos(100 + dx, 100 + dy);
+               regis_begin_vector(REGIS_BOUNDED | REGIS_FILL);
+               regis_absv(300 + dx, 300 + dy);
+               regis_absv(80 + dx, 400 + dy);
+               regis_end_vector();
+               regis_show_page(pg);
+               pg = (pg + 1) & 1;
+               regis_leave();
+
+               usleep(10000);
+       }
+
+       regis_enter();
+       regis_clear();
+       regis_leave();
+       return 0;
+}
+
+static void sig(int s)
+{
+       done = 1;
+}