2006-03-20 16:43:15 +00:00
|
|
|
/*
|
2014-06-01 17:55:23 +00:00
|
|
|
* Portions of this file are copyright Rebirth contributors and licensed as
|
|
|
|
* described in COPYING.txt.
|
|
|
|
* Portions of this file are copyright Parallax Software and licensed
|
|
|
|
* according to the Parallax license below.
|
|
|
|
* See COPYING.txt for license details.
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
|
|
|
|
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
|
|
|
|
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
|
|
|
|
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
|
|
|
|
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
|
|
|
|
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
|
|
|
|
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
|
|
|
|
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
|
2013-02-24 02:39:48 +00:00
|
|
|
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
|
2006-03-20 16:43:15 +00:00
|
|
|
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
|
|
|
*/
|
2009-11-30 22:42:55 +00:00
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
/*
|
|
|
|
*
|
2009-11-30 22:42:55 +00:00
|
|
|
* Graphical routines for drawing lines.
|
2006-03-20 16:43:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "u_mem.h"
|
|
|
|
#include "gr.h"
|
|
|
|
#include "grdef.h"
|
2012-07-01 02:54:33 +00:00
|
|
|
#include "maths.h"
|
2006-03-20 16:43:15 +00:00
|
|
|
#include "clip.h"
|
|
|
|
#ifdef OGL
|
|
|
|
#include "ogl_init.h"
|
|
|
|
#endif
|
|
|
|
|
2015-12-13 18:00:49 +00:00
|
|
|
namespace dcx {
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Symmetric Double Step Line Algorithm
|
|
|
|
by Brian Wyvill
|
|
|
|
from "Graphics Gems", Academic Press, 1990
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* non-zero flag indicates the pixels needing EXCHG back. */
|
2013-10-27 22:00:14 +00:00
|
|
|
static void plot(int x,int y,int flag)
|
2009-11-30 22:42:55 +00:00
|
|
|
{
|
|
|
|
if (flag)
|
2006-03-20 16:43:15 +00:00
|
|
|
gr_upixel(y, x);
|
|
|
|
else
|
|
|
|
gr_upixel(x, y);
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:00:14 +00:00
|
|
|
static int gr_hline(int x1, int x2, int y)
|
2009-11-30 22:42:55 +00:00
|
|
|
{
|
2015-04-02 02:36:54 +00:00
|
|
|
using std::swap;
|
|
|
|
if (x1 > x2)
|
|
|
|
swap(x1,x2);
|
2014-09-26 02:42:14 +00:00
|
|
|
for (int i=x1; i<=x2; i++ )
|
2006-03-20 16:43:15 +00:00
|
|
|
gr_upixel( i, y );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:00:14 +00:00
|
|
|
static int gr_vline(int y1, int y2, int x)
|
2009-11-30 22:42:55 +00:00
|
|
|
{
|
2015-04-02 02:36:54 +00:00
|
|
|
using std::swap;
|
|
|
|
if (y1 > y2) swap(y1,y2);
|
2014-09-26 02:42:14 +00:00
|
|
|
for (int i=y1; i<=y2; i++ )
|
2006-03-20 16:43:15 +00:00
|
|
|
gr_upixel( x, i );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:00:14 +00:00
|
|
|
static void gr_universal_uline(int a1, int b1, int a2, int b2)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
int dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
|
|
|
|
int x1, y1;
|
2014-09-26 02:42:14 +00:00
|
|
|
int sign_x = 1, sign_y = 1, step, reverse;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (a1==a2) {
|
|
|
|
gr_vline(b1,b2,a1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b1==b2) {
|
|
|
|
gr_hline(a1,a2,b1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dx = a2 - a1;
|
|
|
|
dy = b2 - b1;
|
|
|
|
|
|
|
|
if (dx < 0) {
|
|
|
|
sign_x = -1;
|
|
|
|
dx *= -1;
|
|
|
|
}
|
|
|
|
if (dy < 0) {
|
|
|
|
sign_y = -1;
|
|
|
|
dy *= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decide increment sign by the slope sign */
|
|
|
|
if (sign_x == sign_y)
|
|
|
|
step = 1;
|
|
|
|
else
|
|
|
|
step = -1;
|
|
|
|
|
|
|
|
if (dy > dx) { /* chooses axis of greatest movement (make * dx) */
|
2015-04-02 02:36:54 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(a1, b1);
|
|
|
|
swap(a2, b2);
|
|
|
|
swap(dx, dy);
|
2006-03-20 16:43:15 +00:00
|
|
|
reverse = 1;
|
|
|
|
} else
|
|
|
|
reverse = 0;
|
|
|
|
/* note error check for dx==0 should be included here */
|
|
|
|
if (a1 > a2) { /* start from the smaller coordinate */
|
|
|
|
x = a2;
|
|
|
|
y = b2;
|
|
|
|
x1 = a1;
|
|
|
|
y1 = b1;
|
|
|
|
} else {
|
|
|
|
x = a1;
|
|
|
|
y = b1;
|
|
|
|
x1 = a2;
|
|
|
|
y1 = b2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Note dx=n implies 0 - n or (dx+1) pixels to be set */
|
|
|
|
/* Go round loop dx/4 times then plot last 0,1,2 or 3 pixels */
|
|
|
|
/* In fact (dx-1)/4 as 2 pixels are already plottted */
|
|
|
|
xend = (dx - 1) / 4;
|
|
|
|
pixels_left = (dx - 1) % 4; /* number of pixels left over at the
|
2009-11-30 22:42:55 +00:00
|
|
|
* end */
|
2006-03-20 16:43:15 +00:00
|
|
|
plot(x, y, reverse);
|
|
|
|
plot(x1, y1, reverse); /* plot first two points */
|
|
|
|
incr2 = 4 * dy - 2 * dx;
|
|
|
|
if (incr2 < 0) { /* slope less than 1/2 */
|
|
|
|
c = 2 * dy;
|
|
|
|
incr1 = 2 * c;
|
|
|
|
D = incr1 - dx;
|
|
|
|
|
2015-02-14 22:48:27 +00:00
|
|
|
for (uint_fast32_t i = xend; i--;)
|
|
|
|
{ /* plotting loop */
|
2006-03-20 16:43:15 +00:00
|
|
|
++x;
|
|
|
|
--x1;
|
|
|
|
if (D < 0) {
|
|
|
|
/* pattern 1 forwards */
|
|
|
|
plot(x, y, reverse);
|
|
|
|
plot(++x, y, reverse);
|
|
|
|
/* pattern 1 backwards */
|
|
|
|
plot(x1, y1, reverse);
|
|
|
|
plot(--x1, y1, reverse);
|
|
|
|
D += incr1;
|
|
|
|
} else {
|
|
|
|
if (D < c) {
|
|
|
|
/* pattern 2 forwards */
|
|
|
|
plot(x, y, reverse);
|
|
|
|
plot(++x, y += step, reverse);
|
|
|
|
/* pattern 2 backwards */
|
|
|
|
plot(x1, y1, reverse);
|
|
|
|
plot(--x1, y1 -= step, reverse);
|
|
|
|
} else {
|
|
|
|
/* pattern 3 forwards */
|
|
|
|
plot(x, y += step, reverse);
|
|
|
|
plot(++x, y, reverse);
|
|
|
|
/* pattern 3 backwards */
|
|
|
|
plot(x1, y1 -= step, reverse);
|
|
|
|
plot(--x1, y1, reverse);
|
|
|
|
}
|
|
|
|
D += incr2;
|
|
|
|
}
|
|
|
|
} /* end for */
|
|
|
|
|
|
|
|
/* plot last pattern */
|
|
|
|
if (pixels_left) {
|
|
|
|
if (D < 0) {
|
|
|
|
plot(++x, y, reverse); /* pattern 1 */
|
|
|
|
if (pixels_left > 1)
|
|
|
|
plot(++x, y, reverse);
|
|
|
|
if (pixels_left > 2)
|
|
|
|
plot(--x1, y1, reverse);
|
|
|
|
} else {
|
|
|
|
if (D < c) {
|
|
|
|
plot(++x, y, reverse); /* pattern 2 */
|
|
|
|
if (pixels_left > 1)
|
|
|
|
plot(++x, y += step, reverse);
|
|
|
|
if (pixels_left > 2)
|
|
|
|
plot(--x1, y1, reverse);
|
|
|
|
} else {
|
|
|
|
/* pattern 3 */
|
|
|
|
plot(++x, y += step, reverse);
|
|
|
|
if (pixels_left > 1)
|
|
|
|
plot(++x, y, reverse);
|
|
|
|
if (pixels_left > 2)
|
|
|
|
plot(--x1, y1 -= step, reverse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* end if pixels_left */
|
|
|
|
}
|
|
|
|
/* end slope < 1/2 */
|
|
|
|
else { /* slope greater than 1/2 */
|
|
|
|
c = 2 * (dy - dx);
|
|
|
|
incr1 = 2 * c;
|
|
|
|
D = incr1 + dx;
|
2015-02-14 22:48:27 +00:00
|
|
|
for (uint_fast32_t i = xend; i--;)
|
|
|
|
{
|
2006-03-20 16:43:15 +00:00
|
|
|
++x;
|
|
|
|
--x1;
|
|
|
|
if (D > 0) {
|
|
|
|
/* pattern 4 forwards */
|
|
|
|
plot(x, y += step, reverse);
|
|
|
|
plot(++x, y += step, reverse);
|
|
|
|
/* pattern 4 backwards */
|
|
|
|
plot(x1, y1 -= step, reverse);
|
|
|
|
plot(--x1, y1 -= step, reverse);
|
|
|
|
D += incr1;
|
|
|
|
} else {
|
|
|
|
if (D < c) {
|
|
|
|
/* pattern 2 forwards */
|
|
|
|
plot(x, y, reverse);
|
|
|
|
plot(++x, y += step, reverse);
|
|
|
|
|
|
|
|
/* pattern 2 backwards */
|
|
|
|
plot(x1, y1, reverse);
|
|
|
|
plot(--x1, y1 -= step, reverse);
|
|
|
|
} else {
|
|
|
|
/* pattern 3 forwards */
|
|
|
|
plot(x, y += step, reverse);
|
|
|
|
plot(++x, y, reverse);
|
|
|
|
/* pattern 3 backwards */
|
|
|
|
plot(x1, y1 -= step, reverse);
|
|
|
|
plot(--x1, y1, reverse);
|
|
|
|
}
|
|
|
|
D += incr2;
|
|
|
|
}
|
|
|
|
} /* end for */
|
|
|
|
/* plot last pattern */
|
|
|
|
if (pixels_left) {
|
|
|
|
if (D > 0) {
|
|
|
|
plot(++x, y += step, reverse); /* pattern 4 */
|
|
|
|
if (pixels_left > 1)
|
|
|
|
plot(++x, y += step, reverse);
|
|
|
|
if (pixels_left > 2)
|
|
|
|
plot(--x1, y1 -= step, reverse);
|
|
|
|
} else {
|
|
|
|
if (D < c) {
|
|
|
|
plot(++x, y, reverse); /* pattern 2 */
|
|
|
|
if (pixels_left > 1)
|
|
|
|
plot(++x, y += step, reverse);
|
|
|
|
if (pixels_left > 2)
|
|
|
|
plot(--x1, y1, reverse);
|
|
|
|
} else {
|
|
|
|
/* pattern 3 */
|
|
|
|
plot(++x, y += step, reverse);
|
|
|
|
if (pixels_left > 1)
|
|
|
|
plot(++x, y, reverse);
|
|
|
|
if (pixels_left > 2) {
|
|
|
|
if (D > c) /* step 3 */
|
|
|
|
plot(--x1, y1 -= step, reverse);
|
|
|
|
else /* step 2 */
|
|
|
|
plot(--x1, y1, reverse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//unclipped version just calls clipping version for now
|
2015-04-02 02:36:54 +00:00
|
|
|
void gr_uline(fix _a1, fix _b1, fix _a2, fix _b2)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
int a1,b1,a2,b2;
|
|
|
|
a1 = f2i(_a1); b1 = f2i(_b1); a2 = f2i(_a2); b2 = f2i(_b2);
|
|
|
|
switch(TYPE)
|
|
|
|
{
|
|
|
|
#ifdef OGL
|
2009-11-30 22:42:55 +00:00
|
|
|
case BM_OGL:
|
|
|
|
ogl_ulinec(a1,b1,a2,b2,COLOR);
|
2015-04-02 02:36:54 +00:00
|
|
|
return;
|
2006-03-20 16:43:15 +00:00
|
|
|
#endif
|
|
|
|
case BM_LINEAR:
|
2009-11-30 22:42:55 +00:00
|
|
|
gr_universal_uline( a1,b1,a2,b2);
|
2015-04-02 02:36:54 +00:00
|
|
|
return;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
2015-04-02 02:36:54 +00:00
|
|
|
return;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns 0 if drawn with no clipping, 1 if drawn but clipped, and
|
|
|
|
// 2 if not drawn at all.
|
|
|
|
|
2015-04-02 02:36:54 +00:00
|
|
|
void gr_line(fix a1, fix b1, fix a2, fix b2)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
int x1, y1, x2, y2;
|
|
|
|
x1 = i2f(MINX);
|
|
|
|
y1 = i2f(MINY);
|
|
|
|
x2 = i2f(MAXX);
|
|
|
|
y2 = i2f(MAXY);
|
|
|
|
|
2015-04-02 02:36:54 +00:00
|
|
|
CLIPLINE(a1,b1,a2,b2,x1,y1,x2,y2,return,, FIXSCALE );
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
gr_uline( a1, b1, a2, b2 );
|
|
|
|
}
|
2015-12-05 22:57:23 +00:00
|
|
|
|
|
|
|
}
|