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

Author Topic: Loading resources from path with wide(Uniocde) characters in it. C++  (Read 3336 times)

0 Members and 1 Guest are viewing this topic.

AntonBesp

  • Newbie
  • *
  • Posts: 2
    • View Profile
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.
« Last Edit: January 07, 2020, 06:47:32 pm by AntonBesp »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Loading resources from path with wide(Uniocde) characters in it. C++
« Reply #1 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)?
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Loading resources from path with wide(Uniocde) characters in it. C++
« Reply #2 on: January 08, 2020, 11:16:45 am »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything