Advertisement

Alpha sprite blitting

Started by October 30, 1999 05:23 AM
10 comments, last by kieren_j 25 years, 3 months ago
Yup! It doesn't work, never has, and I doubt if it ever will (MS have been promising alpha support forever, but it's still not there.... Maybe with DX9231, who knows).

So, it's time to get your hands dirty and code it yourself !

/Niels

<b>/NJ</b>
DirectDraw doesn't support it in Hardware.

It "was" going to be in DX7, but turns out they decided against it.
Like I said, I have the ZIP file of a cool example that shows Sprites as D3D Quads rendered with alpha blending.

If you want it, just email me at:
rbenson@earthlink.net

Keeb


Advertisement
No that's OK.
I have another question - I cant get the dwRotationAngle to work either - in fact it doesnt even blit at all!! I use DDBLT_ROTATIONANGLE or whatever it is, but - no luck.
Anybody got some code?
Another great DirectDraw feature. There's no software emulation for rotating, so if its not supported by your hardware, you're out of luck.

BlueNexus

-BlueNexus--This is my sig. There are many like it, but this one is mine.
I wanted to use that operation as well but like BlueNexus said it's only in Hardware and many of the good cards don't even support it. Better get cozy with Sin() and Cos()

Keeb

DirectX is really a great name... "DirectAccess" - That is exactly all you get: Direct access to the framebuffer and HW features. The Docs spend quite a few pages talking HAL and HEL, just a shame that HEL never showed up ... Which leaves me wondering if you really needed a COM interface to get a pointer to a piece of memory.. Oh well ...

/Niels

<b>/NJ</b>
Advertisement
Can anyone point me to some good reference material for writing my own blitter with alpha channel?

Thanks,
Marcus

Search on gamedev, there was an article - using MMX or something.
The thing is my card -DOES- support rotation, but has anybody got some demo code?
hmmm... i think it is the old alpha problem again...

Ok, guys. Look at http://cust.nol.at/ppee/ for a quick end to this problem. The code is for 16 bit, but if you look closer it will be easy to transport it for 24 or 32.

Bye.

Here I am a nice guy!

// Container structure for RGB masks
typedef struct _RGBMASK
{
ULONG rgbRed;
ULONG rgbGreen;
ULONG rgbBlue;
} RGBMASK;

// Container structure for (5,6,5 or 5,5,5 and masking)
typedef struct _RGB16
{
RGBQUAD depth;
RGBQUAD amount;
RGBQUAD position;
RGBMASK mask;
} RGB16;

extern RGB16 rgb16;

extern int RedMask, GreenMask, BlueMask; // RGB Masks
extern int RedPos, GreenPos, BluePos; // RGB Positions

#define RED(p) (p >> RedPos) // Extracts Red color
#define GREEN(p) ((p & GreenMask) >> GreenPos) // Extracts Green color
#define BLUE(p) (p & BlueMask) // Extracts Blue color
#define RGB16(r, g, b) ((r << RedPos) | (g << GreenPos) | b) // Creates RGB pixel even if card is in 555 or 565

