Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Getting a "Failed to load image" error, not sure how to fix it.  (Read 9202 times)

0 Members and 1 Guest are viewing this topic.

gws923

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Hello all.  I have a program that contains a list of "points" (which are just what they sound like).  These points are represented on screen by a point.png.

If I have a lot of points on the screen (50+?)  the program eventually crashes, even without user input, and displays the follow error message several times, though NOT as many times as there are points.

Quote
Failed to load image "images/point.png". Reason: Corrupt JPEG
Image /images/point.png failed to load.

Now, first of all, it ain't a JPEG file, so how can it be corrupt?  Also, if I change the image path to point.jpg, it STILL gives me this same error message.  I don't use point.png anywhere else in my code.

Can anyone help me make sense of this error message and how I can address it?

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Getting a "Failed to load image" error, not sure how to fix it.
« Reply #1 on: June 14, 2012, 07:40:05 pm »
Quote
and displays the follow error message several times, though NOT as many times as there are points

By saying that, do you mean that you're loading the same file several times?

[edit]
Someone else might know what your problem is without, but it might help to show how you're loading the image and how you're displaying it.
« Last Edit: June 14, 2012, 07:44:25 pm by Perde »

aBallofWin

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: Getting a "Failed to load image" error, not sure how to fix it.
« Reply #2 on: June 14, 2012, 08:00:41 pm »
Are you loading a new image for each point you have, or are you just loading the image once then assigning it to points?

gws923

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: Getting a "Failed to load image" error, not sure how to fix it.
« Reply #3 on: June 14, 2012, 09:15:03 pm »
Quote
By saying that, do you mean that you're loading the same file several times?

I suppose I am loading the same image several times, which now that I think of it, is a stupid idea, huh?  I'll try and rework it to only load once and let you all know if it fixes it (I'm sure it will).

Thanks, everyone!

gws923

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: Getting a "Failed to load image" error, not sure how to fix it.
« Reply #4 on: June 14, 2012, 09:40:27 pm »
Ok, so I've tried to rework it but I'm still having trouble.  I have a pointmanager to manage all my points, and here is its constructor and then the method by which it should be assigning each point a texture.

PointManager::PointManager()
{
    currentId = 0;
    numLoopPoints = 0;
    numLoops = 0;

    if(!p1Texture.loadFromFile("images/point.png"))
        std::cout << "Image /images/point.png failed to load." << std::endl;

    if(!lp1Texture.loadFromFile("images/loop1point.png"))
        std::cout << "Image /images/loop1point.png failed to load." << std::endl;

    if(!lp2Texture.loadFromFile("images/loop2point.png"))
        std::cout << "Image /images/loop2point.png failed to load." << std::endl;

}

void PointManager::AddPoint(Point * p1)
{
    //set IDs
    if(PointList.empty()){ p1->SetId(0);}
    else{p1->SetId(PointList.back().GetId() + 1);}

    //set Textures
    if(p1->GetType() == "edge"){p1->GetSprite().setTexture(p1Texture);}
    else if(p1->GetType() == "loop1"){p1->GetSprite().setTexture(lp1Texture);}
    else if(p1->GetType() == "loop2"){p1->GetSprite().setTexture(lp2Texture);}

    p1->GetSprite().setOrigin(2,2);
    p1->GetSprite().setPosition(p1->GetX(), p1->GetY());

    currentId = p1->GetId();
    PointList.push_back(*p1);
}
 

This compiles, but the points aren't showing up.  Any ideas?

EDIT: Got it working.  Apparently I had to pass the textures as pointers.

« Last Edit: June 14, 2012, 10:36:41 pm by gws923 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Getting a "Failed to load image" error, not sure how to fix it.
« Reply #5 on: June 14, 2012, 10:37:28 pm »
Quote
This compiles, but the points aren't showing up.  Any ideas?

So it compiles and runs without spitting out errors?

Does GetSprite() return a reference or a copy? Because if it's a copy you will set the texture and the copy will get destroied as soon as you left the scope.

Do you get a white rectangle instead of the image?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gws923

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: Getting a "Failed to load image" error, not sure how to fix it.
« Reply #6 on: June 14, 2012, 10:39:08 pm »
Quote
Does GetSprite() return a reference or a copy? Because if it's a copy you will set the texture and the copy will get destroied as soon as you left the scope.

I think that was exactly the problem.  It was returning a copy.  I just wrote a SetTexture(sf::Texture *texture) method for Point.cpp that circumvented the problem.  Good to keep in mind.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Getting a "Failed to load image" error, not sure how to fix it.
« Reply #7 on: June 15, 2012, 12:37:47 am »
I highly suggest you should read something about references, const and smart pointers. ::)

Raw pointers work fine as long as you handle them right, but seeing that you didn't really understand what happend there it's better to use safer methods. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gws923

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: Getting a "Failed to load image" error, not sure how to fix it.
« Reply #8 on: June 15, 2012, 12:43:34 am »
Yeah, I'm still getting my feet wet with this stuff.  I wrote the GetSprite() function so long ago, though, that I guess it just didn't even occur to me to check it out.  I usually make sure to have my return types as pointers or references when it makes sense to do so, but I either wasn't thinking about it at the time, or it was so long ago I didn't know any better.  Thanks for your help!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Getting a "Failed to load image" error, not sure how to fix it.
« Reply #9 on: June 16, 2012, 01:10:23 am »
...return types as pointers or references..

I highly suggest you should read something about references, const and smart pointers. ::)

Raw pointers work fine as long as you handle them right, but seeing that you didn't really understand what happend there it's better to use safer methods. ;)

This although only applies if you want to write in a newer and safer way. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/