shithub: libgraphics

Download patch

ref: e0baf147d655409b721e41b0e3effabd39a96b34
parent: c0bc9d332f3ab51a43d5e3d0da2d5a32e938b1d2
author: rodri <rgl@antares-labs.eu>
date: Tue Feb 27 06:29:06 EST 2024

have separate routines for drawing and memdrawing.

--- a/fb.c
+++ b/fb.c
@@ -9,9 +9,20 @@
 #include "internal.h"
 
 static void
-framebufctl_draw(Framebufctl *ctl, Memimage *dst)
+framebufctl_draw(Framebufctl *ctl, Image *dst)
 {
+	Framebuf *fb;
+
+	fb = ctl->fb[ctl->idx];
 	lock(&ctl->swplk);
+	loadimage(dst, rectaddpt(fb->r, dst->r.min), byteaddr(fb->cb, fb->r.min), bytesperline(fb->r, fb->cb->depth)*Dy(fb->r));
+	unlock(&ctl->swplk);
+}
+
+static void
+framebufctl_memdraw(Framebufctl *ctl, Memimage *dst)
+{
+	lock(&ctl->swplk);
 	memimagedraw(dst, dst->r, ctl->fb[ctl->idx]->cb, ZP, nil, ZP, SoverD);
 	unlock(&ctl->swplk);
 }
@@ -67,6 +78,7 @@
 	fc->fb[0] = mkfb(r);
 	fc->fb[1] = mkfb(r);
 	fc->draw = framebufctl_draw;
+	fc->memdraw = framebufctl_memdraw;
 	fc->swap = framebufctl_swap;
 	fc->reset = framebufctl_reset;
 	return fc;
--- a/graphics.h
+++ b/graphics.h
@@ -39,6 +39,7 @@
 	Color c;	/* shading color */
 	Point2 uv;	/* texture coordinate */
 
+	/* TODO these attributes should be replaced by a hash table */
 	double intensity;
 	Point3 pos;
 };
@@ -147,7 +148,8 @@
 	uint idx;		/* front buffer index */
 	Lock swplk;
 
-	void (*draw)(Framebufctl*, Memimage*);
+	void (*draw)(Framebufctl*, Image*);
+	void (*memdraw)(Framebufctl*, Memimage*);
 	void (*swap)(Framebufctl*);
 	void (*reset)(Framebufctl*);
 };
@@ -156,6 +158,9 @@
 {
 	RFrame;
 	Framebufctl *fbctl;
+
+	void (*draw)(Viewport*, Image*);
+	void (*memdraw)(Viewport*, Memimage*);
 };
 
 struct Camera
--- a/render.c
+++ b/render.c
@@ -434,7 +434,7 @@
 			t[0][i].c.r = (*ep)->mtl != nil? (*ep)->mtl->Kd.r: 1;
 			t[0][i].c.g = (*ep)->mtl != nil? (*ep)->mtl->Kd.g: 1;
 			t[0][i].c.b = (*ep)->mtl != nil? (*ep)->mtl->Kd.b: 1;
-			t[0][i].c.a = /*(*ep)->mtl != nil? (*ep)->mtl->d:*/ 1;
+			t[0][i].c.a = 1;
 		}
 
 		vsp.v = &t[0][0];
--- a/viewport.c
+++ b/viewport.c
@@ -8,6 +8,18 @@
 #include "graphics.h"
 #include "internal.h"
 
+static void
+viewport_draw(Viewport *v, Image *dst)
+{
+	v->fbctl->draw(v->fbctl, dst);
+}
+
+static void
+viewport_memdraw(Viewport *v, Memimage *dst)
+{
+	v->fbctl->memdraw(v->fbctl, dst);
+}
+
 Viewport *
 mkviewport(Rectangle r)
 {
@@ -18,6 +30,8 @@
 	v->bx = Vec2(1,0);
 	v->by = Vec2(0,1);
 	v->fbctl = mkfbctl(r);
+	v->draw = viewport_draw;
+	v->memdraw = viewport_memdraw;
 	return v;
 }