ref: 90560462c4842dccc0288eb408f15539acc0ab83
parent: 7ddaa1382f064466f158315198788daf33b05052
author: Simon Tatham <anakin@pobox.com>
date: Mon May 30 12:15:34 EDT 2005
First cut at a game timer. Yet another backend function which indicates whether a particular game state should have the timer going (for Mines the initial indeterminate state does not have this property, and neither does a dead or won state); a midend function that optionally (on request from the game) prepends a timer to the front of the status bar text; some complicated midend timing code. It's not great. It's ugly; it's probably slightly inaccurate; it's got no provision for anyone but the game author decreeing whether a game is timed or not. But Mines can't be taken seriously without a timer, so it's a start. [originally from svn r5866]
--- a/cube.c
+++ b/cube.c
@@ -1542,6 +1542,11 @@
return TRUE;
}
+static int game_timing_state(game_state *state)
+{
+ return TRUE;
+}
+
#ifdef COMBINED
#define thegame cube
#endif
@@ -1575,4 +1580,5 @@
game_anim_length,
game_flash_length,
game_wants_statusbar,
+ FALSE, game_timing_state,
};
--- a/fifteen.c
+++ b/fifteen.c
@@ -802,6 +802,11 @@
return TRUE;
}
+static int game_timing_state(game_state *state)
+{
+ return TRUE;
+}
+
#ifdef COMBINED
#define thegame fifteen
#endif
@@ -835,4 +840,5 @@
game_anim_length,
game_flash_length,
game_wants_statusbar,
+ FALSE, game_timing_state,
};
--- a/gtk.c
+++ b/gtk.c
@@ -98,10 +98,14 @@
void status_bar(frontend *fe, char *text)
{
+ char *rewritten;
+
assert(fe->statusbar);
+ rewritten = midend_rewrite_statusbar(fe->me, text);
gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx);
- gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text);
+ gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, rewritten);
+ sfree(rewritten);
}
void start_draw(frontend *fe)
--- a/midend.c
+++ b/midend.c
@@ -43,6 +43,10 @@
float flash_time, flash_pos;
int dir;
+ int timing;
+ float elapsed;
+ char *laststatus;
+
int pressed_mouse_button;
};
@@ -83,6 +87,9 @@
me->dir = 0;
me->ui = NULL;
me->pressed_mouse_button = 0;
+ me->laststatus = NULL;
+ me->timing = FALSE;
+ me->elapsed = 0.0F;
sfree(randseed);
@@ -100,6 +107,7 @@
me->ourgame->free_params(me->params);
if (me->curparams)
me->ourgame->free_params(me->curparams);
+ sfree(me->laststatus);
sfree(me);
}
@@ -114,6 +122,16 @@
me->params = me->ourgame->dup_params(params);
}
+static void midend_set_timer(midend_data *me)
+{
+ me->timing = (me->ourgame->is_timed &&
+ me->ourgame->timing_state(me->states[me->statepos-1].state));
+ if (me->timing || me->flash_time || me->anim_time)
+ activate_timer(me->frontend);
+ else
+ deactivate_timer(me->frontend);
+}
+
void midend_new_game(midend_data *me)
{
while (me->nstates > 0)
@@ -171,6 +189,8 @@
me->nstates++;
me->statepos = 1;
me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
+ me->elapsed = 0.0F;
+ midend_set_timer(me);
if (me->ui)
me->ourgame->free_ui(me->ui);
me->ui = me->ourgame->new_ui(me->states[0].state);
@@ -227,10 +247,7 @@
me->anim_pos = me->anim_time = 0;
me->dir = 0;
- if (me->flash_time == 0 && me->anim_time == 0)
- deactivate_timer(me->frontend);
- else
- activate_timer(me->frontend);
+ midend_set_timer(me);
}
static void midend_stop_anim(midend_data *me)
@@ -266,7 +283,7 @@
me->anim_time = 0.0;
midend_finish_move(me);
midend_redraw(me);
- activate_timer(me->frontend);
+ midend_set_timer(me);
}
static int midend_really_process_key(midend_data *me, int x, int y, int button)
@@ -349,7 +366,7 @@
midend_redraw(me);
- activate_timer(me->frontend);
+ midend_set_timer(me);
return 1;
}
@@ -479,13 +496,22 @@
if (me->anim_time > 0)
midend_finish_move(me);
}
+
me->flash_pos += tplus;
if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
me->flash_pos = me->flash_time = 0;
}
- if (me->flash_time == 0 && me->anim_time == 0)
- deactivate_timer(me->frontend);
+
midend_redraw(me);
+
+ if (me->timing) {
+ float oldelapsed = me->elapsed;
+ me->elapsed += tplus;
+ if ((int)oldelapsed != (int)me->elapsed)
+ status_bar(me->frontend, me->laststatus ? me->laststatus : "");
+ }
+
+ midend_set_timer(me);
}
float *midend_colours(midend_data *me, int *ncolours)
@@ -866,6 +892,36 @@
me->anim_time = 0.0;
midend_finish_move(me);
midend_redraw(me);
- activate_timer(me->frontend);
+ midend_set_timer(me);
return NULL;
+}
+
+char *midend_rewrite_statusbar(midend_data *me, char *text)
+{
+ /*
+ * An important special case is that we are occasionally called
+ * with our own laststatus, to update the timer.
+ */
+ if (me->laststatus != text) {
+ sfree(me->laststatus);
+ me->laststatus = dupstr(text);
+ }
+
+ if (me->ourgame->is_timed) {
+ char timebuf[100], *ret;
+ int min, sec;
+
+ sec = me->elapsed;
+ min = sec / 60;
+ sec %= 60;
+ sprintf(timebuf, "[%d:%02d] ", min, sec);
+
+ ret = snewn(strlen(timebuf) + strlen(text) + 1, char);
+ strcpy(ret, timebuf);
+ strcat(ret, text);
+ return ret;
+
+ } else {
+ return dupstr(text);
+ }
}
--- a/mines.c
+++ b/mines.c
@@ -10,8 +10,6 @@
* That hook can talk to the game_ui and set the cheated flag,
* and then make_move can avoid setting the `won' flag after that.
*
- * - timer
- *
* - question marks (arrgh, preferences?)
*
* - sensible parameter constraints
@@ -2700,6 +2698,13 @@
return TRUE;
}
+static int game_timing_state(game_state *state)
+{
+ if (state->dead || state->won || !state->layout->mines)
+ return FALSE;
+ return TRUE;
+}
+
#ifdef COMBINED
#define thegame mines
#endif
@@ -2733,4 +2738,5 @@
game_anim_length,
game_flash_length,
game_wants_statusbar,
+ TRUE, game_timing_state,
};
--- a/net.c
+++ b/net.c
@@ -2568,6 +2568,11 @@
return TRUE;
}
+static int game_timing_state(game_state *state)
+{
+ return TRUE;
+}
+
#ifdef COMBINED
#define thegame net
#endif
@@ -2601,4 +2606,5 @@
game_anim_length,
game_flash_length,
game_wants_statusbar,
+ FALSE, game_timing_state,
};
--- a/netslide.c
+++ b/netslide.c
@@ -1725,6 +1725,11 @@
return TRUE;
}
+static int game_timing_state(game_state *state)
+{
+ return FALSE;
+}
+
#ifdef COMBINED
#define thegame netslide
#endif
@@ -1758,4 +1763,5 @@
game_anim_length,
game_flash_length,
game_wants_statusbar,
+ FALSE, game_timing_state,
};
--- a/nullgame.c
+++ b/nullgame.c
@@ -215,6 +215,11 @@
return FALSE;
}
+static int game_timing_state(game_state *state)
+{
+ return TRUE;
+}
+
#ifdef COMBINED
#define thegame nullgame
#endif
@@ -248,4 +253,5 @@
game_anim_length,
game_flash_length,
game_wants_statusbar,
+ FALSE, game_timing_state,
};
--- a/osx.m
+++ b/osx.m
@@ -383,7 +383,7 @@
- (void)keyDown:(NSEvent *)ev;
- (void)activateTimer;
- (void)deactivateTimer;
-- (void)setStatusLine:(NSString *)text;
+- (void)setStatusLine:(char *)text;
@end
@implementation MyImageView
@@ -1131,9 +1131,11 @@
[self sheetEndWithStatus:NO];
}
-- (void)setStatusLine:(NSString *)text
+- (void)setStatusLine:(char *)text
{
- [[status cell] setTitle:text];
+ char *rewritten = midend_rewrite_statusbar(me, text);
+ [[status cell] setTitle:[NSString stringWithCString:rewritten]];
+ sfree(rewritten);
}
@end
@@ -1267,7 +1269,7 @@
void status_bar(frontend *fe, char *text)
{
- [fe->window setStatusLine:[NSString stringWithCString:text]];
+ [fe->window setStatusLine:text];
}
/* ----------------------------------------------------------------------
--- a/pattern.c
+++ b/pattern.c
@@ -1105,6 +1105,11 @@
return FALSE;
}
+static int game_timing_state(game_state *state)
+{
+ return TRUE;
+}
+
#ifdef COMBINED
#define thegame pattern
#endif
@@ -1138,6 +1143,7 @@
game_anim_length,
game_flash_length,
game_wants_statusbar,
+ FALSE, game_timing_state,
};
#ifdef STANDALONE_SOLVER
--- a/puzzles.h
+++ b/puzzles.h
@@ -145,6 +145,7 @@
char *midend_text_format(midend_data *me);
char *midend_solve(midend_data *me);
void midend_supersede_game_desc(midend_data *me, char *desc);
+char *midend_rewrite_statusbar(midend_data *me, char *text);
/*
* malloc.c
@@ -239,6 +240,8 @@
float (*flash_length)(game_state *oldstate, game_state *newstate, int dir,
game_ui *ui);
int (*wants_statusbar)(void);
+ int is_timed;
+ int (*timing_state)(game_state *state);
};
/*
--- a/rect.c
+++ b/rect.c
@@ -2515,6 +2515,11 @@
return FALSE;
}
+static int game_timing_state(game_state *state)
+{
+ return TRUE;
+}
+
#ifdef COMBINED
#define thegame rect
#endif
@@ -2548,4 +2553,5 @@
game_anim_length,
game_flash_length,
game_wants_statusbar,
+ FALSE, game_timing_state,
};
--- a/sixteen.c
+++ b/sixteen.c
@@ -961,6 +961,11 @@
return TRUE;
}
+static int game_timing_state(game_state *state)
+{
+ return TRUE;
+}
+
#ifdef COMBINED
#define thegame sixteen
#endif
@@ -994,4 +999,5 @@
game_anim_length,
game_flash_length,
game_wants_statusbar,
+ FALSE, game_timing_state,
};
--- a/solo.c
+++ b/solo.c
@@ -2140,6 +2140,11 @@
return FALSE;
}
+static int game_timing_state(game_state *state)
+{
+ return TRUE;
+}
+
#ifdef COMBINED
#define thegame solo
#endif
@@ -2173,6 +2178,7 @@
game_anim_length,
game_flash_length,
game_wants_statusbar,
+ FALSE, game_timing_state,
};
#ifdef STANDALONE_SOLVER
--- a/twiddle.c
+++ b/twiddle.c
@@ -1111,6 +1111,11 @@
return TRUE;
}
+static int game_timing_state(game_state *state)
+{
+ return TRUE;
+}
+
#ifdef COMBINED
#define thegame twiddle
#endif
@@ -1144,4 +1149,5 @@
game_anim_length,
game_flash_length,
game_wants_statusbar,
+ FALSE, game_timing_state,
};
--- a/windows.c
+++ b/windows.c
@@ -118,6 +118,7 @@
HFONT cfgfont;
char *help_path;
int help_has_contents;
+ char *laststatus;
};
void fatal(char *fmt, ...)
@@ -144,7 +145,14 @@
void status_bar(frontend *fe, char *text)
{
- SetWindowText(fe->statusbar, text);
+ char *rewritten = midend_rewrite_statusbar(fe->me, text);
+ if (!fe->laststatus || strcmp(rewritten, fe->laststatus)) {
+ SetWindowText(fe->statusbar, rewritten);
+ sfree(fe->laststatus);
+ fe->laststatus = rewritten;
+ } else {
+ sfree(rewritten);
+ }
}
void frontend_default_colour(frontend *fe, float *output)
@@ -436,6 +444,8 @@
fe->fonts = NULL;
fe->nfonts = fe->fontsize = 0;
+
+ fe->laststatus = NULL;
{
int i, ncolours;