Sprite not displaying correctly

I sent this question in awhile ago and nobody said anything about it.
I’ll try again. This time with links to screenshots. My program runs;
it displays most sprites correctly, but one it doesn’t display
correctly. Can you guess which one?

The second sprite from the top on the right side is displayed almost as
if it’s overexposed by light. Here’s what it’s supposed to look like:

This is the actual file I’m using for the sprite. It’s supposed to have
a transparent background and indeed in eog and gimp it does. But it
doesn’t display correctly in my program, as you can see.

Here’s my the relevent (AFAIK) snippets of my code:

void drawBattle::loadImage(int index, string filename, bool polarity)
{
if (polarity == true) //It’s an ally
{
aparty[index].loadImage(filename);

  cout << "Got to line drawBattle.cpp:198" << endl;

  this->aimage[index] = aparty[index].getImage();

  Uint32 colorkey = SDL_MapRGB(aimage[index]->format, 0, 255, 0);
  SDL_SetColorKey(aimage[index], SDL_SRCCOLORKEY, colorkey);

  cout << "Got to drawBattle.cpp:205" << endl;

  asrc[index].x = 0;
  asrc[index].y = 0;
  asrc[index].w = this->aimage[index]->w;
  asrc[index].h = this->aimage[index]->h;

  cout << "Got to drawBattle.cpp:212" << endl;

  aparty[index].setRect(asrc[index]);

  cout << "Got to drawBattle.cpp:216" << endl;

  adest[index].x = 600;
  adest[index].y = index * 100;
  adest[index].w = this->aimage[index]->w;
  adest[index].h = this->aimage[index]->h;
  cout << "Got to drawBattle.cpp:222" << endl;

}
else //Must be an enemy…
{
//Add in later the code to access the enemyParty info…
eparty[index].loadImage(filename);

  this->eimage[index] = eparty[index].getImage();

  colorkey = SDL_MapRGB(eimage[index]->format, 0, 0, 255);
  SDL_SetColorKey(eimage[index], SDL_SRCCOLORKEY, colorkey);

  esrc[index].x = 0;
  esrc[index].y = 0;
  esrc[index].w = this->eimage[index]->w;
  esrc[index].h = this->eimage[index]->h;

  eparty[index].setRect(esrc[index]);

  edest[index].x = 0;
  edest[index].y = index * 100;
  edest[index].w = this->eimage[index]->w;
  edest[index].h = this->eimage[index]->h;

}
}

aparty and eparty are arrays of Characters, which load there images from
here:

bool Character::loadImage(string f)
{

int i;

int size = f.size() + 1;
const char* temp = f.c_str();

cout << “Got to line character.cpp:51” << endl;

printf(“filename = %s\n”, temp);

if (temp != NULL)
{
image = IMG_Load(temp);
if (image == NULL)
{
printf(“Unable to load image %s: %s\n”, temp, SDL_GetError());
return false;
}
}
// delete temp;
return true;
}

Now will anyone tell me what the heck is going on with my one rogue sprite?

I haven’t done loading transparent images in a while, so I may be wrong,
but I think you’ll need to edit the image so rather than it being
transparent, it’s a pre-defined color that you also use as your color key
for transparency - SDL will then ignore this color when you blit the sprite.

I think typically older-style games used RGB(255,0,255) (I know Duke Nukem
3d did, anyway), so just use that as your background color and color key in
SDL, and you should be fine. You could use any color, technically, but I
think {255,0,255} was traditionally used because it was a rare color (if
not, can someone correct me, there?).

Hope this helps,

-AlexOn Wed, Oct 3, 2012 at 1:14 PM, Michael Sullivan wrote:

I sent this question in awhile ago and nobody said anything about it.
I’ll try again. This time with links to screenshots. My program runs;
it displays most sprites correctly, but one it doesn’t display
correctly. Can you guess which one?

http://msulli1355.0fees.net/image.png

The second sprite from the top on the right side is displayed almost as
if it’s overexposed by light. Here’s what it’s supposed to look like:

http://msulli1355.0fees.net/Thief.png

This is the actual file I’m using for the sprite. It’s supposed to have
a transparent background and indeed in eog and gimp it does. But it
doesn’t display correctly in my program, as you can see.

Here’s my the relevent (AFAIK) snippets of my code:

