I was just about to write a multi-threaded test.
WCHAR szFile[260] = {0};
void threadOFD () {
OPENFILENAME opendialog = {0};
opendialog.lStructSize = sizeof (opendialog);
opendialog.hwndOwner = NULL;
opendialog.hInstance = GetModuleHandle(NULL);
opendialog.lpstrFile = &szFile[0];
opendialog.nFilterIndex = 0;
opendialog.nMaxFile = 256;
opendialog.lpstrInitialDir = NULL;
opendialog.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
//This call blocks the main thread; blocking causes texture loading to fail
GetOpenFileName(&opendialog);
}
int main () {
sf::Thread thread(&threadOFD);
thread.launch();
//while (szFile[0] == '\0') {}
sf::Texture t1;
while (!t1.loadFromFile("sfml-logo-small.png")) {
std::cout<<"Failed to load t1"<<std::endl;
}
std::cout<<"SUCCESS"<<std::endl;
return 0;
}
When the while-loop is commented:
01) Console opens
02) OFD opens
03) Texture loads
04) Outputs success
05) OFD closes
06) Application closes
When I un-comment the while-loop:
01) Console opens
02) OFD opens
03) Texture fails to load
04) Loop 03
Absolute paths don't work, either. Well, they work BUT they don't work right.
int main () {
OPENFILENAME opendialog = {0};
WCHAR szFile[260] = {0};
opendialog.lStructSize = sizeof (opendialog);
opendialog.hwndOwner = NULL;
opendialog.hInstance = GetModuleHandle(NULL);
opendialog.lpstrFile = &szFile[0];
opendialog.nFilterIndex = 0;
opendialog.nMaxFile = 256;
opendialog.lpstrInitialDir = NULL;
opendialog.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
//This call blocks the main thread; blocking causes texture loading to fail
GetOpenFileName(&opendialog);
sf::Texture t1;
while (!t1.loadFromFile("C:\\Users\\acer\\Desktop\\document.png")) {
std::cout<<"Failed to load t1"<<std::endl;
}
sf::Texture t2;
while (!t2.loadFromFile("C:\\Users\\acer\\Desktop\\document.png")) {
std::cout<<"Failed to load t2"<<std::endl;
}
sf::Texture t3;
while (!t3.loadFromFile("C:\\Users\\acer\\Desktop\\document.png")) {
std::cout<<"Failed to load t3"<<std::endl;
}
sf::Sprite spr1;
sf::Sprite spr2;
sf::Sprite spr3;
spr1.setTexture(t1);
spr2.setTexture(t2);
spr3.setTexture(t3);
spr1.setPosition(0, 100);
spr2.setPosition(0, 200);
spr3.setPosition(0, 300);
sf::RenderWindow w1(sf::VideoMode(800, 600), "test");
while (true) {
w1.clear();
w1.draw(spr1);
w1.draw(spr2);
w1.draw(spr3);
w1.display();
}
std::cout<<"SUCCESS"<<std::endl;
return 0;
}
As you can see, t1, t2 and t3 are all loading the same file from an absolute path. However, t2 "fails quietly". t1 loads the right texture and displays. t3 loads the right texture and displays. t2 loads a blank texture and displays.. Nothing.
Also, in my slightly more complex code that I'm using, I used absolute paths to test it, loads a blank =/ And it's *also* the 2nd texture. I guess I could get around it by loading a blank texture twice after an OFD opens to ensure textures loaded after that run fine..
Hmm..
It would also appear that you were right about the OFD messing my directory up. Any idea how to get around that?