chiark / gitweb /
General robustness patch from James Harvey:
authorSimon Tatham <anakin@pobox.com>
Thu, 30 Jun 2005 09:07:00 +0000 (09:07 +0000)
committerSimon Tatham <anakin@pobox.com>
Thu, 30 Jun 2005 09:07:00 +0000 (09:07 +0000)
 - most game_size() functions now work in doubles internally and
   round to nearest, meaning that they have less tendency to try to
   alter a size they returned happily from a previous call
 - couple of fiddly fixes (memory leaks, precautionary casts in
   printf argument lists)
 - midend_deserialise() now constructs an appropriate drawstate,
   which I can't think how I overlooked myself since I _thought_ I
   went through the entire midend structure field by field!

[originally from svn r6041]

fifteen.c
flip.c
midend.c
mines.c
pattern.c
rect.c
samegame.c
solo.c
twiddle.c

index 5da25486eda3659682c73a3618b06384ac284aa9..bd791eae7b6d474f1fba39a745176571d2e99f4f 100644 (file)
--- a/fifteen.c
+++ b/fifteen.c
@@ -566,19 +566,19 @@ static game_state *execute_move(game_state *from, char *move)
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
     /*
      * Each window dimension equals the tile size times one more
      * than the grid dimension (the border is half the width of the
      * tiles).
      */
-    tsx = *x / (params->w + 1);
-    tsy = *y / (params->h + 1);
+    tsx = (double)*x / ((double)params->w + 1.0);
+    tsy = (double)*y / ((double)params->h + 1.0);
     ts = min(tsx, tsy);
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = TILE_SIZE * params->w + 2 * BORDER;
     *y = TILE_SIZE * params->h + 2 * BORDER;
diff --git a/flip.c b/flip.c
index 4f78ec7157882e3891e918b2032747d60cb04233..9ad3eb234c169b6f3fb31767b067be1fca1523fc 100644 (file)
--- a/flip.c
+++ b/flip.c
@@ -957,19 +957,19 @@ static game_state *execute_move(game_state *from, char *move)
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
     /*
      * Each window dimension equals the tile size times one more
      * than the grid dimension (the border is half the width of the
      * tiles).
      */
-    tsx = *x / (params->w + 1);
-    tsy = *y / (params->h + 1);
+    tsx = (double)*x / ((double)params->w + 1.0);
+    tsy = (double)*y / ((double)params->h + 1);
     ts = min(tsx, tsy);
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = TILE_SIZE * params->w + 2 * BORDER;
     *y = TILE_SIZE * params->h + 2 * BORDER;
index 15d11bbda860eb8945a46292090dce7d0dc76a4a..8928fded10d35a52e8c064cee88f04e9dfe87aa5 100644 (file)
--- a/midend.c
+++ b/midend.c
@@ -149,6 +149,7 @@ void midend_free(midend_data *me)
     random_free(me->random);
     sfree(me->states);
     sfree(me->desc);
+    sfree(me->privdesc);
     sfree(me->seedstr);
     sfree(me->aux_info);
     me->ourgame->free_params(me->params);
