]> git.draconx.ca Git - rrace.git/blobdiff - src/x11.c
motif: Avoid constantly recalculating tile size.
[rrace.git] / src / x11.c
index f827965b8d8a4dc353df510a7bac1fdd44095dad..6d09a7dcfc09f7359db1daeba62d980bae65cfd4 100644 (file)
--- a/src/x11.c
+++ b/src/x11.c
@@ -140,81 +140,45 @@ 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)
+clear_rect(Display *display, Drawable d, int x, int y, int w, int 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);
+       XRectangle r = { x, y, w, h };
+       XGCValues gcv;
+       GC gc;
+
+       gcv.foreground = 0xff0000;
+       gc = XCreateGC(display, d, GCForeground, &gcv);
+       XFillRectangles(display, d, gc, &r, 1);
+       XFreeGC(display, gc);
+
        XFlush(display);
        usleep(70000);
 #endif
 
-       XClearArea(display, d, r.x, r.y, r.width, r.height, 0);
+       XClearArea(display, d, x, y, w, h, 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)
+static void clear_border(Display *display, Drawable d, unsigned sz)
 {
-       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);
+       clear_rect(display, d, 0,    0,    -1, sz);
+       clear_rect(display, d, 0,    4*sz, -1, sz);
+       clear_rect(display, d, 0,    0,    sz, -1);
+       clear_rect(display, d, 4*sz, 0,    sz, -1);
 }
 
 static int
 redraw_tile(struct app_state *state, Display *display, Drawable d,
-            uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
-            int x, int y, Dimension w, Dimension h)
+            uint_least32_t *gp, int x, int y, unsigned sz)
 {
-       uint_fast32_t pos = board_position(x, y);
-       unsigned char tile = 0;
+       unsigned tile = board_tile(gp, 5*y+x);
+       unsigned w = sz, h = sz;
 
 #if X11_RENDER_DEBUG
        if (d == XtWindow(state->game) || d == XtWindow(state->goal)) {
@@ -225,12 +189,7 @@ redraw_tile(struct app_state *state, Display *display, Drawable d,
                usleep(70000);
        }
 #endif
-
-       if (bit0 & pos) tile |= 1;
-       if (bit1 & pos) tile |= 2;
-       if (bit2 & pos) tile |= 4;
        assert(tile < TILE_MAX);
-
        if (tile == TILE_EMPTY) {
                XClearArea(display, d, x*w, y*h, w, h, 0);
        } else {
@@ -240,57 +199,9 @@ redraw_tile(struct app_state *state, Display *display, Drawable d,
        return tile;
 }
 
-static int
-redraw_goal_tile(struct app_state *state, Display *display, Drawable d,
-                 int x, int y, Dimension w, Dimension h)
-{
-       uint_least16_t *gp = state->board.goal;
-
-       return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h);
-}
-
-void x11_redraw_goal(struct app_state *state, uint_fast32_t mask)
+static void set_icon(struct app_state *state, Display *display,
+                     Widget shell, unsigned long *ewmhseq)
 {
-       Display *display = XtDisplay(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, goal, x, y, w, h);
-               }
-
-               /*
-                * Goal bitmaps have a gap of 2 tiles between each row.
-                * This funny shift will accomodate that.
-                */
-               mask >>= 1|x;
-       }
-}
-
-/* Render the goal area as the window's icon */
-void x11_redraw_icon(struct app_state *state, Widget shell)
-{
-       Display *display = XtDisplay(shell);
-       Dimension tilesz = ICON_SIZE/3;
-       unsigned long ewmhseq[9];
-       unsigned i;
-
-       for (i = 0; i < 9; i++) {
-               int tile;
-
-               tile = redraw_goal_tile(state, display, state->icon_pixmap,
-                                              i%3, i/3, tilesz, tilesz);
-
-               ewmhseq[i] = COLOUR_MAX*0x10101 * (tile-1) + 0x20100;
-       }
-
        /*
         * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
         * notice the changed icon.
@@ -303,6 +214,7 @@ void x11_redraw_icon(struct app_state *state, Widget shell)
                Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
                XColor colours[(TILE_MAX-1)*COLOUR_MAX];
                void *wm_icon;
+               int i;
 
                for (i = 0; i < XtNumber(colours); i++) {
                        uint_least32_t *p = state->tile_colour[0];
@@ -320,17 +232,54 @@ void x11_redraw_icon(struct app_state *state, Widget shell)
        }
 }
 
+void
+x11_redraw_goal(struct app_state *state, uint_fast32_t mask, Widget icon_shell)
+{
+       Display *display = XtDisplay(state->goal);
+       Window goal = XtWindow(state->goal);
+       unsigned sz = state->goal_tile_sz;
+       unsigned long ewmhseq[9];
+       int i;
+
+       uint_least32_t gp[3] = {
+               state->board.goal[0],
+               state->board.goal[1],
+               state->board.goal[2]
+       };
+
+       for (i = 0; i < 9; i++) {
+               int x = i%3, y = i/3;
+
+               if (icon_shell) {
+                       unsigned tile;
+
+                       tile = redraw_tile(state, display, state->icon_pixmap,
+                                          gp, x, y, ICON_SIZE/3);
+                       ewmhseq[i] = COLOUR_MAX*0x10101 * (tile-1) + 0x20100;
+               }
+
+               if (mask & 1)
+                       redraw_tile(state, display, goal, gp, x, y, sz);
+
+               /*
+                * Goal bitmaps have a gap of 2 tiles between each row.
+                * This funny shift will accomodate that.
+                */
+               mask >>= 1|x;
+       }
+
+       if (icon_shell)
+               set_icon(state, display, icon_shell, ewmhseq);
+}
+
 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 buf[3], *gp = state->board.game;
-       Dimension w, h;
+       unsigned sz = state->game_tile_sz;
        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];
@@ -341,16 +290,13 @@ void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
 
        /* 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);
+               clear_border(display, game, sz);
                mask &= GOAL_MASK;
        }
 
        for (i = 0; i < 25; i++) {
-               if (mask & 1) {
-                       redraw_tile(state, display, game,
-                                   gp[0], gp[1], gp[2],
-                                   i%5, i/5, w, h);
-               }
+               if (mask & 1)
+                       redraw_tile(state, display, game, gp, i%5, i/5, sz);
                mask >>= 1;
        }
 }
@@ -371,7 +317,7 @@ static Boolean do_render(void *data)
 {
        struct app_state *state = data;
 
-       x11_redraw_goal(state, state->render_goal_mask);
+       x11_redraw_goal(state, state->render_goal_mask, NULL);
        x11_redraw_game(state, state->render_game_mask);
 
        state->render_goal_mask = state->render_game_mask = 0;