SFML community forums

Help => General => Topic started by: SpectreNectar on October 10, 2015, 05:09:26 pm

Title: [SOLVED (sort off)] PhysFS, 7zip and PNG gives crash
Post by: SpectreNectar 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?
Title: Re: PhysFS, 7zip and PNG gives crash
Post by: Nexus 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().
Title: Re: PhysFS, 7zip and PNG gives crash
Post by: SpectreNectar 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;

}
Title: Re: [SOLVED (sort off)] PhysFS, 7zip and PNG gives crash
Post by: Nexus 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 (http://www.bromeon.ch/articles/raii.html). 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;
}