Platon_actual
Platon_actual

Reputation: 1

SDL2 loading files with special characters

I got a problem, that is: in a Windows application using SDL2 & SDL2_Image, it opens image files, for later saving them with modifications on the image data. When it opens an image without special characters (like áéíóúñ, say, "buenos aires.jpg") it works as intended. But, if there is any special character as mentioned (say, "córdoba.jpg"), SDL_Image generates an error saying "Couldn't open file". Whatever, if i use the std::ifstream flux with the exact file name that i got from the CSV file (redundant, as "córdoba.jpg" or "misiónes.jpg"), the ifstream works well... Is it an error using the special characters? UNICODE, UTF, have something to do?

A little information about the environment: Windows 10 (spanish, latin american), SDL2 & SDL2_Image (up to date versions), GCC compiler using Mingw64 7.1.0

About the software I'm trying to make: it uses a CSV form, with the names of various states of Argentina, already tried changing encoding on the .CSV. It loads images based on the names found on the CSV, changes them, and saves.
I know maybe I am missing something basic, but already depleted my resources.

Upvotes: 0

Views: 941

Answers (1)

genpfault
genpfault

Reputation: 52113

IMG_Load() forwards its file argument directly to SDL_RWFromFile():

// http://hg.libsdl.org/SDL_image/file/8fee51506499/IMG.c#l125
SDL_Surface *IMG_Load(const char *file)
{
    SDL_RWops *src = SDL_RWFromFile(file, "rb");
    const char *ext = SDL_strrchr(file, '.');
    if(ext) {
        ext++;
    }
    if(!src) {
        /* The error message has been set in SDL_RWFromFile */
        return NULL;
    }
    return IMG_LoadTyped_RW(src, 1, ext);
}

And SDL_RWFromFile()'s file argument should be a UTF-8 string:

SDL_RWops* SDL_RWFromFile(const char* file,
                          const char* mode)

Function Parameters:

  • file: a UTF-8 string representing the filename to open
  • mode: an ASCII string representing the mode to be used for opening the file; see Remarks for details

So pass UTF-8 paths into IMG_Load().

C++11 has UTF-8 string literal support built-in via the u8 prefix:

IMG_Load( u8"córdoba.jpg" );

Upvotes: 1

Related Questions