2008-05-01 23:50:14 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
2008-05-01 23:58:05 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-05-01 23:50:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common/util.h"
|
|
|
|
#include "graphics/surface.h"
|
|
|
|
#include "graphics/VectorRenderer.h"
|
|
|
|
#include "graphics/colormasks.h"
|
|
|
|
|
|
|
|
namespace Graphics {
|
|
|
|
|
2008-05-01 23:58:05 +00:00
|
|
|
VectorRenderer *createRenderer() {
|
2008-05-01 23:50:14 +00:00
|
|
|
return new VectorRendererAA<uint16,ColorMasks<565>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename PixelType, typename PixelFormat>
|
|
|
|
void VectorRendererSpec<PixelType,PixelFormat>::
|
2008-05-02 07:50:23 +00:00
|
|
|
drawLineAlg(int x1, int x2, int y1, int y2, int dx, int dy) {
|
2008-05-01 23:50:14 +00:00
|
|
|
PixelType *ptr = (PixelType *)_activeSurface->getBasePtr(x1, y1);
|
|
|
|
int pitch = surfacePitch();
|
2008-05-02 07:50:23 +00:00
|
|
|
int xdir = (x2 > x1) ? 1 : -1;
|
2008-05-01 23:50:14 +00:00
|
|
|
|
|
|
|
*ptr = (PixelType)_color;
|
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
if (dx > dy) {
|
2008-05-01 23:50:14 +00:00
|
|
|
int ddy = dy * 2;
|
2008-05-02 07:50:23 +00:00
|
|
|
int dysub = ddy - (dx * 2);
|
2008-05-01 23:50:14 +00:00
|
|
|
int error_term = ddy - dx;
|
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
while (dx--) {
|
|
|
|
if (error_term >= 0) {
|
2008-05-01 23:50:14 +00:00
|
|
|
ptr += pitch;
|
|
|
|
error_term += dysub;
|
|
|
|
} else {
|
|
|
|
error_term += ddy;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr += xdir;
|
|
|
|
*ptr = (PixelType)_color;
|
|
|
|
}
|
2008-05-01 23:58:05 +00:00
|
|
|
} else {
|
2008-05-01 23:50:14 +00:00
|
|
|
int ddx = dx * 2;
|
2008-05-02 07:50:23 +00:00
|
|
|
int dxsub = ddx - (dy * 2);
|
2008-05-01 23:50:14 +00:00
|
|
|
int error_term = ddx - dy;
|
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
while (dy--) {
|
|
|
|
if (error_term >= 0) {
|
2008-05-01 23:50:14 +00:00
|
|
|
ptr += xdir;
|
|
|
|
error_term += dxsub;
|
|
|
|
} else {
|
|
|
|
error_term += ddx;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr += pitch;
|
|
|
|
*ptr = (PixelType)_color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = (PixelType *)_activeSurface->getBasePtr(x2, y2);
|
|
|
|
*ptr = (PixelType)_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename PixelType, typename PixelFormat>
|
|
|
|
void VectorRendererAA<PixelType,PixelFormat>::
|
2008-05-02 07:50:23 +00:00
|
|
|
drawLineAlg(int x1, int x2, int y1, int y2, int dx, int dy)
|
2008-05-01 23:50:14 +00:00
|
|
|
{
|
|
|
|
PixelType *ptr = (PixelType *)_activeSurface->getBasePtr(x1, y1);
|
|
|
|
int pitch = surfacePitch();
|
2008-05-02 07:50:23 +00:00
|
|
|
int xdir = (x2 > x1) ? 1 : -1;
|
2008-05-01 23:50:14 +00:00
|
|
|
|
|
|
|
int error_line, error_tmp, weight;
|
|
|
|
int error_total = 0;
|
|
|
|
uint8 line_r, line_g, line_b;
|
|
|
|
uint8 bg_r, bg_g, bg_b;
|
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
colorToRGB<PixelFormat>(_color, line_r, line_g, line_b);
|
2008-05-01 23:50:14 +00:00
|
|
|
|
|
|
|
uint line_lum = (line_r >> 2) + (line_g >> 1) + (line_b >> 3);
|
|
|
|
uint bg_lum;
|
|
|
|
|
|
|
|
// first pixel, should be perfectly accurate so no fading out
|
|
|
|
*ptr = (PixelType)_color;
|
|
|
|
|
2008-05-01 23:58:05 +00:00
|
|
|
#define __WULINE_PUTPIXEL( pixel_ptr ) { \
|
2008-05-02 07:50:23 +00:00
|
|
|
colorToRGB<PixelFormat>((PixelType)*(pixel_ptr), bg_r, bg_g, bg_b); \
|
2008-05-01 23:50:14 +00:00
|
|
|
bg_lum = (bg_r >> 2) + (bg_g >> 1) + (bg_b >> 3); \
|
2008-05-02 07:50:23 +00:00
|
|
|
weight = (line_lum < bg_lum) ? error_total >> 8 : (error_total >> 8)^0xFF; \
|
2008-05-01 23:50:14 +00:00
|
|
|
*(pixel_ptr) = RGBToColor<PixelFormat>( \
|
2008-05-02 07:50:23 +00:00
|
|
|
antialiasingBlendWeight(line_r, bg_r, weight), \
|
|
|
|
antialiasingBlendWeight(line_g, bg_g, weight), \
|
|
|
|
antialiasingBlendWeight(line_b, bg_b, weight)); \
|
2008-05-01 23:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw from top to bottom while fading out.
|
|
|
|
// optimized for mostly vertical lines
|
2008-05-02 07:50:23 +00:00
|
|
|
if (dy > dx) {
|
|
|
|
error_line = (dx << 16) / dy;
|
|
|
|
while (--dy) {
|
2008-05-01 23:50:14 +00:00
|
|
|
error_tmp = error_total;
|
|
|
|
error_total += error_line;
|
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
if (error_total <= error_tmp)
|
2008-05-01 23:50:14 +00:00
|
|
|
ptr += xdir; // move right or left
|
|
|
|
|
|
|
|
ptr += pitch; // move down
|
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
__WULINE_PUTPIXEL(ptr);
|
|
|
|
__WULINE_PUTPIXEL(ptr + xdir);
|
2008-05-01 23:50:14 +00:00
|
|
|
}
|
|
|
|
} else { // optimized for mostly horizontal lines
|
2008-05-02 07:50:23 +00:00
|
|
|
error_line = (dy << 16) / dx;
|
|
|
|
while (--dx) {
|
2008-05-01 23:50:14 +00:00
|
|
|
error_tmp = error_total;
|
|
|
|
error_total += error_line;
|
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
if (error_total <= error_tmp)
|
2008-05-01 23:50:14 +00:00
|
|
|
ptr += pitch; // move down
|
|
|
|
|
|
|
|
ptr += xdir; // move left or right
|
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
__WULINE_PUTPIXEL(ptr);
|
|
|
|
__WULINE_PUTPIXEL(ptr + pitch);
|
2008-05-01 23:50:14 +00:00
|
|
|
}
|
|
|
|
} // end of line direction cases
|
|
|
|
|
|
|
|
// last pixel, also perfectly accurate.
|
|
|
|
ptr = (PixelType *)_activeSurface->getBasePtr(x2, y2);
|
|
|
|
*ptr = (PixelType)_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename PixelType, typename PixelFormat>
|
|
|
|
void VectorRendererSpec<PixelType,PixelFormat>::
|
2008-05-02 07:50:23 +00:00
|
|
|
drawLine(int x1, int x2, int y1, int y2) {
|
2008-05-01 23:50:14 +00:00
|
|
|
// we draw from top to bottom
|
2008-05-02 07:50:23 +00:00
|
|
|
if (y2 < y1) {
|
|
|
|
SWAP(x1, x2);
|
|
|
|
SWAP(y1, y2);
|
2008-05-01 23:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dx = ABS(x2 - x1);
|
|
|
|
int dy = ABS(y2 - y1);
|
|
|
|
|
|
|
|
// this is a point, not a line. stoopid.
|
2008-05-02 07:50:23 +00:00
|
|
|
if (dy == 0 && dx == 0)
|
2008-05-01 23:50:14 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
PixelType *ptr = (PixelType *)_activeSurface->getBasePtr(x1, y1);
|
|
|
|
int pitch = surfacePitch();
|
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
if (dy == 0) { // horizontal lines
|
2008-05-01 23:50:14 +00:00
|
|
|
// these can be filled really fast with a single memset.
|
|
|
|
// TODO: Platform specific ASM in set_to, would make this thing fly
|
|
|
|
Common::set_to(ptr, ptr + dx + 1, (PixelType)_color);
|
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
} else if (dx == 0) { // vertical lines
|
2008-05-01 23:50:14 +00:00
|
|
|
// these ones use a static pitch increase.
|
|
|
|
while (y1++ <= y2) {
|
|
|
|
*ptr = (PixelType)_color;
|
|
|
|
ptr += pitch;
|
|
|
|
}
|
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
} else if (ABS(dx) == ABS(dy)) { // diagonal lines
|
2008-05-01 23:50:14 +00:00
|
|
|
// these ones also use a fixed pitch increase
|
2008-05-02 07:50:23 +00:00
|
|
|
pitch += (x2 > x1) ? 1 : -1;
|
2008-05-01 23:50:14 +00:00
|
|
|
|
2008-05-02 07:50:23 +00:00
|
|
|
while (dy--) {
|
2008-05-01 23:50:14 +00:00
|
|
|
*ptr = (PixelType)_color;
|
|
|
|
ptr += pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { // generic lines, use the standard algorithm...
|
2008-05-02 07:50:23 +00:00
|
|
|
drawLineAlg(x1, x2, y1, y2, dx, dy);
|
2008-05-01 23:50:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-01 23:58:05 +00:00
|
|
|
} // end of namespace Graphics
|