void drawBattle::loadImage(int index, string filename, bool polarity)
{
if (polarity == true) //It’s an ally
{
aparty[index].loadImage(filename);

  cout << "Got to line drawBattle.cpp:198" << endl;

  this->aimage[index] = aparty[index].getImage();

  Uint32 colorkey = SDL_MapRGB(aimage[index]->format, 0, 255, 0);
  SDL_SetColorKey(aimage[index], SDL_SRCCOLORKEY, colorkey);

  cout << "Got to drawBattle.cpp:205" << endl;

  asrc[index].x = 0;
  asrc[index].y = 0;
  asrc[index].w = this->aimage[index]->w;
  asrc[index].h = this->aimage[index]->h;

  cout << "Got to drawBattle.cpp:212" << endl;

  aparty[index].setRect(asrc[index]);

  cout << "Got to drawBattle.cpp:216" << endl;

  adest[index].x = 600;
  adest[index].y = index * 100;
  adest[index].w = this->aimage[index]->w;
  adest[index].h = this->aimage[index]->h;
  cout << "Got to drawBattle.cpp:222" << endl;

}
else //Must be an enemy…
{
//Add in later the code to access the enemyParty info…
eparty[index].loadImage(filename);

  this->eimage[index] = eparty[index].getImage();

  colorkey = SDL_MapRGB(eimage[index]->format, 0, 0, 255);
  SDL_SetColorKey(eimage[index], SDL_SRCCOLORKEY, colorkey);

  esrc[index].x = 0;
  esrc[index].y = 0;
  esrc[index].w = this->eimage[index]->w;
  esrc[index].h = this->eimage[index]->h;

  eparty[index].setRect(esrc[index]);

  edest[index].x = 0;
  edest[index].y = index * 100;
  edest[index].w = this->eimage[index]->w;
  edest[index].h = this->eimage[index]->h;

}
}

aparty and eparty are arrays of Characters, which load there images from
here:

bool Character::loadImage(string f)
{

int i;

int size = f.size() + 1;
const char* temp = f.c_str();

cout << “Got to line character.cpp:51” << endl;

printf(“filename = %s\n”, temp);

if (temp != NULL)
{
image = IMG_Load(temp);
if (image == NULL)
{
printf(“Unable to load image %s: %s\n”, temp, SDL_GetError());
return false;
}
}
// delete temp;
return true;
}

Now will anyone tell me what the heck is going on with my one rogue sprite?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Well, it’s most likely to be an issue with how IMG_Load() is working and/or
how the png is formatted. Did you try saving it in a different format or
copying the pixels to another png and saving it as the original?

Jonny DOn Wed, Oct 3, 2012 at 1:14 PM, Michael Sullivan wrote:

I sent this question in awhile ago and nobody said anything about it.
I’ll try again. This time with links to screenshots. My program runs;
it displays most sprites correctly, but one it doesn’t display
correctly. Can you guess which one?

http://msulli1355.0fees.net/image.png

The second sprite from the top on the right side is displayed almost as
if it’s overexposed by light. Here’s what it’s supposed to look like:

http://msulli1355.0fees.net/Thief.png

This is the actual file I’m using for the sprite. It’s supposed to have
a transparent background and indeed in eog and gimp it does. But it
doesn’t display correctly in my program, as you can see.

Here’s my the relevent (AFAIK) snippets of my code:

void drawBattle::loadImage(int index, string filename, bool polarity)
{
if (polarity == true) //It’s an ally
{
aparty[index].loadImage(filename);

  cout << "Got to line drawBattle.cpp:198" << endl;

  this->aimage[index] = aparty[index].getImage();

  Uint32 colorkey = SDL_MapRGB(aimage[index]->format, 0, 255, 0);
  SDL_SetColorKey(aimage[index], SDL_SRCCOLORKEY, colorkey);

  cout << "Got to drawBattle.cpp:205" << endl;

  asrc[index].x = 0;
  asrc[index].y = 0;
  asrc[index].w = this->aimage[index]->w;
  asrc[index].h = this->aimage[index]->h;

  cout << "Got to drawBattle.cpp:212" << endl;

  aparty[index].setRect(asrc[index]);

  cout << "Got to drawBattle.cpp:216" << endl;

  adest[index].x = 600;
  adest[index].y = index * 100;
  adest[index].w = this->aimage[index]->w;
  adest[index].h = this->aimage[index]->h;
  cout << "Got to drawBattle.cpp:222" << endl;

}
else //Must be an enemy…
{
//Add in later the code to access the enemyParty info…
eparty[index].loadImage(filename);

  this->eimage[index] = eparty[index].getImage();

  colorkey = SDL_MapRGB(eimage[index]->format, 0, 0, 255);
  SDL_SetColorKey(eimage[index], SDL_SRCCOLORKEY, colorkey);

  esrc[index].x = 0;
  esrc[index].y = 0;
  esrc[index].w = this->eimage[index]->w;
  esrc[index].h = this->eimage[index]->h;

  eparty[index].setRect(esrc[index]);

  edest[index].x = 0;
  edest[index].y = index * 100;
  edest[index].w = this->eimage[index]->w;
  edest[index].h = this->eimage[index]->h;

}
}

aparty and eparty are arrays of Characters, which load there images from
here:

bool Character::loadImage(string f)
{

int i;

int size = f.size() + 1;
const char* temp = f.c_str();

cout << “Got to line character.cpp:51” << endl;

printf(“filename = %s\n”, temp);

if (temp != NULL)
{
image = IMG_Load(temp);
if (image == NULL)
{
printf(“Unable to load image %s: %s\n”, temp, SDL_GetError());
return false;
}
}
// delete temp;
return true;
}

Now will anyone tell me what the heck is going on with my one rogue sprite?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

It looks like your png is saved as palette-indexed (i.e. not true-color).
Are your others saved in the same way? My best guess is that SDL_image is
loading the wrong palette for some reason. The easiest workaround is
probably to save it as true-color (Gimp: Image > Mode > RGB).

Jonny D

It was originally a .gif file that I ran ImageMagick’s convert on to
turn into a .png…On 10/03/12 12:43, Jonathan Dearborn wrote:

Well, it’s most likely to be an issue with how IMG_Load() is working
and/or how the png is formatted. Did you try saving it in a different
format or copying the pixels to another png and saving it as the original?

Jonny D

I did the above Gimp procedure on Thief.gif and ran my program, but the
procedure had no visible effect. Here is my setVideoMode line. Is it
correct for this? I know nothing about True Color.

screen = SDL_SetVideoMode(1025, 600, 16, SDL_DOUBLEBUF | SDL_HWSURFACE);On 10/03/12 12:53, Jonathan Dearborn wrote:

It looks like your png is saved as palette-indexed (i.e. not
true-color). Are your others saved in the same way? My best guess is
that SDL_image is loading the wrong palette for some reason. The
easiest workaround is probably to save it as true-color (Gimp: Image >
Mode > RGB).

Jonny D


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I believe 16 bit doesn’t support alpha, however, I don’t know if IMG_load()
takes care of that for you.On Wed, Oct 3, 2012 at 3:38 PM, Michael Sullivan wrote:

On 10/03/12 12:53, Jonathan Dearborn wrote:

It looks like your png is saved as palette-indexed (i.e. not
true-color). Are your others saved in the same way? My best guess is
that SDL_image is loading the wrong palette for some reason. The
easiest workaround is probably to save it as true-color (Gimp: Image >
Mode > RGB).

Jonny D


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I did the above Gimp procedure on Thief.gif and ran my program, but the
procedure had no visible effect. Here is my setVideoMode line. Is it
correct for this? I know nothing about True Color.

screen = SDL_SetVideoMode(1025, 600, 16, SDL_DOUBLEBUF | SDL_HWSURFACE);


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Gifs don’t store true-color info, only 8-bit (or less) palette indices.
Make sure you change the image format mode and then Save As in a different
format that is capable of storing that data.

Jonny DOn Wed, Oct 3, 2012 at 3:38 PM, Michael Sullivan wrote:

On 10/03/12 12:53, Jonathan Dearborn wrote:

It looks like your png is saved as palette-indexed (i.e. not
true-color). Are your others saved in the same way? My best guess is
that SDL_image is loading the wrong palette for some reason. The
easiest workaround is probably to save it as true-color (Gimp: Image >
Mode > RGB).

Jonny D


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I did the above Gimp procedure on Thief.gif and ran my program, but the
procedure had no visible effect. Here is my setVideoMode line. Is it
correct for this? I know nothing about True Color.

screen = SDL_SetVideoMode(1025, 600, 16, SDL_DOUBLEBUF | SDL_HWSURFACE);


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

The file I’m editing is .png, not .gif.On 10/03/12 19:22, Jonathan Dearborn wrote:

Gifs don’t store true-color info, only 8-bit (or less) palette indices.
Make sure you change the image format mode and then Save As in a
different format that is capable of storing that data.

Jonny D

I think Jon is saying that the PNG didn’t change the palette data, so
you’ll need to manually do that to fix it.On Wed, Oct 3, 2012 at 8:23 PM, Michael Sullivan wrote:

On 10/03/12 19:22, Jonathan Dearborn wrote:

Gifs don’t store true-color info, only 8-bit (or less) palette indices.
Make sure you change the image format mode and then Save As in a
different format that is capable of storing that data.

Jonny D

The file I’m editing is .png, not .gif.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

What OS and version of SDL and SDL_image do you use? I’m on Kubuntu with
SDL 1.2.14, SDL_image 1.2.10, and libpng12.so.0 and my test program
displays correctly:

http://code.bluedinosaurs.com/open/test_Thief.zip

Jonny D