gpt4 book ai didi

c++ - SDL_Texture 导致一切呈现黑色

转载 作者:行者123 更新时间:2023-11-30 03:30:02 27 4
gpt4 key购买 nike

我一直在使用 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();
}
}
};

主要功能一次管理多个窗口,并分别为每个窗口提供 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,我在上面的代码中使用它来呈现绿色背景,从而暴露黑色图像和矩形形状。

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

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

最佳答案

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

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

关于c++ - SDL_Texture 导致一切呈现黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45229531/

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