1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Dismiss Notice

Pandora [solved by my own stupidity]c++ SDL colour key not working

Discussion in 'C /C ++' started by dan3008, Jan 4, 2016.

Tags:
  1. dan3008

    dan3008 Still Fresh

    Joined:
    Sep 20, 2013
    Messages:
    35
    I'm wondering if anyone can help me. I'm learning c++ and SDL by writing a game (currently for the Pandora, but I'll port it to the pyra (probably a simple recompile) when I get one) and I cant get SDL_SetColorKey to work :/

    The texture loading code is taken almost asis from lazyfoo's page, and looks like this:
    Code:
    bool LTexture::loadFromFile( std::string path )
    
    {
        //Get rid of preexisting texture
        free();
    
        //The final texture
        SDL_Texture* newTexture = NULL;
    
        //Load image at specified path
        SDL_Surface* loadedSurface = SDL_LoadBMP( path.c_str() );
        if( loadedSurface == NULL )
        {
            printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), SDL_GetError() );
        }
        else
        {
            //SDL_Surface loaded2 = SDL_DisplayFormatAlpha(loadedSurface);
            //Color key image
            SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0, 255, 255 ) );
    
            //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;
    }
    and the render code:

    Code:
    void LTexture::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 );
    }
    I'm not quite sure why it isnt working :/ but i'm still getting the cyan background displayed :/

    Any advice?

    thanks in advance
     
    Tags:
  2. mjohansson

    mjohansson Supporter

    Joined:
    Feb 10, 2011
    Messages:
    380
  3. dan3008

    dan3008 Still Fresh

    Joined:
    Sep 20, 2013
    Messages:
    35
    well, now i'm embarrassed... The issue was in the image, not with the softwere. When I saved out, I compressed the image down to 8bit... which had changed the colour of the background, so the colour key wouldnt have worked :/ oopsie
     
    PokeParadox and FBnil like this.
  4. PokeParadox

    PokeParadox Founder of Pirate Games - Penjin Coder Staff Member

    Joined:
    Dec 8, 2005
    Messages:
    6,452
    Location:
    UK
    Don't worry... I've done similar things before. I think in my case it was not realising that the screen colour depth was set to 16b. I was picking a colour from the screen to compare to a 32b colour that didn't exist in 16bits. :oops:
     

Share This Page

Loading...