gpt4 book ai didi

c++ - SDL_LoadBMP()成功,但窗口全黑

转载 作者:行者123 更新时间:2023-11-30 05:42:01 26 4
gpt4 key购买 nike

如果这个问题已经被问到,我深表歉意,但我已经研究了大约一个星期,但在任何地方都找不到答案。

我遇到的问题是,当 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 文件位于同一目录中。

最佳答案

问题出在 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() 中提取它。

关于c++ - SDL_LoadBMP()成功,但窗口全黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30868427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com