SFML community forums

General => General discussions => Topic started by: AntonBesp on January 07, 2020, 06:43:45 pm

Title: Loading resources from path with wide(Uniocde) characters in it. C++
Post by: AntonBesp on January 07, 2020, 06:43:45 pm
I want to load resources(images, fonts, sounds ant etc.) that has wide(Unicode) characters in their path. SFML's "loadFromFile" functions takes only standard std::string but not std::wstring.

I tried using custom file input stream but it didn't work.

#include <string>
#include <SFML/System/InputStream.hpp>
#include <SFML/Config.hpp>
#include <fstream>

namespace FileIO
{

class WFileInputStream : public sf::InputStream
{
public:
    std::ifstream stream;

public:
    WFileInputStream()
    : sf::InputStream(), stream() {}

    WFileInputStream(std::wstring path)
    : sf::InputStream(), stream()
    {
        stream.open(path);
    }


    bool open(std::wstring path)
    {
        stream.open(path);
        return stream.is_open();
    }

    sf::Int64 getSize() override
    {
        long l1 = stream.tellg();
        long l2 = l1;
        stream.seekg(0, std::ios::end);
        l1 = static_cast<long>(stream.tellg()) - l1;
        stream.seekg(l2);
        return l1; // Tried adding 1. It didn't work
    }

    sf::Int64 tell() override
    {
        return stream.tellg();
    }

    sf::Int64 seek(sf::Int64 position) override
    {
        stream.seekg(position, std::ios::beg);
        return stream.tellg();
    }

    sf::Int64 read(void* data, sf::Int64 size) override
    {
        long long l1 = stream.tellg();
        stream.read(static_cast<char*>(data), size);
        //stream.seekg(size, std::ios::right);
        return stream.tellg().operator-(l1);
    }
};

}
 

Trying to load an image(I checked its path multiple times) using this stream fails with error "Failed to load image from stream. Reason: Image not of any known type, or corrupt", "Trying to access the pixels of an empty image". But I tried using this stream with plain text files(.txt) and all methods of the stream worked properly.

Maybe you can suggest some other way to load image from directory with wide(Unicode) characters in it. Or fix my stream code.
Title: Re: Loading resources from path with wide(Uniocde) characters in it. C++
Post by: Laurent on January 08, 2020, 08:05:40 am
Your implementation of getSize() looks wrong, you shouldn't subtract the current position since you want the total size, not the remaining one. And you should check and handle errors.

Does the image load fine if you rename it and use loadFromFile(std::string)? Does it load fine with loadFromStream(sf::FileInputStream)?
Title: Re: Loading resources from path with wide(Uniocde) characters in it. C++
Post by: eXpl0it3r on January 08, 2020, 11:16:45 am
Somewhat related topic: https://github.com/SFML/SFML/issues/647