1
General discussions / Loading resources from path with wide(Uniocde) characters in it. C++
« 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.
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.
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);
}
};
}
#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.