]> icculus.org git repositories - mikachu/openbox.git/blob - otk/truerendercontrol.cc
drawSolidBackground seems to work :)
[mikachu/openbox.git] / otk / truerendercontrol.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef    HAVE_CONFIG_H
4 #  include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 #include "truerendercontrol.hh"
8 #include "display.hh"
9 #include "screeninfo.hh"
10 #include "surface.hh"
11 #include "rendertexture.hh"
12
13 extern "C" {
14 #ifdef    HAVE_STDLIB_H
15 #  include <stdlib.h>
16 #endif // HAVE_STDLIB_H
17
18 #include "gettext.h"
19 #define _(str) gettext(str)
20 }
21
22 namespace otk {
23
24 TrueRenderControl::TrueRenderControl(int screen)
25   : RenderControl(screen),
26     _red_offset(0),
27     _green_offset(0),
28     _blue_offset(0)
29 {
30   printf("Initializing TrueColor RenderControl\n");
31
32   Visual *visual = display->screenInfo(_screen)->visual();
33   unsigned long red_mask, green_mask, blue_mask;
34
35   // find the offsets for each color in the visual's masks
36   red_mask = visual->red_mask;
37   green_mask = visual->green_mask;
38   blue_mask = visual->blue_mask;
39
40   while (! (red_mask & 1))   { _red_offset++;   red_mask   >>= 1; }
41   while (! (green_mask & 1)) { _green_offset++; green_mask >>= 1; }
42   while (! (blue_mask & 1))  { _blue_offset++;  blue_mask  >>= 1; }
43
44   _red_shift = _green_shift = _blue_shift = 8;
45   while (red_mask)   { red_mask   >>= 1; _red_shift--;   }
46   while (green_mask) { green_mask >>= 1; _green_shift--; }
47   while (blue_mask)  { blue_mask  >>= 1; _blue_shift--;  }
48 }
49
50 TrueRenderControl::~TrueRenderControl()
51 {
52   printf("Destroying TrueColor RenderControl\n");
53
54
55 }
56
57
58 static inline void renderPixel(XImage *im, unsigned char *dp,
59                                unsigned long pixel)
60 {
61   unsigned int bpp = im->bits_per_pixel + (im->byte_order == MSBFirst ? 1 : 0);
62
63   switch (bpp) {
64   case  8: //  8bpp
65     *dp++ = pixel;
66     break;
67   case 16: // 16bpp LSB
68     *dp++ = pixel;
69     *dp++ = pixel >> 8;
70     break;
71   case 17: // 16bpp MSB
72     *dp++ = pixel >> 8;
73     *dp++ = pixel;
74     break;
75   case 24: // 24bpp LSB
76     *dp++ = pixel;
77     *dp++ = pixel >> 8;
78     *dp++ = pixel >> 16;
79     break;
80   case 25: // 24bpp MSB
81     *dp++ = pixel >> 16;
82     *dp++ = pixel >> 8;
83     *dp++ = pixel;
84     break;
85   case 32: // 32bpp LSB
86     *dp++ = pixel;
87     *dp++ = pixel >> 8;
88     *dp++ = pixel >> 16;
89     *dp++ = pixel >> 24;
90     break;
91   case 33: // 32bpp MSB
92     *dp++ = pixel >> 24;
93     *dp++ = pixel >> 16;
94     *dp++ = pixel >> 8;
95     *dp++ = pixel;
96     break;
97   default:
98     assert(false); // wtf?
99   }
100 }
101
102 void TrueRenderControl::drawBackground(Surface& sf,
103                                        const RenderTexture &texture) const
104 {
105   assert(_screen == sf._screen);
106   assert(_screen == texture.color().screen());
107
108   if (texture.gradient() == RenderTexture::Solid) {
109     drawSolidBackground(sf, texture);
110   } else {
111     int w = sf.width(), h = sf.height();
112
113     const ScreenInfo *info = display->screenInfo(_screen);
114     XImage *im = XCreateImage(**display, info->visual(), info->depth(),
115                               ZPixmap, 0, NULL, w, h, 32, 0);
116   
117     unsigned char *data = new unsigned char[im->bytes_per_line * h];
118     unsigned char *dp = data;
119     unsigned int bytes_per_pixel = im->bits_per_pixel/8;
120
121     for (int y = 0; y < h/3; ++y)
122       for (int x = 0; x < w; ++x, dp += bytes_per_pixel)
123         renderPixel(im, dp, (255*x/w) >> _red_shift << _red_offset);
124     for (int y = 0; y < h/3; ++y)
125       for (int x = 0; x < w; ++x, dp += bytes_per_pixel)
126         renderPixel(im, dp, (255*x/w) >> _green_shift << _green_offset);
127     for (int y = 0; y < h/3; ++y)
128       for (int x = 0; x < w; ++x, dp += bytes_per_pixel)
129         renderPixel(im, dp, (255*x/w) >> _blue_shift << _blue_offset);
130
131     im->data = (char*) data;
132
133 //  sf.setPixmap(im);
134     sf.setPixmap(texture.color());
135 //  sf.setPixmap(RenderColor(_screen, 0xff, 0xff, 0));
136
137     delete [] im->data;
138     im->data = NULL;
139     XDestroyImage(im);
140   }
141 }
142
143 }