make all the rendering code in RenderControl. use true/pseudo RenderControl classes just for reducing the pixel32 data to the appropriate bitdepth.

This commit is contained in:
Dana Jansens 2003-02-12 00:27:32 +00:00
parent f30b2a8908
commit 72ff846dbb
6 changed files with 299 additions and 328 deletions

View file

@ -30,19 +30,7 @@ PseudoRenderControl::~PseudoRenderControl()
printf("Destroying PseudoColor RenderControl\n");
}
void PseudoRenderControl::drawBackground(Surface& sf,
const RenderTexture &texture) const
{
assert(_screen == sf._screen);
assert(_screen == texture.color().screen());
// in psuedo color, gradients aren't even worth while! just draw a solid!
//if (texture.gradient() == RenderTexture::Solid) {
drawSolidBackground(sf, texture);
}
void PseudoRenderControl::drawImage(Surface &sf, int w, int h,
unsigned long *data) const
void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const
{
}

View file

@ -4,32 +4,18 @@
#include "rendercontrol.hh"
extern "C" {
#ifdef HAVE_STDINT_H
# include <stdint.h>
#else
# ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
# endif
#endif
}
#include <vector>
namespace otk {
class PseudoRenderControl : public RenderControl {
private:
// add some vars!!!
virtual void reduceDepth(Surface &sf, XImage *im) const;
public:
PseudoRenderControl(int screen);
virtual ~PseudoRenderControl();
virtual void drawBackground(Surface& sf, const RenderTexture &texture) const;
virtual void drawImage(Surface &sf, int w, int h,
unsigned long *data) const;
};
}

View file

@ -214,4 +214,285 @@ void RenderControl::drawMask(Surface &sf, const RenderColor &color,
XSetClipOrigin(**display, color.gc(), 0, 0);
}
void RenderControl::drawGradientBackground(
Surface &sf, const RenderTexture &texture) const
{
unsigned int r,g,b;
int w = sf.size().width(), h = sf.size().height();
int off, x;
const ScreenInfo *info = display->screenInfo(_screen);
XImage *im = XCreateImage(**display, info->visual(), info->depth(),
ZPixmap, 0, NULL, w, h, 32, 0);
im->byte_order = endian;
switch (texture.gradient()) {
case RenderTexture::Vertical:
verticalGradient(sf, texture);
break;
case RenderTexture::Diagonal:
diagonalGradient(sf, texture);
break;
case RenderTexture::CrossDiagonal:
crossDiagonalGradient(sf, texture);
break;
default:
printf("unhandled gradient\n");
}
pixel32 *data = sf.pixelData();
pixel32 current;
if (texture.relief() == RenderTexture::Flat && texture.border()) {
r = texture.borderColor().red();
g = texture.borderColor().green();
b = texture.borderColor().blue();
current = (r << default_red_shift)
+ (g << default_green_shift)
+ (b << default_blue_shift);
for (off = 0, x = 0; x < w; ++x, off++) {
*(data + off) = current;
*(data + off + ((h-1) * w)) = current;
}
for (off = 0, x = 0; x < h; ++x, off++) {
*(data + (off * w)) = current;
*(data + (off * w) + w - 1) = current;
}
}
if (texture.relief() != RenderTexture::Flat) {
if (texture.bevel() == RenderTexture::Bevel1) {
for (off = 1, x = 1; x < w - 1; ++x, off++)
highlight(data + off,
data + off + (h-1) * w,
texture.relief()==RenderTexture::Raised);
for (off = 0, x = 0; x < h; ++x, off++)
highlight(data + off * w,
data + off * w + w - 1,
texture.relief()==RenderTexture::Raised);
}
if (texture.bevel() == RenderTexture::Bevel2) {
for (off = 2, x = 2; x < w - 2; ++x, off++)
highlight(data + off + w,
data + off + (h-2) * w,
texture.relief()==RenderTexture::Raised);
for (off = 1, x = 1; x < h-1; ++x, off++)
highlight(data + off * w + 1,
data + off * w + w - 2,
texture.relief()==RenderTexture::Raised);
}
}
reduceDepth(sf, im);
im->data = (char*) data;
sf.setPixmap(im);
im->data = NULL;
XDestroyImage(im);
}
void RenderControl::verticalGradient(Surface &sf,
const RenderTexture &texture) const
{
pixel32 *data = sf.pixelData();
pixel32 current;
float dr, dg, db;
unsigned int r,g,b;
int w = sf.size().width(), h = sf.size().height();
dr = (float)(texture.secondary_color().red() - texture.color().red());
dr/= (float)h;
dg = (float)(texture.secondary_color().green() - texture.color().green());
dg/= (float)h;
db = (float)(texture.secondary_color().blue() - texture.color().blue());
db/= (float)h;
for (int y = 0; y < h; ++y) {
r = texture.color().red() + (int)(dr * y);
g = texture.color().green() + (int)(dg * y);
b = texture.color().blue() + (int)(db * y);
current = (r << default_red_shift)
+ (g << default_green_shift)
+ (b << default_blue_shift);
for (int x = 0; x < w; ++x, ++data)
*data = current;
}
}
void RenderControl::diagonalGradient(Surface &sf,
const RenderTexture &texture) const
{
pixel32 *data = sf.pixelData();
pixel32 current;
float drx, dgx, dbx, dry, dgy, dby;
unsigned int r,g,b;
int w = sf.size().width(), h = sf.size().height();
for (int y = 0; y < h; ++y) {
drx = (float)(texture.secondary_color().red() - texture.color().red());
dry = drx/(float)h;
drx/= (float)w;
dgx = (float)(texture.secondary_color().green() - texture.color().green());
dgy = dgx/(float)h;
dgx/= (float)w;
dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
dby = dbx/(float)h;
dbx/= (float)w;
for (int x = 0; x < w; ++x, ++data) {
r = texture.color().red() + ((int)(drx * x) + (int)(dry * y))/2;
g = texture.color().green() + ((int)(dgx * x) + (int)(dgy * y))/2;
b = texture.color().blue() + ((int)(dbx * x) + (int)(dby * y))/2;
current = (r << default_red_shift)
+ (g << default_green_shift)
+ (b << default_blue_shift);
*data = current;
}
}
}
void RenderControl::crossDiagonalGradient(
Surface &sf, const RenderTexture &texture) const
{
pixel32 *data = sf.pixelData();
pixel32 current;
float drx, dgx, dbx, dry, dgy, dby;
unsigned int r,g,b;
int w = sf.size().width(), h = sf.size().height();
for (int y = 0; y < h; ++y) {
drx = (float)(texture.secondary_color().red() - texture.color().red());
dry = drx/(float)h;
drx/= (float)w;
dgx = (float)(texture.secondary_color().green() - texture.color().green());
dgy = dgx/(float)h;
dgx/= (float)w;
dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
dby = dbx/(float)h;
dbx/= (float)w;
for (int x = w; x > 0; --x, ++data) {
r = texture.color().red() + ((int)(drx * (x-1)) + (int)(dry * y))/2;
g = texture.color().green() + ((int)(dgx * (x-1)) + (int)(dgy * y))/2;
b = texture.color().blue() + ((int)(dbx * (x-1)) + (int)(dby * y))/2;
current = (r << default_red_shift)
+ (g << default_green_shift)
+ (b << default_blue_shift);
*data = current;
}
}
}
void RenderControl::highlight(pixel32 *x, pixel32 *y, bool raised) const
{
int r, g, b;
pixel32 *up, *down;
if (raised) {
up = x;
down = y;
} else {
up = y;
down = x;
}
r = (*up >> default_red_shift) & 0xFF;
r += r >> 1;
g = (*up >> default_green_shift) & 0xFF;
g += g >> 1;
b = (*up >> default_blue_shift) & 0xFF;
b += b >> 1;
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
*up = (r << default_red_shift) + (g << default_green_shift)
+ (b << default_blue_shift);
r = (*down >> default_red_shift) & 0xFF;
r = (r >> 1) + (r >> 2);
g = (*down >> default_green_shift) & 0xFF;
g = (g >> 1) + (g >> 2);
b = (*down >> default_blue_shift) & 0xFF;
b = (b >> 1) + (b >> 2);
*down = (r << default_red_shift) + (g << default_green_shift)
+ (b << default_blue_shift);
}
void RenderControl::drawBackground(Surface& sf,
const RenderTexture &texture) const
{
assert(_screen == sf._screen);
assert(_screen == texture.color().screen());
if (texture.gradient() == RenderTexture::Solid)
drawSolidBackground(sf, texture);
else
drawGradientBackground(sf, texture);
}
void RenderControl::drawImage(Surface &sf, int w, int h,
unsigned long *data) const
{
pixel32 *bg = sf.pixelData();
int startx, x, y, c;
unsigned int i, e;
x = (sf.size().width() - w) / 2;
y = (sf.size().height() - h) / 2;
if (x < 0) x = 0;
if (y < 0) y = 0;
// XX SCALING!@!&*(@! to make it fit on the surface
startx = x;
for (i = 0, c = 0, e = w*h; i < e; ++i) {
unsigned char alpha = data[i] >> 24;
unsigned char r = data[i] >> 16;
unsigned char g = data[i] >> 8;
unsigned char b = data[i];
// background color
unsigned char bgr = bg[i] >> default_red_shift;
unsigned char bgg = bg[i] >> default_green_shift;
unsigned char bgb = bg[i] >> default_blue_shift;
r = bgr + (r - bgr) * alpha >> 8;
g = bgg + (g - bgg) * alpha >> 8;
b = bgb + (b - bgb) * alpha >> 8;
bg[i] = (r << default_red_shift) | (g << default_green_shift) |
(b << default_blue_shift);
if (++c >= w) {
++y;
x = startx;
c = 0;
} else
++x;
}
const ScreenInfo *info = display->screenInfo(_screen);
XImage *im = XCreateImage(**display, info->visual(), info->depth(),
ZPixmap, 0, NULL, sf.size().width(),
sf.size().height(), 32, 0);
im->byte_order = endian;
reduceDepth(sf, im);
im->data = (char*) bg;
sf.setPixmap(im);
im->data = NULL;
XDestroyImage(im);
}
}

