gpt4 book ai didi

c++ - SDL2 两个窗口在不同的线程

转载 作者:太空狗 更新时间:2023-10-29 20:41:12 28 4
gpt4 key购买 nike

我正在尝试创建一个有两个窗口的进程。每个窗口都有不同的绘图线程。问题是,当我运行该程序时,经过一段随机的时间后出现问题并给我一个段错误。这是我的代码:

主要.cpp

std::thread *thr1 = new std::thread(thread_func);
std::thread *thr2 = new std::thread(thread_func);

线程.cpp:

 Window *win = new Window(display_name, 1024, 768);
Renderer *rend = win->CreateRenderer();
Texture *pic = new Texture(rend, path);

while (!quit)
{
usleep (10000);
pic->DrawToWindow(src_rect,rect);
rend->SetColor(255,255,255,255);
rend->DrawLine(10,10,300,300,4);

}
delete pic;
delete rend;
delete win;

窗口.cpp:

Window::Window(std::string &name, uint32_t w, uint32_t h, uint32_t x, uint32_t y)
: window(nullptr),
windowRect()
{
if (!SDL_WasInit(SDL_INIT_VIDEO))
{
PDEBUG("ERROR: SDL Was not inited please init platform first!\n");
return;
}

//Create Window
window = SDL_CreateWindow(name.c_str(), x, y, w, h, SDL_WINDOW_OPENGL);
if (window == nullptr)
{
PDEBUG("ERROR: SDL_CreateWindow() %s\n", SDL_GetError());
return;
}

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
}

Window::~Window()
{
if (window != nullptr)
{
SDL_DestroyWindow(window);
window = nullptr;
}
}

void
Window::Flush()
{
SDL_GL_SwapWindow(window);
}


Renderer*
Window::CreateRenderer()
{
return new Renderer(this);
}

渲染器.cpp:

Renderer::Renderer(Window *win)
: window(win),
render(nullptr),
context()
{
if (win == nullptr)
{
PDEBUG("ERROR: Window is NULL\n");
return;
}

//Create Renderer
render = SDL_CreateRenderer(win->window, -1, SDL_RENDERER_ACCELERATED);
if (render == nullptr)
{
PDEBUG("ERROR: SDL_CreateRenderer(): %s\n", SDL_GetError());
return;
}

//Create Window OpenGL Context
//context = SDL_GL_CreateContext(win->window);
//if (SDL_GL_MakeCurrent(window->window, context) != 0)
// PDEBUG("ERROR: SDL_GL_MakeCurrent() %s\n", SDL_GetError());

Clear();
}

Renderer::~Renderer()
{
if (render != nullptr)
{
SDL_DestroyRenderer(render);
render = nullptr;
//SDL_GL_DeleteContext(context);
}
}

bool
Renderer::DrawLine(int xStart, int yStart, int xEnd, int yEnd, int width)
{
//if (SDL_GL_MakeCurrent(window->window, context) != 0)
// PDEBUG("ERROR: SDL_GL_MakeCurrent() %s\n", SDL_GetError());
//}
glLineWidth(width);
if (SDL_RenderDrawLine(render, xStart, yStart, xEnd, yEnd) < 0)
{
PDEBUG("ERROR: SDL_RenderDrawLine() %s\n", SDL_GetError());
return false;
}
return true;
}

我是否必须为两个窗口仅在一个线程中绘制,使用同步绘图,或对线程使用 SDL_Thread

最佳答案

Did i have to draw in only one thread for two windows

是的。

From the SDL2 threading documentation:

NOTE: You should not expect to be able to create a window, render, or receive events on any thread other than the main one. For platform-specific exceptions or complicated options ask on the mailing list or forum.

关于c++ - SDL2 两个窗口在不同的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22320906/

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