Game Programming and Development Tools

Could someone evaluate the following DirectX direct access code? – Sturmritter

Sturmritter

Member

Posts: 17
From: San Rafael, CA, USA
Registered: 06-13-2002
Hi all,

Was just wondering if anyone can evaluate where I am failing in my approach to directly accessing the surface in a DirectDraw Surface.

Here is my code, Please assume that the Surface has been properly locked before calling the DrawLine function.

WORD GetNumberOfBits(DWORD dwMask) // used to determine bits in bitmask
{
WORD wBits = 0;
while( dwMask )
{
dwMask = dwMask & ( dwMask - 1 );
wBits++;
}
return wBits;
}

WORD GetMaskPos(DWORD dwMask) // used to get mask position in pixel
{
WORD wPos = 0;
while( !(dwMask & (1 << wPos)) ) wPos++;
return wPos;
}


void DrawLine(LPVOID *SurfPtr, long x1, long y1, long x2, long y2, DWORD Color)
{
LPDDSURFACEDESC2 SurfDesc;
WORD NumRBits, NumGBits, NumBBits, RPos, GPos, BPos;
if((x1 == x2) && (y1 == y2)) return; // fail if only a pixel draw
long Pitch = Surface->GetPitch();
long BPP = Surface->GetBPP();
DWORD Pixel, SurfOff;
// Determine Color masks
long Bits = Surface->GetBPP();
Surface->GetInterface(&DDS7);
DDS7->GetSurfaceDesc(SurfDesc);
// Calculate Number of bits per mask
NumRBits=GetNumberOfBits(SurfDesc->ddpfPixelFormat.dwRBitMask);
NumGBits=GetNumberOfBits(SurfDesc->ddpfPixelFormat.dwGBitMask);
NumBBits=GetNumberOfBits(SurfDesc->ddpfPixelFormat.dwBBitMask);
RPos=GetMaskPos(SurfDesc->ddpfPixelFormat.dwRBitMask);
GPos=GetMaskPos(SurfDesc->ddpfPixelFormat.dwGBitMask);
BPos=GetMaskPos(SurfDesc->ddpfPixelFormat.dwBBitMask);
// Calculate mask positions
BYTE NewRed = GetRValue(Color) >> (8 - NumRBits);
BYTE NewGreen = GetGValue(Color) >> (8 - NumGBits);
BYTE NewBlue = GetBValue(Color) >> (8 - NumBBits);
// Mask off red bits and store NewRed to the DWORD (steps 2-4):
if(BPP != 8)
{
Pixel = (Pixel & ~SurfDesc->ddpfPixelFormat.dwRBitMask) |
(NewRed << RPos);
}
else
{
Pixel = (Pixel & ~SurfDesc->ddpfPixelFormat.dwRBitMask) << (8 - NumRBits);
}
// Mask off green bits and store NewGreen:
if(BPP != 8)
{
Pixel = (Pixel & ~SurfDesc->ddpfPixelFormat.dwGBitMask) |
(NewGreen << GPos);
}
else
{
Pixel = (Pixel & ~SurfDesc->ddpfPixelFormat.dwGBitMask) << (8 - NumGBits);
}

// Mask off blue bits and store NewBlue:
if(BPP != 8)
{
Pixel = (Pixel &~SurfDesc->ddpfPixelFormat.dwBBitMask) |
(NewBlue << BPos);
}
else
{
Pixel = (Pixel & ~SurfDesc->ddpfPixelFormat.dwBBitMask) << (8 - NumBBits);
}
// Get line draw variables set up
int offx, offy, deltax, deltay, linereset;
deltay = abs(y2 - y1);
if (y1 > y2)
{
offy = - 1;
}
else offy = 1;
deltax = abs(x2 - x1);
if (x1 > x2)
{
offx = -1;
}
else offx = 1;
linereset = 0;
// Draw Stage
if (deltax > deltay)
{
int length = deltax + 1;
for(int i = 0; i < length; i++)
{
// locate the Screen pixel and write back to screen buffer
SurfOff = y1*Pitch + x1*BPP;
Pixel = *((LPDWORD)((DWORD)SurfPtr + SurfOff));
// Move to next pixel
x1 += offx;
linereset += deltay;
if(linereset > deltax) // check if a y-direction move needed
{
linereset -= deltax;
y1 += offy;
}
}
}
else
{
int length = deltay + 1;
for(int i = 0; i < length; i++)
{
// locate the Screen pixel and write back to screen buffer
SurfOff = y1*Pitch + x1*BPP;
Pixel = *((LPDWORD)((DWORD)SurfPtr + SurfOff));
// Move to next pixel
x1 += offx;
linereset += deltay;
if(linereset > deltax) // check if a y-direction move needed
{
linereset -= deltax;
y1 += offy;
}
}
}
return;
}

------------------

Thanks in advance!

YBIC,
Sturmritter

quote:
...in the immortal words of Socrates who said, "I drank WHAT?!?!?"

[This message has been edited by Sturmritter (edited July 10, 2002).]