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 6816357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:54:57+00:00 2026-05-26T20:54:57+00:00

I am loading PNG file (with some transparent places) into my SDL application. Googling

  • 0

I am loading PNG file (with some transparent places) into my SDL application.

Googling on how to do it provided me with this code sample:

SDL_Surface *LoadImage(std::string filename)
{
    SDL_Surface* loaded_image = 0, compatible_image = 0;

    if (!filename.c_str())
        return 0;

    loaded_image = IMG_Load(filename.c_str());

    if (!loaded_image)
        return 0;

    compatible_image = SDL_DisplayFormat(loaded_image);

    SDL_FreeSurface(loaded_image);

    return compatible_image;
}

But when the line compatible_image = SDL_DisplayFormat(loaded_image); is reached, application halts with an uncatchable exception (even try { /* ... */ } catch (...) { /* ... */ } does not help). Replacing SDL_DisplayFormat() with SDL_DisplayFormatAlpha() did not help too. So, i just removed the exception-trowable lines and get this code working to load images:

SDL_Surface *LoadImage(std::string filename)
{
    if (!filename.c_str())
        return 0;

    return IMG_Load(filename.c_str());
}

And i’ve found such unpleasant thing: when some sprite overlaps with transparent pieces of another one, artifacts appear. Something like this:

normal state

artifacts appearing

I am animating my “hero” with this simple algorithm:

// SDL_Surface sprite = LoadImage("hero.bmp");
// hero.bmp contains animation frames followed one-by-one in a single line
// spriteFrameCnt is a number of animation frames
// spriteWidth and spriteHeight contain single frame params

SDL_Rect srcRect;
srcRect.x = spriteFrame * spriteWidth;
srcRect.w = spriteWidth;
srcRect.y = 0;
srcRect.h = spriteHeight;
spriteFrame = ++spriteFrame % spriteFrameCnt;

SDL_BlitSurface(sprite, &srcRect, screen, &rcSprite);

How can this be explained and fixed?

  • 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-05-26T20:54:58+00:00Added an answer on May 26, 2026 at 8:54 pm

    Found a solution: when a tile with transparent pieces is drawn and another sprite is drawn on the top of the first one, artifacts appear. Sure they will, because nothing lies behind that first sprite!

    The first and the most-quick fix is to fill the entire background with some color before any further blittings.

    The second and the most beautiful way (in my case) is to draw some background image (grass as shown).

    Here are the results:

    first example

    second example

    As you can see, no artifacts appearing. Well done, me! =)

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

Sidebar

Related Questions

In an MFC application. I'm loading a 32bit PNG file with alpha channel bits
Saving the file: FileOutputStream fo = null; try { fo = this.openFileOutput(test.png, Context.MODE_WORLD_READABLE); }
I'm writing on a custom view in android. It uses some png's, provided as
Here's some sample code that I want to control using jQuery (white button bg
I'm having difficulty loading my tab-delimited input file into MATLABs nprtool, which I think
I'm doing some lazy loading of images into an array when the app has
I'm looking for an example of loading a png-file on MonoDroid using OpenTK. Anyone?
I'm curious why I'm having trouble with this function. I'm downloading a PNG file
i'm trying to add some html code with javascript and jQuery into a div
I have a UIImageView that starts off with an image (loading.png) specified in IB.

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.