chiark / gitweb /
Removed an extra layer of complexity for count colors
[sgt-puzzles.git] / magnets.c
index 5437c61443979be0eddc0d3be38b27676c69a6c7..59841544a8f1e487f74bc722962a19a265b3e234 100644 (file)
--- a/magnets.c
+++ b/magnets.c
@@ -1746,11 +1746,10 @@ struct game_drawstate {
 #define DS_ERROR    0x10
 #define DS_CURSOR   0x20
 #define DS_SET      0x40
-#define DS_FULL     0x80
-#define DS_NOTPOS   0x100
-#define DS_NOTNEG   0x200
-#define DS_NOTNEU   0x400
-#define DS_FLASH    0x800
+#define DS_NOTPOS   0x80
+#define DS_NOTNEG   0x100
+#define DS_NOTNEU   0x200
+#define DS_FLASH    0x400
 
 #define PREFERRED_TILE_SIZE 32
 #define TILE_SIZE (ds->tilesize)
@@ -1960,7 +1959,7 @@ static void game_free_drawstate(drawing *dr, game_drawstate *ds)
     sfree(ds);
 }
 
-static void draw_num_col(drawing *dr, game_drawstate *ds, int rowcol, int which,
+static void draw_num(drawing *dr, game_drawstate *ds, int rowcol, int which,
                          int idx, int colbg, int col, int num)
 {
     char buf[32];
@@ -1988,13 +1987,6 @@ static void draw_num_col(drawing *dr, game_drawstate *ds, int rowcol, int which,
     draw_update(dr, cx, cy, TILE_SIZE, TILE_SIZE);
 }
 
-static void draw_num(drawing *dr, game_drawstate *ds, int rowcol, int which,
-                     int idx, unsigned long c, int num)
-{
-    draw_num_col(dr, ds, rowcol, which, idx, COL_BACKGROUND,
-                 (c & DS_ERROR) ? COL_ERROR : COL_TEXT, num);
-}
-
 static void draw_sym(drawing *dr, game_drawstate *ds, int x, int y, int which, int col)
 {
     int cx = COORD(x), cy = COORD(y);
@@ -2132,6 +2124,18 @@ static void draw_tile(drawing *dr, game_drawstate *ds, int *dominoes,
     draw_update(dr, cx, cy, TILE_SIZE, TILE_SIZE);
 }
 
+static int get_count_color(const game_state *state, int rowcol, int which,
+                           int index, int target)
+{
+    int count = count_rowcol(state, index, rowcol, which);
+
+    if ((count > target) ||
+        (count < target && !count_rowcol(state, index, rowcol, -1))) {
+        return COL_ERROR;
+    }
+
+    return COL_TEXT;
+}
 
 static void game_redraw(drawing *dr, game_drawstate *ds,
                         const game_state *oldstate, const game_state *state,
@@ -2139,7 +2143,6 @@ static void game_redraw(drawing *dr, game_drawstate *ds,
                         float animtime, float flashtime)
 {
     int x, y, w = state->w, h = state->h, which, i, j, flash;
-    unsigned long c = 0;
 
     flash = (int)(flashtime * 5 / FLASH_TIME) % 2;
 
@@ -2162,8 +2165,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds,
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
             int idx = y*w+x;
-
-            c = state->grid[idx];
+            unsigned long c = state->grid[idx];
 
             if (state->flags[idx] & GS_ERROR)
                 c |= DS_ERROR;
@@ -2191,33 +2193,24 @@ static void game_redraw(drawing *dr, game_drawstate *ds,
     }
     /* Draw counts around side */
     for (which = POSITIVE, j = 0; j < 2; which = OPPOSITE(which), j++) {
-        int target, count;
         for (i = 0; i < w; i++) {
-            target = state->common->colcount[i*3+which];
-            count = count_rowcol(state, i, COLUMN, which);
-            c = 0;
-            if ((count > target) ||
-                (count < target && !count_rowcol(state, i, COLUMN, -1)))
-                c |= DS_ERROR;
-            if (count == target) c |= DS_FULL;
-            if (c != ds->colwhat[i*3+which] || !ds->started) {
-                draw_num(dr, ds, COLUMN, which, i, c,
-                         state->common->colcount[i*3+which]);
-                ds->colwhat[i*3+which] = c;
+            int index = i * 3 + which;
+            int target = state->common->colcount[index];
+            int color = get_count_color(state, COLUMN, which, i, target);
+
+            if (color != ds->colwhat[index] || !ds->started) {
+                draw_num(dr, ds, COLUMN, which, i, COL_BACKGROUND, color, target);
+                ds->colwhat[index] = color;
             }
         }
         for (i = 0; i < h; i++) {
-            target = state->common->rowcount[i*3+which];
-            count = count_rowcol(state, i, ROW, which);
-            c = 0;
-            if ((count > target) ||
-                (count < target && !count_rowcol(state, i, ROW, -1)))
-                c |= DS_ERROR;
-            if (count == target) c |= DS_FULL;
-            if (c != ds->rowwhat[i*3+which] || !ds->started) {
-                draw_num(dr, ds, ROW, which, i, c,
-                         state->common->rowcount[i*3+which]);
-                ds->rowwhat[i*3+which] = c;
+            int index = i * 3 + which;
+            int target = state->common->rowcount[index];
+            int color = get_count_color(state, ROW, which, i, target);
+
+            if (color != ds->rowwhat[index] || !ds->started) {
+                draw_num(dr, ds, ROW, which, i, COL_BACKGROUND, color, target);
+                ds->rowwhat[index] = color;
             }
         }
     }
@@ -2282,11 +2275,11 @@ static void game_print(drawing *dr, const game_state *state, int tilesize)
     draw_sym(dr, ds, state->w, state->h, NEGATIVE, ink);
     for (which = POSITIVE, j = 0; j < 2; which = OPPOSITE(which), j++) {
         for (i = 0; i < w; i++) {
-            draw_num_col(dr, ds, COLUMN, which, i, paper, ink,
+            draw_num(dr, ds, COLUMN, which, i, paper, ink,
                          state->common->colcount[i*3+which]);
         }
         for (i = 0; i < h; i++) {
-            draw_num_col(dr, ds, ROW, which, i, paper, ink,
+            draw_num(dr, ds, ROW, which, i, paper, ink,
                          state->common->rowcount[i*3+which]);
         }
     }