Advertisement Jump to content
Sign in to follow this  

Adding padding and colorkey to image in SDL/OpenGL

This topic is 2502 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

I've been trying to implement a texture class for my engine in C++ and SDL/OpenGL.

One of the loading methods applies padding to a texture (when NPOT textures are
not supported and the image is not POT) and then applies a colorkey. Since OpenGL
does not support colorkeying, I simply loop through the pixels, check if they match
the colorkey, set their alpha value to fully transparent and then draw with blending.

To do this, I create a new POT SDL_Surface with SDL_CreateRGBSurfaceFrom(...) (let's call it
the destination) filled with the colorkey, then blit the source image onto the destination using
SDL_BlitSurface. The problem is that if the source image I'm loading contains an alpha channel,
its colors are blended with the destination and the resulting color will not match the colorkey.
With my test image, there is a visible border of blended colorkey colors around the sprite's edges.

I think I've tried every possible way to do this: Calling with different flags, using different
calls in different order, Googling and reading the docs. It still does not work. The only "solution" I can
think of is to manually go through the pixels in the source image and set them in the destination
disregarding the alpha channel. Both images are SDL_Surfaces which would then be converted to an
OpenGL texture.

Is there an easier way to do this?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!