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

Author Topic: Loading Images and Textures Failed  (Read 2082 times)

0 Members and 1 Guest are viewing this topic.

NicuBaciu

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Loading Images and Textures Failed
« 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)
« Last Edit: June 10, 2018, 04:36:52 pm by eXpl0it3r »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Loading Images and Textures Failed
« Reply #1 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".

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Loading Images and Textures Failed
« Reply #2 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
Could it therefore be just because you left the handle around?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Loading Images and Textures Failed
« Reply #3 on: June 12, 2018, 12:21:41 am »
Some sources 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.
« Last Edit: June 12, 2018, 12:26:42 am by Arcade »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Loading Images and Textures Failed
« Reply #4 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

NicuBaciu

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Loading Images and Textures Failed
« Reply #5 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)

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Loading Images and Textures Failed
« Reply #6 on: June 13, 2018, 03:13:30 pm »
The image seems locked.
Out of curiosity, did you try closing the handle?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

NicuBaciu

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Loading Images and Textures Failed
« Reply #7 on: June 15, 2018, 11:19:42 am »
No, how can I do that?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Loading Images and Textures Failed
« Reply #8 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything