X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/74a4e547b35f162350dcf205aa453fd32820d249..1d228b10b0f6bbc1fffb5442d2ad934a5e6aaaed:/midend.c diff --git a/midend.c b/midend.c index a2aee45..30ad32b 100644 --- a/midend.c +++ b/midend.c @@ -14,6 +14,7 @@ struct midend_data { frontend *frontend; random_state *random; + const game *ourgame; char *seed; int fresh_seed; @@ -30,6 +31,9 @@ struct midend_data { game_ui *ui; float anim_time, anim_pos; float flash_time, flash_pos; + int dir; + + int pressed_mouse_button; }; #define ensure(me) do { \ @@ -39,15 +43,20 @@ struct midend_data { } \ } while (0) -midend_data *midend_new(frontend *fe, void *randseed, int randseedsize) +midend_data *midend_new(frontend *fe, const game *ourgame) { midend_data *me = snew(midend_data); + void *randseed; + int randseedsize; + + get_random_seed(&randseed, &randseedsize); me->frontend = fe; + me->ourgame = ourgame; me->random = random_init(randseed, randseedsize); me->nstates = me->statesize = me->statepos = 0; me->states = NULL; - me->params = default_params(); + me->params = ourgame->default_params(); me->seed = NULL; me->fresh_seed = FALSE; me->drawstate = NULL; @@ -57,7 +66,11 @@ midend_data *midend_new(frontend *fe, void *randseed, int randseedsize) me->npresets = me->presetsize = 0; me->anim_time = me->anim_pos = 0.0F; me->flash_time = me->flash_pos = 0.0F; + me->dir = 0; me->ui = NULL; + me->pressed_mouse_button = 0; + + sfree(randseed); return me; } @@ -66,59 +79,61 @@ void midend_free(midend_data *me) { sfree(me->states); sfree(me->seed); - free_params(me->params); + me->ourgame->free_params(me->params); sfree(me); } void midend_size(midend_data *me, int *x, int *y) { - game_size(me->params, x, y); + me->ourgame->size(me->params, x, y); } void midend_set_params(midend_data *me, game_params *params) { - free_params(me->params); - me->params = dup_params(params); + me->ourgame->free_params(me->params); + me->params = me->ourgame->dup_params(params); } void midend_new_game(midend_data *me) { while (me->nstates > 0) - free_game(me->states[--me->nstates]); + me->ourgame->free_game(me->states[--me->nstates]); if (me->drawstate) - game_free_drawstate(me->drawstate); + me->ourgame->free_drawstate(me->drawstate); assert(me->nstates == 0); if (!me->fresh_seed) { sfree(me->seed); - me->seed = new_game_seed(me->params, me->random); + me->seed = me->ourgame->new_seed(me->params, me->random); } else me->fresh_seed = FALSE; ensure(me); - me->states[me->nstates++] = new_game(me->params, me->seed); + me->states[me->nstates++] = me->ourgame->new_game(me->params, me->seed); me->statepos = 1; - me->drawstate = game_new_drawstate(me->states[0]); + me->drawstate = me->ourgame->new_drawstate(me->states[0]); if (me->ui) - free_ui(me->ui); - me->ui = new_ui(me->states[0]); + me->ourgame->free_ui(me->ui); + me->ui = me->ourgame->new_ui(me->states[0]); + me->pressed_mouse_button = 0; } void midend_restart_game(midend_data *me) { while (me->nstates > 1) - free_game(me->states[--me->nstates]); + me->ourgame->free_game(me->states[--me->nstates]); me->statepos = me->nstates; - free_ui(me->ui); - me->ui = new_ui(me->states[0]); + me->ourgame->free_ui(me->ui); + me->ui = me->ourgame->new_ui(me->states[0]); } static int midend_undo(midend_data *me) { if (me->statepos > 1) { me->statepos--; + me->dir = -1; return 1; } else return 0; @@ -128,6 +143,7 @@ static int midend_redo(midend_data *me) { if (me->statepos < me->nstates) { me->statepos++; + me->dir = +1; return 1; } else return 0; @@ -138,9 +154,10 @@ static void midend_finish_move(midend_data *me) float flashtime; if (me->oldstate || me->statepos > 1) { - flashtime = game_flash_length(me->oldstate ? me->oldstate : - me->states[me->statepos-2], - me->states[me->statepos-1]); + flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate : + me->states[me->statepos-2], + me->states[me->statepos-1], + me->oldstate ? me->dir : +1); if (flashtime > 0) { me->flash_pos = 0.0F; me->flash_time = flashtime; @@ -148,9 +165,10 @@ static void midend_finish_move(midend_data *me) } if (me->oldstate) - free_game(me->oldstate); + me->ourgame->free_game(me->oldstate); me->oldstate = NULL; me->anim_pos = me->anim_time = 0; + me->dir = 0; if (me->flash_time == 0 && me->anim_time == 0) deactivate_timer(me->frontend); @@ -158,37 +176,44 @@ static void midend_finish_move(midend_data *me) activate_timer(me->frontend); } -int midend_process_key(midend_data *me, int x, int y, int button) +static void midend_stop_anim(midend_data *me) { - game_state *oldstate = dup_game(me->states[me->statepos - 1]); - float anim_time; - if (me->oldstate || me->anim_time) { midend_finish_move(me); midend_redraw(me); } +} + +static int midend_really_process_key(midend_data *me, int x, int y, int button) +{ + game_state *oldstate = me->ourgame->dup_game(me->states[me->statepos - 1]); + float anim_time; if (button == 'n' || button == 'N' || button == '\x0E') { + midend_stop_anim(me); 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); if (!midend_undo(me)) return 1; } else if (button == '\x12') { + midend_stop_anim(me); if (!midend_redo(me)) return 1; } else if (button == 'q' || button == 'Q' || button == '\x11') { - free_game(oldstate); + me->ourgame->free_game(oldstate); return 0; } else { - game_state *s = make_move(me->states[me->statepos-1], me->ui, - x, y, button); + game_state *s = me->ourgame->make_move(me->states[me->statepos-1], + me->ui, x, y, button); if (s == me->states[me->statepos-1]) { /* @@ -199,13 +224,15 @@ int midend_process_key(midend_data *me, int x, int y, int button) midend_redraw(me); return 1; } else if (s) { + midend_stop_anim(me); while (me->nstates > me->statepos) - free_game(me->states[--me->nstates]); + me->ourgame->free_game(me->states[--me->nstates]); ensure(me); me->states[me->nstates] = s; me->statepos = ++me->nstates; + me->dir = +1; } else { - free_game(oldstate); + me->ourgame->free_game(oldstate); return 1; } } @@ -213,7 +240,8 @@ int midend_process_key(midend_data *me, int x, int y, int button) /* * See if this move requires an animation. */ - anim_time = game_anim_length(oldstate, me->states[me->statepos-1]); + anim_time = me->ourgame->anim_length(oldstate, me->states[me->statepos-1], + me->dir); me->oldstate = oldstate; if (anim_time > 0) { @@ -231,19 +259,118 @@ int midend_process_key(midend_data *me, int x, int y, int button) return 1; } +int midend_process_key(midend_data *me, int x, int y, int button) +{ + int ret = 1; + + /* + * Harmonise mouse drag and release messages. + * + * Some front ends might accidentally switch from sending, say, + * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a + * drag. (This can happen on the Mac, for example, since + * RIGHT_DRAG is usually done using Command+drag, and if the + * user accidentally releases Command half way through the drag + * then there will be trouble.) + * + * It would be an O(number of front ends) annoyance to fix this + * in the front ends, but an O(number of back ends) annoyance + * to have each game capable of dealing with it. Therefore, we + * fix it _here_ in the common midend code so that it only has + * to be done once. + * + * The possible ways in which things can go screwy in the front + * end are: + * + * - in a system containing multiple physical buttons button + * presses can inadvertently overlap. We can see ABab (caps + * meaning button-down and lowercase meaning button-up) when + * the user had semantically intended AaBb. + * + * - in a system where one button is simulated by means of a + * modifier key and another button, buttons can mutate + * between press and release (possibly during drag). So we + * can see Ab instead of Aa. + * + * Definite requirements are: + * + * - button _presses_ must never be invented or destroyed. If + * the user presses two buttons in succession, the button + * presses must be transferred to the backend unchanged. So + * if we see AaBb , that's fine; if we see ABab (the button + * presses inadvertently overlapped) we must somehow + * `correct' it to AaBb. + * + * - every mouse action must end up looking like a press, zero + * or more drags, then a release. This allows back ends to + * make the _assumption_ that incoming mouse data will be + * sane in this regard, and not worry about the details. + * + * So my policy will be: + * + * - treat any button-up as a button-up for the currently + * pressed button, or ignore it if there is no currently + * pressed button. + * + * - treat any drag as a drag for the currently pressed + * button, or ignore it if there is no currently pressed + * button. + * + * - if we see a button-down while another button is currently + * pressed, invent a button-up for the first one and then + * pass the button-down through as before. + * + */ + if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) { + if (me->pressed_mouse_button) { + if (IS_MOUSE_DRAG(button)) { + button = me->pressed_mouse_button + + (LEFT_DRAG - LEFT_BUTTON); + } else { + button = me->pressed_mouse_button + + (LEFT_RELEASE - LEFT_BUTTON); + } + } else + return ret; /* ignore it */ + } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) { + /* + * Fabricate a button-up for the previously pressed button. + */ + ret = ret && midend_really_process_key + (me, x, y, (me->pressed_mouse_button + + (LEFT_RELEASE - LEFT_BUTTON))); + } + + /* + * Now send on the event we originally received. + */ + ret = ret && midend_really_process_key(me, x, y, button); + + /* + * And update the currently pressed button. + */ + if (IS_MOUSE_RELEASE(button)) + me->pressed_mouse_button = 0; + else if (IS_MOUSE_DOWN(button)) + me->pressed_mouse_button = button; + + return ret; +} + void midend_redraw(midend_data *me) { if (me->statepos > 0 && me->drawstate) { start_draw(me->frontend); if (me->oldstate && me->anim_time > 0 && me->anim_pos < me->anim_time) { - game_redraw(me->frontend, me->drawstate, me->oldstate, - me->states[me->statepos-1], me->ui, me->anim_pos, - me->flash_pos); + assert(me->dir != 0); + me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate, + me->states[me->statepos-1], me->dir, + me->ui, me->anim_pos, me->flash_pos); } else { - game_redraw(me->frontend, me->drawstate, NULL, - me->states[me->statepos-1], me->ui, 0.0, - me->flash_pos); + me->ourgame->redraw(me->frontend, me->drawstate, NULL, + me->states[me->statepos-1], +1 /*shrug*/, + me->ui, 0.0, me->flash_pos); } end_draw(me->frontend); } @@ -272,16 +399,16 @@ float *midend_colours(midend_data *me, int *ncolours) float *ret; if (me->nstates == 0) { - char *seed = new_game_seed(me->params, me->random); - state = new_game(me->params, seed); + char *seed = me->ourgame->new_seed(me->params, me->random); + state = me->ourgame->new_game(me->params, seed); sfree(seed); } else state = me->states[0]; - ret = game_colours(me->frontend, state, ncolours); + ret = me->ourgame->colours(me->frontend, state, ncolours); if (me->nstates == 0) - free_game(state); + me->ourgame->free_game(state); return ret; } @@ -292,7 +419,7 @@ int midend_num_presets(midend_data *me) char *name; game_params *preset; - while (game_fetch_preset(me->npresets, &name, &preset)) { + while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) { if (me->presetsize <= me->npresets) { me->presetsize = me->npresets + 10; me->presets = sresize(me->presets, me->presetsize, @@ -320,23 +447,23 @@ void midend_fetch_preset(midend_data *me, int n, int midend_wants_statusbar(midend_data *me) { - return game_wants_statusbar(); + return me->ourgame->wants_statusbar(); } config_item *midend_get_config(midend_data *me, int which, char **wintitle) { - char *titlebuf; + char *titlebuf, *parstr; config_item *ret; - titlebuf = snewn(40 + strlen(game_name), char); + titlebuf = snewn(40 + strlen(me->ourgame->name), char); switch (which) { case CFG_SETTINGS: - sprintf(titlebuf, "%s configuration", game_name); + sprintf(titlebuf, "%s configuration", me->ourgame->name); *wintitle = dupstr(titlebuf); - return game_configure(me->params); + return me->ourgame->configure(me->params); case CFG_SEED: - sprintf(titlebuf, "%s game selection", game_name); + sprintf(titlebuf, "%s game selection", me->ourgame->name); *wintitle = dupstr(titlebuf); ret = snewn(2, config_item); @@ -344,7 +471,15 @@ config_item *midend_get_config(midend_data *me, int which, char **wintitle) ret[0].type = C_STRING; ret[0].name = "Game ID"; ret[0].ival = 0; - ret[0].sval = dupstr(me->seed); + /* + * The text going in here will be a string encoding of the + * parameters, plus a colon, plus the game seed. This is a + * full game ID. + */ + parstr = me->ourgame->encode_params(me->params); + ret[0].sval = snewn(strlen(parstr) + strlen(me->seed) + 2, char); + sprintf(ret[0].sval, "%s:%s", parstr, me->seed); + sfree(parstr); ret[1].type = C_END; ret[1].name = ret[1].sval = NULL; @@ -357,6 +492,59 @@ config_item *midend_get_config(midend_data *me, int which, char **wintitle) return NULL; } +char *midend_game_id(midend_data *me, char *id, int def_seed) +{ + char *error, *par, *seed; + game_params *params; + + seed = strchr(id, ':'); + + if (seed) { + /* + * We have a colon separating parameters from game seed. So + * `par' now points to the parameters string, and `seed' to + * the seed string. + */ + *seed++ = '\0'; + par = id; + } else { + /* + * We only have one string. Depending on `def_seed', we + * take it to be either parameters or seed. + */ + if (def_seed) { + seed = id; + par = NULL; + } else { + seed = NULL; + par = id; + } + } + + if (par) { + params = me->ourgame->decode_params(par); + error = me->ourgame->validate_params(params); + if (error) { + me->ourgame->free_params(params); + return error; + } + me->ourgame->free_params(me->params); + me->params = params; + } + + if (seed) { + error = me->ourgame->validate_seed(me->params, seed); + if (error) + return error; + + sfree(me->seed); + me->seed = dupstr(seed); + me->fresh_seed = TRUE; + } + + return NULL; +} + char *midend_set_config(midend_data *me, int which, config_item *cfg) { char *error; @@ -364,27 +552,22 @@ char *midend_set_config(midend_data *me, int which, config_item *cfg) switch (which) { case CFG_SETTINGS: - params = custom_params(cfg); - error = validate_params(params); + params = me->ourgame->custom_params(cfg); + error = me->ourgame->validate_params(params); if (error) { - free_params(params); + me->ourgame->free_params(params); return error; } - free_params(me->params); + me->ourgame->free_params(me->params); me->params = params; break; case CFG_SEED: - error = validate_seed(me->params, cfg[0].sval); + error = midend_game_id(me, cfg[0].sval, TRUE); if (error) return error; - - sfree(me->seed); - me->seed = dupstr(cfg[0].sval); - me->fresh_seed = TRUE; - break; }