Skip to main content
gamedev.net
🔒 Locked

Blending Function

Started by Mullvalen 1 reply 1.1k views
Original Post
Mullvalen
Mullvalen
Hi, earlier, i had some problems with my sprite rendering function but when i made it work, this new problem popped up. There is something wrong with the colors. The image i load is an TIFF image and i use the SDL_Image library to load it. Uint32 fh_renderer::a_blend( unsigned int bg, unsigned int fg) { int bg_r = (bg >> 0) & 0xff; int bg_g = (bg >> 8) & 0xff; int bg_b = (bg >> 16) & 0xff; int fg_r = (fg >> 0) & 0xff; int fg_g = (fg >> 8) & 0xff; int fg_b = (fg >> 16) & 0xff; float bg_a = ( ( bg >> 24 ) & 0xff ) / 255.0; float fg_a = ( ( fg >> 24 ) & 0xff ) / 255.0; bg_a = 1 - ( 1 - fg_a ) * ( 1 - bg_a ); bg_r = bg_r + ( fg_a * ( fg_r - bg_r ) ); bg_g = bg_g + ( fg_a * ( fg_g - bg_g ) ); bg_b = bg_b + ( fg_a * ( fg_b - bg_b ) ); int int_bg_a = bg_a * 255; return (bg_r << 0) | (bg_g << 8) | (bg_b << 16) | (int_bg_a << 24); } http://xs64.xs.to/pics/06033/Corrupt_colors.png.xs.jpg If i switched bg_r with bg_g it rendered in the right way. int bg_b = (bg >> 0) & 0xff; int bg_g = (bg >> 8) & 0xff; int bg_r = (bg >> 16) & 0xff; But that doesnt feel logical, does it? At least not to me... SDL_BlitSurface rendered it in the right way so it cant be that it loaded the image in a wrong way...
My blog about personal development for studying people - www.tricktolearn.com
sgaffga
sgaffga
Hi!

I am not familiar with the TIFF-Format, but I assume that the order of the
color components is "RGBA" - if this is correct, then your shifts seem to have
an error.

int bg_r = (bg >> 0) & 0xff;
int bg_g = (bg >> 8) & 0xff;
int bg_b = (bg >> 16) & 0xff;

You get the Alpha component in bg_r and the Blue in bg_b and the Red in bg_b...

Try changing (all of the occurencies in your code) to :

alpha = (color >> 0) & 0xff;
blue = (color >> 8) & 0xff;
green = (color >> 16) & 0xff;
red = (color >> 24) & 0xff;

Does it work now?

Greetings
Stefan
Mullvalen
Mullvalen
Nope, it didnt work. I really think the colours are configured right as they are ...
My blog about personal development for studying people - www.tricktolearn.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.