SFML community forums

Help => Graphics => Topic started by: NicuBaciu on June 10, 2018, 02:42:48 pm

Title: Loading Images and Textures Failed
Post by: NicuBaciu on June 10, 2018, 02:42:48 pm
Hello.
I have a project in VS2017 in which i use SFML. It is a database for students.
I have encountered an error as I was trying to implement a dialog box for opening files. As soon as the dialog box is closed (with no error message) the program displays errors in opening the fonts and the images that previously worked.

The code I am using for displaying the dialog box is as follows:

string testing()
{
        OPENFILENAME ofn;       // common dialog box structure
        char szFile[260];       // buffer for file name
        HWND hwnd = NULL;              // owner window
        HANDLE hf;              // file handle

                                                        // Initialize OPENFILENAME
        ZeroMemory(&ofn, sizeof(ofn));
        ofn.lStructSize = sizeof(ofn);
        ofn.hwndOwner = hwnd;
        ofn.lpstrFile = szFile;
        // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
        // use the contents of szFile to initialize itself.
        ofn.lpstrFile[0] = '\0';
        ofn.nMaxFile = sizeof(szFile);
        ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
        ofn.nFilterIndex = 1;
        ofn.lpstrFileTitle = NULL;
        ofn.nMaxFileTitle = 0;
        ofn.lpstrInitialDir = NULL;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

        // Display the Open dialog box.

        if (GetOpenFileName(&ofn) == TRUE)
        {
                hf = CreateFile(ofn.lpstrFile,
                        GENERIC_READ,
                        0,
                        (LPSECURITY_ATTRIBUTES)NULL,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,
                        (HANDLE)NULL);

                return ofn.lpstrFile;
        }
       
        return "";
}
 


And the function is called like this:



string s;

s = testing();

cout << s << '\n';


The error messages I get are:

Failed to load image "images/flag.png". Reason: Unable to open file
Failed to load image "images/logo_nb.png". Reason: Unable to open file
Failed to load image "images/student_data.png". Reason: Unable to open file
Failed to load font "fonts/Times_New_Roman_Normal.ttf" (failed to create the font face)
Title: Re: Loading Images and Textures Failed
Post by: Arcade on June 11, 2018, 04:21:56 pm
Just based on what you said, my first guess is that GetOpenFileName() changes the current working directory (CWD) of your program, presumably to the directory of the file you selected to open. You will then see these errors if that new location doesn't have folders called "images" or "fonts".
Title: Re: Loading Images and Textures Failed
Post by: Hapax on June 12, 2018, 12:13:29 am
I have used GetOpenFileName without it causing these troubles so I looked a little bit deeper.

It seemed that it might be the CreateFile section so I looked up information on that and found this:
Quote
When an application is finished using the object handle returned by CreateFile, use the CloseHandle function to close the handle. This not only frees up system resources, but can have wider influence on things like sharing the file or device and committing data to disk. Specifics are noted within this topic as appropriate.
Source: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx (https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx)
Could it therefore be just because you left the handle around?
Title: Re: Loading Images and Textures Failed
Post by: Arcade on June 12, 2018, 12:21:41 am
Some sources  (https://stackoverflow.com/questions/41244561/why-the-current-directory-changes?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa)seem to indicate that you need to use the OFN_NOCHANGEDIR flag if you want GetOpenFileName() to restore the current working directory afterwards. However, I've never tried these things for myself, so I can't speak to the accuracy of that.
Title: Re: Loading Images and Textures Failed
Post by: Hapax on June 12, 2018, 01:13:45 am
This is true for the OPENFILENAME structure, yes. However, according to the docs: "This flag is ineffective for GetOpenFileName."
I saw the article you posted where the answer states that it is supported by GetOpenFileName even though the docs say that it isn't. If it is, I reckon they wrote that to mess with our heads.
Title: Re: Loading Images and Textures Failed
Post by: NicuBaciu on June 13, 2018, 09:26:02 am
Thanks very much for helping me out. I used the flag OFN_NOCHEANGEDIR and it worked.

The only thing is it does not display the image until I restart the app. It is the same error message but only for the image i loaded. (I save the path of the image in a text file and then I load it back into the program when the program starts)
Title: Re: Loading Images and Textures Failed
Post by: Hapax on June 13, 2018, 03:13:30 pm
The image seems locked.
Out of curiosity, did you try closing the handle?
Title: Re: Loading Images and Textures Failed
Post by: NicuBaciu on June 15, 2018, 11:19:42 am
No, how can I do that?
Title: Re: Loading Images and Textures Failed
Post by: Hapax on June 22, 2018, 09:03:52 pm
Did you read the quote that I posted above?
The CloseHandle function mentioned is linked in the original to here:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx

Basically, use CloseHandle.