如果已经提出这个问题,我很抱歉,但我现在已经研究了大约一个星期而且无法找到答案。
我遇到的问题是,当SDL_LoadBMP()成功加载图像时,窗口根本不渲染图像,而是呈现完全黑屏。但是我确实知道正在加载某些东西(不仅仅是因为SDL_LoadBMP()还没有返回错误,因为当我运行带有SDL_LoadBMP()调用的程序注释掉时窗口保持完全白色。
如果有帮助我一直在写下the Lazyfoo tutorial located here.代码......
来自Main.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文件位于同一目录中。
答案 0 :(得分:1)
问题出在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()
中提取。