X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/5c9f61fd500f6fd53a9f33721a1e66b9d5e1d9cc..4c02061d05c4d4047bb5a6ea064a95ec341b4bbe:/midend.c diff --git a/midend.c b/midend.c index 7bc2d4f..b5efda6 100644 --- a/midend.c +++ b/midend.c @@ -78,7 +78,7 @@ struct midend_data { int pressed_mouse_button; - int winwidth, winheight; + int tilesize, winwidth, winheight; }; #define ensure(me) do { \ @@ -121,7 +121,7 @@ midend_data *midend_new(frontend *fe, const game *ourgame) me->laststatus = NULL; me->timing = FALSE; me->elapsed = 0.0F; - me->winwidth = me->winheight = 0; + me->tilesize = me->winwidth = me->winheight = 0; sfree(randseed); @@ -169,11 +169,63 @@ void midend_free(midend_data *me) sfree(me); } +static void midend_size_new_drawstate(midend_data *me) +{ + /* + * Don't even bother, if we haven't worked out our tile size + * anyway yet. + */ + if (me->tilesize > 0) { + me->ourgame->compute_size(me->params, me->tilesize, + &me->winwidth, &me->winheight); + me->ourgame->set_size(me->drawstate, me->params, me->tilesize); + } +} + void midend_size(midend_data *me, int *x, int *y, int expand) { - me->ourgame->size(me->params, me->drawstate, x, y, expand); - me->winwidth = *x; - me->winheight = *y; + int min, max; + int rx, ry; + + /* + * Find the tile size that best fits within the given space. If + * `expand' is TRUE, we must actually find the _largest_ such + * tile size; otherwise, we bound above at the game's preferred + * tile size. + */ + if (expand) { + max = 1; + do { + max *= 2; + me->ourgame->compute_size(me->params, max, &rx, &ry); + } while (rx <= *x && ry <= *y); + } else + max = me->ourgame->preferred_tilesize + 1; + min = 1; + + /* + * Now binary-search between min and max. We're looking for a + * boundary rather than a value: the point at which tile sizes + * stop fitting within the given dimensions. Thus, we stop when + * max and min differ by exactly 1. + */ + while (max - min > 1) { + int mid = (max + min) / 2; + me->ourgame->compute_size(me->params, mid, &rx, &ry); + if (rx <= *x && ry <= *y) + min = mid; + else + max = mid; + } + + /* + * Now `min' is a valid size, and `max' isn't. So use `min'. + */ + + me->tilesize = min; + midend_size_new_drawstate(me); + *x = me->winwidth; + *y = me->winheight; } void midend_set_params(midend_data *me, game_params *params) @@ -192,12 +244,6 @@ static void midend_set_timer(midend_data *me) deactivate_timer(me->frontend); } -static void midend_size_new_drawstate(midend_data *me) -{ - me->ourgame->size(me->params, me->drawstate, &me->winwidth, &me->winheight, - TRUE); -} - void midend_force_redraw(midend_data *me) { if (me->drawstate) @@ -387,37 +433,38 @@ static int midend_really_process_key(midend_data *me, int x, int y, int button) me->ourgame->dup_game(me->states[me->statepos - 1].state); int special = FALSE, gotspecial = FALSE, ret = 1; float anim_time; + game_state *s; + char *movestr; + + movestr = + me->ourgame->interpret_move(me->states[me->statepos-1].state, + me->ui, me->drawstate, x, y, button); - if (button == 'n' || button == 'N' || button == '\x0E') { - midend_stop_anim(me); - midend_new_game(me); - midend_redraw(me); - goto done; /* never animate */ - } else if (button == 'u' || button == 'u' || - button == '\x1A' || button == '\x1F') { - midend_stop_anim(me); - special = special(me->states[me->statepos-1].movetype); - gotspecial = TRUE; - if (!midend_undo(me)) + if (!movestr) { + if (button == 'n' || button == 'N' || button == '\x0E') { + midend_stop_anim(me); + midend_new_game(me); + midend_redraw(me); + goto done; /* never animate */ + } else if (button == 'u' || button == 'u' || + button == '\x1A' || button == '\x1F') { + midend_stop_anim(me); + special = special(me->states[me->statepos-1].movetype); + gotspecial = TRUE; + if (!midend_undo(me)) + goto done; + } else if (button == 'r' || button == 'R' || + button == '\x12' || button == '\x19') { + midend_stop_anim(me); + if (!midend_redo(me)) + goto done; + } else if (button == 'q' || button == 'Q' || button == '\x11') { + ret = 0; + goto done; + } else goto done; - } else if (button == 'r' || button == 'R' || - button == '\x12' || button == '\x19') { - midend_stop_anim(me); - if (!midend_redo(me)) - goto done; - } else if (button == 'q' || button == 'Q' || button == '\x11') { - ret = 0; - goto done; } else { - game_state *s; - char *movestr; - - movestr = - me->ourgame->interpret_move(me->states[me->statepos-1].state, - me->ui, me->drawstate, x, y, button); - if (!movestr) - s = NULL; - else if (!*movestr) + if (!*movestr) s = me->states[me->statepos-1].state; else { s = me->ourgame->execute_move(me->states[me->statepos-1].state,