aboutsummaryrefslogtreecommitdiff
path: root/guess.c
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2005-07-05 18:13:31 +0000
committerSimon Tatham <anakin@pobox.com>2005-07-05 18:13:31 +0000
commitb74dac6de22e783ac6619411743df6bca1755a8b (patch)
tree3291c5880b6167a0af7625d399ae9093a1f1cdfa /guess.c
parenta4e3d69de23af4597fb1fd9a465f354a06568ffe (diff)
downloadpuzzles-b74dac6de22e783ac6619411743df6bca1755a8b.zip
puzzles-b74dac6de22e783ac6619411743df6bca1755a8b.tar.gz
puzzles-b74dac6de22e783ac6619411743df6bca1755a8b.tar.bz2
puzzles-b74dac6de22e783ac6619411743df6bca1755a8b.tar.xz
Refactored the game_size() interface, which was getting really
unpleasant and requiring lots of special cases to be taken care of by every single game. The new interface exposes an integer `tile size' or `scale' parameter to the midend and provides two much simpler routines: one which computes the pixel window size given a game_params and a tile size, and one which is given a tile size and must set up a drawstate appropriately. All the rest of the complexity is handled in the midend, mostly by binary search, so grubby special cases only have to be dealt with once. [originally from svn r6059]
Diffstat (limited to 'guess.c')
-rw-r--r--guess.c47
1 files changed, 23 insertions, 24 deletions
diff --git a/guess.c b/guess.c
index 9b94123..39241b0 100644
--- a/guess.c
+++ b/guess.c
@@ -805,18 +805,18 @@ static game_state *execute_move(game_state *from, char *move)
#define BORDER 0.5
-static void game_size(game_params *params, game_drawstate *ds,
- int *x, int *y, int expand)
+static void game_compute_size(game_params *params, int tilesize,
+ int *x, int *y)
{
- double hmul, vmul_c, vmul_g, vmul, szx, szy;
- int sz, colh, guessh;
+ double hmul, vmul_c, vmul_g, vmul;
+ int hintw = (params->npegs+1)/2;
hmul = BORDER * 2.0 + /* border */
1.0 * 2.0 + /* vertical colour bar */
1.0 * params->npegs + /* guess pegs */
PEG_GAP * params->npegs + /* guess gaps */
- PEG_HINT * ds->hintw + /* hint pegs */
- PEG_GAP * (ds->hintw - 1); /* hint gaps */
+ PEG_HINT * hintw + /* hint pegs */
+ PEG_GAP * (hintw - 1); /* hint gaps */
vmul_c = BORDER * 2.0 + /* border */
1.0 * params->ncolours + /* colour pegs */
@@ -828,13 +828,16 @@ static void game_size(game_params *params, game_drawstate *ds,
vmul = max(vmul_c, vmul_g);
- szx = *x / hmul;
- szy = *y / vmul;
- sz = max(min((int)szx, (int)szy), 1);
- if (expand)
- ds->pegsz = sz;
- else
- ds->pegsz = min(sz, PEG_PREFER_SZ);
+ *x = (int)ceil((double)tilesize * hmul);
+ *y = (int)ceil((double)tilesize * vmul);
+}
+
+static void game_set_size(game_drawstate *ds, game_params *params,
+ int tilesize)
+{
+ int colh, guessh, x, y;
+
+ ds->pegsz = tilesize;
ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
@@ -843,25 +846,21 @@ static void game_size(game_params *params, game_drawstate *ds,
ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
ds->hintrad = (ds->hintsz-1)/2;
- *x = (int)ceil((double)ds->pegsz * hmul);
- *y = (int)ceil((double)ds->pegsz * vmul);
- ds->w = *x; ds->h = *y;
-
colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
guessh += ds->gapsz + ds->pegsz; /* solution */
+ game_compute_size(params, tilesize, &x, &y);
ds->colx = ds->border;
- ds->coly = (*y - colh) / 2;
+ ds->coly = (y - colh) / 2;
ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
- ds->guessy = (*y - guessh) / 2;
+ ds->guessy = (y - guessh) / 2;
ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
- if (ds->pegsz > 0) {
- if (ds->blit_peg) blitter_free(ds->blit_peg);
- ds->blit_peg = blitter_new(ds->pegsz, ds->pegsz);
- }
+ assert(ds->pegsz > 0);
+ if (ds->blit_peg) blitter_free(ds->blit_peg);
+ ds->blit_peg = blitter_new(ds->pegsz, ds->pegsz);
}
static float *game_colours(frontend *fe, game_state *state, int *ncolours)
@@ -1296,7 +1295,7 @@ const struct game thegame = {
game_changed_state,
interpret_move,
execute_move,
- game_size,
+ PEG_PREFER_SZ, game_compute_size, game_set_size,
game_colours,
game_new_drawstate,
game_free_drawstate,