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

Author Topic: [SOLVED (sort off)] PhysFS, 7zip and PNG gives crash  (Read 1934 times)

0 Members and 1 Guest are viewing this topic.

SpectreNectar

  • Newbie
  • *
  • Posts: 39
    • View Profile
[SOLVED (sort off)] PhysFS, 7zip and PNG gives crash
« on: October 10, 2015, 05:09:26 pm »
Not sure if right forum but I'm clueless and here goes:
I'm trying to use PhysFS to load png files and draw them with SFML .
Using 7zip (15.8) file manager for file compression I have successfully done this but with the zip format rather than the 7z format.


I get this from the console with 7z:
Failed to load image from stream. reason: image not of any known type, or corrupt

Any ideas what could cause this?
« Last Edit: October 10, 2015, 05:52:58 pm by SpectreNectar »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: PhysFS, 7zip and PNG gives crash
« Reply #1 on: October 10, 2015, 05:12:02 pm »
Failed to load image from stream. reason: image not of any known type, or corrupt

Any ideas what could cause this?
Yes, the reason mentioned in the error output.

You're doing something wrong when encoding/decoding the zip format. Make sure it's correct by first loading it into memory and using loadFromMemory(). Once that works, rewrite the code to use streams and loadFromStream().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

SpectreNectar

  • Newbie
  • *
  • Posts: 39
    • View Profile
Re: PhysFS, 7zip and PNG gives crash
« Reply #2 on: October 10, 2015, 05:23:02 pm »
EDIT: It seems this works when I add to a 7z archive with no compression .. Which only changes filesize by 2MB anyway. Thanks for your time.

I'm not sure how.

I commented out what I did earlier (loadFromStream), updated with loadFromMemory, but got the same result:
sf::Texture* Sprites::load(const std::string& path) {

    PhysFsStream gfxStream;
    gfxStream.open(path.c_str());

    void* data = malloc(1024*1024*4);
    gfxStream.read(data, 1024*1024*4);



    sf::Texture* img = new sf::Texture();
    if(!img->loadFromMemory(data, 1024*1024*4)) {
        delete img;
        img = 0;
    }

    free(data);

/*
    sf::Texture* img = new sf::Texture();
    if(!img->loadFromStream(gfxStream)) {
        delete img;
        img = 0;
    }*/


    return img;

}
« Last Edit: October 10, 2015, 05:52:37 pm by SpectreNectar »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [SOLVED (sort off)] PhysFS, 7zip and PNG gives crash
« Reply #3 on: October 11, 2015, 12:21:56 pm »
It probably works without compression because you did something wrong with compression ;)

You should really use smart pointers, see my article about RAII. It makes your code simpler and safer. There's no reason to manage memory manually in C++...

As you see, the following code is shorter and doesn't need to explicitly deallocate anything.
std::unique_ptr<sf::Texture> Sprites::load(const std::string& path) {

    PhysFsStream gfxStream;
    gfxStream.open(path.c_str()); // <- shouldn't this be checked for errors?

    // allocate buffer
    const std::size_t size = 1024*1024*4;
    std::unique_ptr<char[]> data(new char[size]);

    gfxStream.read(data.get(), size);

    // allocate texture
    auto img = std::make_unique<sf::Texture>();
    if(!img->loadFromMemory(data.get(), size))
        return nullptr;

    return img;
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything