Sign in to follow this  

SDL color blending?

This topic is 4746 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would I blit an image so that, say, it appears more green/red/blue than it really is? For instance, I want to blit an image and have it blended with rgb (255,0,0). Is there any built in support for this type of thing?

Share this post


Link to post
Share on other sites
Just create a new surface, and make the surface red/green/blue and then set the alpha, then blit this surface over the surface you want more red/green/blue. . .


#include <SDL/SDL.h>

int main(int argc, char * argv[])
{
SDL_Surface * MainScreen = SDL_SetVideoMode(800, 600, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_Surface * Image = SDL_LoadBMP("smile.bmp");
SDL_Surface * Cover = SDL_CreateRGBSurface(Image->flags, Image->w, Image->h, Image->format->BitsPerPixel, Image->format->Rmask, Image->format->Bmask, Image->format->Gmask, Image->format->Amask);
int Red = 0;
int Green = 0;
int Blue = 100;
SDL_FillRect(Cover, NULL, SDL_MapRGB(Cover->format, Red, Green, Blue));
int Opacity = 127; // 0 being totally transparent and 255 being totally solid
SDL_SetAlpha(Cover, SDL_SRCALPHA, Opacity);

bool Done(false);
SDL_Event Event;

while(!Done)
{
while(SDL_PollEvent(&Event))
{
switch(Event.type)
{
case SDL_QUIT:
Done = true;
break;
}
}

SDL_BlitSurface(Image, NULL, MainScreen, NULL);
SDL_BlitSurface(Cover, NULL, MainScreen, NULL);

SDL_Flip(MainScreen);
SDL_FillRect(MainScreen, NULL, SDL_MapRGB(MainScreen->format, 0, 0, 0));
}
SDL_FreeSurface(Cover);
SDL_FreeSurface(Image);
SDL_FreeSurface(MainScreen);
SDL_Quit();
return 0;
}



[Edited by - ImperfectFreak on December 15, 2004 11:09:45 PM]

Share this post


Link to post
Share on other sites
If you're not using OpenGL, you can do it using palettes. Make sure the bitmap you load is 8-bit. (You can use irfanview to convert images)

After you load the surface, do NOT convert it to the display format.

Create an array - SDL_Color oldcolors[255]
Copy the palette from surface->format->palette->colors to the 'oldcolors' array.


Now you need to make a function like so:

void SetColors(SDL_Surface* surf,Uint8 red, Uint8 green, Uint8 blue, SDLColor* oldcolors)
{

Here you do this for every color:

surface->format->palette->colors[i].r = oldcolors.r + red;
surface->format->palette->colors[i].g = oldcolors.r + green;
surface->format->palette->colors[i].b = oldcolors.r + blue;

You have to add checks to make sure the value doesn't exceed 255.


Finally you call:

SDL_SetColors(surf,surface->format->palette->colors,0,256);
}

Now, whenever you want to change colors, call
SetColors(surface,255,0,0,oldcolors)

Share this post


Link to post
Share on other sites

This topic is 4746 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this