Changed and hopefully fixed support for DirectColor with support for gammaramps.
authorBob Pendleton
Fri, 09 Jan 2009 20:39:33 +0000
changeset 3009546c022a9ae5
parent 3008 786a48f8309c
child 3010 a6694a812119
Changed and hopefully fixed support for DirectColor with support for gammaramps.
src/video/x11/SDL_x11window.c
     1.1 --- a/src/video/x11/SDL_x11window.c	Fri Jan 09 15:41:45 2009 +0000
     1.2 +++ b/src/video/x11/SDL_x11window.c	Fri Jan 09 20:39:33 2009 +0000
     1.3 @@ -28,6 +28,7 @@
     1.4  
     1.5  #include "SDL_x11video.h"
     1.6  #include "SDL_x11mouse.h"
     1.7 +#include "SDL_x11gamma.h"
     1.8  #include "../Xext/extensions/StdCmap.h"
     1.9  
    1.10  static void
    1.11 @@ -213,6 +214,7 @@
    1.12      xattr.border_pixel = 0;
    1.13  
    1.14      if (visual->class == PseudoColor) {
    1.15 +/*         printf("asking for PseudoColor\n"); */
    1.16          int nmaps;
    1.17          XStandardColormap cmap;
    1.18          XStandardColormap *stdmaps;
    1.19 @@ -293,18 +295,116 @@
    1.20                                                    displaydata->screen),
    1.21                                         visual, AllocAll);
    1.22              XStoreColors(data->display, colormap, colorcells, ncolors);
    1.23 -            SDL_free(colorcells);
    1.24  
    1.25              xattr.colormap = colormap;
    1.26              X11_TrackColormap(data->display, displaydata->screen, colormap,
    1.27 -                              &cmap, visual);
    1.28 +                              visual, colorcells);
    1.29 +            SDL_free(colorcells);
    1.30          }
    1.31      } else if (visual->class == DirectColor) {
    1.32 -        /* FIXME: Allocate a read-write colormap for gamma fading someday */
    1.33 -        xattr.colormap =
    1.34 -            XCreateColormap(data->display,
    1.35 -                            RootWindow(data->display, displaydata->screen),
    1.36 -                            visual, AllocNone);
    1.37 +        Status status;
    1.38 +        XStandardColormap cmap;
    1.39 +        XColor *colorcells;
    1.40 +        Colormap colormap;
    1.41 +        int i;
    1.42 +        int ncolors;
    1.43 +        int rmax, gmax, bmax;
    1.44 +        int rmask, gmask, bmask;
    1.45 +        int rshift, gshift, bshift;
    1.46 +
    1.47 +        /* Is the colormap we need already registered in SDL? */
    1.48 +        if (colormap =
    1.49 +            X11_LookupColormap(data->display,
    1.50 +                               displaydata->screen, visual->visualid)) {
    1.51 +            xattr.colormap = colormap;
    1.52 +/*             printf("found existing colormap\n"); */
    1.53 +        } else {
    1.54 +            /* The colormap is not known to SDL so we will create it */
    1.55 +            colormap = XCreateColormap(data->display,
    1.56 +                                       RootWindow(data->display,
    1.57 +                                                  displaydata->screen),
    1.58 +                                       visual, AllocAll);
    1.59 +/*             printf("colormap = %x\n", colormap); */
    1.60 +
    1.61 +            /* If we can't create a colormap, then we must die */
    1.62 +            if (!colormap) {
    1.63 +                SDL_SetError
    1.64 +                    ("Couldn't create window: Could not create wriatable colormap");
    1.65 +                return -1;
    1.66 +            }
    1.67 +
    1.68 +            /* OK, we got a colormap, now fill it in as best as we can */
    1.69 +
    1.70 +            colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
    1.71 +            if (NULL == colorcells) {
    1.72 +                SDL_SetError("out of memory in X11_CreateWindow");
    1.73 +                return -1;
    1.74 +            }
    1.75 +            ncolors = visual->map_entries;
    1.76 +            rmax = 0xffff;
    1.77 +            gmax = 0xffff;
    1.78 +            bmax = 0xffff;
    1.79 +
    1.80 +            rshift = 0;
    1.81 +            rmask = visual->red_mask;
    1.82 +            while (0 == (rmask & 1)) {
    1.83 +                rshift++;
    1.84 +                rmask >>= 1;
    1.85 +            }
    1.86 +
    1.87 +/*             printf("rmask = %4x rshift = %4d\n", rmask, rshift); */
    1.88 +
    1.89 +            gshift = 0;
    1.90 +            gmask = visual->green_mask;
    1.91 +            while (0 == (gmask & 1)) {
    1.92 +                gshift++;
    1.93 +                gmask >>= 1;
    1.94 +            }
    1.95 +
    1.96 +/*             printf("gmask = %4x gshift = %4d\n", gmask, gshift); */
    1.97 +
    1.98 +            bshift = 0;
    1.99 +            bmask = visual->blue_mask;
   1.100 +            while (0 == (bmask & 1)) {
   1.101 +                bshift++;
   1.102 +                bmask >>= 1;
   1.103 +            }
   1.104 +
   1.105 +/*             printf("bmask = %4x bshift = %4d\n", bmask, bshift); */
   1.106 +
   1.107 +            /* build the color table pixel values */
   1.108 +            for (i = 0; i < ncolors; i++) {
   1.109 +                Uint32 red = (rmax * i) / (ncolors - 1);
   1.110 +                Uint32 green = (gmax * i) / (ncolors - 1);
   1.111 +                Uint32 blue = (bmax * i) / (ncolors - 1);
   1.112 +
   1.113 +                Uint32 rbits = (rmask * i) / (ncolors - 1);
   1.114 +                Uint32 gbits = (gmask * i) / (ncolors - 1);
   1.115 +                Uint32 bbits = (bmask * i) / (ncolors - 1);
   1.116 +
   1.117 +                Uint32 pix =
   1.118 +                    (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
   1.119 +
   1.120 +                colorcells[i].pixel = pix;
   1.121 +
   1.122 +                colorcells[i].red = red;
   1.123 +                colorcells[i].green = green;
   1.124 +                colorcells[i].blue = blue;
   1.125 +
   1.126 +                colorcells[i].flags = DoRed | DoGreen | DoBlue;
   1.127 +/* 		printf("%2d:%4x [%4x %4x %4x]\n", i, pix, red, green, blue); */
   1.128 +
   1.129 +            }
   1.130 +
   1.131 +            status =
   1.132 +                XStoreColors(data->display, colormap, colorcells, ncolors);
   1.133 +
   1.134 +            xattr.colormap = colormap;
   1.135 +            X11_TrackColormap(data->display, displaydata->screen,
   1.136 +                              colormap, visual, colorcells);
   1.137 +
   1.138 +            SDL_free(colorcells);
   1.139 +        }
   1.140      } else {
   1.141          xattr.colormap =
   1.142              XCreateColormap(data->display,