X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/ef29354ca2a5a9d8ec5a36cd80b28467aca9608c..eddb22e8e99adfc4514498d0f1b10c32c9d21a00:/rect.c diff --git a/rect.c b/rect.c index 7992598..dcd8ef7 100644 --- a/rect.c +++ b/rect.c @@ -28,12 +28,6 @@ * selection to produce a few large rectangles more often * than oodles of small ones? Unsure, but might be worth a * try. - * - * - During redraw, do corner analysis centrally in game_redraw() - * itself so that we can take it into account when computing the - * `visible' array. If we can do this, we can actually _turn on_ - * the `visible' processing and keep redraws to the minimum - * required. */ #include @@ -76,6 +70,9 @@ struct game_params { #define TILE_SIZE 24 #define BORDER 18 +#define CORNER_TOLERANCE 0.15F +#define CENTRE_TOLERANCE 0.15F + #define FLASH_TIME 0.13F #define COORD(x) ( (x) * TILE_SIZE + BORDER ) @@ -173,8 +170,8 @@ char *validate_params(game_params *params) { if (params->w <= 0 && params->h <= 0) return "Width and height must both be greater than zero"; - if (params->w * params->h < 4) - return "Total area must be at least 4"; + if (params->w < 2 && params->h < 2) + return "Grid area must be greater than one"; return NULL; } @@ -197,9 +194,13 @@ static struct rectlist *get_rectlist(game_params *params, int *grid) int nrects = 0, rectsize = 0; /* - * Maximum rectangle area is 1/6 of total grid size. + * Maximum rectangle area is 1/6 of total grid size, unless + * this means we can't place any rectangles at all in which + * case we set it to 2 at minimum. */ maxarea = params->w * params->h / 6; + if (maxarea < 2) + maxarea = 2; for (rw = 1; rw <= params->w; rw++) for (rh = 1; rh <= params->h; rh++) { @@ -209,25 +210,6 @@ static struct rectlist *get_rectlist(game_params *params, int *grid) continue; for (x = 0; x <= params->w - rw; x++) for (y = 0; y <= params->h - rh; y++) { - /* - * We have a candidate rectangle placement. See - * if it's unobstructed. - */ - int xx, yy; - int ok; - - ok = TRUE; - for (xx = x; xx < x+rw; xx++) - for (yy = y; yy < y+rh; yy++) - if (index(params, grid, xx, yy) >= 0) { - ok = FALSE; - goto break1; /* break both loops at once */ - } - break1: - - if (!ok) - continue; - if (nrects >= rectsize) { rectsize = nrects + 256; rects = sresize(rects, rectsize, struct rect); @@ -690,7 +672,7 @@ char *validate_seed(game_params *params, char *seed) } else if (n == '_') { /* do nothing */; } else if (n > '0' && n <= '9') { - squares += atoi(seed-1); + squares++; while (*seed >= '0' && *seed <= '9') seed++; } else @@ -910,29 +892,88 @@ void free_ui(game_ui *ui) sfree(ui); } -int coord_round(float coord) +void coord_round(float x, float y, int *xr, int *yr) { - int i; - float dist; + float xs, ys, xv, yv, dx, dy, dist; /* - * Find the nearest integer. + * Find the nearest square-centre. */ - i = (int)(coord + 0.5F); + xs = (float)floor(x) + 0.5F; + ys = (float)floor(y) + 0.5F; /* - * Find the distance from us to that integer. + * And find the nearest grid vertex. */ - dist = (float)fabs(coord - (float)i); + xv = (float)floor(x + 0.5F); + yv = (float)floor(y + 0.5F); /* - * If we're within the tolerance limit, return the edge - * coordinate. Otherwise, return the centre coordinate. + * We allocate clicks in parts of the grid square to either + * corners, edges or square centres, as follows: + * + * +--+--------+--+ + * | | | | + * +--+ +--+ + * | `. ,' | + * | +--+ | + * | | | | + * | +--+ | + * | ,' `. | + * +--+ +--+ + * | | | | + * +--+--------+--+ + * + * (Not to scale!) + * + * In other words: we measure the square distance (i.e. + * max(dx,dy)) from the click to the nearest corner, and if + * it's within CORNER_TOLERANCE then we return a corner click. + * We measure the square distance from the click to the nearest + * centre, and if that's within CENTRE_TOLERANCE we return a + * centre click. Failing that, we find which of the two edge + * centres is nearer to the click and return that edge. */ - if (dist < 0.3F) - return i * 2; - else - return 1 + 2 * (int)coord; + + /* + * Check for corner click. + */ + dx = (float)fabs(x - xv); + dy = (float)fabs(y - yv); + dist = (dx > dy ? dx : dy); + if (dist < CORNER_TOLERANCE) { + *xr = 2 * (int)xv; + *yr = 2 * (int)yv; + } else { + /* + * Check for centre click. + */ + dx = (float)fabs(x - xs); + dy = (float)fabs(y - ys); + dist = (dx > dy ? dx : dy); + if (dist < CENTRE_TOLERANCE) { + *xr = 1 + 2 * (int)xs; + *yr = 1 + 2 * (int)ys; + } else { + /* + * Failing both of those, see which edge we're closer to. + * Conveniently, this is simply done by testing the relative + * magnitude of dx and dy (which are currently distances from + * the square centre). + */ + if (dx > dy) { + /* Vertical edge: x-coord of corner, + * y-coord of square centre. */ + *xr = 2 * (int)xv; + *yr = 1 + 2 * (int)ys; + } else { + /* Horizontal edge: x-coord of square centre, + * y-coord of corner. */ + *xr = 1 + 2 * (int)xs; + *yr = 2 * (int)yv; + } + } + } } static void ui_draw_rect(game_state *state, game_ui *ui, @@ -996,8 +1037,7 @@ game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button) return NULL; } - xc = coord_round(FROMCOORD((float)x)); - yc = coord_round(FROMCOORD((float)y)); + coord_round(FROMCOORD((float)x), FROMCOORD((float)y), &xc, &yc); if (startdrag) { ui->drag_start_x = xc; @@ -1043,7 +1083,7 @@ game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button) * We've made a real change to the grid. Check to see * if the game has been completed. */ - if (!ret->completed) { + if (ret && !ret->completed) { int x, y, ok; unsigned char *correct = get_correct(ret); @@ -1080,7 +1120,7 @@ game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button) * Drawing routines. */ -#define CORRECT 256 +#define CORRECT 65536 #define COLOUR(k) ( (k)==1 ? COL_LINE : COL_DRAG ) #define MAX(x,y) ( (x)>(y) ? (x) : (y) ) @@ -1089,7 +1129,7 @@ game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button) struct game_drawstate { int started; int w, h; - unsigned short *visible; + unsigned int *visible; }; void game_size(game_params *params, int *x, int *y) @@ -1136,7 +1176,7 @@ game_drawstate *game_new_drawstate(game_state *state) ds->started = FALSE; ds->w = state->w; ds->h = state->h; - ds->visible = snewn(ds->w * ds->h, unsigned short); + ds->visible = snewn(ds->w * ds->h, unsigned int); for (i = 0; i < ds->w * ds->h; i++) ds->visible[i] = 0xFFFF; @@ -1150,7 +1190,8 @@ void game_free_drawstate(game_drawstate *ds) } void draw_tile(frontend *fe, game_state *state, int x, int y, - unsigned char *hedge, unsigned char *vedge, int correct) + unsigned char *hedge, unsigned char *vedge, + unsigned char *corners, int correct) { int cx = COORD(x), cy = COORD(y); char str[80]; @@ -1188,34 +1229,18 @@ void draw_tile(frontend *fe, game_state *state, int x, int y, /* * Draw corners. */ - if ((HRANGE(state,x-1,y) && index(state,hedge,x-1,y)) || - (VRANGE(state,x,y-1) && index(state,vedge,x,y-1))) + if (index(state,corners,x,y)) draw_rect(fe, cx, cy, 2, 2, - COLOUR(MAX4(index(state,hedge,x-1,y), - index(state,vedge,x,y-1), - index(state,hedge,x,y), - index(state,vedge,x,y)))); - if ((HRANGE(state,x+1,y) && index(state,hedge,x+1,y)) || - (VRANGE(state,x+1,y-1) && index(state,vedge,x+1,y-1))) + COLOUR(index(state,corners,x,y))); + if (x+1 < state->w && index(state,corners,x+1,y)) draw_rect(fe, cx+TILE_SIZE-1, cy, 2, 2, - COLOUR(MAX4(index(state,hedge,x+1,y), - index(state,vedge,x+1,y-1), - index(state,hedge,x,y), - index(state,vedge,x+1,y)))); - if ((HRANGE(state,x-1,y+1) && index(state,hedge,x-1,y+1)) || - (VRANGE(state,x,y+1) && index(state,vedge,x,y+1))) + COLOUR(index(state,corners,x+1,y))); + if (y+1 < state->h && index(state,corners,x,y+1)) draw_rect(fe, cx, cy+TILE_SIZE-1, 2, 2, - COLOUR(MAX4(index(state,hedge,x-1,y+1), - index(state,vedge,x,y+1), - index(state,hedge,x,y+1), - index(state,vedge,x,y)))); - if ((HRANGE(state,x+1,y+1) && index(state,hedge,x+1,y+1)) || - (VRANGE(state,x+1,y+1) && index(state,vedge,x+1,y+1))) + COLOUR(index(state,corners,x,y+1))); + if (x+1 < state->w && y+1 < state->h && index(state,corners,x+1,y+1)) draw_rect(fe, cx+TILE_SIZE-1, cy+TILE_SIZE-1, 2, 2, - COLOUR(MAX4(index(state,hedge,x+1,y+1), - index(state,vedge,x+1,y+1), - index(state,hedge,x,y+1), - index(state,vedge,x+1,y)))); + COLOUR(index(state,corners,x+1,y+1))); draw_update(fe, cx, cy, TILE_SIZE+1, TILE_SIZE+1); } @@ -1226,7 +1251,7 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, { int x, y; unsigned char *correct; - unsigned char *hedge, *vedge; + unsigned char *hedge, *vedge, *corners; correct = get_correct(state); @@ -1241,6 +1266,28 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, vedge = state->vedge; } + corners = snewn(state->w * state->h, unsigned char); + memset(corners, 0, state->w * state->h); + for (x = 0; x < state->w; x++) + for (y = 0; y < state->h; y++) { + if (x > 0) { + int e = index(state, vedge, x, y); + if (index(state,corners,x,y) < e) + index(state,corners,x,y) = e; + if (y+1 < state->h && + index(state,corners,x,y+1) < e) + index(state,corners,x,y+1) = e; + } + if (y > 0) { + int e = index(state, hedge, x, y); + if (index(state,corners,x,y) < e) + index(state,corners,x,y) = e; + if (x+1 < state->w && + index(state,corners,x+1,y) < e) + index(state,corners,x+1,y) = e; + } + } + if (!ds->started) { draw_rect(fe, 0, 0, state->w * TILE_SIZE + 2*BORDER + 1, @@ -1255,22 +1302,29 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, for (x = 0; x < state->w; x++) for (y = 0; y < state->h; y++) { - unsigned short c = 0; + unsigned int c = 0; if (HRANGE(state,x,y)) c |= index(state,hedge,x,y); - if (HRANGE(state,x+1,y)) - c |= index(state,hedge,x+1,y) << 2; + if (HRANGE(state,x,y+1)) + c |= index(state,hedge,x,y+1) << 2; if (VRANGE(state,x,y)) c |= index(state,vedge,x,y) << 4; - if (VRANGE(state,x,y+1)) - c |= index(state,vedge,x,y+1) << 6; + if (VRANGE(state,x+1,y)) + c |= index(state,vedge,x+1,y) << 6; + c |= index(state,corners,x,y) << 8; + if (x+1 < state->w) + c |= index(state,corners,x+1,y) << 10; + if (y+1 < state->h) + c |= index(state,corners,x,y+1) << 12; + if (x+1 < state->w && y+1 < state->h) + c |= index(state,corners,x+1,y+1) << 14; if (index(state, correct, x, y) && !flashtime) c |= CORRECT; if (index(ds,ds->visible,x,y) != c) { - draw_tile(fe, state, x, y, hedge, vedge, c & CORRECT); - /* index(ds,ds->visible,x,y) = c; */ + draw_tile(fe, state, x, y, hedge, vedge, corners, c & CORRECT); + index(ds,ds->visible,x,y) = c; } }