ref: 8b6a38b1230441d6850634dec79af0c9996bd5e8
parent: 8a03fbfdfacb4b1d1a3dc6aaeafe9078bd5164c4
author: sirjofri <[email protected]>
date: Fri May 24 07:28:24 EDT 2024
fixes redraw-on-resize bug, only direct draw now
--- a/plan9.c
+++ b/plan9.c
@@ -12,16 +12,20 @@
#error Plan 9 should not be COMBINED
#endif
-//#define DIRECTDRAW
//#define PROFILE
+int dolog = 0;
+int logfd = -1;
+int logpipe[2];
+#define LOG(c) { if (dolog) fprint(logpipe[1], "%s\n", c); }
+
struct frontend {
- Image *image;
midend *me;
Image *background;
Image **colors;
int ncolors;
Point ZP;
+ Rectangle rect;
Controlset *cs;
Channel *c;
Channel *settingschan;
@@ -110,11 +114,7 @@
{
// todo: align, fontsize, fonttype
frontend *fe = (frontend*)handle;
-#ifdef DIRECTDRAW
string(screen, addpt(Pt(x, y), fe->ZP), fe->colors[color], ZP, font, text);
-#else
- string(fe->image, Pt(x, y), fe->colors[color], ZP, font, text);
-#endif
}
static void
@@ -121,11 +121,7 @@
p9_draw_rect(void *handle, int x, int y, int w, int h, int color)
{
frontend *fe = (frontend*)handle;
-#ifdef DIRECTDRAW
draw(screen, rectaddpt(Rect(x, y, x+w, y+h), fe->ZP), fe->colors[color], nil, ZP);
-#else
- draw(fe->image, Rect(x, y, x+w, y+h), fe->colors[color], nil, ZP);
-#endif
}
static void
@@ -132,11 +128,7 @@
p9_draw_line(void *handle, int x1, int y1, int x2, int y2, int color)
{
frontend *fe = (frontend*)handle;
-#ifdef DIRECTDRAW
line(screen, addpt(Pt(x1, y1), fe->ZP), addpt(Pt(x2, y2), fe->ZP), Endsquare, Endsquare, 0, fe->colors[color], ZP);
-#else
- line(fe->image, Pt(x1, y1), Pt(x2, y2), Endsquare, Endsquare, 0, fe->colors[color], ZP);
-#endif
}
static void
@@ -143,11 +135,7 @@
p9_draw_thick_line(void *handle, float thickness, float x1, float y1, float x2, float y2, int color)
{
frontend *fe = (frontend*)handle;
-#ifdef DIRECTDRAW
line(screen, addpt(Pt(x1, y1), fe->ZP), addpt(Pt(x2, y2), fe->ZP), Endsquare, Endsquare, thickness-1, fe->colors[color], ZP);
-#else
- line(fe->image, Pt(x1, y1), Pt(x2, y2), Endsquare, Endsquare, thickness-1, fe->colors[color], ZP);
-#endif
}
static void
@@ -158,26 +146,14 @@
points = malloc(npoints * sizeof(Point));
for (int i = 0; i < npoints; i++) {
-#ifdef DIRECTDRAW
points[i].x = coords[i*2+0] + fe->ZP.x;
points[i].y = coords[i*2+1] + fe->ZP.y;
-#else
- points[i].x = coords[i*2+0];
- points[i].y = coords[i*2+1];
-#endif
}
-#ifdef DIRECTDRAW
if (fillcolor > 0)
fillpoly(screen, points, npoints, 0, fe->colors[fillcolor], ZP);
if (outlinecolor > 0)
poly(screen, points, npoints, Endsquare, Endsquare, 1, fe->colors[outlinecolor], ZP);
-#else
- if (fillcolor > 0)
- fillpoly(fe->image, points, npoints, 0, fe->colors[fillcolor], ZP);
- if (outlinecolor > 0)
- poly(fe->image, points, npoints, Endsquare, Endsquare, 1, fe->colors[outlinecolor], ZP);
-#endif
free(points);
}
@@ -186,15 +162,9 @@
p9_draw_circle(void *handle, int cx, int cy, int radius, int fillcolor, int outlinecolor)
{
frontend *fe = (frontend*)handle;
-#ifdef DIRECTDRAW
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);
-#else
- Point c = Pt(cx, cy);
- fillellipse(fe->image, c, radius, radius, fe->colors[fillcolor], ZP);
- ellipse(fe->image, c, radius, radius, 0, fe->colors[outlinecolor], ZP);
-#endif
}
static void
@@ -202,8 +172,6 @@
{
USED(handle, x, y, w, h);
//frontend *fe = (frontend*)handle;
- //print("draw_update\n");
- //chanprint(fe->cs->ctl, showcmd);
}
static void
@@ -210,7 +178,7 @@
p9_clip(void *handle, int x, int y, int w, int h)
{
frontend *fe = (frontend*)handle;
- fe->image->clipr = Rect(x, y, x + w, y + h);
+ screen->clipr = rectaddpt(Rect(x, y, x + w, y + h), fe->ZP);
}
static void
@@ -217,7 +185,7 @@
p9_unclip(void *handle)
{
frontend *fe = (frontend*)handle;
- fe->image->clipr = fe->image->r;
+ screen->clipr = screen->r;
}
long drawtime;
@@ -226,6 +194,7 @@
p9_start_draw(void *handle)
{
USED(handle);
+ LOG("start_draw");
#ifdef PROFILE
drawtime = times(nil);
#endif
@@ -235,7 +204,7 @@
p9_end_draw(void *handle)
{
frontend *fe = (frontend*)handle;
- chanprint(fe->cs->ctl, showcmd);
+ LOG("end_draw");
#ifdef PROFILE
ptimes.draw = times(nil) - drawtime;
#endif
@@ -271,7 +240,7 @@
p9_blitter_save(void *handle, blitter *bl, int x, int y)
{
frontend *fe = (frontend*)handle;
- draw(bl->blimg, Rect(x, y, x + bl->blimg->r.max.x, y + bl->blimg->r.max.y), fe->image, nil, Pt(x, y)); // fix ZP if needed
+ draw(bl->blimg, Rect(x, y, x + bl->blimg->r.max.x, y + bl->blimg->r.max.y), screen, nil, Pt(x, y)); // fix position
}
static void
@@ -278,7 +247,7 @@
p9_blitter_load(void *handle, blitter *bl, int x, int y)
{
frontend *fe = (frontend*)handle;
- draw(fe->image, Rect(x, y, x + bl->blimg->r.max.x, y + bl->blimg->r.max.y), bl->blimg, nil, Pt(x, y));
+ draw(screen, Rect(x, y, x + bl->blimg->r.max.x, y + bl->blimg->r.max.y), bl->blimg, nil, Pt(x, y)); // fix position
}
static const drawing_api p9_drawing = {
@@ -328,15 +297,11 @@
void
initui(Controlset *cs, Channel *c)
{
- Control *b_game, *b_settings, *c_game, *c_settings, *stackmain;
+ Control *b_game, *b_settings, *c_settings;
Point p;
createrow(cs, "rowmain");
- stackmain = createstack(cs, "stackmain");
- chanprint(cs->ctl, "stackmain border 1");
- controlwire(stackmain, "event", c);
-
b_game = createtextbutton(cs, "b_game");
p = stringsize(font, "game");
chanprint(cs->ctl, "b_game border 1");
@@ -343,14 +308,6 @@
chanprint(cs->ctl, "b_game align center");
chanprint(cs->ctl, "b_game text game");
chanprint(cs->ctl, "b_game size %d %d 500 %d", p.x, p.y, p.y);
- c_game = createbox(cs, "c_game");
- chanprint(cs->ctl, "c_game border 1");
-#ifdef DIRECTDRAW
- chanprint(cs->ctl, "c_game image background");
-#else
- chanprint(cs->ctl, "c_game image frame");
-#endif
- controlwire(c_game, "event", c);
controlwire(b_game, "event", c);
b_settings = createtextbutton(cs, "b_settings");
@@ -360,16 +317,15 @@
chanprint(cs->ctl, "b_settings text settings");
chanprint(cs->ctl, "b_settings size %d %d 500 %d", p.x, p.y, p.y);
c_settings = createcolumn(cs, "c_settings");
+ chanprint(cs->ctl, "c_settings hide");
controlwire(b_settings, "event", c);
createlabel(cs, "l_status");
- chanprint(cs->ctl, "stackmain add c_game c_settings");
chanprint(cs->ctl, "rowmain add b_game\nrowmain add b_settings");
activate(b_game);
activate(b_settings);
- activate(c_game);
}
void
@@ -381,8 +337,6 @@
float bgcol[3];
Channel *c, *d;
- fe->image = allocimage(display, screen->r, screen->chan, 0, 0);
-
frontend_default_colour(fe, bgcol);
fe->background = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, rgb2col(bgcol[0]*255., bgcol[1]*255., bgcol[2]*255.));
@@ -402,7 +356,6 @@
fe->cs = newcontrolset(screen, d, c, mousectl->resizec);
fe->c = chancreate(sizeof(char*), 0);
ctldeletequits = 1;
- namectlimage(fe->image, "frame");
namectlimage(display->black, "i_black");
namectlimage(display->white, "i_white");
namectlimage(fe->background, "background");
@@ -456,33 +409,29 @@
sarea = screen->r;
sarea.min.y = sarea.max.y - font->height;
- if (fe->image) {
- freeimage(fe->image);
- fe->image = nil;
- }
newsize = resize(&resizenop);
- fe->image = allocimage(display, Rect(0, 0, newsize.x, newsize.y), screen->chan, 0, 0);
if (0 && resizenop)
return;
draw(screen, screen->r, fe->background, nil, ZP);
-#ifndef DIRECTDRAW
- midend_force_redraw(fe->me);
-#endif
+ fe->rect = rarea;
+ fe->ZP = rarea.min;
chanprint(cs->ctl, "rowmain rect %R\nrowmain show", rmenu);
- chanprint(cs->ctl, "c_game rect %R\nc_settings rect %R", rarea, rarea);
- chanprint(cs->ctl, "stackmain rect %R\nstackmain show", rarea);
- chanprint(cs->ctl, "stackmain reveal %d", fe->showframe);
chanprint(cs->ctl, "l_status rect %R\nl_status show", sarea);
-#ifdef DIRECTDRAW
- if (fe->showframe == GAME)
+ switch (fe->showframe) {
+ case GAME:
+ chanprint(cs->ctl, "c_settings hide");
midend_force_redraw(fe->me);
-#endif
+ flushimage(display, 1);
+ break;
+ case SETTINGS:
+ chanprint(cs->ctl, "c_settings rect %R\nc_settings reveal\nc_settings show", rarea);
+ break;
+ }
- ctl = controlcalled("c_game");
- fe->ZP = ctl->rect.min;
+ LOG("resizecontrolset");
}
void
@@ -673,6 +622,7 @@
}
}
loadoptions();
+ LOG("saved options");
return r;
}
@@ -708,10 +658,9 @@
void
showframe(int frame)
{
- if (frame == GAME)
- midend_force_redraw(fe->me);
fe->showframe = frame;
- chanprint(fe->cs->ctl, "stackmain reveal %d", frame);
+ resizecontrolset(fe->cs);
+ LOG("showframe");
}
int
@@ -728,7 +677,6 @@
return 1; /* return 1 to quit */
case 'n':
midend_process_key(fe->me, 0, 0, UI_NEWGAME);
- chanprint(fe->cs->ctl, showcmd);
break;
default:
if (midend_process_key(fe->me, 0, 0, k) == PKR_QUIT)
@@ -784,18 +732,15 @@
processmouse(Mouse *m, int *lm)
{
int x, y, r;
- Control *c;
if (fe->showframe != GAME)
goto Ctrl;
- c = controlcalled("c_game");
-
- if (!ptinrect(m->xy, c->rect))
+ if (!ptinrect(m->xy, fe->rect))
goto Ctrl;
- x = m->xy.x - c->rect.min.x;
- y = m->xy.y - c->rect.min.y;
+ x = m->xy.x - fe->rect.min.x;
+ y = m->xy.y - fe->rect.min.y;
r = -1;
if ( ((*lm)&1) && !(m->buttons&1))
@@ -811,7 +756,6 @@
if (!((*lm)&4) && (m->buttons&2))
r = midend_process_key(fe->me, x, y, RIGHT_BUTTON);
if (r >= 0) {
- chanprint(fe->cs->ctl, showcmd);
}
*lm = m->buttons;
@@ -822,6 +766,14 @@
}
void
+doexit(void)
+{
+ if (dolog) {
+ close(logfd);
+ }
+}
+
+void
threadmain(int argc, char **argv)
{
int lastmouse;
@@ -828,7 +780,6 @@
char *s, *args[6];
int doprintoptions = 0;
char *wintitle;
- Channel *c;
config_item *cfg;
int changedprefs = 0;
float delta;
@@ -857,6 +808,9 @@
case 'o':
doprintoptions++;
break;
+ case 'l':
+ dolog++;
+ break;
case '-':
parseoption(cfg, ARGF());
changedprefs++;
@@ -876,7 +830,19 @@
exits(nil);
}
- if (initdraw(nil, nil, wintitle) < 0) {
+ atexit(doexit);
+
+ if (dolog) {
+ pipe(logpipe);
+ logfd = create("/srv/puzzles", OWRITE|ORCLOSE, 0666);
+ if (logfd < 0)
+ sysfatal("error opening log file /srv/puzzles: %r");
+ fprint(logfd, "%d", logpipe[0]);
+ close(logpipe[0]);
+ LOG(thegame.name);
+ }
+
+ if (initdraw(nil, nil, thegame.name) < 0) {
sysfatal("initdraw failed: %r");
}
@@ -916,8 +882,9 @@
if (saveoptions()) {
midend_new_game(fe->me);
showframe(GAME);
- resizecontrolset(fe->cs);
}
+ } else {
+ LOG(args[0]);
}
break;
case 1: /* timer */