]> git.draconx.ca Git - rrace.git/blobdiff - src/x11.c
motif: simplify border_clear implementation.
[rrace.git] / src / x11.c
index 6954f06a15a5a30345ce43be670b5227d411bc0f..b098d0ea7a43034c3e688ab737389e653b93e788 100644 (file)
--- a/src/x11.c
+++ b/src/x11.c
@@ -26,7 +26,9 @@
 #include <X11/StringDefs.h>
 #include <X11/Xatom.h>
 #include <X11/Shell.h>
+
 #include "motif.h"
+#include "ewmhicon.h"
 
 /* Size of the traditional icon pixmap (multiple of 3) */
 #define ICON_SIZE 48
@@ -138,72 +140,36 @@ 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)
+static void clear_rect(struct app_state *state, 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
+       XRectangle r = { x, y, w, h };
+
        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);
+       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)
 {
-       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(state, display, d, 0,   0,   5*w,   h);
+       clear_rect(state, display, d, 0,   4*h, 5*w,   h);
+       clear_rect(state, display, d, 0,   0,     w, 5*h);
+       clear_rect(state, display, d, 4*w, 0,     w, 5*h);
 }
 
 static int
@@ -277,36 +243,16 @@ void x11_redraw_icon(struct app_state *state, Widget shell)
 {
        Display *display = XtDisplay(shell);
        Dimension tilesz = ICON_SIZE/3;
-       int i, j, tile;
-
-       XColor colours[(TILE_MAX-1)*COLOUR_MAX];
-       unsigned long *icons[ICON_MAX];
-       void *wm_icon = NULL;
-
-       if (state->use_ewmh_icons && (wm_icon = ewmh_icon_alloc(icons))) {
-               Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
-
-               for (i = 0; i < TILE_MAX-1; i++) {
-                       for (j = 0; j < COLOUR_MAX; j++) {
-                               XColor *c = &colours[i*COLOUR_MAX+j];
-                               c->pixel = state->tile_colour[i][j];
-                       }
-               }
-
-               XQueryColors(display, cmap, colours, i*j);
-       }
+       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);
+                                              i%3, i/3, tilesz, tilesz);
 
-               if (wm_icon) {
-                       XColor *c = &colours[(tile-1)*COLOUR_MAX];
-                       ewmh_tile16(icons[ICON_16x16], c, i%3, i/3);
-                       ewmh_tile24(icons[ICON_24x24], c, i%3, i/3);
-                       ewmh_tile32(icons[ICON_32x32], c, i%3, i/3);
-                       ewmh_tile48(icons[ICON_48x48], c, i%3, i/3);
-               }
+               ewmhseq[i] = COLOUR_MAX*0x10101 * (tile-1) + 0x20100;
        }
 
        /*
@@ -316,13 +262,24 @@ void x11_redraw_icon(struct app_state *state, Widget shell)
        XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
        XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
 
-       if (wm_icon) {
+       if (state->use_ewmh_icons) {
                Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
+               Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
+               XColor colours[(TILE_MAX-1)*COLOUR_MAX];
+               void *wm_icon;
 
-               XChangeProperty(display, XtWindow(shell), net_wm_icon,
-                               XA_CARDINAL, 32, PropModeReplace,
-                               wm_icon, EWMH_ICON_NELEM);
+               for (i = 0; i < XtNumber(colours); i++) {
+                       uint_least32_t *p = state->tile_colour[0];
+                       colours[i].pixel = p[i];
+               }
 
+               XQueryColors(display, cmap, colours, XtNumber(colours));
+               wm_icon = ewmh_icon_generate(ewmhseq, colours);
+               if (wm_icon) {
+                       XChangeProperty(display, XtWindow(shell), net_wm_icon,
+                                       XA_CARDINAL, 32, PropModeReplace,
+                                       wm_icon, EWMH_ICON_NELEM);
+               }
                free(wm_icon);
        }
 }
@@ -399,14 +356,15 @@ static void start_render(void *data, XtIntervalId *id)
        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)
+void x11_queue_render(struct app_state *state, uint_fast32_t mask, int mode)
 {
        uint_fast32_t changed = 0;
        XtAppContext app;
 
-       changed |= state->render_game_mask |= game_mask;
-       changed |= state->render_goal_mask |= goal_mask;
+       if (mode & RENDER_MODE_GOAL)
+               changed |= state->render_goal_mask |= mask;
+       if (mode & RENDER_MODE_GAME)
+               changed |= state->render_game_mask |= mask;
 
        if (state->render_tick || !changed)
                return;