Results 1 to 11 of 11

Thread: C++: 2D Alpha Blend Q:

Threaded View

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    C++: 2D Alpha Blend Q:

    Alright I wrote a very basic AlphaBlending technique using Dx7 Surfaces and some Win32API: GetPixel/SetPixel.

    Check it out:
    PHP Code:
    //-----------------
    //Alpha Blend -- DC Basic
    //-----------------
    void DDSurface7::AlphaBlendDC(LPDIRECTDRAWSURFACE7 &DestinationUSINT AlphaValue)
    {
        
    COLORREF SrcRGBDestRGB;
        
    BYTE r,g,b;
        
    HDC hdcDestination=0hdcAlpha=0;

        while (
    AlphaValue 255AlphaValue -= 255;

        
    Destination->GetDC(&hdcDestination);
        
    m_AlphaSurface->GetDC(&hdcAlpha);

        for (
    USINT y 0m_Heighty++)
        {
            for (
    USINT x 0m_Widthx++)
            {
                
    SrcRGB GetDCPixel(x,y);
                
    DestRGB GetPixel(hdcDestinationx,y);

                
    = (AlphaValue * ( GetRValue(SrcRGB) + 256 GetRValue(DestRGB) )) / 256 GetRValue(DestRGB) - AlphaValue;
                
    = (AlphaValue * ( GetGValue(SrcRGB) + 256 GetGValue(DestRGB) )) / 256 GetGValue(DestRGB) - AlphaValue;
                
    = (AlphaValue * ( GetBValue(SrcRGB) + 256 GetBValue(DestRGB) )) / 256 GetBValue(DestRGB) - AlphaValue;
                
                
    SetPixelhdcAlpha,x,y,RGB(r,g,b) );
            }
        }

        
    Destination->ReleaseDC(hdcDestination);
        
    m_AlphaSurface->ReleaseDC(hdcAlpha);

    All I did was get that formula for calculating the finalpixel value...from one of ElectroMans posts on the subject.

    Now what this does essentially is:
    Loops through row by row.
    Gets the pixel from the surface in the class and the destination surface passed.
    Calculates the proper alpha value.
    Sets the pixel in a new surface which is then bltfast to the screen later.


    My problem is:
    I know 2D Alpha is naturally slow, but jeez, doing it every frame takes at least 10 seconds a frame on my comp for a picture the size of the screen.
    This is super slow...
    It works...works fine. I get a nice alpha blend for translucency.

    All I really need it for is screen fading...Nothing more.
    Last edited by Electroman; Dec 8th, 2004 at 07:53 PM. Reason: Added Language to title.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width