shithub: puzzles

Download patch

ref: c321a88408c4541160c66151210bd048761ae392
parent: 56e01e54fad38cf1470b0994ea2cc70c714ced5d
author: Simon Tatham <anakin@pobox.com>
date: Fri Aug 5 13:17:23 EDT 2005

Cleanups to completion flashes: all four of these games used to
redraw the whole window _every_ time game_redraw() was called during
a flash. Now they only redraw the whole window every time the
background colour actually changes. Thanks to James H for much of
the work.

[originally from svn r6166]

--- a/blackbox.c
+++ b/blackbox.c
@@ -859,7 +859,7 @@
     int tilesize, crad, rrad, w, h; /* w and h to make macros work... */
     unsigned int *grid;          /* as the game_state grid */
     int started, reveal;
-    int flash_laserno;
+    int flash_laserno, isflash;
 };
 
 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
@@ -1112,6 +1112,7 @@
     memset(ds->grid, 0, (state->w+2)*(state->h+2)*sizeof(unsigned int));
     ds->started = ds->reveal = 0;
     ds->flash_laserno = LASER_EMPTY;
+    ds->isflash = 0;
 
     return ds;
 }
@@ -1266,7 +1267,6 @@
     if (flashtime > 0) {
         int frame = (int)(flashtime / FLASH_FRAME);
         isflash = (frame % 2) == 0;
-        force = 1;
         debug(("game_redraw: flashtime = %f", flashtime));
     }
 
@@ -1299,6 +1299,8 @@
         ds->started = 1;
     }
 
