在 C++ 中使用 DevIL 和 OpenGL 加载图像

Odd*_*ore 3 c++ opengl image-processing devil

正如标题所示,我尝试使用 DevIL 加载图像,然后将其传递给 OpenGL。我使用这段代码作为错误控制

const char* filename = "back.bmp";

if (!ilLoadImage(filename))
{
    throw runtime_error(std::string("Unable to load image") +filename);
}
Run Code Online (Sandbox Code Playgroud)

对于 if 语句,返回错误

error C2664: 'ilLoadImage' : cannot convert parameter 1 from 'const char *' to 'const wchar_t *'
Run Code Online (Sandbox Code Playgroud)

如果我定义filenameconst wchar_t* filename,我会收到错误

error C2664: 'ilLoadImage' : cannot convert parameter 1 from 'const char *' to 'const wchar_t *'
Run Code Online (Sandbox Code Playgroud)

所以现在我不会因为好奇为什么 [filename].bmp 文件是 wchar_t* 类型,或者 wchar_t 是什么(谷歌让我困惑)而让你感到好奇,我只会问我必须做什么才能使它工作:为什么它不起作用?我错过了什么?我确信一定有一个非常简短的解决方案。它看起来就像是那种错误。

谢谢。

Bli*_*ndy 5

如果您使用 Unicode 函数,则必须使用 Unicode 文本。具体来说,L将返回静态文本的宽字符表示形式:

ilLoadImage(L"back.bmp");
Run Code Online (Sandbox Code Playgroud)

如果您正在为 ANSI 和宽字符串编译代码,请_T()改为使用,它会返回正确的字符串类型:

ilLoadImage(_T("back.bmp"));
Run Code Online (Sandbox Code Playgroud)

如果您处于后一种情况并且不知道char在编译时需要什么类型的 s,请使用TCHAR,它是 aCHAR或 a ,WCHAR具体取决于是否UNICODE定义。