Move the colour configuration into midend.c so that it becomes
[sgt/puzzles] / midend.c
index 0c91491..192d7e5 100644 (file)
--- a/midend.c
+++ b/midend.c
@@ -2,12 +2,14 @@
  * midend.c: general middle fragment sitting between the
  * platform-specific front end and game-specific back end.
  * Maintains a move list, takes care of Undo and Redo commands, and
- * processes standard keystrokes for undo/redo/new/restart/quit.
+ * processes standard keystrokes for undo/redo/new/quit.
  */
 
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <stdlib.h>
+#include <ctype.h>
 
 #include "puzzles.h"
 
@@ -238,6 +240,8 @@ void midend_restart_game(midend_data *me)
 {
     game_state *s;
 
+    midend_stop_anim(me);
+
     assert(me->statepos >= 1);
     if (me->statepos == 1)
         return;                        /* no point doing anything at all! */
@@ -272,11 +276,6 @@ static int midend_really_process_key(midend_data *me, int x, int y, int button)
        midend_new_game(me);
         midend_redraw(me);
         return 1;                      /* never animate */
-    } else if (button == 'r' || button == 'R') {
-       midend_stop_anim(me);
-       midend_restart_game(me);
-        midend_redraw(me);
-        return 1;                      /* never animate */
     } else if (button == 'u' || button == 'u' ||
                button == '\x1A' || button == '\x1F') {
        midend_stop_anim(me);
@@ -284,7 +283,8 @@ static int midend_really_process_key(midend_data *me, int x, int y, int button)
         gotspecial = TRUE;
        if (!midend_undo(me))
             return 1;
-    } else if (button == '\x12') {
+    } else if (button == 'r' || button == 'R' ||
+               button == '\x12') {
        midend_stop_anim(me);
        if (!midend_redo(me))
             return 1;
@@ -500,6 +500,32 @@ float *midend_colours(midend_data *me, int *ncolours)
 
     ret = me->ourgame->colours(me->frontend, state, ncolours);
 
+    {
+        int i;
+
+        /*
+         * Allow environment-based overrides for the standard
+         * colours by defining variables along the lines of
+         * `NET_COLOUR_4=6000c0'.
+         */
+
+        for (i = 0; i < *ncolours; i++) {
+            char buf[80], *e;
+            unsigned int r, g, b;
+            int j;
+
+            sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
+            for (j = 0; buf[j]; j++)
+                buf[j] = toupper((unsigned char)buf[j]);
+            if ((e = getenv(buf)) != NULL &&
+                sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
+                ret[i*3 + 0] = r / 255.0;
+                ret[i*3 + 1] = g / 255.0;
+                ret[i*3 + 2] = b / 255.0;
+            }
+        }
+    }
+
     if (me->nstates == 0)
         me->ourgame->free_game(state);
 
@@ -527,6 +553,53 @@ int midend_num_presets(midend_data *me)
         }
     }
 
+    {
+        /*
+         * Allow environment-based extensions to the preset list by
+         * defining a variable along the lines of `SOLO_PRESETS=2x3
+         * Advanced:2x3da'. Colon-separated list of items,
+         * alternating between textual titles in the menu and
+         * encoded parameter strings.
+         */
+        char buf[80], *e, *p;
+        int j;
+
+        sprintf(buf, "%s_PRESETS", me->ourgame->name);
+        for (j = 0; buf[j]; j++)
+            buf[j] = toupper((unsigned char)buf[j]);
+
+        if ((e = getenv(buf)) != NULL) {
+            p = e = dupstr(e);
+
+            while (*p) {
+                char *name, *val;
+                game_params *preset;
+
+                name = p;
+                while (*p && *p != ':') p++;
+                if (*p) *p++ = '\0';
+                val = p;
+                while (*p && *p != ':') p++;
+                if (*p) *p++ = '\0';
+
+                preset = me->ourgame->default_params();
+                me->ourgame->decode_params(preset, val);
+
+                if (me->presetsize <= me->npresets) {
+                    me->presetsize = me->npresets + 10;
+                    me->presets = sresize(me->presets, me->presetsize,
+                                          game_params *);
+                    me->preset_names = sresize(me->preset_names,
+                                               me->presetsize, char *);
+                }
+
+                me->presets[me->npresets] = preset;
+                me->preset_names[me->npresets] = name;
+                me->npresets++;
+            }
+        }
+    }
+
     return me->npresets;
 }