X-Git-Url: http://git.draconx.ca/gitweb/rrace.git/blobdiff_plain/5d299c8b50dd94caac916b410af6782d1b20f3fe..31d8eda9987703c64fc63f4ddb4088cf3be5b7cc:/src/x11.c diff --git a/src/x11.c b/src/x11.c index 1bd19c9..6954f06 100644 --- a/src/x11.c +++ b/src/x11.c @@ -31,10 +31,20 @@ /* Size of the traditional icon pixmap (multiple of 3) */ #define ICON_SIZE 48 -typedef String colour_tab[COLOUR_MAX*(TILE_MAX-1)]; +/* Define to 1 to add highlights and delays for visually debugging redraws */ +#if X11_RENDER_DEBUG +# include +#endif + +enum { + NUM_TILE_COLOURS = COLOUR_MAX*(TILE_MAX-1), + COLOUR_UI_GAMEBG = NUM_TILE_COLOURS, + COLOUR_UI_MAX +}; +typedef Pixel colour_tab[COLOUR_UI_MAX]; #define COLOURRES(name, index, def) { \ - (name), (NULL), XtRString, sizeof (String), \ + (name), (NULL), XtRPixel, sizeof (String), \ offsetof(struct { colour_tab t; }, t[index]), \ XtRString, (def) } @@ -59,31 +69,26 @@ static XtResource colour_resources[] = { COLOURRES("colourLight3", 15, COLOUR3_LIGHT), COLOURRES("colourLight4", 16, COLOUR4_LIGHT), COLOURRES("colourLight5", 17, COLOUR5_LIGHT), + + COLOURRES("gameBackground", COLOUR_UI_GAMEBG, "#181818") }; static void init_colours(struct app_state *state, Widget shell) { - Display *display = XtDisplay(shell); - Screen *screen = XtScreen(shell); - Colormap cmap = DefaultColormapOfScreen(screen); unsigned i; - colour_tab names; - XtGetApplicationResources(shell, &names, colour_resources, + colour_tab colours; + XtGetApplicationResources(shell, &colours, colour_resources, XtNumber(colour_resources), NULL, 0); - for (i = 0; i < XtNumber(names); i++) { + for (i = 0; i < NUM_TILE_COLOURS; i++) { unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1); - XColor c; - assert(names[i]); - if (!XAllocNamedColor(display, cmap, names[i], &c, &c)) { - printf("warning: could not allocate colour: %s\n", - names[i]); - c.pixel = 0; - } - state->tile_colour[tile][shade] = c.pixel; + state->tile_colour[tile][shade] = colours[i]; } + + XtVaSetValues(state->game, XtNbackground, colours[COLOUR_UI_GAMEBG], + (char *)NULL); } void x11_initialize(struct app_state *state, Widget shell) @@ -133,6 +138,74 @@ static void draw_tile(struct app_state *state, Display *display, Drawable d, XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4); } +/* + * Clear a contiguous rectangle of tiles from top-left (x0, y0) to + * bottom-right (x1, y1). + */ +static void +clear_tiles(struct app_state *state, Display *display, Drawable d, + int x0, int y0, int x1, int y1, Dimension w, Dimension h) +{ + XRectangle r = { x0*w, y0*h, (x1+1)*w, (y1+1)*h }; + +#if X11_RENDER_DEBUG + XSetForeground(display, state->tile_gc, 0xff0000); + XFillRectangles(display, d, state->tile_gc, &r, 1); + XFlush(display); + usleep(70000); +#endif + + XClearArea(display, d, r.x, r.y, r.width, r.height, 0); +} + +/* + * Efficiently clear all the border tiles in the game area. The mask indicates + * which tiles need clearing, but for the border clear it is safe to wipe an + * entire row or column of the border using a single XClearArea. + * + * The idea is to pick whichever row or column has the most tiles to clear, + * clear them, and then repeat until none are left (repeats at most 4 times). + */ +static void clear_border(struct app_state *state, Display *display, Drawable d, + Dimension w, Dimension h, uint_fast32_t mask) +{ + uint_fast32_t best_mask = 0; + int best_count = -1; + int i, best = -1; + + if (!(mask &= ~GOAL_MASK & 0x1ffffff)) + return; + + for (i = 0; i < 4; i++) { + uint_fast32_t this_mask, tmp; + int this_count = 0; + + if (i & 2) + this_mask = board_column(4*(i & 1)); + else + this_mask = board_row(4*(i & 1)); + + /* Count set bits */ + for (tmp = mask & this_mask; tmp; this_count++) + tmp &= tmp - 1; + + if (this_count > best_count) { + best_count = this_count; + best_mask = this_mask; + best = i; + } + } + + switch (best) { + case 0: clear_tiles(state, display, d, 0, 0, 4, 0, w, h); break; + case 1: clear_tiles(state, display, d, 0, 4, 4, 4, w, h); break; + case 2: clear_tiles(state, display, d, 0, 0, 0, 4, w, h); break; + case 3: clear_tiles(state, display, d, 4, 0, 4, 4, w, h); break; + } + + clear_border(state, display, d, w, h, mask & ~best_mask); +} + static int redraw_tile(struct app_state *state, Display *display, Drawable d, uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2, @@ -141,6 +214,16 @@ redraw_tile(struct app_state *state, Display *display, Drawable d, uint_fast32_t pos = board_position(x, y); unsigned char tile = 0; +#if X11_RENDER_DEBUG + if (d == XtWindow(state->game) || d == XtWindow(state->goal)) { + XRectangle r = { x*w, y*h, w, h }; + XSetForeground(display, state->tile_gc, 0xff0000); + XFillRectangles(display, d, state->tile_gc, &r, 1); + XFlush(display); + usleep(70000); + } +#endif + if (bit0 & pos) tile |= 1; if (bit1 & pos) tile |= 2; if (bit2 & pos) tile |= 4; @@ -167,17 +250,18 @@ redraw_goal_tile(struct app_state *state, Display *display, Drawable d, void x11_redraw_goal(struct app_state *state, uint_fast32_t mask) { Display *display = XtDisplay(state->goal); - Window window = XtWindow(state->goal); + Window goal = XtWindow(state->goal); Dimension w, h; int i; XtVaGetValues(state->goal, XtNwidth, &w, XtNheight, &h, (char *)NULL); + w /= 3; h /= 3; + for (i = 0; i < 9; i++) { int x = i%3, y = i/3; if (mask & 1) { - redraw_goal_tile(state, display, window, - x, y, w/3, h/3); + redraw_goal_tile(state, display, goal, x, y, w, h); } /* @@ -247,13 +331,27 @@ void x11_redraw_game(struct app_state *state, uint_fast32_t mask) { Display *display = XtDisplay(state->goal); Window game = XtWindow(state->game); - uint_least32_t *gp = state->board.game; + uint_least32_t buf[3], *gp = state->board.game; Dimension w, h; int i; XtVaGetValues(state->game, XtNwidth, &w, XtNheight, &h, (char *)NULL); w /= 5; h /= 5; + if (state->view_goal_on_game) { + for (i = 0; i < 3; i++) { + buf[i] = state->board.goal[i]; + buf[i] = (gp[i] & ~GOAL_MASK) | (buf[i] << GOAL_SHIFT); + } + gp = buf; + } + + /* Optimize the game end case where the outer frame is cleared */ + if (((gp[0] | gp[1] | gp[2]) & ~GOAL_MASK) == 0) { + clear_border(state, display, game, w, h, mask); + mask &= GOAL_MASK; + } + for (i = 0; i < 25; i++) { if (mask & 1) { redraw_tile(state, display, game, @@ -263,3 +361,56 @@ void x11_redraw_game(struct app_state *state, uint_fast32_t mask) mask >>= 1; } } + +/* + * Deferred redraw of tiles after resize/expose to avoid redundant drawing. + * + * Record any tiles that need to be redrawn due to resizes or expose events, + * then, after a short delay, perform all the accumulated redraws at once. + * + * This is implemented using both a work proc and a timeout, because it seems + * that rendering directly inside the timeout callback gives poor results: + * possibly redrawing outdated intermediate positions during "fast" resizes + * long after the resizing has stopped. This does not happen when drawing + * from a work proc. + */ +static Boolean do_render(void *data) +{ + struct app_state *state = data; + + x11_redraw_goal(state, state->render_goal_mask); + x11_redraw_game(state, state->render_game_mask); + + state->render_goal_mask = state->render_game_mask = 0; + state->render_proc = 0; + state->render_tick = 0; + return True; +} + +static void start_render(void *data, XtIntervalId *id) +{ + struct app_state *state = data; + XtAppContext app; + + if (state->render_proc) + return; + + app = XtWidgetToApplicationContext(state->game); + state->render_proc = XtAppAddWorkProc(app, do_render, state); +} + +void x11_queue_render(struct app_state *state, uint_fast32_t game_mask, + uint_fast16_t goal_mask) +{ + uint_fast32_t changed = 0; + XtAppContext app; + + changed |= state->render_game_mask |= game_mask; + changed |= state->render_goal_mask |= goal_mask; + + if (state->render_tick || !changed) + return; + + app = XtWidgetToApplicationContext(state->game); + state->render_tick = XtAppAddTimeOut(app, 3, start_render, state); +}