General robustness patch from James Harvey:

- 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]
This commit is contained in:
Simon Tatham
2005-06-30 09:07:00 +00:00
parent 101324af67
commit 118abb4fc9
9 changed files with 50 additions and 42 deletions

View File

@ -566,19 +566,19 @@ static game_state *execute_move(game_state *from, char *move)
static void game_size(game_params *params, game_drawstate *ds, static void game_size(game_params *params, game_drawstate *ds,
int *x, int *y, int expand) int *x, int *y, int expand)
{ {
int tsx, tsy, ts; double tsx, tsy, ts;
/* /*
* Each window dimension equals the tile size times one more * Each window dimension equals the tile size times one more
* than the grid dimension (the border is half the width of the * than the grid dimension (the border is half the width of the
* tiles). * tiles).
*/ */
tsx = *x / (params->w + 1); tsx = (double)*x / ((double)params->w + 1.0);
tsy = *y / (params->h + 1); tsy = (double)*y / ((double)params->h + 1.0);
ts = min(tsx, tsy); ts = min(tsx, tsy);
if (expand) if (expand)
ds->tilesize = ts; ds->tilesize = (int)(ts + 0.5);
else else
ds->tilesize = min(ts, PREFERRED_TILE_SIZE); ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
*x = TILE_SIZE * params->w + 2 * BORDER; *x = TILE_SIZE * params->w + 2 * BORDER;
*y = TILE_SIZE * params->h + 2 * BORDER; *y = TILE_SIZE * params->h + 2 * BORDER;

10
flip.c
View File

@ -957,19 +957,19 @@ static game_state *execute_move(game_state *from, char *move)
static void game_size(game_params *params, game_drawstate *ds, static void game_size(game_params *params, game_drawstate *ds,
int *x, int *y, int expand) int *x, int *y, int expand)
{ {
int tsx, tsy, ts; double tsx, tsy, ts;
/* /*
* Each window dimension equals the tile size times one more * Each window dimension equals the tile size times one more
* than the grid dimension (the border is half the width of the * than the grid dimension (the border is half the width of the
* tiles). * tiles).
*/ */
tsx = *x / (params->w + 1); tsx = (double)*x / ((double)params->w + 1.0);
tsy = *y / (params->h + 1); tsy = (double)*y / ((double)params->h + 1);
ts = min(tsx, tsy); ts = min(tsx, tsy);
if (expand) if (expand)
ds->tilesize = ts; ds->tilesize = (int)(ts + 0.5);
else else
ds->tilesize = min(ts, PREFERRED_TILE_SIZE); ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
*x = TILE_SIZE * params->w + 2 * BORDER; *x = TILE_SIZE * params->w + 2 * BORDER;
*y = TILE_SIZE * params->h + 2 * BORDER; *y = TILE_SIZE * params->h + 2 * BORDER;

View File

@ -149,6 +149,7 @@ void midend_free(midend_data *me)
random_free(me->random); random_free(me->random);
sfree(me->states); sfree(me->states);
sfree(me->desc); sfree(me->desc);
sfree(me->privdesc);
sfree(me->seedstr); sfree(me->seedstr);
sfree(me->aux_info); sfree(me->aux_info);
me->ourgame->free_params(me->params); me->ourgame->free_params(me->params);
@ -1351,7 +1352,7 @@ char *midend_deserialise(midend_data *me,
uistr = val; uistr = val;
val = NULL; val = NULL;
} else if (!strcmp(key, "TIME")) { } else if (!strcmp(key, "TIME")) {
elapsed = strtod(val, NULL); elapsed = atof(val);
} else if (!strcmp(key, "NSTATES")) { } else if (!strcmp(key, "NSTATES")) {
nstates = atoi(val); nstates = atoi(val);
if (nstates <= 0) { if (nstates <= 0) {
@ -1516,6 +1517,12 @@ char *midend_deserialise(midend_data *me,
midend_set_timer(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! */ ret = NULL; /* success! */
cleanup: cleanup:

10
mines.c
View File

@ -2607,19 +2607,19 @@ static game_state *execute_move(game_state *from, char *move)
static void game_size(game_params *params, game_drawstate *ds, static void game_size(game_params *params, game_drawstate *ds,
int *x, int *y, int expand) 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 * Each window dimension equals the tile size times 3 more than
* the grid dimension (the border is 3/2 the width of the * the grid dimension (the border is 3/2 the width of the
* tiles). * tiles).
*/ */
tsx = *x / (params->w + 3); tsx = (double)*x / ((double)params->w + 3.0);
tsy = *y / (params->h + 3); tsy = (double)*y / ((double)params->h + 3.0);
ts = min(tsx, tsy); ts = min(tsx, tsy);
if (expand) if (expand)
ds->tilesize = ts; ds->tilesize = (int)(ts + 0.5);
else else
ds->tilesize = min(ts, PREFERRED_TILE_SIZE); ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
*x = BORDER * 2 + TILE_SIZE * params->w; *x = BORDER * 2 + TILE_SIZE * params->w;
*y = BORDER * 2 + TILE_SIZE * params->h; *y = BORDER * 2 + TILE_SIZE * params->h;

View File

@ -30,7 +30,7 @@ enum {
( ((x) - (BORDER + GUTTER + TILE_SIZE * TLBORDER(d))) / TILE_SIZE ) ( ((x) - (BORDER + GUTTER + TILE_SIZE * TLBORDER(d))) / TILE_SIZE )
#define SIZE(d) (2*BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (d))) #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))) #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] == '/'); assert(desc[desclen-1] == '/');
desc[desclen-1] = '\0'; desc[desclen-1] = '\0';
sfree(rowdata); sfree(rowdata);
sfree(grid);
return desc; return desc;
} }
@ -858,8 +859,8 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
if (move_needed) { if (move_needed) {
char buf[80]; char buf[80];
sprintf(buf, "%c%d,%d,%d,%d", sprintf(buf, "%c%d,%d,%d,%d",
(ui->state == GRID_FULL ? 'F' : (char)(ui->state == GRID_FULL ? 'F' :
ui->state == GRID_EMPTY ? 'E' : 'U'), ui->state == GRID_EMPTY ? 'E' : 'U'),
x1, y1, x2-x1+1, y2-y1+1); x1, y1, x2-x1+1, y2-y1+1);
return dupstr(buf); return dupstr(buf);
} else } 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, static void game_size(game_params *params, game_drawstate *ds,
int *x, int *y, int expand) int *x, int *y, int expand)
{ {
int ts; double ts;
ts = min(GETTILESIZE(params->w, *x), GETTILESIZE(params->h, *y)); ts = min(GETTILESIZE(params->w, *x), GETTILESIZE(params->h, *y));
if (expand) if (expand)
ds->tilesize = ts; ds->tilesize = (int)(ts + 0.5);
else else
ds->tilesize = min(ts, PREFERRED_TILE_SIZE); ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
*x = SIZE(params->w); *x = SIZE(params->w);
*y = SIZE(params->h); *y = SIZE(params->h);

10
rect.c
View File

@ -2498,7 +2498,7 @@ static game_state *execute_move(game_state *from, char *move)
static void game_size(game_params *params, game_drawstate *ds, static void game_size(game_params *params, game_drawstate *ds,
int *x, int *y, int expand) 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 * Each window dimension equals the tile size times 1.5 more
* than the grid dimension (the border is 3/4 the width of the * 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 * We must cast to unsigned before multiplying by two, because
* *x might be INT_MAX. * *x might be INT_MAX.
*/ */
tsx = 2 * (unsigned)*x / (2 * params->w + 3); tsx = 2.0 * (double)*x / (2.0 * (double)params->w + 3.0);
tsy = 2 * (unsigned)*y / (2 * params->h + 3); tsy = 2.0 * (double)*y / (2.0 * (double)params->h + 3.0);
ts = min(tsx, tsy); ts = min(tsx, tsy);
if (expand) if (expand)
ds->tilesize = ts; ds->tilesize = (int)(ts + 0.5);
else else
ds->tilesize = min(ts, PREFERRED_TILE_SIZE); ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
*x = params->w * TILE_SIZE + 2*BORDER + 1; *x = params->w * TILE_SIZE + 2*BORDER + 1;
*y = params->h * TILE_SIZE + 2*BORDER + 1; *y = params->h * TILE_SIZE + 2*BORDER + 1;

View 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, static void game_size(game_params *params, game_drawstate *ds, int *x, int *y,
int expand) int expand)
{ {
int tsx, tsy, ts; double tsx, tsy, ts;
/* /*
* We could choose the tile gap dynamically as well if we * 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 * We must cast to unsigned before adding to *x and *y, since
* they might be INT_MAX! * they might be INT_MAX!
*/ */
tsx = (unsigned)(*x + ds->tilegap) / (params->w + 1); tsx = ((double)*x + (double)ds->tilegap) / ((double)params->w + 1.0);
tsy = (unsigned)(*y + ds->tilegap) / (params->h + 1); tsy = ((double)*y + (double)ds->tilegap) / ((double)params->h + 1.0);
ts = min(tsx, tsy); ts = min(tsx, tsy);
if (expand) if (expand)
ds->tileinner = ts - ds->tilegap; ds->tileinner = (int)(ts+0.5) - ds->tilegap;
else 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; *x = TILE_SIZE * params->w + 2 * BORDER - TILE_GAP;
*y = TILE_SIZE * params->h + 2 * BORDER - TILE_GAP; *y = TILE_SIZE * params->h + 2 * BORDER - TILE_GAP;

10
solo.c
View File

@ -2024,7 +2024,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
return NULL; return NULL;
sprintf(buf, "%c%d,%d,%d", 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; 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 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, static void game_size(game_params *params, game_drawstate *ds,
int *x, int *y, int expand) int *x, int *y, int expand)
{ {
int c = params->c, r = params->r, cr = c*r; int c = params->c, r = params->r, cr = c*r;
int ts; double ts;
ts = min(GETTILESIZE(cr, *x), GETTILESIZE(cr, *y)); ts = min(GETTILESIZE(cr, *x), GETTILESIZE(cr, *y));
if (expand) if (expand)
ds->tilesize = ts; ds->tilesize = (int)(ts+0.5);
else else
ds->tilesize = min(ts, PREFERRED_TILE_SIZE); ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
*x = SIZE(cr); *x = SIZE(cr);
*y = SIZE(cr); *y = SIZE(cr);

View File

@ -747,19 +747,19 @@ static game_state *execute_move(game_state *from, char *move)
static void game_size(game_params *params, game_drawstate *ds, static void game_size(game_params *params, game_drawstate *ds,
int *x, int *y, int expand) int *x, int *y, int expand)
{ {
int tsx, tsy, ts; double tsx, tsy, ts;
/* /*
* Each window dimension equals the tile size times one more * Each window dimension equals the tile size times one more
* than the grid dimension (the border is half the width of the * than the grid dimension (the border is half the width of the
* tiles). * tiles).
*/ */
tsx = *x / (params->w + 1); tsx = (double)*x / ((double)params->w + 1.0);
tsy = *y / (params->h + 1); tsy = (double)*y / ((double)params->h + 1.0);
ts = min(tsx, tsy); ts = min(tsx, tsy);
if (expand) if (expand)
ds->tilesize = ts; ds->tilesize = (int)(ts + 0.5);
else else
ds->tilesize = min(ts, PREFERRED_TILE_SIZE); ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
*x = TILE_SIZE * params->w + 2 * BORDER; *x = TILE_SIZE * params->w + 2 * BORDER;
*y = TILE_SIZE * params->h + 2 * BORDER; *y = TILE_SIZE * params->h + 2 * BORDER;