After discussion with Simon, the game redraw functions are now passed a new
authorjacob <jacob@cda61777-01e9-0310-a592-d414129be87e>
Mon, 16 Aug 2004 16:29:54 +0000 (16:29 +0000)
committerjacob <jacob@cda61777-01e9-0310-a592-d414129be87e>
Mon, 16 Aug 2004 16:29:54 +0000 (16:29 +0000)
argument `dir' which tells them whether this redraw is due to an undo, rather
than have them second-guess it from game state.
Note that none of the actual games yet take advantage of this; so it hasn't
been tested in anger (although it has been inspected by debugging).

git-svn-id: svn://svn.tartarus.org/sgt/puzzles@4469 cda61777-01e9-0310-a592-d414129be87e

HACKING.but
cube.c
fifteen.c
midend.c
net.c
netslide.c
nullgame.c
puzzles.h
rect.c
sixteen.c

index f6c93f0..e1abf75 100644 (file)
@@ -166,7 +166,8 @@ chronological order, the second one contains the direction field
 which corresponds to the actual difference between the states.
 However, when it is passed a pair of states in the opposite order
 due to an undo, it should be looking in the \e{first} one to find
-the direction field. Sixteen solves this by also storing the current
-move count in the game state, so that \cw{game_redraw()} can compare
-the two move counts to work out whether it's drawing an undo or not,
-and look in the right place for the direction field.
+the direction field.
+
+For this reason, in the redraw functions you are provided with an
+extra argument \c{dir} which tells you which state was chronologically
+first; \c{dir} is +1 for a normal move and -1 for an undo.
diff --git a/cube.c b/cube.c
index 72ac527..45b38bc 100644 (file)
--- a/cube.c
+++ b/cube.c
@@ -1351,7 +1351,7 @@ void game_free_drawstate(game_drawstate *ds)
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, game_ui *ui,
+                 game_state *state, int dir, game_ui *ui,
                  float animtime, float flashtime)
 {
     int i, j;
@@ -1510,12 +1510,12 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     }
 }
 
-float game_anim_length(game_state *oldstate, game_state *newstate)
+float game_anim_length(game_state *oldstate, game_state *newstate, int dir)
 {
     return ROLLTIME;
 }
 
-float game_flash_length(game_state *oldstate, game_state *newstate)
+float game_flash_length(game_state *oldstate, game_state *newstate, int dir)
 {
     return 0.0F;
 }
index 1476513..f6ff24a 100644 (file)
--- a/fifteen.c
+++ b/fifteen.c
@@ -553,7 +553,7 @@ static void draw_tile(frontend *fe, game_state *state, int x, int y,
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, game_ui *ui,
+                 game_state *state, int dir, game_ui *ui,
                  float animtime, float flashtime)
 {
     int i, pass, bgcolour;
@@ -702,12 +702,12 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     }
 }
 
-float game_anim_length(game_state *oldstate, game_state *newstate)
+float game_anim_length(game_state *oldstate, game_state *newstate, int dir)
 {
     return ANIM_TIME;
 }
 
-float game_flash_length(game_state *oldstate, game_state *newstate)
+float game_flash_length(game_state *oldstate, game_state *newstate, int dir)
 {
     if (!oldstate->completed && newstate->completed)
         return 2 * FLASH_FRAME;
index 9a6a403..a9cd768 100644 (file)
--- a/midend.c
+++ b/midend.c
@@ -30,6 +30,7 @@ struct midend_data {
     game_ui *ui;
     float anim_time, anim_pos;
     float flash_time, flash_pos;
+    int dir;
 };
 
 #define ensure(me) do { \
@@ -57,6 +58,7 @@ 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;
 
     return me;
@@ -119,6 +121,7 @@ static int midend_undo(midend_data *me)
 {
     if (me->statepos > 1) {
        me->statepos--;
+        me->dir = -1;
         return 1;
     } else
         return 0;
@@ -128,6 +131,7 @@ static int midend_redo(midend_data *me)
 {
     if (me->statepos < me->nstates) {
        me->statepos++;
+        me->dir = +1;
         return 1;
     } else
         return 0;
@@ -140,7 +144,8 @@ static void midend_finish_move(midend_data *me)
     if (me->oldstate || me->statepos > 1) {
        flashtime = game_flash_length(me->oldstate ? me->oldstate :
                                      me->states[me->statepos-2],
-                                     me->states[me->statepos-1]);
+                                     me->states[me->statepos-1],
+                                      me->oldstate ? me->dir : +1);
        if (flashtime > 0) {
            me->flash_pos = 0.0F;
            me->flash_time = flashtime;
@@ -151,6 +156,7 @@ static void midend_finish_move(midend_data *me)
        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);
@@ -212,6 +218,7 @@ int midend_process_key(midend_data *me, int x, int y, int button)
             ensure(me);
             me->states[me->nstates] = s;
             me->statepos = ++me->nstates;
+            me->dir = +1;
         } else {
             free_game(oldstate);
             return 1;
@@ -221,7 +228,7 @@ 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 = game_anim_length(oldstate, me->states[me->statepos-1], me->dir);
 
     me->oldstate = oldstate;
     if (anim_time > 0) {
@@ -245,13 +252,14 @@ void midend_redraw(midend_data *me)
         start_draw(me->frontend);
         if (me->oldstate && me->anim_time > 0 &&
             me->anim_pos < me->anim_time) {
+            assert(me->dir != 0);
             game_redraw(me->frontend, me->drawstate, me->oldstate,
-                        me->states[me->statepos-1], me->ui, me->anim_pos,
-                       me->flash_pos);
+                        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->states[me->statepos-1], +1 /*shrug*/,
+                        me->ui, 0.0, me->flash_pos);
         }
         end_draw(me->frontend);
     }
diff --git a/net.c b/net.c
index 8512a6a..4e752ed 100644 (file)
--- a/net.c
+++ b/net.c
@@ -1251,7 +1251,7 @@ static void draw_tile(frontend *fe, game_state *state, int x, int y, int tile,
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, game_ui *ui, float t, float ft)
+                 game_state *state, int dir, game_ui *ui, float t, float ft)
 {
     int x, y, tx, ty, frame;
     unsigned char *active;
@@ -1404,7 +1404,7 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     sfree(active);
 }
 
-float game_anim_length(game_state *oldstate, game_state *newstate)
+float game_anim_length(game_state *oldstate, game_state *newstate, int dir)
 {
     int x, y;
 
@@ -1421,7 +1421,7 @@ float game_anim_length(game_state *oldstate, game_state *newstate)
     return 0.0F;
 }
 
-float game_flash_length(game_state *oldstate, game_state *newstate)
+float game_flash_length(game_state *oldstate, game_state *newstate, int dir)
 {
     /*
      * If the game has just been completed, we display a completion
index c4f8ae6..ec40852 100644 (file)
@@ -1287,7 +1287,7 @@ static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy)
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, game_ui *ui, float t, float ft)
+                 game_state *state, int dir, game_ui *ui, float t, float ft)
 {
     int x, y, tx, ty, frame;
     unsigned char *active;
@@ -1481,12 +1481,12 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     sfree(active);
 }
 
-float game_anim_length(game_state *oldstate, game_state *newstate)
+float game_anim_length(game_state *oldstate, game_state *newstate, int dir)
 {
     return ANIM_TIME;
 }
 
-float game_flash_length(game_state *oldstate, game_state *newstate)
+float game_flash_length(game_state *oldstate, game_state *newstate, int dir)
 {
     /*
      * If the game has just been completed, we display a completion
index a318131..e704de9 100644 (file)
@@ -177,7 +177,7 @@ void game_free_drawstate(game_drawstate *ds)
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, game_ui *ui,
+                 game_state *state, int dir, game_ui *ui,
                  float animtime, float flashtime)
 {
     /*
@@ -189,12 +189,12 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     draw_rect(fe, 0, 0, 200, 200, COL_BACKGROUND);
 }
 
-float game_anim_length(game_state *oldstate, game_state *newstate)
+float game_anim_length(game_state *oldstate, game_state *newstate, int dir)
 {
     return 0.0F;
 }
 
-float game_flash_length(game_state *oldstate, game_state *newstate)
+float game_flash_length(game_state *oldstate, game_state *newstate, int dir)
 {
     return 0.0F;
 }
index b04ec97..b0947fc 100644 (file)
--- a/puzzles.h
+++ b/puzzles.h
@@ -184,10 +184,10 @@ float *game_colours(frontend *fe, game_state *state, int *ncolours);
 game_drawstate *game_new_drawstate(game_state *state);
 void game_free_drawstate(game_drawstate *ds);
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *newstate, game_ui *ui, float anim_time,
+                 game_state *newstate, int dir, game_ui *ui, float anim_time,
                  float flash_time);
-float game_anim_length(game_state *oldstate, game_state *newstate);
-float game_flash_length(game_state *oldstate, game_state *newstate);
+float game_anim_length(game_state *oldstate, game_state *newstate, int dir);
+float game_flash_length(game_state *oldstate, game_state *newstate, int dir);
 int game_wants_statusbar(void);
 
 #endif /* PUZZLES_PUZZLES_H */
diff --git a/rect.c b/rect.c
index 4e5d92a..b9d7cda 100644 (file)
--- a/rect.c
+++ b/rect.c
@@ -1482,7 +1482,7 @@ void draw_tile(frontend *fe, game_state *state, int x, int y,
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, game_ui *ui,
+                 game_state *state, int dir, game_ui *ui,
                  float animtime, float flashtime)
 {
     int x, y;
@@ -1573,12 +1573,12 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     sfree(correct);
 }
 
-float game_anim_length(game_state *oldstate, game_state *newstate)
+float game_anim_length(game_state *oldstate, game_state *newstate, int dir)
 {
     return 0.0F;
 }
 
-float game_flash_length(game_state *oldstate, game_state *newstate)
+float game_flash_length(game_state *oldstate, game_state *newstate, int dir)
 {
     if (!oldstate->completed && newstate->completed)
         return FLASH_TIME;
index 1e865c2..1e1f8b8 100644 (file)
--- a/sixteen.c
+++ b/sixteen.c
@@ -575,7 +575,7 @@ static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy)
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, game_ui *ui,
+                 game_state *state, int dir, game_ui *ui,
                  float animtime, float flashtime)
 {
     int i, bgcolour;
@@ -750,12 +750,12 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     }
 }
 
-float game_anim_length(game_state *oldstate, game_state *newstate)
+float game_anim_length(game_state *oldstate, game_state *newstate, int dir)
 {
     return ANIM_TIME;
 }
 
-float game_flash_length(game_state *oldstate, game_state *newstate)
+float game_flash_length(game_state *oldstate, game_state *newstate, int dir)
 {
     if (!oldstate->completed && newstate->completed)
         return 2 * FLASH_FRAME;