shithub: puzzles

Download patch

ref: 5518b554dd7848acd7839e1de01cfa485ba32bd8
parent: dd3a0f224d9472f4969c425980218e550d357eb0
author: sirjofri <[email protected]>
date: Mon Jun 10 07:19:08 EDT 2024

back to indirect draw, fixes blitter bug, adds todo

--- a/plan9.c
+++ b/plan9.c
@@ -33,7 +33,6 @@
 int dolog = 0;
 int logfd = -1;
 int logpipe[2];
-#define LOG(c) { if (dolog) fprint(logpipe[1], "%s\n", c); }
 static void
 Log(char *fmt, ...)
 {
@@ -41,7 +40,7 @@
 	if (!dolog)
 		return;
 	va_start(arg, fmt);
-	vfprint(logfd, fmt, arg);
+	vfprint(logpipe[1], fmt, arg);
 	va_end(arg);
 }
 
@@ -55,6 +54,7 @@
 struct frontend {
 	midend *me;
 	Image *background;
+	Image *img;
 	Image **colors;
 	int ncolors;
 	Point ZP;
@@ -210,7 +210,7 @@
 	else if (align & ALIGN_HRIGHT)
 		p.x -= size.x;
 	
-	string(screen, addpt(p, fe->ZP), fe->colors[color], ZP, f, text);
+	string(fe->img, p, fe->colors[color], ZP, f, text);
 }
 
 static void
@@ -217,7 +217,7 @@
 p9_draw_rect(void *handle, int x, int y, int w, int h, int color)
 {
 	frontend *fe = (frontend*)handle;
-	draw(screen, rectaddpt(Rect(x, y, x+w, y+h), fe->ZP), fe->colors[color], nil, ZP);
+	draw(fe->img, Rect(x, y, x+w, y+h), fe->colors[color], nil, ZP);
 }
 
 static void
@@ -224,7 +224,7 @@
 p9_draw_line(void *handle, int x1, int y1, int x2, int y2, int color)
 {
 	frontend *fe = (frontend*)handle;
-	line(screen, addpt(Pt(x1, y1), fe->ZP), addpt(Pt(x2, y2), fe->ZP), Endsquare, Endsquare, 0, fe->colors[color], ZP);
+	line(fe->img, Pt(x1, y1), Pt(x2, y2), Endsquare, Endsquare, 0, fe->colors[color], ZP);
 }
 
 static void
@@ -231,28 +231,25 @@
 p9_draw_thick_line(void *handle, float thickness, float x1, float y1, float x2, float y2, int color)
 {
 	frontend *fe = (frontend*)handle;
-	line(screen, addpt(Pt(x1, y1), fe->ZP), addpt(Pt(x2, y2), fe->ZP), Endsquare, Endsquare, thickness-1, fe->colors[color], ZP);
+	line(fe->img, Pt(x1, y1), Pt(x2, y2), Endsquare, Endsquare, thickness-1, fe->colors[color], ZP);
 }
 
 static void
 p9_draw_poly(void *handle, const int *coords, int npoints, int fillcolor, int outlinecolor)
 {
-	Point *points;
+	Point *points = (Point*)coords;
 	frontend *fe = (frontend*)handle;
 	
-	points = malloc((npoints+1) * sizeof(Point));
-	for (int i = 0; i < npoints; i++) {
-		points[i].x = coords[i*2+0] + fe->ZP.x;
-		points[i].y = coords[i*2+1] + fe->ZP.y;
-	}
-	points[npoints] = points[0];
-	
 	if (fillcolor >= 0)
-		fillpoly(screen, points, npoints, 0, fe->colors[fillcolor], ZP);
-	if (outlinecolor >= 0)
-		poly(screen, points, npoints+1, Endsquare, Endsquare, 0, fe->colors[outlinecolor], ZP);
+		fillpoly(fe->img, (Point*)coords, npoints, 0, fe->colors[fillcolor], ZP);
 	
-	free(points);
+	if (outlinecolor < 0)
+		return;
+	
+	/* outline from first to last point */
+	poly(fe->img, points, npoints, Endsquare, Endsquare, 0, fe->colors[outlinecolor], ZP);
+	/* close the outline shape by drawing from the last to the first point */
+	line(fe->img, points[npoints-1], points[0], Endsquare, Endsquare, 0, fe->colors[outlinecolor], ZP);
 }
 
 static void
@@ -259,16 +256,18 @@
 p9_draw_circle(void *handle, int cx, int cy, int radius, int fillcolor, int outlinecolor)
 {
 	frontend *fe = (frontend*)handle;
-	Point c = addpt(Pt(cx, cy), fe->ZP);
-	fillellipse(screen, c, radius, radius, fe->colors[fillcolor], ZP);
-	ellipse(screen, c, radius, radius, 0, fe->colors[outlinecolor], ZP);
+	Point c = Pt(cx, cy);
+	fillellipse(fe->img, c, radius, radius, fe->colors[fillcolor], ZP);
+	ellipse(fe->img, c, radius, radius, 0, fe->colors[outlinecolor], ZP);
+	Log("ellipse\n");
 }
 
 static void
 p9_draw_update(void *handle, int x, int y, int w, int h)
 {
-	USED(handle, x, y, w, h);
-	//frontend *fe = (frontend*)handle;
+	frontend *fe = (frontend*)handle;
+	draw(screen, rectaddpt(Rect(x, y, x+w, y+h), fe->ZP), fe->img, nil, fe->ZP);
+	Log("draw_update\n");
 }
 
 static void
