SDL_LoadBMP() 成功,但 window 变成全黑

SDL_LoadBMP() is successful, but the window becomes entirely black

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

我遇到的问题是,虽然 SDL_LoadBMP() 成功加载图像,但 window 根本不渲染图像,而是渲染全黑屏幕。但是我确实知道正在加载某些东西(不仅仅是因为 SDL_LoadBMP() 没有返回错误,而且)因为当我 运行 带有 SDL_LoadBMP() 调用的程序评论时window 保持完全白色。

如果有帮助,我一直在编写下面的 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());
}

或者,您可以 return 甚至从 SurfaceList.back() 中提取它。