]> icculus.org git repositories - dana/openbox.git/blob - otk/surface.cc
store the pixel32 data in the surface so it can be reused
[dana/openbox.git] / otk / surface.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "surface.hh"
6 #include "display.hh"
7 #include "screeninfo.hh"
8 #include "rendercolor.hh"
9
10 extern "C" {
11 #include <X11/Xutil.h>
12 #include <cstring>
13 }
14
15 namespace otk {
16
17 Surface::Surface(int screen, const Size &size)
18   : _screen(screen),
19     _size(size),
20     _pixel_data(new pixel32[size.width()*size.height()]),
21     _pixmap(None),
22     _xftdraw(0)
23 {
24 }
25
26 Surface::~Surface()
27 {
28   destroyObjects();
29   delete [] _pixel_data;
30 }
31
32 void Surface::setPixmap(const RenderColor &color)
33 {
34   if (_pixmap == None)
35     createObjects();
36
37   XFillRectangle(**display, _pixmap, color.gc(), 0, 0,
38                  _size.width(), _size.height());
39
40   pixel32 val = 0; // XXX set this from the color and shift amounts!
41   for (unsigned int i = 0, s = _size.width() * _size.height(); i < s; ++i) {
42     unsigned char *p = (unsigned char*)&_pixel_data[i];
43     *p = (unsigned char) (val >> 24);
44     *++p = (unsigned char) (val >> 16);
45     *++p = (unsigned char) (val >> 8);
46     *++p = (unsigned char) val;
47   }
48 }
49
50 void Surface::setPixmap(XImage *image)
51 {
52   assert(image->width == _size.width());
53   assert(image->height == _size.height());
54   
55   if (_pixmap == None)
56     createObjects();
57
58   XPutImage(**display, _pixmap, DefaultGC(**display, _screen),
59             image, 0, 0, 0, 0, _size.width(), _size.height());
60 }
61
62 void Surface::createObjects()
63 {
64   assert(_pixmap == None); assert(!_xftdraw);
65
66   const ScreenInfo *info = display->screenInfo(_screen);
67   
68   _pixmap = XCreatePixmap(**display, info->rootWindow(),
69                           _size.width(), _size.height(), info->depth());
70   assert(_pixmap != None);
71     
72   _xftdraw = XftDrawCreate(**display, _pixmap,
73                            info->visual(), info->colormap());
74   assert(_xftdraw);
75 }
76
77 void Surface::destroyObjects()
78 {
79   if (_xftdraw) {
80     XftDrawDestroy(_xftdraw);
81     _xftdraw = 0;
82   }
83
84   if (_pixmap != None) {
85     XFreePixmap(**display, _pixmap);
86     _pixmap = None;
87   }
88 }
89
90 }