@@ -275,8 +274,8 @@
 p9_clip(void *handle, int x, int y, int w, int h)
 {
 	frontend *fe = (frontend*)handle;
-	Rectangle r = rectaddpt(Rect(x, y, x + w, y + h), fe->ZP);
-	replclipr(screen, 0, r);
+	Rectangle r = Rect(x, y, x + w, y + h);
+	replclipr(fe->img, 0, r);
 }
 
 static void
@@ -283,7 +282,7 @@
 p9_unclip(void *handle)
 {
 	USED(handle);
-	replclipr(screen, 0, screen->r);
+	replclipr(fe->img, 0, fe->img->r);
 }
 
 long drawtime;
@@ -292,7 +291,7 @@
 p9_start_draw(void *handle)
 {
 	USED(handle);
-	LOG("start_draw");
+	Log("start_draw\n");
 #ifdef PROFILE
 	drawtime = times(nil);
 #endif
@@ -301,9 +300,10 @@
 static void
 p9_end_draw(void *handle)
 {
-	USED(handle);
+	frontend *fe = (frontend*)handle;
+	draw(screen, fe->rect, fe->img, nil, ZP);
 	flushimage(display, 1);
-	LOG("end_draw");
+	Log("end_draw\n");
 #ifdef PROFILE
 	ptimes.draw = times(nil) - drawtime;
 #endif
@@ -320,8 +320,8 @@
 static blitter*
 p9_blitter_new(void *handle, int w, int h)
 {
-	blitter *bl;
 	USED(handle);
+	blitter *bl;
 	bl = malloc(sizeof(blitter));
 	bl->blimg = allocimage(display, Rect(0, 0, w, h), screen->chan, 0, 0);
 	return bl;
@@ -338,20 +338,21 @@
 static void
 p9_blitter_save(void *handle, blitter *bl, int x, int y)
 {
-	USED(handle);
+	frontend *fe = (frontend*)handle;
 	bl->pos = Pt(x, y);
-	draw(bl->blimg, Rect(x, y, x + bl->blimg->r.max.x, y + bl->blimg->r.max.y), screen, nil, Pt(x, y));
+	flushimage(display, 0);
+	draw(bl->blimg, bl->blimg->r, fe->img, nil, Pt(x, y));
 }
 
 static void
 p9_blitter_load(void *handle, blitter *bl, int x, int y)
 {
-	USED(handle);
+	frontend *fe = (frontend*)handle;
 	if (x == BLITTER_FROMSAVED)
 		x = bl->pos.x;
 	if (y == BLITTER_FROMSAVED)
 		y = bl->pos.y;
-	draw(screen, Rect(x, y, x + bl->blimg->r.max.x, y + bl->blimg->r.max.y), bl->blimg, nil, Pt(x, y));
+	draw(fe->img, rectaddpt(bl->blimg->r, Pt(x, y)), bl->blimg, nil, ZP);
 }
 
 static const drawing_api p9_drawing = {
@@ -519,6 +520,12 @@
 		return;
 	draw(screen, screen->r, fe->background, nil, ZP);
 	
+	if (fe->img)
+		freeimage(fe->img);
+	fe->img = allocimage(display, Rpt(ZP, newsize), screen->chan, 0, 0);
+	if (!fe->img)
+		sysfatal("cannot allocate new frame: %r");
+	
 	/* calculate offset to place game area in the center */
 	offset = Pt(Dx(rarea), Dy(rarea)); // size of total game area
 	offset = subpt(divpt(addpt(offset, newsize), 2), newsize);
@@ -539,7 +546,7 @@
 		break;
 	}
 	
-	LOG("resizecontrolset");
+	Log("resizecontrolset\n");
 }
 
 typedef struct Option Option;
@@ -740,7 +747,7 @@
 		configs[i] = nil;
 	}
 	loadoptions();
-	LOG("saved options");
+	Log("saved options\n");
 	return r;
 }
 
@@ -787,7 +794,7 @@
 {
 	fe->showframe = frame;
 	resizecontrolset(fe->cs);
-	LOG("showframe");
+	Log("showframe\n");
 }
 
 static int
@@ -826,6 +833,7 @@
 {
 #ifdef PROFILE
 	char msg[128];
+	Point p;
 #endif
 	
 	if (fe->timeractive) {
@@ -834,7 +842,9 @@
 	
 #ifdef PROFILE
 	snprint(msg, 128, "draw: %ld", ptimes.draw);
-	chanprint(fe->cs->ctl, "l_status value %q", msg);
+	p = stringsize(font, msg);
+	draw(screen, rectaddpt(Rect(0, 0, p.x, p.y), Pt(10, 30)), fe->background, nil, ZP);
+	string(screen, Pt(10, 30), display->black, ZP, font, msg);
 #endif
 }
 
@@ -958,7 +968,7 @@
 			sysfatal("error opening log file /srv/puzzles: %r");
 		fprint(logfd, "%d", logpipe[0]);
 		close(logpipe[0]);
-		LOG(thegame.name);
+		Log("%s\n", thegame.name);
 	}
 	
 	if (initdraw(nil, nil, thegame.name) < 0) {
--- /dev/null
+++ b/todo.plan9
@@ -1,0 +1,11 @@
+inertia - unplayable (missing draws)
+loopy - crashes (fp bug)
+mines - sometimes crashes (assertion)
+net - white screen, unplayable
+pattern - sometimes hangs (?)
+solo - crashes (bad magic)
+
+- fp bug - no idea
+- bad magic - no idea
+- white screen - no idea
+- missing draws - no idea