editBox をシミュレートするために SDL2 を使用して C++ アプリを作成しました。Windowsエクスプローラーでファイルを開いて選択する機能を追加するまで、問題なく機能しました。
ファイルブラウザで「開く」をクリックした直後に、TTF_OpenFont()が使えなくなりました...
初期化時に宣言した TextSprites を引き続き使用できますが、それらに関連付けられている文字列を変更することはできません。editBox はメイン ループで文字列 var を表示する必要があるため、これは非常に面倒です。デバッグ ブレーク ポイントを使用してフォント パスを既に確認しましたが、フォント サイズも変更されませんでした (絶対パスも同じです)。
私はこれを解決するために多くのことを試みました: 別の .ttf を使用し、別の TTF_Font *var を使用します。 windows Events を実行してから Sdl_Event を使用しましたが、うまくいきませんでした。私は明らかに、同様の問題をウェブで検索するのに何時間も費やしましたが、未解決の投稿しか見つかりませんでした.
開いたファイルの名前を取得できる機能は次のとおりです。
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;
}
そして、 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;
}
最後に、プロジェクトに WxWidget を追加し、wxFileDialog を使用して問題が解決するかどうかを確認することを考えましたが、SDL2 と wxWidget を混在させると、野蛮な混乱に陥ります :-(
GetOpenFileName() でファイルを選択して開いた後、tt_font を再度開くことができない理由を知っている人はいますか?
または、これを解決するための提案がありますか?
前もって感謝します