Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7730197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:09:48+00:00 2026-06-01T06:09:48+00:00

I want to load an SDL_Surface into an OpenGL texture with padding (so that

  • 0

I want to load an SDL_Surface into an OpenGL texture with padding (so that NPOT->POT) and apply a color key on the surface afterwards. I either end up colorkeying all pixels, regardless of their color, or not colorkey anything at all. I have tried a lot of different things, but none of them seem to work.

Here’s the working snippet of my code. I use a custom color class for the colorkey (range [0-1]):

// Create an empty surface with the same settings as the original image
SDL_Surface* paddedImage = SDL_CreateRGBSurface(image->flags, width, height,
                                                    image->format->BitsPerPixel,
                                                    #if SDL_BYTEORDER == SDL_BIG_ENDIAN
                                                        0xff000000,
                                                        0x00ff0000,
                                                        0x0000ff00,
                                                        0x000000ff
                                                    #else
                                                        0x000000ff,
                                                        0x0000ff00,
                                                        0x00ff0000,
                                                        0xff000000
                                                    #endif
                                                    );
// Map RGBA color to pixel format value
Uint32 colorKeyPixelFormat = SDL_MapRGBA(paddedImage->format,
                                         static_cast<Uint8>(colorKey.R * 255),
                                         static_cast<Uint8>(colorKey.G * 255),
                                         static_cast<Uint8>(colorKey.B * 255),
                                         static_cast<Uint8>(colorKey.A * 255));

SDL_FillRect(paddedImage, NULL, colorKeyPixelFormat);

// Blit the image onto the padded image
SDL_BlitSurface(image, NULL, paddedImage, NULL);

SDL_SetColorKey(paddedImage, SDL_SRCCOLORKEY, colorKeyPixelFormat);

Afterwards, I generate an OpenGL texture from paddedImage using similar code to the SDL+OpenGL texture loading code found online (I’ll post if necessary). This code works if I just want the texture with or without padding, and is likely not the problem.

I realize that I set all pixels in paddedImage to have alpha zero which causes the first problem I mentioned, but I can’t seem to figure out how to do this. Should I just loop over the pixels and set the appropriate colors to have alpha zero?

PARTIAL SOLUTION:

  1. Create paddedImage as above
  2. SDL_FillRect the paddedImage with the colorkey
  3. Generate the texture “as usual”
  4. Manually copy the image (SDL_Surface*) pixels to the paddedImage (OGL texture)

This works almost always expect some cases where the image has 3 color components (i.e. no alpha channel). I’m trying to fix that now by converting them to 4 color components

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:09:49+00:00Added an answer on June 1, 2026 at 6:09 am

    I think that It could be used together with OpenGL if you can convert SDL_Surfaces into OGL textures, and then you could use the blit function to combine your textures, and manipulate thing using the SDL workflow.
    I dont know what you want to achieve. You want to transfer one surface to an OGL texture and preserve the Colorkey, or just want to apply the colorkeyed surface to another surface which then you convert into an OGL texture.
    Also you dont have to use Per-pixel alphas, as SDL gives you the ability to use per-surface alphas, but its quite complex as of what alphas and colorkeys can be combined and used together.

    As this is a complex thing, please refer to the SDL reference, and this tutorial may be helpful too(tough it doesnt handle OGL stuff):
    http://www.sdltutorials.com/the-ins-and-outs-and-overlays-of-alpha-blending

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to load the data into session so that when the next button
I want to load one or more DLLs dynamically so that they run with
I want to load some images into my application from the file system. There's
I want to load some data from mysql into my cocoa application view before
I want to load defined folders into my website from same system, now I
I want to load key/values configuration pairs stored in XML file. To bind a
i want to load some photos into my sd card image in the emulator.
I want to load a nib that isn't a view in a UITabViewController. Here's
I'm trying to convert a SDL surface to an openGL texture using a snippet
I want to load a blue/transparent image into an android drawable. I want the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.