]> git.draconx.ca Git - rrace.git/blobdiff - src/x11.c
Add build-time option to help debug X11 rendering.
[rrace.git] / src / x11.c
index ea0fdbac90a47688a38c4787a125118b93d655c1..0a9bef0a78684ad47ae791c2fd017cc3a1fc0be6 100644 (file)
--- a/src/x11.c
+++ b/src/x11.c
@@ -23,7 +23,7 @@
 #include <assert.h>
 
 #include <X11/Intrinsic.h>
-#include <Xm/XmStrDefs.h>
+#include <X11/StringDefs.h>
 #include <X11/Xatom.h>
 #include <X11/Shell.h>
 #include "motif.h"
 /* 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 <unistd.h>
+#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)
@@ -167,17 +172,23 @@ 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, XmNwidth, &w, XmNheight, &h, (char *)NULL);
+       XtVaGetValues(state->goal, XtNwidth, &w, XtNheight, &h, (char *)NULL);
        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);
+#if X11_RENDER_DEBUG
+                       XRectangle r = { w/3*(i%3), w/3*(i/3), w/3, h/3 };
+                       XSetForeground(display, state->tile_gc, 0xff0000);
+                       XFillRectangles(display, goal, state->tile_gc, &r, 1);
+                       XFlush(display);
+                       usleep(70000);
+#endif
+                       redraw_goal_tile(state, display, goal, x, y, w/3, h/3);
                }
 
                /*
@@ -226,11 +237,11 @@ void x11_redraw_icon(struct app_state *state, Widget shell)
        }
 
        /*
-        * Clear and reset XmNiconPixmap otherwise it seems dtwm will not
+        * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
         * notice the changed icon.
         */
-       XtVaSetValues(shell, XmNiconPixmap, None, (char *)NULL);
-       XtVaSetValues(shell, XmNiconPixmap, state->icon_pixmap, (char *)NULL);
+       XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
+       XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
 
        if (wm_icon) {
                Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
@@ -247,15 +258,30 @@ 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, XmNwidth, &w, XmNheight, &h, (char *)NULL);
+       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;
+       }
+
        for (i = 0; i < 25; i++) {
                if (mask & 1) {
+#if X11_RENDER_DEBUG
+                       XRectangle r = { w*(i%5), w*(i/5), w, h };
+                       XSetForeground(display, state->tile_gc, 0xff0000);
+                       XFillRectangles(display, game, state->tile_gc, &r, 1);
+                       XFlush(display);
+                       usleep(70000);
+#endif
                        redraw_tile(state, display, game,
                                    gp[0], gp[1], gp[2],
                                    i%5, i/5, w, h);