]> icculus.org git repositories - mikachu/openbox.git/blob - otk/truerendercontrol.cc
provide RenderControls to all otk from the display class. initialize them all there...
[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
12 extern "C" {
13 #ifdef    HAVE_STDLIB_H
14 #  include <stdlib.h>
15 #endif // HAVE_STDLIB_H
16
17 #include "gettext.h"
18 #define _(str) gettext(str)
19 }
20
21 namespace otk {
22
23 TrueRenderControl::TrueRenderControl(int screen)
24   : RenderControl(screen),
25     _red_offset(0),
26     _green_offset(0),
27     _blue_offset(0)
28 {
29   printf("Initializing TrueColor RenderControl\n");
30
31   Visual *visual = display->screenInfo(_screen)->visual();
32   unsigned long red_mask, green_mask, blue_mask;
33
34   // find the offsets for each color in the visual's masks
35   red_mask = visual->red_mask;
36   green_mask = visual->green_mask;
37   blue_mask = visual->blue_mask;
38
39   while (! (red_mask & 1))   { _red_offset++;   red_mask   >>= 1; }
40   while (! (green_mask & 1)) { _green_offset++; green_mask >>= 1; }
41   while (! (blue_mask & 1))  { _blue_offset++;  blue_mask  >>= 1; }
42
43   _red_shift = _green_shift = _blue_shift = 8;
44   while (red_mask)   { red_mask   >>= 1; _red_shift--;   }
45   while (green_mask) { green_mask >>= 1; _green_shift--; }
46   while (blue_mask)  { blue_mask  >>= 1; _blue_shift--;  }
47 }
48
49 TrueRenderControl::~TrueRenderControl()
50 {
51   printf("Destroying TrueColor RenderControl\n");
52
53
54 }
55
56
57 static inline void renderPixel(XImage *im, unsigned char *dp,
58                                unsigned long pixel)
59 {
60   unsigned int bpp = im->bits_per_pixel + (im->byte_order == MSBFirst ? 1 : 0);
61
62   switch (bpp) {
63   case  8: //  8bpp
64     *dp++ = pixel;
65     break;
66   case 16: // 16bpp LSB
67     *dp++ = pixel;
68     *dp++ = pixel >> 8;
69     break;
70   case 17: // 16bpp MSB
71     *dp++ = pixel >> 8;
72     *dp++ = pixel;
73     break;
74   case 24: // 24bpp LSB
75     *dp++ = pixel;
76     *dp++ = pixel >> 8;
77     *dp++ = pixel >> 16;
78     break;
79   case 25: // 24bpp MSB
80     *dp++ = pixel >> 16;
81     *dp++ = pixel >> 8;
82     *dp++ = pixel;
83     break;
84   case 32: // 32bpp LSB
85     *dp++ = pixel;
86     *dp++ = pixel >> 8;
87     *dp++ = pixel >> 16;
88     *dp++ = pixel >> 24;
89     break;
90   case 33: // 32bpp MSB
91     *dp++ = pixel >> 24;
92     *dp++ = pixel >> 16;
93     *dp++ = pixel >> 8;
94     *dp++ = pixel;
95     break;
96   default:
97     assert(false); // wtf?
98   }
99 }
100
101 void TrueRenderControl::drawBackground(Surface *sf,
102                                        const RenderTexture &texture) const
103 {
104   assert(sf);
105   
106   int w = sf->width(), h = sf->height();
107   XImage *im = sf->_im;
108   Pixmap pm = sf->_pm;
109   assert(im); assert(pm != None);
110
111   unsigned char *data = new unsigned char[im->bytes_per_line * h];
112   unsigned char *dp = data;
113   unsigned int bytes_per_pixel = im->bits_per_pixel/8;
114
115   for (int y = 0; y < h/3; ++y)
116     for (int x = 0; x < w; ++x, dp += bytes_per_pixel)
117       renderPixel(im, dp, (255*x/w) >> _red_shift << _red_offset);
118   for (int y = 0; y < h/3; ++y)
119     for (int x = 0; x < w; ++x, dp += bytes_per_pixel)
120       renderPixel(im, dp, (255*x/w) >> _green_shift << _green_offset);
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) >> _blue_shift << _blue_offset);
124
125   im->data = (char*) data;
126   
127   XPutImage(**display, pm, DefaultGC(**display, _screen),
128             sf->_im, 0, 0, 0, 0, w, h);
129 }
130
131 }