It's for DirectX8 and it only works for 32bit color.
But maybe you can use it. But you have to remove or replace some of the variables with your own (such as m_iScreenWidth and my specific buffer names).
Oh yeah and it uses goto...HAHA..
PHP Code:
void LineDraw(int x1, int y1, int x2, int y2, int r, int g, int b)
{
LPDIRECT3DSURFACE8 pBackBuffer;
D3DLOCKED_RECT D3DLockedRect;
HRESULT hr;
int *pSurfaceData;
int *p;
int Color32 = 0x0000000; //ARGB
if (m_iBitsPerPixel == 32)
{
Color32 |= b;
Color32 |= (g <<8);
Color32 |= (r <<16);
}
else
return;
int x, y;
int dx, dy;
int incx, incy;
int balance;
if ( (x1<0) || (x1<=m_iScreenWidth) || (y1<0) || (y1>=m_iScreenHeight) || (x2 >= m_iScreenWidth) || (x2 < 0) || (y2<0) || (y2 >= m_iScreenHeight) )
goto LINEDRAWWITHCHECK;
//lock backbuffer
m_pd3dDevice->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
hr = pBackBuffer->LockRect(&D3DLockedRect, NULL, D3DLOCK_NO_DIRTY_UPDATE);
if (hr != D3D_OK)
{
pLog->Log("Gfx::LineDraw - unable to lock backbuffer");
return;
}
pSurfaceData = (int*)D3DLockedRect.pBits;
if (x2 >= x1)
{
dx = x2 - x1;
incx = 1;
}
else
{
dx = x1 - x2;
incx = -1;
}
if (y2 >= y1)
{
dy = y2 - y1;
incy = 1;
}
else
{
dy = y1 - y2;
incy = -1;
}
x = x1;
y = y1;
if (dx >= dy)
{
dy <<= 1;
balance = dy - dx;
dx <<= 1;
while (x != x2)
{
//plotpixel
p = pSurfaceData;
p += (y * (D3DLockedRect.Pitch/4)) + x;
*p = Color32;
if (balance >= 0)
{
y += incy;
balance -= dx;
}
balance += dy;
x += incx;
}
//plotpixel
p = pSurfaceData;
p += (y * (D3DLockedRect.Pitch/4)) + x;
*p = Color32;
}
else
{
dx <<= 1;
balance = dx - dy;
dy <<= 1;
while (y != y2)
{
//plotpixel
p = pSurfaceData;
p += (y * (D3DLockedRect.Pitch/4)) + x;
*p = Color32;
if (balance >= 0)
{
x += incx;
balance -= dy;
}
balance += dx;
y += incy;
}
//plotpixel
p = pSurfaceData;
p += (y * (D3DLockedRect.Pitch/4)) + x;
*p = Color32;
}
//unlock backbuffer
hr = pBackBuffer->UnlockRect();
if (hr != D3D_OK)
{
pLog->Log("Gfx::Unable to unlock backbuffer. ");
}
return;
LINEDRAWWITHCHECK:
//lock backbuffer
m_pd3dDevice->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
hr = pBackBuffer->LockRect(&D3DLockedRect, NULL, D3DLOCK_NO_DIRTY_UPDATE);
if (hr != D3D_OK)
return;
pSurfaceData = (int*)D3DLockedRect.pBits;
if (x2 >= x1)
{
dx = x2 - x1;
incx = 1;
}
else
{
dx = x1 - x2;
incx = -1;
}
if (y2 >= y1)
{
dy = y2 - y1;
incy = 1;
}
else
{
dy = y1 - y2;
incy = -1;
}
x = x1;
y = y1;
if (dx >= dy)
{
dy <<= 1;
balance = dy - dx;
dx <<= 1;
while (x != x2)
{
//plotpixel
if ( (x >=0) && (x <m_iScreenWidth) && (y >=0) && (y < m_iScreenHeight) )
{
p = pSurfaceData;
p += (y * (D3DLockedRect.Pitch/4)) + x;
*p = Color32;
}
if (balance >= 0)
{
y += incy;
balance -= dx;
}
balance += dy;
x += incx;
}
//plotpixel
if ( (x >=0) && (x <m_iScreenWidth) && (y >=0) && (y < m_iScreenHeight) )
{
p = pSurfaceData;
p += (y * (D3DLockedRect.Pitch/4)) + x;
*p = Color32;
}
}
else
{
dx <<= 1;
balance = dx - dy;
dy <<= 1;
while (y != y2)
{
//plotpixel
if ( (x >=0) && (x <m_iScreenWidth) && (y >=0) && (y < m_iScreenHeight) )
{
p = pSurfaceData;
p += (y * (D3DLockedRect.Pitch/4)) + x;
*p = Color32;
}
if (balance >= 0)
{
x += incx;
balance -= dy;
}
balance += dx;
y += incy;
}
//plotpixel
if ( (x >=0) && (x <m_iScreenWidth) && (y >=0) && (y < m_iScreenHeight) )
{
p = pSurfaceData;
p += (y * (D3DLockedRect.Pitch/4)) + x;
*p = Color32;
}
}
//unlock backbuffer
hr = pBackBuffer->UnlockRect();
if (hr != D3D_OK)
pLog->Log("Gfx::LineDraw() - Unable to unlock backbuffer.");
}