Sign in to follow this  
Dark_Glitch

SDL: SDL_MapRGB

Recommended Posts

OK.... I've looked up help for this and I can't seem to find any. Maybe I should go back to the basics but I was just wondering about this. I want to write my colors by their values such as R: 255, G: 255, B: 255. According to a tutorial I read, I have to write them in hex format. Can I write the values for colors in decimal format? I was trying to make this color: R 253, G 239, B 0, which happens to be a shade of yellow, a transparent color for this sprite I was testing. It didn't work. Help please.

Share this post


Link to post
Share on other sites
There is absolutely no need to put the numbers in hex format. In C and C++ "0xff" and "255" have the same meaning.

Those color values you've specified should give you yellow, however if you want transparency you'll need to either set the per-surface alpha or change the alpha value for all of the pixels (you can use SDL_MapRGBA). What exactly happens when you try to set that color?

Share this post


Link to post
Share on other sites
Nothing. The image stays the same with the yellow background. Here is the function I used to optimize the images I loaded. I'm a novice, so I tried to edit the function I was given in the tutorial, however, it seems that I'm missing something.



SDL_Surface *load_image( string filename)
{
SDL_Surface* loadedImage = NULL;

SDL_Surface* optimizedImage = NULL;

loadedImage = IMG_Load( filename.c_str() );

if(loadedImage = NULL)
{
optimizedImage = SDL_DisplayFormat(loadedImage);

SDL_FreeSurface(loadedImage);

if(optimizedImage != NULL)
{
//Map the color
Uint32 colorkey = SDL_MapRGBA( optimizedImage->format, 255, 255, 0, 0);

//Set all pixels of color R 255, G 255, B 0, A 0 to be transparent
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey);

}

// Return the optimized image
return optimizedImage;
}

}


Thanks for the response.

EDIT: Forgot to add I change the color to total yellow to see if it would change anything. I edited the sprite as well to make sure I wasn't losing my mind and it still doesn't work. It compiles and all but I get no transparency.

Share this post


Link to post
Share on other sites
Well, I'm not sure that would even work. I tried it, and it didn't. Maybe I didn't do it right or something. Like I said, I am a novice.

Well here's the code. I basically copied the code from Lazy Foo's tutorials but changed the color for the sprite I was testing and it just doesn't work. ALSO, for some reason, when I click the 'X' to exit out of the program, it doesn't work either. I have to use the Task Manager to force the program to shut down.

It's all messed up.


// Color Keying (Making transparent colors in an image)


#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>

using namespace std;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface *background = NULL;
SDL_Surface *sprite = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;

SDL_Surface *load_image( string filename)
{
SDL_Surface* loadedImage = NULL;

SDL_Surface* optimizedImage = NULL;

loadedImage = IMG_Load( filename.c_str() );

if(loadedImage != NULL)
{
//Map the color
optimizedImage = SDL_DisplayFormat( loadedImage );

SDL_FreeSurface( loadedImage );

if( optimizedImage != NULL )
{

Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 255, 255, 0);

//Set all pixels of color R 255, G 255, B 0 to be transparent
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey);

}

// Return the optimized image
return optimizedImage;
}

}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;

//Give the offsets to the rectangle
offset.x = x;
offset.y = y;

//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}

bool init()
{ //Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}

//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
IMG_SetError("SDL_SetVideoMode() Failed!");
SDL_Delay(2000);
return false;
}

//Set the window caption
SDL_WM_SetCaption( "Event test", NULL );

//If everything initialized fine
return true;
}

bool load_files()
{
//Load the image
background = load_image("background.png");

//If there was an error in loading...
if(background == NULL )
{
IMG_SetError(" The file background.png is nowhere to be found! Place images in proper folder!");
SDL_Delay(2000);
return false;
}

sprite = load_image("XTele19.png");

if(sprite == NULL )
{
IMG_SetError(" The file Xtele19.png is nowhere to be found! Place images in proper folder!");
SDL_Delay(2000);
return false;
}

//If it worked
return true;


}


void clean_up()
{
//Free the image
SDL_FreeSurface( background );
SDL_FreeSurface( sprite );

//Quit SDL
SDL_Quit();
}



int main( int argc, char* args[] )
{
//Make sure that main waits for a quit
bool quit = false;

//Initialize
if( init() == false)
{
return 1;
}

//Load the files
if(load_files() == false)
{
return 1;
}


//Apply the surfaces to the screen
apply_surface(0, 0, background, screen);
apply_surface(240, 190, sprite, screen);

//Update the screen
if(SDL_Flip( screen ) == -1)
{
return 1;
}

//Update Screen
if(SDL_Flip( screen ) == -1)
{
return 1;
}

//While the user hasn't quit
while(quit == false)
{
//While there's an event to handle
while( SDL_PollEvent( &event ) )
{
if(event.type == SDL_QUIT)
{
//Quit the program
quit == true;
}
}

}

//Clean up
clean_up();

return 0;

}



EDIT: Also, I remove the images from the folder and my error messages don't even attempt to pop up. I was trying to check for errors to find out where I was screwing up at, but not even this worked.

Share this post


Link to post
Share on other sites
IMG_SetError is not what you are wanting to use to display errors.
IMG_GetError returns the error message in a char* format, which you can then pass to std::cout or printf or whatever, but it doesn't display a error.

Example:
if( screen == NULL )
{
IMG_SetError("SDL_SetVideoMode() Failed!");
SDL_Delay(2000);
return false;
}
//Should be:
if( screen == NULL )
{
printf("Error message: %s\n", IMG_GetError());
SDL_Delay(2000);
return false;
}
printf(when used in SDL) sends text to a file called 'stdout.txt' in the same folder that your program runs in. Neither printf or IMG_SetError will make a pop-up message box. [printf]


Onto your main problem: Do you want your yellow color do not show at all, or be clear-ish, so it somewhat shows? If you want it entirely invisible, you change the parameters here: (Note the underlined parameters)
Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 255, 255, 0);

So for your yellow color to be invisible, you'd set it to:
Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 253, 239, 0);

Do not use SDL_MapRGBA, but use SDL_MapRGB. If you are using SDL_MapRGBA, make sure you have the fourth color parameter(the fifth parameter including 'optimizedImage->format') set as 255! SDL has 255 as 'Opaque', and 0 as fully transparent. To be on the safe side, set that parameter as SDL_ALPHA_OPAQUE.

If you want your yellow color to be partially-transparent(see-through but not 100% see-through), that's a different thing entirely, one which I dont have experience in, and cannot help you with. But if you want to just change the background color in this tutorial, the below change works.

This:
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );

Is the exact same thing as this:
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 255, 255 );

And for your yellow color, you want this:
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 253, 239, 0 );

Share this post


Link to post
Share on other sites
OMG it worked.

Thanks alot.

Apparently I need to find a color checker or something that will tell me the exact value of the color. I used Photoshop to edit the background of that sprite and just got the color from there. Apparently it wasn't completely accurate or something.

Again, thank you all.

Share this post


Link to post
Share on other sites

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