SDL_LoadBMP() 成功,但窗口完全变黑

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

本文关键字:窗口 LoadBMP 成功 SDL      更新时间:2023-10-16

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

我遇到的问题是,当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()中提取它。