Cleanups to completion flashes: all four of these games used to
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Fri, 5 Aug 2005 17:17:23 +0000 (17:17 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Fri, 5 Aug 2005 17:17:23 +0000 (17:17 +0000)
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.

git-svn-id: svn://svn.tartarus.org/sgt/puzzles@6166 cda61777-01e9-0310-a592-d414129be87e

blackbox.c
mines.c
samegame.c
untangle.c

index 4f22704..817004f 100644 (file)
@@ -859,7 +859,7 @@ struct game_drawstate {
     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 @@ static game_drawstate *game_new_drawstate(game_state *state)
     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 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     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 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
         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 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     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];
diff --git a/mines.c b/mines.c
index d17fdd6..2781f15 100644 (file)
--- a/mines.c
+++ b/mines.c
@@ -2384,7 +2384,7 @@ static void game_changed_state(game_ui *ui, game_state *oldstate,
 }
 
 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 @@ static game_drawstate *game_new_drawstate(game_state *state)
     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 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
                (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;
index d8c58ac..2331799 100644 (file)
@@ -1432,6 +1432,7 @@ static game_drawstate *game_new_drawstate(game_state *state)
     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 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
             * 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);
index bb63eba..e74fefa 100644 (file)
@@ -1063,6 +1063,8 @@ static void game_changed_state(game_ui *ui, game_state *oldstate,
 
 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,14 +1238,23 @@ static float *game_colours(frontend *fe, game_state *state, int *ncolours)
 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;
 }
 
 static void game_free_drawstate(game_drawstate *ds)
 {
+    sfree(ds->y);
+    sfree(ds->x);
     sfree(ds);
 }
 
@@ -1265,7 +1276,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     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 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     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 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
      */
 
     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 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
        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 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
                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
            }
        }