@@ -1351,7 +1352,7 @@ char *midend_deserialise(midend_data *me,
                 uistr = val;
                 val = NULL;
             } else if (!strcmp(key, "TIME")) {
-                elapsed = strtod(val, NULL);
+                elapsed = atof(val);
             } else if (!strcmp(key, "NSTATES")) {
                 nstates = atoi(val);
                 if (nstates <= 0) {
@@ -1516,6 +1517,12 @@ char *midend_deserialise(midend_data *me,
 
     midend_set_timer(me);
 
+    if (me->drawstate)
+        me->ourgame->free_drawstate(me->drawstate);
+    me->drawstate =
+        me->ourgame->new_drawstate(me->states[me->statepos-1].state);
+    midend_size_new_drawstate(me);
+
     ret = NULL;                        /* success! */
 
     cleanup:
diff --git a/mines.c b/mines.c
index d2ec3958a52122127bb76eff2c0a1aa0cd99b1fa..6cfb634bd5dbc83b02c7552a0678748a56f833bf 100644 (file)
--- a/mines.c
+++ b/mines.c
@@ -2607,19 +2607,19 @@ static game_state *execute_move(game_state *from, char *move)
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
     /*
      * Each window dimension equals the tile size times 3 more than
      * the grid dimension (the border is 3/2 the width of the
      * tiles).
      */
-    tsx = *x / (params->w + 3);
-    tsy = *y / (params->h + 3);
+    tsx = (double)*x / ((double)params->w + 3.0);
+    tsy = (double)*y / ((double)params->h + 3.0);
     ts = min(tsx, tsy);
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = BORDER * 2 + TILE_SIZE * params->w;
     *y = BORDER * 2 + TILE_SIZE * params->h;
index 19d16ff102a7f39ed998361f9c849aab3dc40fb5..b7c5334fc36028aa985570dc67545ff0f08d2c11 100644 (file)
--- a/pattern.c
+++ b/pattern.c
@@ -30,7 +30,7 @@ enum {
         ( ((x) - (BORDER + GUTTER + TILE_SIZE * TLBORDER(d))) / TILE_SIZE )
 
 #define SIZE(d) (2*BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (d)))
-#define GETTILESIZE(d, w) (w / (2 + TLBORDER(d) + (d)))
+#define GETTILESIZE(d, w) ((double)w / (2.0 + (double)TLBORDER(d) + (double)(d)))
 
 #define TOCOORD(d, x) (BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (x)))
 
@@ -549,6 +549,7 @@ static char *new_game_desc(game_params *params, random_state *rs,
     assert(desc[desclen-1] == '/');
     desc[desclen-1] = '\0';
     sfree(rowdata);
+    sfree(grid);
     return desc;
 }
 
@@ -858,8 +859,8 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
         if (move_needed) {
            char buf[80];
            sprintf(buf, "%c%d,%d,%d,%d",
-                   (ui->state == GRID_FULL ? 'F' :
-                    ui->state == GRID_EMPTY ? 'E' : 'U'),
+                   (char)(ui->state == GRID_FULL ? 'F' :
+                          ui->state == GRID_EMPTY ? 'E' : 'U'),
                    x1, y1, x2-x1+1, y2-y1+1);
            return dupstr(buf);
         } else
@@ -947,13 +948,13 @@ static game_state *execute_move(game_state *from, char *move)
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int ts;
+    double ts;
 
     ts = min(GETTILESIZE(params->w, *x), GETTILESIZE(params->h, *y));
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = SIZE(params->w);
     *y = SIZE(params->h);
diff --git a/rect.c b/rect.c
index 5852f59b37f594e417d668bf696ab22069f53fc0..f092a2d4d4c3a35d83eca47deda22ecaa93bc441 100644 (file)
--- a/rect.c
+++ b/rect.c
@@ -2498,7 +2498,7 @@ static game_state *execute_move(game_state *from, char *move)
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
     /*
      * Each window dimension equals the tile size times 1.5 more
      * than the grid dimension (the border is 3/4 the width of the
@@ -2507,13 +2507,13 @@ static void game_size(game_params *params, game_drawstate *ds,
      * We must cast to unsigned before multiplying by two, because
      * *x might be INT_MAX.
      */
-    tsx = 2 * (unsigned)*x / (2 * params->w + 3);
-    tsy = 2 * (unsigned)*y / (2 * params->h + 3);
+    tsx = 2.0 * (double)*x / (2.0 * (double)params->w + 3.0);
+    tsy = 2.0 * (double)*y / (2.0 * (double)params->h + 3.0);
     ts = min(tsx, tsy);
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = params->w * TILE_SIZE + 2*BORDER + 1;
     *y = params->h * TILE_SIZE + 2*BORDER + 1;
index 18110f597183b427a6f4260b7ccc830b7e79931e..ed516e2cbd173236895f0aab95294fa126b15a1b 100644 (file)
@@ -689,7 +689,7 @@ static game_state *execute_move(game_state *from, char *move)
 static void game_size(game_params *params, game_drawstate *ds, int *x, int *y,
                       int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
 
     /*
      * We could choose the tile gap dynamically as well if we
@@ -708,14 +708,14 @@ static void game_size(game_params *params, game_drawstate *ds, int *x, int *y,
      * We must cast to unsigned before adding to *x and *y, since
      * they might be INT_MAX!
      */
-    tsx = (unsigned)(*x + ds->tilegap) / (params->w + 1);
-    tsy = (unsigned)(*y + ds->tilegap) / (params->h + 1);
+    tsx = ((double)*x + (double)ds->tilegap) / ((double)params->w + 1.0);
+    tsy = ((double)*y + (double)ds->tilegap) / ((double)params->h + 1.0);
 
     ts = min(tsx, tsy);
     if (expand)
-        ds->tileinner = ts - ds->tilegap;
+        ds->tileinner = (int)(ts+0.5) - ds->tilegap;
     else
-        ds->tileinner = min(ts, PREFERRED_TILE_SIZE) - ds->tilegap;
+        ds->tileinner = min((int)ts, PREFERRED_TILE_SIZE) - ds->tilegap;
 
     *x = TILE_SIZE * params->w + 2 * BORDER - TILE_GAP;
     *y = TILE_SIZE * params->h + 2 * BORDER - TILE_GAP;
diff --git a/solo.c b/solo.c
index bbe0c31822476dbf657cbd933f36ab85a1d5f426..b9510e23a1a94d6152fb85fb3ae1f711bfcb6fed 100644 (file)
--- a/solo.c
+++ b/solo.c
@@ -2024,7 +2024,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
             return NULL;
 
        sprintf(buf, "%c%d,%d,%d",
-               ui->hpencil && n > 0 ? 'P' : 'R', ui->hx, ui->hy, n);
+               (char)(ui->hpencil && n > 0 ? 'P' : 'R'), ui->hx, ui->hy, n);
 
        ui->hx = ui->hy = -1;
 
@@ -2090,19 +2090,19 @@ static game_state *execute_move(game_state *from, char *move)
  */
 
 #define SIZE(cr) ((cr) * TILE_SIZE + 2*BORDER + 1)
-#define GETTILESIZE(cr, w) ( (w-1) / (cr+1) )
+#define GETTILESIZE(cr, w) ( (double)(w-1) / (double)(cr+1) )
 
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
     int c = params->c, r = params->r, cr = c*r;
-    int ts;
+    double ts;
 
     ts = min(GETTILESIZE(cr, *x), GETTILESIZE(cr, *y));
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts+0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = SIZE(cr);
     *y = SIZE(cr);
index 7e2b8165c533d62637ffccbd3c535d36881eb255..fc124113d5b7b99216c91a4affb0d5ab33a9879b 100644 (file)
--- a/twiddle.c
+++ b/twiddle.c
@@ -747,19 +747,19 @@ static game_state *execute_move(game_state *from, char *move)
 static void game_size(game_params *params, game_drawstate *ds,
                       int *x, int *y, int expand)
 {
-    int tsx, tsy, ts;
+    double tsx, tsy, ts;
     /*
      * Each window dimension equals the tile size times one more
      * than the grid dimension (the border is half the width of the
      * tiles).
      */
-    tsx = *x / (params->w + 1);
-    tsy = *y / (params->h + 1);
+    tsx = (double)*x / ((double)params->w + 1.0);
+    tsy = (double)*y / ((double)params->h + 1.0);
     ts = min(tsx, tsy);
     if (expand)
-        ds->tilesize = ts;
+        ds->tilesize = (int)(ts + 0.5);
     else
-        ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
+        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
 
     *x = TILE_SIZE * params->w + 2 * BORDER;
     *y = TILE_SIZE * params->h + 2 * BORDER;