void DDGetPixelFormat()
{
DDSURFACEDESC2 ddsd; // DirectDraw Surface Description
BYTE shiftcount; // Shift Counter

// Get a surface despriction.
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_PIXELFORMAT;
FrontSurf->Surface->GetSurfaceDesc(&ddsd);
// Fill in the masking values for extracting colors.
rgb16.mask.rgbRed = ddsd.ddpfPixelFormat.dwRBitMask;
rgb16.mask.rgbGreen = ddsd.ddpfPixelFormat.dwGBitMask;
rgb16.mask.rgbBlue = ddsd.ddpfPixelFormat.dwBBitMask;

// Get red surface information.
shiftcount = 0;
while(!(ddsd.ddpfPixelFormat.dwRBitMask & 1))
{
ddsd.ddpfPixelFormat.dwRBitMask >>= 1;
shiftcount++;
}
rgb16.depth.rgbRed = (BYTE)ddsd.ddpfPixelFormat.dwRBitMask;
rgb16.position.rgbRed = shiftcount;
rgb16.amount.rgbRed = (ddsd.ddpfPixelFormat.dwRBitMask == 0x1f) ? 3 : 2;

// Get green surface information.
shiftcount = 0;
while(!(ddsd.ddpfPixelFormat.dwGBitMask & 1))
{
ddsd.ddpfPixelFormat.dwGBitMask >>= 1;
shiftcount++;
}
rgb16.depth.rgbGreen =(BYTE)ddsd.ddpfPixelFormat.dwGBitMask;
rgb16.position.rgbGreen = shiftcount;
rgb16.amount.rgbGreen = (ddsd.ddpfPixelFormat.dwGBitMask == 0x1f) ? 3 : 2;

// Get Blue surface information.
shiftcount = 0;
while(!(ddsd.ddpfPixelFormat.dwBBitMask & 1))
{
ddsd.ddpfPixelFormat.dwBBitMask >>= 1;
shiftcount++;
}
rgb16.depth.rgbBlue =(BYTE)ddsd.ddpfPixelFormat.dwBBitMask;
rgb16.position.rgbBlue = shiftcount;
rgb16.amount.rgbBlue = (ddsd.ddpfPixelFormat.dwBBitMask == 0x1f) ? 3 : 2;
// Fill in variables so we dont' have to access the structure anymore.
RedMask = rgb16.mask.rgbRed; // Red Mask
GreenMask = rgb16.mask.rgbGreen; // Green Mask
BlueMask = rgb16.mask.rgbBlue; // Blue Mask
RedPos = rgb16.position.rgbRed; // Red Position
GreenPos = rgb16.position.rgbGreen; // Green Position
BluePos = rgb16.position.rgbBlue; // Blue Position
}

//This will blit the src color you pass to it with the background color at x,y location, you can have 256 levels of alpha. The math is already optimized but doing pixel by pixel is overkill you must find a way to optimize even more
int DDPlotAlphaPixel(int pX, int pY, int pR, int pG, int pB, int pA, G16SURFACEPTR pSurf)
{
int Alpha = pA;

USHORT src = RGB16(pR,pG,pB);
USHORT dest = ((USHORT*)pSurf->Buffer)[pX + (pY*pSurf->Pitch>>1)];

dest = ((RedMask & ((dest & RedMask) + ((int)(((int)(src & RedMask) - (int)(dest & RedMask)) * Alpha) >>8))) |
(GreenMask & ((dest & GreenMask) + ((int)(((int)(src & GreenMask) - (int)(dest & GreenMask)) * Alpha) >>8))) |
(BlueMask & ((dest & BlueMask) + ((int)(((int)(src & BlueMask) - (int)(dest & BlueMask)) * Alpha) >>8))));

((USHORT*)pSurf->Buffer)[pX + (pY*pSurf->Pitch>>1)] = dest;

return(1);
}

// Does additive blending meanning. Good for doing explosions and stuff. What you do is you draw the explosions in you bitmao on a black background. After when you blit you add the background color that you got at x,y location on you screen to the bitmap. So black wich is 0 plus the color at x,y location gives the same color but. lets say the back ground color plus yellow somewhere in you fire will give the mixed color. This wau you wont have a black ora around your sprite explosion when you blit on a non black background.
int DDAddToPixel(int pX, int pY, int pR, int pG, int pB, G16SURFACEPTR pSurf)
{
USHORT tdest = 0;
USHORT t;

USHORT src = ((USHORT*)pSurf->Buffer)[pX + (pY*pSurf->Pitch>>1)];
USHORT dest = RGB16(pR,pG,pB);

t = (src & RedMask) + (dest & RedMask);
if(t > RedMask) t = RedMask;
tdest |= t;

t = (src & GreenMask) + (dest & GreenMask);
if(t > GreenMask) t = GreenMask;
tdest |= t;

t = (src & BlueMask) + (dest & BlueMask);
if(t > BlueMask) t = BlueMask;
tdest |= t;

((USHORT*)pSurf->Buffer)[pX + (pY*pSurf->Pitch>>1)] = tdest;

return(1);
}

Hardcore Until The End.

This topic is closed to new replies.

Advertisement