SDL_Texture 导致一切都呈现黑色

SDL_Texture causes everything to render black

我一直在使用 SDL 开发一个相当大的应用程序,最近发现了一个奇怪的错误;每当 window 关闭时,另一个 window 上的所有内容都会呈现完全黑色。当我画一条红色的线时,它会变成黑色,而当我画一个 SDL_Texture 时,它会在图像的位置绘制一个黑色的矩形。

一段时间后,我通过从头开始制作应用程序的简化版本,成功地重现了这个问题。该程序包括一个 window class,它存储一个 window、它的渲染器和一个 SDL_Texture。 window class 还包含一个渲染函数,用于将内容渲染到其 window 上,包括 SDL_Texture。还有一个函数允许每个 window 对象处理 SDL 事件,例如 window 被关闭。当window关闭时,window、渲染器和纹理都被销毁。

class Window {
    SDL_Window *window;
    SDL_Renderer *renderer;
    Uint32 windowID;

    SDL_Texture *texture;

    void cleanup() {
        SDL_DestroyWindow(window);
        SDL_DestroyRenderer(renderer);
        SDL_DestroyTexture(texture);
    }

    bool running = true;
public:

    void init() {

        window = SDL_CreateWindow("Window", 50, 50, 721, 558, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
        renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
        windowID = SDL_GetWindowID(window);
        SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);

        // load texture
        SDL_Surface* loadedSurface = IMG_Load("picture.png");
        texture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
        SDL_FreeSurface(loadedSurface);

    }

    bool isRunning() {
        return running;
    }

    void render() {
        // clear the screen with a green color
        SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
        SDL_RenderClear(renderer);

        // create a rectangle for drawing things
        SDL_Rect rect = {0, 0, 100, 100};

        // draw a red rectangle
        SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
        SDL_RenderFillRect(renderer, &rect);

        // draw the texture/image on top of the rectangle
        if (SDL_RenderCopy(renderer, texture, NULL, &rect) < 0) {
            printf("Unable to render texture! Error: %s\n", SDL_GetError());
        }



        SDL_RenderPresent(renderer);
    }

    void events(SDL_Event &e) {
        if (e.window.windowID == windowID && e.window.event == SDL_WINDOWEVENT_CLOSE) {
            running = false;
            cleanup();
        }
    }
};

main 函数一次管理多个 windows,并分别提供每个 SDL 事件:

int main(int argc, const char * argv[]) {

    if (SDL_Init(SDL_INIT_VIDEO) < 0) { // initialize SDL and IMG
        printf("SDL could not initialize! Error: %s\n", SDL_GetError());
    } else if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
        printf("SDL_image could not initialize! Error: %s\n", IMG_GetError());
    } else {
        std::vector<Window*> windows; // vector of window objects

        // add window objects to the vector
        windows.push_back(new Window());
        windows.push_back(new Window());
        windows.push_back(new Window());
        windows.push_back(new Window());

        // initialize all windows
        for (int i = 0; i < windows.size(); i++) {
            windows[i]->init();
        }

        // game loop
        bool loop = true;
        while (loop) {
            SDL_Delay(50); // delay between each frame

            // render all windows
            for (int i = 0; i < windows.size(); i++) {
                windows[i]->render();
            }

            // handle new events
            SDL_Event e;
            while (SDL_PollEvent(&e)) {

                // loop backward through windows
                // in case one of them has to be
                // removed from the vector
                for (unsigned long i = windows.size(); i-- > 0;) {
                    if (windows[i]->isRunning()) {
                        windows[i]->events(e);
                    } else {
                        // delete a window if it has been closed
                        delete windows[i];
                        windows.erase(windows.begin() + i);
                    }
                }
            }

            if (windows.empty()) { // if all windows are closed,
                loop = false;      // stop the loop
            }
        }

    }

    return 0;
}

现在我已经展示了我的代码,我将更详细地解释这个问题。程序启动时,会创建四个 window 对象,并且每个 windows 都显示在屏幕上。每个 windows 都正确渲染,PNG 图像覆盖在红色矩形的顶部。第四个 window,或最后一个要初始化的,将仅在任何其他 window 关闭后 [=27] 渲染黑色 中的图像、矩形和线条等内容[=27] =].唯一会以不同颜色呈现的是 SDL_RenderClear,我在上面的代码中使用它来呈现绿色背景,从而暴露出黑色图像和矩形形状。

我已经测试确保纹理、渲染器和 window 在出现此问题后仍然有效,并且没有错误。

我至少想找出导致问题的原因以及是否有任何可能的解决方案。

你不应该在渲染器还活着的时候销毁window。尝试以相反的顺序在 cleanup() 中执行方法。

void cleanup()
{
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
}