SDL_LoadBMP() 成功,但窗口完全变黑
SDL_LoadBMP() is successful, but the window becomes entirely black
如果这个问题已经被问过,我很抱歉,但我已经研究了大约一个星期,在任何地方都找不到答案。
我遇到的问题是,当SDL_LoadBMP()成功加载图像时,窗口根本不渲染图像,而是渲染完全黑屏。但是,我确实知道正在加载某些内容(不仅因为 SDL_LoadBMP() 没有返回错误,还因为当我在注释出 SDL_LoadBMP() 调用的情况下运行程序时,窗口保持完全白色。
如果它有帮助,我一直在编写位于此处的 Lazyfoo 教程。下面的代码...
从主.cpp
int main(int argc, char* args[])
{
//the surface that we will be applying an image on
SDL_Surface* ImageSurface = NULL;
//try to initalize SDL
try
{
initSDL();
}
//if an error is caught
catch (string Error)
{
//print out the error
cout << "SDL error occurred! SDL Error: " << Error << endl;
//return an error
return -1;
}
//try loading an image on to the ImageSurface
try
{
loadMedia(ImageSurface, "ImageTest.bmp");
}
//if an error is caught
catch(string Error)
{
//print the error out
cout << "SDL error occurred! SDL Error: " << Error << endl;
//return an error
SDL_Delay(6000);
return -1;
}
//Apply Image surface to the main surface
SDL_BlitSurface(ImageSurface, NULL, Surface, NULL);
//upadte the surface of the main window
SDL_UpdateWindowSurface(Window);
//wait for 2 seconds (2000 miliseconds)
SDL_Delay(10000);
//close SDL
close();
//return
return 0;
}
来自 SDLBackend.cpp(我只会发布与图像加载过程相关的代码)
void loadMedia(SDL_Surface* surface, string path)
{
cout << "Attempting to load an image!" << endl;
//load the image at path into our surface
surface = SDL_LoadBMP(path.c_str());
//if there was an error in the loading procdure
if(surface == NULL)
{
//make a string to store our error in
string Error = SDL_GetError();
//throw our error
throw Error;
}
cout << "Successfully loaded an image!" << endl;
cout << "Pushing surface into the Surface List" << endl;
//Put the surface in to our list
SurfaceList.push_back(surface);
return;
}
我正在使用Visual Studio 2013进行编译,图像ImageTest.bmp
与vcxproj文件位于同一目录中。
问题出在loadMedia()
中。加载的曲面被指定给局部变量。您需要使用对指针的引用,
void loadMedia(SDL_Surface*& surface, string path)
{
surface = SDL_LoadBMP(path.c_str());
}
或双指针(也许首选,澄清意图),
void loadMedia(SDL_Surface** surface, string path)
{
*surface = SDL_LoadBMP(path.c_str());
}
或者,您可以将其退回,甚至可以从SurfaceList.back()
中提取它。
相关文章:
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 问:如何使用C++中的按钮从窗口打开窗口
- SDL 窗口不会弹出
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 如何在cpp文件之间切换窗口?在Qt中
- QuadTree只在窗口的右上角绘制
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 如何在C++中找到active directory中禁用和锁定的窗口帐户
- 处理闪烁窗口事件
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- C++win32 API创建多个类似视口的窗口
- SFML RenderWindow打开窗口需要很长时间
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 获取 SFML 窗口的 HWND 和高可用性?
- 如何获取 GLFW 窗口 ID?
- GLEW/GLUT:调用init并创建一个窗口后,取消初始化并重新初始化?
- 用于窗口的 HID 终端
- SFML 文本未绘制在窗口上
- 如何为窗口截屏
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息