我在 SDL 中开发了一个相当大的应用程序有一段时间了,最近发现了一个奇怪的错误;每当关闭一个窗口时,另一个窗口上的所有内容都将完全呈现黑色。当我画一条红色的线时,它会是黑色的,当我画一个 SDL_Texture 时,它会画一个黑色的矩形来代替图像。
过了一段时间,我设法通过从头开始制作应用程序的简化版本来重现问题。该程序包括一个存储窗口、其渲染器和 SDL_Texture 的窗口类。窗口类还包括一个渲染函数,用于将内容渲染到其窗口,包括 SDL_Texture。还有一个函数允许每个窗口对象处理 SDL 事件,例如窗口被关闭。当窗口关闭时,窗口、渲染器和纹理都被销毁。
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 函数一次管理多个窗口,并分别为每个窗口提供 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;
}
现在我已经展示了我的代码,我将更详细地解释这个问题。程序启动时,会创建四个窗口对象,每个窗口都显示在屏幕上。每个窗口都正确渲染,一个 PNG 图像覆盖在一个红色矩形的顶部。第四个窗口,或最后一个要初始化的窗口,只会在任何其他窗口关闭后以黑色渲染图像、矩形和线条等内容。唯一会以不同颜色渲染的是SDL_RenderClear
,我在上面的代码中使用它来渲染绿色背景,它暴露了黑色图像和矩形形状。
我已经测试确保纹理、渲染器和窗口在出现此问题后仍然有效,并且没有错误。
我至少想找出导致问题的原因以及是否有任何可能的解决方案。