+    if (isflash != ds->isflash) force = 1;
+
     /* draw the arena */
     for (x = 0; x < state->w; x++) {
         for (y = 0; y < state->h; y++) {
@@ -1324,6 +1326,7 @@
     draw_update(fe, TODRAW(0), TODRAW(0), TILE_SIZE, TILE_SIZE);
     ds->reveal = state->reveal;
     ds->flash_laserno = ui->flash_laserno;
+    ds->isflash = isflash;
 
     {
         char buf[256];
--- a/mines.c
+++ b/mines.c
@@ -2384,7 +2384,7 @@
 }
 
 struct game_drawstate {
-    int w, h, started, tilesize;
+    int w, h, started, tilesize, bg;
     signed char *grid;
     /*
      * Items in this `grid' array have all the same values as in
@@ -2723,6 +2723,7 @@
     ds->started = FALSE;
     ds->tilesize = 0;                  /* not decided yet */
     ds->grid = snewn(ds->w * ds->h, signed char);
+    ds->bg = -1;
 
     memset(ds->grid, -99, ds->w * ds->h);
 
@@ -2963,11 +2964,12 @@
 		(abs(x-ui->hx) <= ui->hradius && abs(y-ui->hy) <= ui->hradius))
 		v -= 20;
 
-	    if (ds->grid[y*ds->w+x] != v || bg != COL_BACKGROUND) {
+	    if (ds->grid[y*ds->w+x] != v || bg != ds->bg) {
 		draw_tile(fe, ds, COORD(x), COORD(y), v, bg);
-		ds->grid[y*ds->w+x] = (bg == COL_BACKGROUND ? v : -10);
+		ds->grid[y*ds->w+x] = v;
 	    }
 	}
+    ds->bg = bg;
 
     if (!state->layout->mines)
 	mines = state->layout->n;
--- a/samegame.c
+++ b/samegame.c
@@ -1432,6 +1432,7 @@
     ds->started = 0;
     ds->tileinner = ds->tilegap = 0;   /* not decided yet */
     ds->tiles = snewn(state->n, int);
+    ds->bgcolour = -1;
     for (i = 0; i < state->n; i++)
 	ds->tiles[i] = -1;
 
@@ -1564,7 +1565,6 @@
 	     * no animation); when we do we might well want to be looking
 	     * at the tile colours from oldstate, not state. */
 	    if ((oldstate && COL(oldstate,x,y) != col) ||
-		(flashtime > 0.0) ||
 		(ds->bgcolour != bgcolour) ||
 		(tile != ds->tiles[i])) {
 		tile_redraw(fe, ds, x, y, dright, dbelow, tile, bgcolour);
--- a/untangle.c
+++ b/untangle.c
@@ -1063,6 +1063,8 @@
 
 struct game_drawstate {
     long tilesize;
+    int bg, dragpoint;
+    long *x, *y;
 };
 
 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
@@ -1236,8 +1238,15 @@
 static game_drawstate *game_new_drawstate(game_state *state)
 {
     struct game_drawstate *ds = snew(struct game_drawstate);
+    int i;
 
     ds->tilesize = 0;
+    ds->x = snewn(state->params.n, long);
+    ds->y = snewn(state->params.n, long);
+    for (i = 0; i < state->params.n; i++)
+        ds->x[i] = ds->y[i] = -1;
+    ds->bg = -1;
+    ds->dragpoint = -1;
 
     return ds;
 }
@@ -1244,6 +1253,8 @@
 
 static void game_free_drawstate(game_drawstate *ds)
 {
+    sfree(ds->y);
+    sfree(ds->x);
     sfree(ds);
 }
 
@@ -1265,7 +1276,7 @@
     int w, h;
     edge *e;
     int i, j;
-    int bg;
+    int bg, points_moved;
 
     /*
      * There's no terribly sensible way to do partial redraws of
@@ -1280,6 +1291,43 @@
     else
         bg = COL_FLASH2;
 
+    /*
+     * To prevent excessive spinning on redraw during a completion
+     * flash, we first check to see if _either_ the flash
+     * background colour has changed _or_ at least one point has
+     * moved _or_ a drag has begun or ended, and abandon the redraw
+     * if neither is the case.
+     * 
+     * Also in this loop we work out the coordinates of all the
+     * points for this redraw.
+     */
+    points_moved = FALSE;
+    for (i = 0; i < state->params.n; i++) {
+        point p = state->pts[i];
+        long x, y;
+
+        if (ui->dragpoint == i)
+            p = ui->newpoint;
+
+        if (oldstate)
+            p = mix(oldstate->pts[i], p, animtime / ui->anim_length);
+
+	x = p.x * ds->tilesize / p.d;
+	y = p.y * ds->tilesize / p.d;
+
+        if (ds->x[i] != x || ds->y[i] != y)
+            points_moved = TRUE;
+
+        ds->x[i] = x;
+        ds->y[i] = y;
+    }
+
+    if (ds->bg == bg && ds->dragpoint == ui->dragpoint && !points_moved)
+        return;                        /* nothing to do */
+
+    ds->dragpoint = ui->dragpoint;
+    ds->bg = bg;
+
     game_compute_size(&state->params, ds->tilesize, &w, &h);
     draw_rect(fe, 0, 0, w, h, bg);
 
@@ -1288,27 +1336,7 @@
      */
 
     for (i = 0; (e = index234(state->graph->edges, i)) != NULL; i++) {
-	point p1, p2;
-	long x1, y1, x2, y2;
-
-	p1 = state->pts[e->a];
-	p2 = state->pts[e->b];
-	if (ui->dragpoint == e->a)
-	    p1 = ui->newpoint;
-	else if (ui->dragpoint == e->b)
-	    p2 = ui->newpoint;
-
-	if (oldstate) {
-	    p1 = mix(oldstate->pts[e->a], p1, animtime / ui->anim_length);
-	    p2 = mix(oldstate->pts[e->b], p2, animtime / ui->anim_length);
-	}
-
-	x1 = p1.x * ds->tilesize / p1.d;
-	y1 = p1.y * ds->tilesize / p1.d;
-	x2 = p2.x * ds->tilesize / p2.d;
-	y2 = p2.y * ds->tilesize / p2.d;
-
-	draw_line(fe, x1, y1, x2, y2,
+	draw_line(fe, ds->x[e->a], ds->y[e->a], ds->x[e->b], ds->y[e->b],
 #ifdef SHOW_CROSSINGS
 		  (oldstate?oldstate:state)->crosses[i] ?
 		  COL_CROSSEDLINE :
@@ -1326,12 +1354,9 @@
 	int thisc = (j == 0 ? COL_POINT :
 		     j == 1 ? COL_NEIGHBOUR : COL_DRAGPOINT);
 	for (i = 0; i < state->params.n; i++) {
-	    long x, y;
             int c;
-	    point p = state->pts[i];
 
 	    if (ui->dragpoint == i) {
-		p = ui->newpoint;
 		c = COL_DRAGPOINT;
 	    } else if (ui->dragpoint >= 0 &&
 		       isedge(state->graph->edges, ui->dragpoint, i)) {
@@ -1340,23 +1365,19 @@
 		c = COL_POINT;
 	    }
 
-	    if (oldstate)
-		p = mix(oldstate->pts[i], p, animtime / ui->anim_length);
-
 	    if (c == thisc) {
-		x = p.x * ds->tilesize / p.d;
-		y = p.y * ds->tilesize / p.d;
-
 #ifdef VERTEX_NUMBERS
-		draw_circle(fe, x, y, DRAG_THRESHOLD, bg, bg);
+		draw_circle(fe, ds->x[i], ds->y[i], DRAG_THRESHOLD, bg, bg);
 		{
 		    char buf[80];
 		    sprintf(buf, "%d", i);
-		    draw_text(fe, x, y, FONT_VARIABLE, DRAG_THRESHOLD*3/2,
+		    draw_text(fe, ds->x[i], ds->y[i], FONT_VARIABLE,
+                              DRAG_THRESHOLD*3/2,
 			      ALIGN_VCENTRE|ALIGN_HCENTRE, c, buf);
 		}
 #else
-		draw_circle(fe, x, y, CIRCLE_RADIUS, c, COL_OUTLINE);
+		draw_circle(fe, ds->x[i], ds->y[i], CIRCLE_RADIUS,
+                            c, COL_OUTLINE);
 #endif
 	    }
 	}