无法获取 SDL_LoadBMP 来显示图像(C++)?
Cannot get SDL_LoadBMP to display an image(C++)?
void MainGame::drawGame() {
glClearDepth(1.0);
// clear colour and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
windowSurface = SDL_GetWindowSurface(_window);
menuImage = SDL_LoadBMP("\Liam C\Documents\Visual Studio 2015\Projects\graphicsPractice\graphicsPractice\ForehalenIntro_Screen.bmp");
if (menuImage == NULL) {
fatalError("Unable to load bitmap, 'ForhalenIntro_Screen.bmp'!");
}
//swap buffer/window displayed and draw to screen
SDL_GL_SwapWindow(_window);
}
// Wont exit unless _gameState is equal to EXIT
void MainGame::gameLoop() {
while (_gameState != GameState::EXIT) {
procInput();
drawGame();
}
}
我正在尝试在 window 上显示位图图像。我为 window 创建了一个 SDL_Surface,并为初始化为 NULL 的图像创建了一个 SDL_Surface。我的错误 "Unable to load bitmap, 'ForhalenIntro_Screen.bmp'!" 正在返回,所以我知道代码在 menuImage 被分配位图函数的行失败,该位图函数以图像的路径作为参数。我已经仔细检查了文件名、位置和路径。我试过只将文件名作为路径。该文件与我的 vcrxproj 文件和 main.cpp 文件位于同一文件夹中。我哪里出错了?我没有收到任何语法错误,而且我显然已经包含了必要的头文件。
编辑:
我现在也用 SDL_image 试过了,但还是不行。
您的问题是无意的转义(很可能是错误的路径)。
menuImage = SDL_LoadBMP("\Liam C\Documents\Visual Studio 2015\Projects\graphicsPractice\graphicsPractice\ForehalenIntro_Screen.bmp");
假设这是在 Visual Studio 为您创建的默认目录中,正确的路径是:"C:\Users\Liam C\Documents\Visual Studio 2015\Projects\graphicsPractice\graphicsPractice\ForehalenIntro_Screen.bmp"
在大多数编程语言中,\
用于创建通常无法键入的特殊字符,例如空字符 ('[=13=]'
) 或 Unicode 字符 (u16'\u263A'
或 u32'\U0001F60A'
)。因此,您在字符串中使用 \
字符(不小心)试图从 '\L'
、'\D'
、'\V'
、'\P'
、'\g'
、'\g'
和 '\F'
。
您可以加倍反斜杠('\'
组成 \
字符)或将分隔符更改为正斜杠:
menuImage = SDL_LoadBMP("C:\Users\Liam C\Documents\Visual Studio 2015\Projects\graphicsPractice\graphicsPractice\ForehalenIntro_Screen.bmp");
// OR
menuImage = SDL_LoadBMP("C:/Users/Liam C/Documents/Visual Studio 2015/Projects/graphicsPractice/graphicsPractice/ForehalenIntro_Screen.bmp");
void MainGame::drawGame() {
glClearDepth(1.0);
// clear colour and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
windowSurface = SDL_GetWindowSurface(_window);
menuImage = SDL_LoadBMP("\Liam C\Documents\Visual Studio 2015\Projects\graphicsPractice\graphicsPractice\ForehalenIntro_Screen.bmp");
if (menuImage == NULL) {
fatalError("Unable to load bitmap, 'ForhalenIntro_Screen.bmp'!");
}
//swap buffer/window displayed and draw to screen
SDL_GL_SwapWindow(_window);
}
// Wont exit unless _gameState is equal to EXIT
void MainGame::gameLoop() {
while (_gameState != GameState::EXIT) {
procInput();
drawGame();
}
}
我正在尝试在 window 上显示位图图像。我为 window 创建了一个 SDL_Surface,并为初始化为 NULL 的图像创建了一个 SDL_Surface。我的错误 "Unable to load bitmap, 'ForhalenIntro_Screen.bmp'!" 正在返回,所以我知道代码在 menuImage 被分配位图函数的行失败,该位图函数以图像的路径作为参数。我已经仔细检查了文件名、位置和路径。我试过只将文件名作为路径。该文件与我的 vcrxproj 文件和 main.cpp 文件位于同一文件夹中。我哪里出错了?我没有收到任何语法错误,而且我显然已经包含了必要的头文件。 编辑: 我现在也用 SDL_image 试过了,但还是不行。
您的问题是无意的转义(很可能是错误的路径)。
menuImage = SDL_LoadBMP("\Liam C\Documents\Visual Studio 2015\Projects\graphicsPractice\graphicsPractice\ForehalenIntro_Screen.bmp");
假设这是在 Visual Studio 为您创建的默认目录中,正确的路径是:"C:\Users\Liam C\Documents\Visual Studio 2015\Projects\graphicsPractice\graphicsPractice\ForehalenIntro_Screen.bmp"
在大多数编程语言中,\
用于创建通常无法键入的特殊字符,例如空字符 ('[=13=]'
) 或 Unicode 字符 (u16'\u263A'
或 u32'\U0001F60A'
)。因此,您在字符串中使用 \
字符(不小心)试图从 '\L'
、'\D'
、'\V'
、'\P'
、'\g'
、'\g'
和 '\F'
。
您可以加倍反斜杠('\'
组成 \
字符)或将分隔符更改为正斜杠:
menuImage = SDL_LoadBMP("C:\Users\Liam C\Documents\Visual Studio 2015\Projects\graphicsPractice\graphicsPractice\ForehalenIntro_Screen.bmp");
// OR
menuImage = SDL_LoadBMP("C:/Users/Liam C/Documents/Visual Studio 2015/Projects/graphicsPractice/graphicsPractice/ForehalenIntro_Screen.bmp");