GetOpenFileName with SDL2

506 Views Asked by At

I wrote a c++ app using SDL2 to simulate an editBox. It worked ok untill i added a function to open and select a file on Windows explorer.

Precisely after i click "Open" on the file browser, i can not use TTF_OpenFont() anymore...

I am still able to use TextSprites i have declared at the initialisation but i can't change the string associate to them no more. And that's really annoying because my editBox have to display a string var in my main loop. I've already checked my font path with debug break points and it didn't change (still the same absolute path) nor the font size.

I've tryed many things to solve this : use another .ttf, use another TTF_Font *var, ect Also tryed to put my openfiledialog function in a separate thread, that didn't change anything, so i tryed to control the new thread with windows Events and then with Sdl_Event but had no luck. I obviously spent hours and hours of testing searching the web for similar issues and found nothing but unsolved posts.

Here is the funtion that allows me to get the name of the file opened :

void CMain::changeDirectoryPath()
{
    OPENFILENAME ofn;
    TCHAR szFile[MAX_PATH];
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.lpstrFile = szFile;
    ofn.lpstrFile[0] = '\0';
    ofn.hwndOwner = NULL;
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = TEXT("Text Files\0*.txt\0Any File\0*.*\0");
    ofn.nFilterIndex = 1;
    ofn.lpstrTitle   = TEXT("Select dictionary");
    ofn.lpstrInitialDir = L"data\\dictionary";
    ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
    if(GetOpenFileName(&ofn))
    {
        OutputDebugString(ofn.lpstrFile);
        int cSize = WideCharToMultiByte (CP_ACP, 0, ofn.lpstrFile, wcslen(ofn.lpstrFile), NULL, 0, NULL, NULL);
        string output(static_cast<size_t>(cSize), '\0');
        WideCharToMultiByte (CP_ACP, 0, ofn.lpstrFile, wcslen(ofn.lpstrFile), reinterpret_cast<char*>(&output[0]), cSize, NULL, NULL);
        cout<<output<<endl;
    }
    cdpOn = false;
}

And the one that used to change the text displayed on my TextSprite :

bool CDictionary::loadFromRenderedText(std::string textureText)
{
    if(Message!=NULL)
    {
        SDL_DestroyTexture(Message);
        Message = NULL;
        TTF_CloseFont(font);
    }
    font = TTF_OpenFont(filePath.c_str(), policeSize);
    if(!font)
    {
        cout<<"TTF_OpenFont: "<<TTF_GetError()<<endl;
        return 0;
    }
    textSurface = TTF_RenderText_Solid(font, textureText.c_str(), textColor);
    if(textSurface != NULL)
    {
        Message = SDL_CreateTextureFromSurface(renderer, textSurface);
        if(Message==NULL)
        {
            printf("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());
        }
        else
        {
            position.x=50;
            position.y=50;
            position.w=textSurface->w;
            position.h=textSurface->h;
        }
        SDL_FreeSurface(textSurface);
    }
    else
    {
        printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
    }
    return Message != NULL;
}

At last i thought to add WxWidget in my project and use wxFileDialog to see if it solve the problem but i'm affraid mixing SDL2 and wxWidget will resort in a savage mess :-(

Does anybody knows why i can not reopen a tt_font after i select and open a file with GetOpenFileName()?

Or have suggestion to possibly solve this?

Thanks in advance

1

There are 1 best solutions below

1
On

Comment under this functions MSDN page says "Current Working Directory is altered when a file is opened", which is exactly what you're describing. Revert it back with SetCurrentDirectory (query at launch with GetCurrentDirectory, once).

Another way would be not closing the font since you're using it quite often.