From d9042e46098a903de391e74ac1f5f12340c3cfee Mon Sep 17 00:00:00 2001 From: Marc-Antoine Lortie Date: Sat, 15 Feb 2020 18:02:12 -0500 Subject: [PATCH] Fixed SimpleTexturedOpenGL sample. Several places in the sample's code were calling Unicode versions of Win32 functions with "multibyte" strings. A few changes were required to fix it. I added a class "UTFConverter", which handles calls to unicode/multibyte string conversions. This should help minimize the impacts on code change in case C++'s codecvt_utf8 ever changes. In addition, seveal memory leaks have been found, but these fixes will come in another PR because it goes beyond the scope of this PR. DevIL.lib was removed in CMakeFiles.txt, as it is unused in the sample. Here is a list of the changes: - Fixed MB string calls to Unicode functions. - Added class UTFConverter to handle string conversions. - Removed reference to DevIL.lib. - Fixed compile warnings. --- samples/SimpleTexturedOpenGL/CMakeLists.txt | 2 +- .../src/model_loading.cpp | 74 +++++++++++++------ 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/samples/SimpleTexturedOpenGL/CMakeLists.txt b/samples/SimpleTexturedOpenGL/CMakeLists.txt index 40138c49b..941e18cea 100644 --- a/samples/SimpleTexturedOpenGL/CMakeLists.txt +++ b/samples/SimpleTexturedOpenGL/CMakeLists.txt @@ -35,7 +35,7 @@ ADD_EXECUTABLE( assimp_simpletexturedogl WIN32 SET_PROPERTY(TARGET assimp_simpletexturedogl PROPERTY DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX}) -TARGET_LINK_LIBRARIES( assimp_simpletexturedogl assimp ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} DevIL.lib ) +TARGET_LINK_LIBRARIES( assimp_simpletexturedogl assimp ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ) SET_TARGET_PROPERTIES( assimp_simpletexturedogl PROPERTIES OUTPUT_NAME assimp_simpletexturedogl diff --git a/samples/SimpleTexturedOpenGL/SimpleTexturedOpenGL/src/model_loading.cpp b/samples/SimpleTexturedOpenGL/SimpleTexturedOpenGL/src/model_loading.cpp index 8d25aaaed..8428248d1 100644 --- a/samples/SimpleTexturedOpenGL/SimpleTexturedOpenGL/src/model_loading.cpp +++ b/samples/SimpleTexturedOpenGL/SimpleTexturedOpenGL/src/model_loading.cpp @@ -21,6 +21,8 @@ #define STB_IMAGE_IMPLEMENTATION #include "contrib/stb_image/stb_image.h" +#include +#include #include //to map image filenames to textureIds @@ -75,6 +77,36 @@ GLuint* textureIds; // pointer to texture Array // Create an instance of the Importer class Assimp::Importer importer; +// Used to convert between multibyte and unicode strings. +class UTFConverter { + using UTFConverterImpl = std::wstring_convert, wchar_t>; +public: + UTFConverter(const char* s) : + s_(s), + ws_(impl_.from_bytes(s)) { + } + UTFConverter(const std::string& s) : + s_(s), + ws_(impl_.from_bytes(s)) { + } + UTFConverter(const std::wstring& s) : + s_(impl_.to_bytes(s)), + ws_(s) { + } + inline const std::string& str() const { + return s_; + } + inline const wchar_t* c_wstr() const { + return ws_.c_str(); + } +private: + static UTFConverterImpl impl_; + std::string s_; + std::wstring ws_; +}; + +typename UTFConverter::UTFConverterImpl UTFConverter::impl_; + void createAILogger() { // Change this line to normal if you not want to analyse the import process @@ -120,7 +152,7 @@ bool Import3DFromFile( const std::string& pFile) } else { - MessageBox(NULL, ("Couldn't open file: " + pFile).c_str() , "ERROR", MB_OK | MB_ICONEXCLAMATION); + MessageBox(NULL, UTFConverter("Couldn't open file: " + pFile).c_wstr() , TEXT("ERROR"), MB_OK | MB_ICONEXCLAMATION); logInfo( importer.GetErrorString()); return false; } @@ -205,7 +237,7 @@ int LoadGLTextures(const aiScene* scene) } } - int numTextures = textureIdMap.size(); + const size_t numTextures = textureIdMap.size(); /* array with DevIL image IDs */ @@ -217,13 +249,13 @@ int LoadGLTextures(const aiScene* scene) /* create and fill array with GL texture ids */ textureIds = new GLuint[numTextures]; - glGenTextures(numTextures, textureIds); /* Texture name generation */ + glGenTextures(static_cast(numTextures), textureIds); /* Texture name generation */ /* get iterator */ std::map::iterator itr = textureIdMap.begin(); std::string basepath = getBasePath(modelpath); - for (int i=0; i 1) { std::wstring modelpathW(argv[1]); - modelpath = std::string(modelpathW.begin(), modelpathW.end()); + modelpath = UTFConverter(modelpathW).str(); } if (!Import3DFromFile(modelpath)) return 0; logInfo("=============== Post Import ===================="); - if (MessageBox(NULL, "Would You Like To Run In Fullscreen Mode?", "Start Fullscreen?", MB_YESNO|MB_ICONEXCLAMATION)==IDNO) + if (MessageBox(NULL, TEXT("Would You Like To Run In Fullscreen Mode?"), TEXT("Start Fullscreen?"), MB_YESNO|MB_ICONEXCLAMATION)==IDNO) { fullscreen=FALSE; } @@ -881,5 +913,5 @@ int WINAPI WinMain( HINSTANCE hInstance, // The instance destroyAILogger(); KillGLWindow(); - return (msg.wParam); + return static_cast(msg.wParam); }