SDL2两个窗口在不同的线程中

时间:2014-03-11 09:03:52

标签: c++ sdl sdl-2

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

的main.cpp

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

thread.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;

Window.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);
}

Renderer.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作为线程?

2 个答案:

答案 0 :(得分:5)

  

我是否只需要为两个窗口绘制一个线程

From the SDL2 threading documentation:

  

注意:您不应期望能够在除主要线程之外的任何线程上创建窗口,呈现或接收事件。对于特定于平台的例外或复杂的选项,请在邮件列表或论坛上查询。

答案 1 :(得分:-1)

您好我在不同的线程中找到了2个窗口的解决方案。 这是一个例子:

static bool quit = false;

void
thread_function(SDL_Window* win, SDL_Renderer *rend)
{
  SDL_Event event;
  while(!quit)
  {
     //DO THINGS with renderer and window. (Draw, Fill, Present Textures and others)

     SDL_PollEvent(&event);
  }
}

对于每个线程,您需要轮询事件,因为窗口事件也在事件查询中。