我如何减慢此填充算法,以便用户可以逐渐填充图像?

发布于 2025-01-26 12:54:10 字数 1409 浏览 2 评论 0原文

我尝试使用sleep()redraw()结合使用,但这会导致程序崩溃。 flood_fineded消息导致调用redraw()

void Redraw()
{
    InvalidateRect(g_hWnd, NULL, FALSE);
    UpdateWindow(g_hWnd);
}
void FloodFillUtil(CDIB32* scr, int xPos, int yPos)
{
    //using namespace std::chrono_literals;
    if (IsValid(scr, xPos, yPos))
    {
        //Redraw();
        scr->SetRGB(xPos, yPos, 255, 0, 0);
        FloodFillUtil(scr, xPos + 1, yPos);
        FloodFillUtil(scr, xPos - 1, yPos);
        FloodFillUtil(scr, xPos, yPos + 1);
        FloodFillUtil(scr, xPos, yPos - 1);
    }
    return;
}

void FloodFill(CDIB32* scr, CPoint& mid)
{
    int xPos = mid.x;
    int yPos = mid.y;
    FloodFillUtil(scr, xPos, yPos);
}
void SetupDoItThread()
{
    std::thread Worker(DoIt);
    Worker.join();
}


void SetupDogThreads(CPoint mid) 
{
    //Redraw();
    FloodFill(&m_screenDib, mid);
}

void DoIt()
{
    if (!m_done)
    {
        CPoint mid;
        std::vector<std::thread> threads;
        for (int i = 0; i < REZ * REZ; ++i)
        {
            CPoint mid;
            GetBlobMidPoint(i, mid);
            threads.push_back(std::thread(SetupDogThreads, mid));
        }
        for (auto& th : threads)
        {
            th.join();
        }
        ::PostMessage(g_hWnd, FLOOD_FINISHED, NULL, NULL);
        m_done = true;
    }
}

I have tried to use sleep() in conjunction with Redraw() but this causes the program to crash. The FLOOD_FINISHED message causes a call to Redraw()

void Redraw()
{
    InvalidateRect(g_hWnd, NULL, FALSE);
    UpdateWindow(g_hWnd);
}
void FloodFillUtil(CDIB32* scr, int xPos, int yPos)
{
    //using namespace std::chrono_literals;
    if (IsValid(scr, xPos, yPos))
    {
        //Redraw();
        scr->SetRGB(xPos, yPos, 255, 0, 0);
        FloodFillUtil(scr, xPos + 1, yPos);
        FloodFillUtil(scr, xPos - 1, yPos);
        FloodFillUtil(scr, xPos, yPos + 1);
        FloodFillUtil(scr, xPos, yPos - 1);
    }
    return;
}

void FloodFill(CDIB32* scr, CPoint& mid)
{
    int xPos = mid.x;
    int yPos = mid.y;
    FloodFillUtil(scr, xPos, yPos);
}
void SetupDoItThread()
{
    std::thread Worker(DoIt);
    Worker.join();
}


void SetupDogThreads(CPoint mid) 
{
    //Redraw();
    FloodFill(&m_screenDib, mid);
}

void DoIt()
{
    if (!m_done)
    {
        CPoint mid;
        std::vector<std::thread> threads;
        for (int i = 0; i < REZ * REZ; ++i)
        {
            CPoint mid;
            GetBlobMidPoint(i, mid);
            threads.push_back(std::thread(SetupDogThreads, mid));
        }
        for (auto& th : threads)
        {
            th.join();
        }
        ::PostMessage(g_hWnd, FLOOD_FINISHED, NULL, NULL);
        m_done = true;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文