Hi all
i have converted some functions of shocky 's lib for software drawing to C/C++ , also i have added it to my OgreMagic library , so i posted it here in the hope that it will be usefull for some one.
void DOT(int x,int y,unsigned int color)
{
if(x>0 && x<Xres && y>0 && y<Yres-1)
{
Buffer[ x + y * Xres ] = color;
}
}
void EDGE(int x1,int y1,int x2,int y2,unsigned int color)
{
if(!Buffer) FireError(3);
double xdistance,ydistance;
int h2;
double StartX,StartY,XRatio,YRatio;
xdistance = x2 - x1;
ydistance = y2 - y1;
h2 = sqrt( xdistance * xdistance + ydistance * ydistance );
StartX = x1;
StartY = y1;
XRatio = xdistance * ( 1.0 / h2 );
YRatio = ydistance * ( 1.0 / h2 );
for(int i = 0; i<h2; i++)
{
if(StartX>0 && StartX<Xres && StartY>0 && StartY<Yres-1)
{
Buffer[ int(StartX) + (int(StartY) * Xres ) ] = color;
}
StartX = StartX + XRatio;
StartY = StartY + YRatio;
}
}
void Curve(int x0, int y0,int x3,int y3,int x1,int y1,int x2,int y2,unsigned int color)
{
int x,y,cx,bx,ax,cy,by,ay,ox,oy;
cx = 3*(x1-x0);
bx = 3*(x2-x1)-cx;
ax = x3-x0-cx-bx;
cy=3*(y1-y0);
by=3*(y2-y1)-cy;
ay=y3-y0-cy-by;
for(double t=0; t<1.01; t+=0.01)
{
if (t>0)
{
ox=x;
oy=y;
}
x = ax * pow(t,3) + bx * pow(t,2) + cx * t + x0;
y = ay * pow(t,3) + by * pow(t,2) + cy * t + y0;
if (t>0)
EDGE(x,y,ox,oy,color);
}
}
void Rect(int X1,int Y1,int XW,int YH,unsigned int color)
{
int slice,LP,TC;
if (X1 < 0)
{
XW = XW + X1;
X1 = 0;
}
if (Y1 < 0)
{
YH = YH + Y1;
Y1=0;
}
if (XW + X1 > Xres) XW = Xres - X1;
if (YH + Y1 > Yres) YH = (Yres - Y1);
if (XW>0 && YH>0) TC = color;
for (LP =Y1; LP<(YH+Y1); LP++)
{
int* pp=(int*)&Buffer[(DA_rowSkip*LP)+X1];
slice = XW;
if (slice>0)
for(register int k=0; k<slice; k++) pp[k]=TC;
}
}
void Circle(int cx,int cy,int r,unsigned int color)
{
int r2,cc,loopy,ww,l,clipl , clipr,slice,tc;
r2=r*r;
cc=-r;
for(int loopy = cc; loopy<r; loopy++)
{
ww = sqrt(r2-loopy*loopy);
if(loopy+cy>=0 && loopy+cy<Yres)
{
clipl = cx-ww;
clipr = (cx+ww)-1;
if(clipl<0) clipl=0;
if(clipr>Xres-1) clipr = Xres-1;
int* pp=(int*)&Buffer[DA_rowSkip*(loopy+cy)+clipl];
slice = clipr-clipl;
if (slice>0)
for(register int k=0; k<slice; k++) pp[k]=color;
}
}
}
void Triangle(int X1,int Y1,int X2,int Y2,int X3,int Y3,unsigned int color)
{
int TEMPX,TEMPY;
int PX[4],PY[4],TFLAG=0;
int IL1,IL2,SLICE;
PX[1]= X1;
PX[2]= X2;
PX[3]= X3;
PY[1]= Y1;
PY[2]= Y2;
PY[3]= Y3;
for(int L0=1; L0<=2; L0++)
for(int LI=1; LI<=2; LI++)
if (PY[LI+1] <= PY[LI])
{
TEMPX = PX[LI]; TEMPY = PY[LI];
PX[LI] = PX[LI+1];
PY[LI] = PY[LI+1];
PX[LI+1] = TEMPX;
PY[LI+1] = TEMPY;
}
/// BOOT OUT INVISIBLE TRIANGLES!
if(PX[1]<0 && PX[2]<0 && PX[3]< 0) TFLAG=1;
if(PX[1]>Xres && PX[2]>Xres && PX[3]>Xres) TFLAG=1;
if(PY[1]>Yres && PY[2]>Yres && PY[3]>Yres) TFLAG=1;
double XP1,XP2; /// SCREEN POSITIONS.
double XI1,XI2; /// INTERPOLATIONS.
/***
'*** REGULAR TRIANGLE (Y1<Y2 Y2<Y3)
'***/
if(PY[1]<PY[2] && PY[2]<PY[3] || (PY[2] = PY[3]))
{
TFLAG=1;
XP1 = PX[1];
XP2 = PX[1];
XI1 = (PX[1]-PX[2]) / (PY[2] - PY[1]);
XI2 = (PX[1]-PX[3]) / (PY[3] - PY[1]);
for(int L0=PY[1]; L0<PY[2]; L0++)
{
if(L0>=0 && L0<Yres)
{
if(XP1<=XP2)
{
IL1=XP1;
IL2=XP2;
}
else
{
IL1=XP2;
IL2=XP1;
}
if(IL2>Xres) IL2=Xres;
if(IL1<0) IL1=0;
SLICE = IL2-IL1;
int* pp=(int*)&Buffer[IL1+(L0*DA_rowSkip)];
if (SLICE>0)
for(register int k=0; k<SLICE; k++) pp[k]=color;
}
XP1=XP1-XI1;
XP2=XP2-XI2;
}
/* this part crashed so i comment it
XI1 = (PX[2]-PX[3]) / (PY[3] - PY[2]);
XP1 = PX[2];
for(int L0=PY[2]; L0<=PY[3]; L0++)
{
if(L0>=0 && L0<Yres)
{
if(XP1<=XP2)
{
IL1=XP1;
IL2=XP2;
}
else
{
IL1=XP2;
IL2=XP1;
}
if(IL2>Xres) IL2=Xres;
if(IL1<0) IL1=0;
SLICE = IL2-IL1;
int* pp=(int*)&Buffer[IL1+(L0*DA_rowSkip)];
if (SLICE>0)
for(register int k=0; k<SLICE; k++) pp[k]=color;
}
XP1=XP1-XI1;
XP2=XP2-XI2;
}
*/
}
/***
'*** FLAT TOPPED TRIANGLE Y1=Y2
'***/
if(TFLAG==0 && PY[1]== PY[2])
{
TFLAG=1;
XP1 = PX[1];
XP2 = PX[2];
XI1 = (PX[1]-PX[3]) / (PY[3] - PY[1]);
XI2 = (PX[2]-PX[3]) / (PY[3] - PY[2]);
for(int L0=PY[1]; L0<PY[3]; L0++)
{
if(L0>=0 && L0<Yres)
{
if(XP1<=XP2)
{
IL1=XP1;
IL2=XP2;
}
else
{
IL1=XP2;
IL2=XP1;
}
if(IL2>Xres) IL2=Xres;
if(IL1<0) IL1=0;
SLICE = IL2-IL1;
int* pp=(int*)&Buffer[IL1+(L0*DA_rowSkip)];
if (SLICE>0)
for(register int k=0; k<SLICE; k++) pp[k]=color;
}
XP1=XP1-XI1;
XP2=XP2-XI2;
}
}
}



