+static void edge_bbox(game_drawstate *ds, grid *g, grid_edge *e,
+ int *x, int *y, int *w, int *h)
+{
+ int x1 = e->dot1->x;
+ int y1 = e->dot1->y;
+ int x2 = e->dot2->x;
+ int y2 = e->dot2->y;
+ int xmin, xmax, ymin, ymax;
+
+ grid_to_screen(ds, g, x1, y1, &x1, &y1);
+ grid_to_screen(ds, g, x2, y2, &x2, &y2);
+ /* Allow extra margin for dots, and thickness of lines */
+ xmin = min(x1, x2) - 2;
+ xmax = max(x1, x2) + 2;
+ ymin = min(y1, y2) - 2;
+ ymax = max(y1, y2) + 2;
+
+ *x = xmin;
+ *y = ymin;
+ *w = xmax - xmin + 1;
+ *h = ymax - ymin + 1;
+}
+
+static void dot_bbox(game_drawstate *ds, grid *g, grid_dot *d,
+ int *x, int *y, int *w, int *h)
+{
+ int x1, y1;
+
+ grid_to_screen(ds, g, d->x, d->y, &x1, &y1);
+
+ *x = x1 - 2;
+ *y = y1 - 2;
+ *w = 5;
+ *h = 5;
+}
+
+static const int loopy_line_redraw_phases[] = {
+ COL_FAINT, COL_LINEUNKNOWN, COL_FOREGROUND, COL_HIGHLIGHT, COL_MISTAKE
+};
+#define NPHASES lenof(loopy_line_redraw_phases)
+
+static void game_redraw_line(drawing *dr, game_drawstate *ds,
+ game_state *state, int i, int phase)
+{
+ grid *g = state->game_grid;
+ grid_edge *e = g->edges + i;
+ int x1, x2, y1, y2;
+ int line_colour;
+
+ if (state->line_errors[i])
+ line_colour = COL_MISTAKE;
+ else if (state->lines[i] == LINE_UNKNOWN)
+ line_colour = COL_LINEUNKNOWN;
+ else if (state->lines[i] == LINE_NO)
+ line_colour = COL_FAINT;
+ else if (ds->flashing)
+ line_colour = COL_HIGHLIGHT;
+ else
+ line_colour = COL_FOREGROUND;
+ if (line_colour != loopy_line_redraw_phases[phase])
+ return;
+
+ /* Convert from grid to screen coordinates */
+ grid_to_screen(ds, g, e->dot1->x, e->dot1->y, &x1, &y1);
+ grid_to_screen(ds, g, e->dot2->x, e->dot2->y, &x2, &y2);
+
+ if (line_colour == COL_FAINT) {
+ static int draw_faint_lines = -1;
+ if (draw_faint_lines < 0) {
+ char *env = getenv("LOOPY_FAINT_LINES");
+ draw_faint_lines = (!env || (env[0] == 'y' ||
+ env[0] == 'Y'));
+ }
+ if (draw_faint_lines)
+ draw_line(dr, x1, y1, x2, y2, line_colour);
+ } else {
+ draw_thick_line(dr, 3.0,
+ x1 + 0.5, y1 + 0.5,
+ x2 + 0.5, y2 + 0.5,
+ line_colour);
+ }
+}
+
+static void game_redraw_dot(drawing *dr, game_drawstate *ds,
+ game_state *state, int i)
+{
+ grid *g = state->game_grid;
+ grid_dot *d = g->dots + i;
+ int x, y;
+
+ grid_to_screen(ds, g, d->x, d->y, &x, &y);
+ draw_circle(dr, x, y, 2, COL_FOREGROUND, COL_FOREGROUND);
+}
+
+static int boxes_intersect(int x0, int y0, int w0, int h0,
+ int x1, int y1, int w1, int h1)
+{
+ /*
+ * Two intervals intersect iff neither is wholly on one side of
+ * the other. Two boxes intersect iff their horizontal and
+ * vertical intervals both intersect.
+ */
+ return (x0 < x1+w1 && x1 < x0+w0 && y0 < y1+h1 && y1 < y0+h0);
+}
+
+static void game_redraw_in_rect(drawing *dr, game_drawstate *ds,
+ game_state *state, int x, int y, int w, int h)
+{
+ grid *g = state->game_grid;
+ int i, phase;
+ int bx, by, bw, bh;
+
+ clip(dr, x, y, w, h);
+ draw_rect(dr, x, y, w, h, COL_BACKGROUND);
+
+ for (i = 0; i < g->num_faces; i++) {
+ if (state->clues[i] >= 0) {
+ face_text_bbox(ds, g, &g->faces[i], &bx, &by, &bw, &bh);
+ if (boxes_intersect(x, y, w, h, bx, by, bw, bh))
+ game_redraw_clue(dr, ds, state, i);
+ }
+ }
+ for (phase = 0; phase < NPHASES; phase++) {
+ for (i = 0; i < g->num_edges; i++) {
+ edge_bbox(ds, g, &g->edges[i], &bx, &by, &bw, &bh);
+ if (boxes_intersect(x, y, w, h, bx, by, bw, bh))
+ game_redraw_line(dr, ds, state, i, phase);
+ }
+ }
+ for (i = 0; i < g->num_dots; i++) {
+ dot_bbox(ds, g, &g->dots[i], &bx, &by, &bw, &bh);
+ if (boxes_intersect(x, y, w, h, bx, by, bw, bh))
+ game_redraw_dot(dr, ds, state, i);
+ }
+
+ unclip(dr);
+ draw_update(dr, x, y, w, h);