简体   繁体   English

优化GDI +绘图?

[英]Optimizing GDI+ drawing?

I'm using C++ and GDI+ I'm going to be making a vector drawing application and want to use GDI+ for the drawing. 我正在使用C ++和GDI +,我将要制作矢量绘图应用程序,并想在绘图中使用GDI +。

I'v created a simple test to get familiar with it: 我创建了一个简单的测试来熟悉它:

case WM_PAINT:
        GetCursorPos(&mouse);
        GetClientRect(hWnd,&rct);

        hdc = BeginPaint(hWnd, &ps);
        MemDC = CreateCompatibleDC(hdc);
        bmp = CreateCompatibleBitmap(hdc, 600, 600);
        SelectObject(MemDC,bmp);
        g = new Graphics(MemDC);

        for(int i = 0; i < 1; ++i)
        {
            SolidBrush sb(Color(255,255,255));
            g->FillRectangle(&sb,rct.top,rct.left,rct.right,rct.bottom);
        }

        for(int i = 0; i < 250; ++i)
        {
            pts[0].X = 0;
            pts[0].Y = 0;

            pts[1].X = 10 + mouse.x * i;
            pts[1].Y = 0 + mouse.y * i;

            pts[2].X = 10 * i + mouse.x;
            pts[2].Y = 10 + mouse.y * i;

            pts[3].X = 0 + mouse.x;
            pts[3].Y = (rand() % 600) + mouse.y;

            Point p1, p2;
            p1.X = 0;
            p1.Y = 0;
            p2.X = 300;
            p2.Y = 300;

            g->FillPolygon(&b,pts,4);
        }


        BitBlt(hdc,0,0,900,900,MemDC,0,0,SRCCOPY);

        EndPaint(hWnd, &ps);

        DeleteObject(bmp);
        g->ReleaseHDC(MemDC);
        DeleteDC(MemDC);
        delete g;
        break;

I'm wondering if I'm doing it right, or if I have areas killing the cpu. 我想知道我是否做对了,还是有地方杀死了CPU。 Because right now it takes ~ 1sec to render this and I want to be able to have it redraw itself very quickly. 因为现在需要大约1秒的时间才能渲染此图像,所以我希望能够使其快速重绘。 Thanks 谢谢

In a real situation would it be better just to figure out the portion of the screen to redraw and only redraw the elements withing bounds of this? 在实际情况下,仅找出屏幕上要重绘的部分并仅在此范围内重绘元素会更好吗?

The slowest line in your code at the moment is probably BitBlt(hdc,0,0,900,900,MemDC,0,0,SRCCOPY); 目前,您代码中最慢的行可能是BitBlt(hdc,0,0,900,900,MemDC,0,0,SRCCOPY); . Blit functions are usually very slow and hard on the CPU. Blit函数通常非常慢,并且在CPU上很难。 Using the GPU would be faster, but far more involved (if it's even possible to tie into GDI+). 使用GPU会更快,但会涉及更多(如果甚至可以绑定到GDI +)。 Try to find a way to draw to the surface you'll be using in the end, without copying pixel-by-pixel. 尝试找到一种方法来绘制最终要使用的表面,而不逐像素复制。

Instead of creating all the resources and tearing them all down for every WM_PAINT, you could try off-loading this to application setup and cleanup. 您可以尝试将其卸载到应用程序设置和清理中,而不必创建所有资源并为每个WM_PAINT拆除所有资源。 In other words, shift all the stuff like CreateCompatibleDC, CreateCompatibleBitmap to WM_CREATE, and the corresponding deletes to WM_DESTROY. 换句话说,将诸如CreateCompatibleDC,CreateCompatibleBitmap之类的所有内容移动到WM_CREATE,并将相应的删除内容移动到WM_DESTROY。 You can keep references to all the device contexts, brush handles, and the like as class attributes or static variables. 您可以保留对所有设备上下文,画笔句柄等的引用,作为类属性或静态变量。 That way when it's WM_PAINT time, all the setup is already done, and you only need to handle the actual drawing. 这样,当它是WM_PAINT时,所有的设置都已经完成,您只需要处理实际的图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM