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

Author Topic: Open Music which consists of a unicode filename  (Read 1644 times)

0 Members and 1 Guest are viewing this topic.

Yoshko

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Open Music which consists of a unicode filename
« on: September 24, 2017, 10:55:23 am »
Hey.
This method: bool sf::Music::openFromFile ( const std::string& filename )
takes only a std::string as parameter, but my file name consists of the japenese song name.

How can I open my music from file, which consists of a unicode file name?




Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Open Music which consists of a unicode filename
« Reply #1 on: September 24, 2017, 12:37:39 pm »
If you are able to open the file that has a unicode filename using normal C++, you can open the music file from memory.

Here's a quick, simple example of loading a file into memory and then opening it for music:
std::unique_ptr<char[]> musicMemory;
sf::Music music;
std::ifstream musicFile(filename, std::ios::binary | std::ios::ate); // open at end of file to get size
if (!musicFile.is_open())
    std::cerr << "Unable to open file." << std::endl;
else
{
    auto filesize = musicFile.tellg(); // store file size
    musicMemory.reset(new char[filesize]); // create memory block with the same size as the file
    musicFile.seekg(0, std::ios::beg); // go back to the beginning of the file
    musicFile.read(musicMemory.get(), filesize); // read entire file into memory block
    musicFile.close();
    music.openFromMemory(musicMemory.get(), filesize); // open sf::Music from that memory block instead of a file
}
This code is C++11. If you use C++14 (or later), you'd probably use make::unique instead of new. If you use pre-C++11, you'll need to use a raw pointer instead of the unique_ptr.

I wrote this here so it's untested but it should work.
Note that this is just an example of opening from memory; if you are able to use unicode files with C++, you'll have to adapt it to your way.
Note also that both the musicMemory object also needs to stay alive as the music is streamed from that memory instead of from a file.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Yoshko

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Open Music which consists of a unicode filename
« Reply #2 on: September 24, 2017, 06:31:25 pm »
THANKYOU ;D!
This works now perfectly:D

PS: I used std::vector<char>, because the datatype of filesize is std::streampos
      and new char[std::streampos] do not work (maybe there's a detour, but std::vector does).

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Open Music which consists of a unicode filename
« Reply #3 on: September 24, 2017, 09:29:11 pm »
You're welcome :)

new char[static_cast<unsigned int>(filesize)]
should cast it to an acceptable type.
However, if a vector of char works for you, it's all good ;D (assuming you use .data() instead of .get())
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*