SDL Image Not loading transparent images.

Started by
1 comment, last by rip-off 10 years, 3 months ago

EDIT: I have figured out that it is SDL_DisplayFormat that is causing the issue. Now to just figure out what to do about it.....

EDIT 2: SDL_DisplayFormatAlpha fixed the issue. It carries over the alpha value of the image, hence making it transparent. I am not sure whether I should delete this post since it is solved,or if maybe I should leave it up for another beginner who has the same issue? maybe a moderator can help me out.

I am following this tutorial: http://www.sdltutorials.com/sdl-coordinates-and-blitting. The only difference is I am using the SDL_Image extension to SDL so I can load in .png files. I have used the exact code as the author of the tutorial, except I use IMG_Load("whatever file") instead of SDL_LoadBMP so that I can have transparent images. The images however are not transparent when I blit them to the screen. Yes, they actual images files themself are transparent.One problem I thought might be the issue is that I am using SDL_BlitSurface. I'm not sure if there is another function I need to use with IMG_Load to make the images transparent, but I don't believe so. Below I have the code I am using. If someone could help me out I'd really appreciate it!. Thanks!!

The code below is the function that loads in my image. I thought maybe SDL_DisplayFormat might be the problem, but I have not found anything info on Google agreeing with this.


SDL_Surface* CSurface::OnLoad(char* File){

    SDL_Surface* Surf_Temp = NULL;//The variable that temporarily stores the image when it is loaded in
    SDL_Surface* Surf_Return = NULL;//The variable where the modified image from Surf_Temp will be stored

    //Loads an image in or returns NULL if no image is loaded
    if ((Surf_Temp = IMG_Load(File)) == NULL){
        return NULL;
    }

    Surf_Return = SDL_DisplayFormat(Surf_Temp);//Formats the image in Surf_Return to match the display
    SDL_FreeSurface(Surf_Temp);//Frees the temporary variable Surf_Temp from memory since it is no longer needed

    return Surf_Return;//The function returns the image in Surf_Return
}




Below is the code that blits the image onto the screen:

 bool CSurface::OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y){
    if (Surf_Dest == NULL || Surf_Src == NULL){
        return false;
    }

    SDL_Rect DestR;

    DestR.x = X;
    DestR.y = Y;

    SDL_BlitSurface(Surf_Src, NULL, Surf_Dest, &DestR);

    return true;
}
Advertisement

Leave the post up because it might save someone else a headache. That's the whole point of this site. smile.png

Edit the title and put [solved] at the beginning, and then only people who are interested in the solution will click through.

- Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG


Edit the title and put [solved] at the beginning, and then only people who are interested in the solution will click through.

We actively discourage this. Sometimes, the OP may believe an issue is solved, but their solution might be poor, or might be outright wrong (e.g. relying on undefined behaviour that happens to "work").

A simple follow up reply, mentioning the correct solution, is good. I personally don't like when people edit the original post, but provided such edits are clearly marked, like here, it isn't necessarily a bad thing.

In this case, the solution appears to be broadly correct. But to treat this as an example, I might point out that failing to optimise the surface for display doesn't have to be treated as a fatal error, the unoptimised surface could be used (possibly a warning could be logged too):

SDL_Surface* CSurface::OnLoad(char* File){
    SDL_Surface *loaded = IMG_Load(File);
    if(loaded) {
        SDL_Surface *optimised = SDL_DisplayFormatAlpha(loaded);
        if(optimised) {
            SDL_FreeSurface(loaded);
            return optimised;
        }
        return loaded;
    }
    return nullptr;
}

This topic is closed to new replies.

Advertisement