2
\$\begingroup\$

I have drawn all of the images of my sprite in paint. Is there a way to remove all white pixels?

Before blitting white pixels. After blitting white pixels.

bool Game::loadFromFile(std::string path)
{
    free();

    SDL_Texture* newTexture = NULL;
    SDL_Surface* loadedSurface = IMG_Load(path.c_str());

    if(loadedSurface == NULL)
    {
        printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
    }

    else
    {
        //Color key image
        Uint32 colorKey = SDL_MapRGB(loadedSurface->format, 255, 255, 255);
        SDL_SetColorKey(loadedSurface, SDL_TRUE, colorKey);

        //Create texture from surface pixels
        newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);

        if(newTexture == NULL)
        {
            printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
        }

        else
        {
            //Get image dimensions
            mWidth = loadedSurface->w;
            mHeight = loadedSurface->h;
        }

        //Get rid of old loaded surface
        SDL_FreeSurface(loadedSurface);
    }

    //Return success
    mTexture = newTexture;
    return mTexture != NULL;
}

void Game::free()
{
    //Free texture if it exists
    if(mTexture != NULL)
    {
        SDL_DestroyTexture(mTexture);
        mTexture = NULL;
        mWidth = 0;
        mHeight = 0;
    }
}

void Game::render(int x, int y, SDL_Rect* clip)
{
    //Set rendering space and render to screen
    SDL_Rect renderQuad = {x, y, mWidth, mHeight};

    //Set clip rendering dimensions
    if(clip != NULL)
    {
        renderQuad.w = clip->w;
        renderQuad.h = clip->h;
    }

    //Render to screen
    SDL_RenderCopy(gRenderer, mTexture, clip, &renderQuad);
}

int Game::getWidth()
{
    return mWidth;
}

int Game::getHeight()
{
    return mHeight;
}

bool Game::Initialize()
{
    bool success = true;

    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
        success = false;
    }

    else
    {
        if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
        {
            printf("Warning: Linear texture filtering not enabled!");
        }

        gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

        if(gWindow == NULL)
        {
            printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
            success = false;
        }

        else
        {
            gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
            sBackground = SDL_LoadBMP("BackgroundLeft.bmp");
            tBackground = SDL_CreateTextureFromSurface(gRenderer, sBackground);

            if(gRenderer == NULL)
            {
                printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
                success = false;
            }

            else
            {
                SDL_SetRenderDrawColor(gRenderer, 255, 0, 255, 0xFF);

                int imgFlags = IMG_INIT_PNG;

                if(!(IMG_Init(imgFlags) & imgFlags))
                {
                    printf("SDL_image could not initialize! SDL_mage Error: %s\n", IMG_GetError());
                    success = false;
                }
            }
        }
    }

    return success;
}
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to a StackExchange site! Here, when an answer has help you fix the issue that you have, you need to tick the checkmark beside the answer, to mark it as "accepted". The system uses that to show other users that the issue is solved, and to show which one is considered the best solution by you. We don't edit the title with "solved" :) \$\endgroup\$
    – Vaillancourt
    Feb 5 at 3:56
2
\$\begingroup\$

The SDL_SetColorKey() function color keys a specific color in an image. In this case it's the color white (255, 255, 255).

If you zoom in on the second image you've added to your post, where the color key has been applied, you'll see that the border around your character isn't actually completely white and those pixels hasn't been color keyed. Some of the pixels are almost white, some of them are light gray, some are dark gray etc.

enter image description here

To make the color keying to work properly, the pixels that you want to color key has to be completely white.

Your other choice is to use an image format that supports an alpha channel, like the png format for example. Instead of using a white background color around the character, you instead erase the background and let the IMG_Load()function (from the SDL_Image library) handle the alpha/color keying for you.

\$\endgroup\$
1
\$\begingroup\$

Because the source image is monochrome, you can use use a different blend mode to get nicely blended edges. If you multiply the sprite colour by the thing it's drawn on top of, you will get the blended effect that you want.

That's because white is 1, and black is 0. Multiplying by 1 will do nothing, and multiplying by zero will give you black.

To get that effect you need to use SDL_SetSurfaceBlendMode() instead of SDL_SetColorKey().

SDL_SetSurfaceBlendMode(loadedSurface, SDL_BLENDMODE_MOD);

If you want to use a more colourful sprite image, then you will need an alpha channel, along with SDL_BLENDMODE_BLEND.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your help! Going back into paint and removing all of the pixels that were not black fixed the issue. \$\endgroup\$ Feb 5 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.