Results 1 to 11 of 11

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

  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

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: 2D Alpha Blend Q:

    If you want it faster why not use D3D. And use TnL vertices to create your light halos?

  3. #3

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

    Re: 2D Alpha Blend Q:

    light halo's???

    I was pondering using D3D...but it is so much easier to simply modify a surface to have it slowly fade to black.

    It is also intensly slow.

    I plan on using D3D Alpha for everything else of course...But fading in or out the screen seems easiest with 2D...
    "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

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: 2D Alpha Blend Q:

    Pretty easy to do fading in D3D too (with alpha blending), here is even an example.

    http://www.gametutorials.com/Tutoria...irectX_Pg2.htm

  5. #5
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: 2D Alpha Blend Q:

    He's using DirectX7 and those are DirectX9 examples in www.gametutorials.com . I'm not sure how difficult it would be to convert them.

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: 2D Alpha Blend Q:

    Ohhhhh.......well that might be a problem...wouldn't be to difficult if he used DX8 tho'...but D3D has big changes between DX7 and DX8..

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: 2D Alpha Blend Q:

    Ohhh...but TnL vertices was in DX7 too......so that technique can still be applied...and the best thing is that the coordinates for TnL is in screen coordinates, so that makes them perfect for 2D apps...

  8. #8
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: 2D Alpha Blend Q:

    Is this the untitled thread?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  9. #9
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: 2D Alpha Blend Q:

    Quote Originally Posted by Tec-Nico
    Is this the untitled thread?

    No...the Culling one is...but it got back it's title after I PIIIIIPed the forum...

    ØØ

  10. #10

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

    Re: 2D Alpha Blend Q:

    The allusive untitled thread.
    So where did this thread go???
    "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

  11. #11
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: 2D Alpha Blend Q:

    Quote Originally Posted by Halsafar
    The allusive untitled thread.
    So where did this thread go???
    It was to do with the tooltips that give a preview of the thread. For some reason the begining of your thread interferred with the HTML Coments used for the Tooltip and ended up commenting out most of the page . FF only as well.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

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