X-Git-Url: https://git.draconx.ca/gitweb/rrace.git/blobdiff_plain/43a709ff25b9973a83327a06a6ecc94b09e7787b..320851f215662265a2ab323c2bc7424e1b6acd33:/src/x11.c diff --git a/src/x11.c b/src/x11.c index 95e0cf0..0a9bef0 100644 --- a/src/x11.c +++ b/src/x11.c @@ -19,10 +19,11 @@ #include #include #include +#include #include #include -#include +#include #include #include #include "motif.h" @@ -30,40 +31,74 @@ /* Size of the traditional icon pixmap (multiple of 3) */ #define ICON_SIZE 48 -/* TODO user-selectable colours */ -static const char * const colours[][3] = { - /*primary bottom top */ - "#8d2e28", "#6a1b17", "#a14842", /* red */ - "#b46e28", "#924a16", "#c7904f", /* orange */ - "#d8b740", "#c59f39", "#e2c65d", /* yellow */ - "#286428", "#194719", "#4e874e", /* green */ - "#003471", "#001f4f", "#00528b", /* blue */ - "#dcdcdc", "#c0c0c0", "#eaeaea", /* white */ +/* 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), XtRPixel, sizeof (String), \ + offsetof(struct { colour_tab t; }, t[index]), \ + XtRString, (def) } + +static XtResource colour_resources[] = { + COLOURRES("colour0", 0, COLOUR0_PRIMARY), + COLOURRES("colour1", 1, COLOUR1_PRIMARY), + COLOURRES("colour2", 2, COLOUR2_PRIMARY), + COLOURRES("colour3", 3, COLOUR3_PRIMARY), + COLOURRES("colour4", 4, COLOUR4_PRIMARY), + COLOURRES("colour5", 5, COLOUR5_PRIMARY), + + COLOURRES("colourDark0", 6, COLOUR0_DARK), + COLOURRES("colourDark1", 7, COLOUR1_DARK), + COLOURRES("colourDark2", 8, COLOUR2_DARK), + COLOURRES("colourDark3", 9, COLOUR3_DARK), + COLOURRES("colourDark4", 10, COLOUR4_DARK), + COLOURRES("colourDark5", 11, COLOUR5_DARK), + + COLOURRES("colourLight0", 12, COLOUR0_LIGHT), + COLOURRES("colourLight1", 13, COLOUR1_LIGHT), + COLOURRES("colourLight2", 14, COLOUR2_LIGHT), + 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, Screen *screen) +static void init_colours(struct app_state *state, Widget shell) { - Display *display = DisplayOfScreen(screen); - Colormap cmap = DefaultColormapOfScreen(screen); - XColor colour, junk; - unsigned i, j; + unsigned i; - for (j = 0; j < COLOUR_MAX; j++) { - for (i = 0; i < TILE_MAX-1; i++) { - XAllocNamedColor(display, cmap, colours[i][j], - &colour, &junk); - state->tile_colour[i][j] = colour.pixel; - } + colour_tab colours; + XtGetApplicationResources(shell, &colours, colour_resources, + XtNumber(colour_resources), NULL, 0); + + for (i = 0; i < NUM_TILE_COLOURS; i++) { + unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1); + + state->tile_colour[tile][shade] = colours[i]; } + + XtVaSetValues(state->game, XtNbackground, colours[COLOUR_UI_GAMEBG], + (char *)NULL); } -void x11_initialize(struct app_state *state, Screen *screen) +void x11_initialize(struct app_state *state, Widget shell) { - Display *display = DisplayOfScreen(screen); + Display *display = XtDisplay(shell); + Screen *screen = XtScreen(shell); Window root = RootWindowOfScreen(screen); XGCValues gcv; - init_colours(state, screen); + init_colours(state, shell); gcv.line_width = 1; state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv); @@ -134,16 +169,34 @@ redraw_goal_tile(struct app_state *state, Display *display, Drawable d, return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h); } -void x11_redraw_goal(struct app_state *state) +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); - for (i = 0; i < 9; i++) - redraw_goal_tile(state, display, window, i%3, i/3, w/3, h/3); + 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) { +#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); + } + + /* + * 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 */ @@ -184,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); @@ -201,21 +254,38 @@ void x11_redraw_icon(struct app_state *state, Widget shell) } } -void x11_redraw_game(struct app_state *state) +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; int i; - XtVaGetValues(state->game, XmNwidth, &w, XmNheight, &h, (char *)NULL); + XtVaGetValues(state->game, XtNwidth, &w, XtNheight, &h, (char *)NULL); w /= 5; h /= 5; - for (i = 0; i < 25; i++) { - uint_least32_t *gp = state->board.game; + 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; + } - redraw_tile(state, display, game, - gp[0], gp[1], gp[2], - i%5, i/5, w, h); + 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); + } + mask >>= 1; } }