View file

@ -65,9 +65,17 @@ protected:
RenderControl(int screen);
virtual void reduceDepth(Surface &sf, XImage *im) const = 0;
inline void highlight(pixel32 *x, pixel32 *y, bool raised) const;
void verticalGradient(Surface &sf, const RenderTexture &texture) const;
void diagonalGradient(Surface &sf, const RenderTexture &texture) const;
void crossDiagonalGradient(Surface &sf, const RenderTexture &texture) const;
virtual void drawGradientBackground(Surface &sf,
const RenderTexture &texture) const;
virtual void drawSolidBackground(Surface& sf,
const RenderTexture& texture) const;
public:
virtual ~RenderControl();
@ -80,7 +88,7 @@ public:
This function will overwrite the entire surface.
*/
virtual void drawBackground(Surface &sf,
const RenderTexture &texture) const = 0;
const RenderTexture &texture) const;
//! Draws an image onto the surface
/*!
@ -89,7 +97,7 @@ public:
background will be used for applying the alpha.
*/
virtual void drawImage(Surface &sf, int w, int h,
unsigned long *data) const = 0;
unsigned long *data) const;
//! Draws a string onto a Surface
virtual void drawString(Surface &sf, const Font &font, int x, int y,

View file

@ -53,182 +53,6 @@ TrueRenderControl::~TrueRenderControl()
printf("Destroying TrueColor RenderControl\n");
}
void TrueRenderControl::drawGradientBackground(
Surface &sf, const RenderTexture &texture) const
{
unsigned int r,g,b;
int w = sf.size().width(), h = sf.size().height();
int off, x;
const ScreenInfo *info = display->screenInfo(_screen);
XImage *im = XCreateImage(**display, info->visual(), info->depth(),
ZPixmap, 0, NULL, w, h, 32, 0);
im->byte_order = endian;
switch (texture.gradient()) {
case RenderTexture::Vertical:
verticalGradient(sf, texture);
break;
case RenderTexture::Diagonal:
diagonalGradient(sf, texture);
break;
case RenderTexture::CrossDiagonal:
crossDiagonalGradient(sf, texture);
break;
default:
printf("unhandled gradient\n");
}
pixel32 *data = sf.pixelData();
pixel32 current;
if (texture.relief() == RenderTexture::Flat && texture.border()) {
r = texture.borderColor().red();
g = texture.borderColor().green();
b = texture.borderColor().blue();
current = (r << default_red_shift)
+ (g << default_green_shift)
+ (b << default_blue_shift);
for (off = 0, x = 0; x < w; ++x, off++) {
*(data + off) = current;
*(data + off + ((h-1) * w)) = current;
}
for (off = 0, x = 0; x < h; ++x, off++) {
*(data + (off * w)) = current;
*(data + (off * w) + w - 1) = current;
}
}
if (texture.relief() != RenderTexture::Flat) {
if (texture.bevel() == RenderTexture::Bevel1) {
for (off = 1, x = 1; x < w - 1; ++x, off++)
highlight(data + off,
data + off + (h-1) * w,
texture.relief()==RenderTexture::Raised);
for (off = 0, x = 0; x < h; ++x, off++)
highlight(data + off * w,
data + off * w + w - 1,
texture.relief()==RenderTexture::Raised);
}
if (texture.bevel() == RenderTexture::Bevel2) {
for (off = 2, x = 2; x < w - 2; ++x, off++)
highlight(data + off + w,
data + off + (h-2) * w,
texture.relief()==RenderTexture::Raised);
for (off = 1, x = 1; x < h-1; ++x, off++)
highlight(data + off * w + 1,
data + off * w + w - 2,
texture.relief()==RenderTexture::Raised);
}
}
reduceDepth(sf, im);
im->data = (char*) data;
sf.setPixmap(im);
im->data = NULL;
XDestroyImage(im);
}
void TrueRenderControl::verticalGradient(Surface &sf,
const RenderTexture &texture) const
{
pixel32 *data = sf.pixelData();
pixel32 current;
float dr, dg, db;
unsigned int r,g,b;
int w = sf.size().width(), h = sf.size().height();
dr = (float)(texture.secondary_color().red() - texture.color().red());
dr/= (float)h;
dg = (float)(texture.secondary_color().green() - texture.color().green());
dg/= (float)h;
db = (float)(texture.secondary_color().blue() - texture.color().blue());
db/= (float)h;
for (int y = 0; y < h; ++y) {
r = texture.color().red() + (int)(dr * y);
g = texture.color().green() + (int)(dg * y);
b = texture.color().blue() + (int)(db * y);
current = (r << default_red_shift)
+ (g << default_green_shift)
+ (b << default_blue_shift);
for (int x = 0; x < w; ++x, ++data)
*data = current;
}
}
void TrueRenderControl::diagonalGradient(Surface &sf,
const RenderTexture &texture) const
{
pixel32 *data = sf.pixelData();
pixel32 current;
float drx, dgx, dbx, dry, dgy, dby;
unsigned int r,g,b;
int w = sf.size().width(), h = sf.size().height();
for (int y = 0; y < h; ++y) {
drx = (float)(texture.secondary_color().red() - texture.color().red());
dry = drx/(float)h;
drx/= (float)w;
dgx = (float)(texture.secondary_color().green() - texture.color().green());
dgy = dgx/(float)h;
dgx/= (float)w;
dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
dby = dbx/(float)h;
dbx/= (float)w;
for (int x = 0; x < w; ++x, ++data) {
r = texture.color().red() + ((int)(drx * x) + (int)(dry * y))/2;
g = texture.color().green() + ((int)(dgx * x) + (int)(dgy * y))/2;
b = texture.color().blue() + ((int)(dbx * x) + (int)(dby * y))/2;
current = (r << default_red_shift)
+ (g << default_green_shift)
+ (b << default_blue_shift);
*data = current;
}
}
}
void TrueRenderControl::crossDiagonalGradient(
Surface &sf, const RenderTexture &texture) const
{
pixel32 *data = sf.pixelData();
pixel32 current;
float drx, dgx, dbx, dry, dgy, dby;
unsigned int r,g,b;
int w = sf.size().width(), h = sf.size().height();
for (int y = 0; y < h; ++y) {
drx = (float)(texture.secondary_color().red() - texture.color().red());
dry = drx/(float)h;
drx/= (float)w;
dgx = (float)(texture.secondary_color().green() - texture.color().green());
dgy = dgx/(float)h;
dgx/= (float)w;
dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
dby = dbx/(float)h;
dbx/= (float)w;
for (int x = w; x > 0; --x, ++data) {
r = texture.color().red() + ((int)(drx * (x-1)) + (int)(dry * y))/2;
g = texture.color().green() + ((int)(dgx * (x-1)) + (int)(dgy * y))/2;
b = texture.color().blue() + ((int)(dbx * (x-1)) + (int)(dby * y))/2;
current = (r << default_red_shift)
+ (g << default_green_shift)
+ (b << default_blue_shift);
*data = current;
}
}
}
void TrueRenderControl::reduceDepth(Surface &sf, XImage *im) const
{
// since pixel32 is the largest possible pixel size, we can share the array
@ -274,109 +98,4 @@ void TrueRenderControl::reduceDepth(Surface &sf, XImage *im) const
}
}
void TrueRenderControl::highlight(pixel32 *x, pixel32 *y, bool raised) const
{
int r, g, b;
pixel32 *up, *down;
if (raised) {
up = x;
down = y;
} else {
up = y;
down = x;
}
r = (*up >> default_red_shift) & 0xFF;
r += r >> 1;
g = (*up >> default_green_shift) & 0xFF;
g += g >> 1;
b = (*up >> default_blue_shift) & 0xFF;
b += b >> 1;
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
*up = (r << default_red_shift) + (g << default_green_shift)
+ (b << default_blue_shift);
r = (*down >> default_red_shift) & 0xFF;
r = (r >> 1) + (r >> 2);
g = (*down >> default_green_shift) & 0xFF;
g = (g >> 1) + (g >> 2);
b = (*down >> default_blue_shift) & 0xFF;
b = (b >> 1) + (b >> 2);
*down = (r << default_red_shift) + (g << default_green_shift)
+ (b << default_blue_shift);
}
void TrueRenderControl::drawBackground(Surface& sf,
const RenderTexture &texture) const
{
assert(_screen == sf._screen);
assert(_screen == texture.color().screen());
if (texture.gradient() == RenderTexture::Solid)
drawSolidBackground(sf, texture);
else
drawGradientBackground(sf, texture);
}
void TrueRenderControl::drawImage(Surface &sf, int w, int h,
unsigned long *data) const
{
pixel32 *bg = sf.pixelData();
int startx, x, y, c;
unsigned int i, e;
x = (sf.size().width() - w) / 2;
y = (sf.size().height() - h) / 2;
if (x < 0) x = 0;
if (y < 0) y = 0;
// XX SCALING!@!&*(@! to make it fit on the surface
startx = x;
for (i = 0, c = 0, e = w*h; i < e; ++i) {
unsigned char alpha = data[i] >> 24;
unsigned char r = data[i] >> 16;
unsigned char g = data[i] >> 8;
unsigned char b = data[i];
// background color
unsigned char bgr = bg[i] >> default_red_shift;
unsigned char bgg = bg[i] >> default_green_shift;
unsigned char bgb = bg[i] >> default_blue_shift;
r = bgr + (r - bgr) * alpha >> 8;
g = bgg + (g - bgg) * alpha >> 8;
b = bgb + (b - bgb) * alpha >> 8;
bg[i] = (r << default_red_shift) | (g << default_green_shift) |
(b << default_blue_shift);
if (++c >= w) {
++y;
x = startx;
c = 0;
} else
++x;
}
const ScreenInfo *info = display->screenInfo(_screen);
XImage *im = XCreateImage(**display, info->visual(), info->depth(),
ZPixmap, 0, NULL, sf.size().width(),
sf.size().height(), 32, 0);
im->byte_order = endian;
reduceDepth(sf, im);
im->data = (char*) bg;
sf.setPixmap(im);
im->data = NULL;
XDestroyImage(im);
}
}

View file

@ -21,22 +21,11 @@ private:
int _green_offset;
int _blue_offset;
inline void highlight(pixel32 *x, pixel32 *y, bool raised) const;
void reduceDepth(Surface &sf, XImage *im) const;
void verticalGradient(Surface &sf, const RenderTexture &texture) const;
void diagonalGradient(Surface &sf, const RenderTexture &texture) const;
void crossDiagonalGradient(Surface &sf, const RenderTexture &texture) const;
virtual void drawGradientBackground(Surface &sf,
const RenderTexture &texture) const;
virtual void reduceDepth(Surface &sf, XImage *im) const;
public:
TrueRenderControl(int screen);
virtual ~TrueRenderControl();
virtual void drawBackground(Surface& sf, const RenderTexture &texture) const;
virtual void drawImage(Surface &sf, int w, int h,
unsigned long *data) const;
};
}