Handmade Hero»Forums»Code
Mike Nimmo
2 posts
Easy optimisation to speed up the bitmap rendering
Edited by Mike Nimmo on Reason: Fixing the typo in the code block (thanks mmozeiko)
As I'm also following on a laptop I sped up the bitmap rendering trivially by immediately rejecting pixels with zero alpha and not doing any alpha blending calculations for pixels with full alpha.

i.e something like;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
if ( alpha > 0 )
{
    if ( alpha == 255 )
    {
        *destinationPixel = *sourcePixel;
    }
    else
    {
        // Do full alpha blending calculations here
    }
}
++destinationPixel;
++sourcePixel;


Hope this helps someone
Mike
Mārtiņš Možeiko
2559 posts / 2 projects
Easy optimisation to speed up the bitmap rendering
I hope you don't have "if (alpha=255)" in your code and its a typo, because that will make all bitmaps opaque (but it will be faster of course). Basically use compare "alpha==255" not assign "alpha=255" in if condition.
Mike Nimmo
2 posts
Easy optimisation to speed up the bitmap rendering
mmozeiko: You are right, that was a typo in my post.
Corrected the example now.

Thanks
Jari Komppa
41 posts / 1 project
Programmer, designer, writer, a lot of other things; http://iki.fi/sol
Easy optimisation to speed up the bitmap rendering
Some people are so afraid of bugs of that kind, they write the comparisons in reverse order (which will cause the compiler to give an error).

1
if (255 = alpha)


..Personally I feel that's bending backwards a bit too much. Modern compilers will typically give you a warning if you do an assignment where a comparison is expected.