Home > database >  Why does my window lag when I run multiple instances of it?
Why does my window lag when I run multiple instances of it?

Time:12-24

I created a win32 window app that moves around the screen occasionally, sort of like a pet. As it moves, it switches between 2 bitmaps to show 'animation' of it moving. The implementation involves multiple WM_TIMER messages: One timer Moves the window, Another changes the bitmap and windows region (to only display the bitmap without the transparent parts) as it is moving, and another changes the direction the window moves.

The window runs perfectly smoothly by itself, but when I open multiple instances, the animations and movements start to lag - it is not so noticeable at 2 windows, but 3 instances and above causes every single window to start lagging very noticably. The movement and animations are choppy and even freeze occasionaly.

I have tried removing portions of the code to pinpoint the cause of the issue, and apparently this only occurs when a section of the following code is put in (I have marked it out with comments):

 HBITMAP hBitMap = NULL;
                BITMAP infoBitMap;
                hBitMap = LoadBitmap(GetModuleHandle(NULL), IDB_BITMAP2);
                if (hBitMap == NULL)
                {
                    MessageBoxA(NULL, "COULD NOT LOAD PET BITMAP", "ERROR", MB_OK);
                }

                HRGN BaseRgn = CreateRectRgn(0, 0, 0, 0);
                HDC winDC = GetDC(hwnd);
                HDC hMem = CreateCompatibleDC(winDC);
                GetObject(hBitMap, sizeof(infoBitMap), &infoBitMap);
                HDC hMemOld = SelectObject(hMem, hBitMap);
                COLORREF transparentCol = RGB(255, 255, 255);
                for (int y = 0; y < infoBitMap.bmHeight; y  ) //<<<< THIS SECTION ONWARDS
                {

                    int x, xLeft, xRight;
                    x = 0;
                    do {
                        xLeft = xRight = 0;
                        while (x < infoBitMap.bmWidth && (GetPixel(hMem, x, y) == transparentCol))
                        {
                            x  ;
                        }
                        xLeft = x;
                        while (x < infoBitMap.bmWidth && (GetPixel(hMem, x, y) != transparentCol))
                        {
                            x  ;
                        }
                        xRight = x;
                        HRGN TempRgn;
                        TempRgn = CreateRectRgn(xLeft, y, xRight, y   1);
                        int ret = CombineRgn(BaseRgn, BaseRgn, TempRgn, RGN_OR);
                        if (ret == ERROR)
                        {
                            MessageBoxA(NULL, "COMBINE REGION FAILED", "ERROR", MB_OK);
                        }
                        DeleteObject(TempRgn);
                    } while (x < infoBitMap.bmWidth);
                }
                SetWindowRgn(hwnd, BaseRgn, TRUE);   //<<<<---- UNTIL HERE
                BitBlt(winDC, 0, 0, infoBitMap.bmWidth, infoBitMap.bmHeight, hMem, 0, 0, SRCCOPY);
                SelectObject(hMem, hMemOld);
                DeleteDC(hMem);
                ReleaseDC(hwnd, winDC);

The commented section is the code I use to eliminate the transparent parts of the bitmap from being displayed in the window client region. It is run every time the app changes bitmap to display animation.

The app works perfectly fine if I remove that code, so I suspect this is causing the issue. Does someone know why this section of code causes lag, and ONLY with multiple instances open? Is there a way to deal with this lag? Thank you very much for taking the time to read through this.

CodePudding user response:

You're iterating over each pixel in each update (correct me if I'm wrong.) which is a fairly slow process (relatively.)

A better option would be to use something like this: https://stackoverflow.com/a/3970218/19192256 to create a mask color and simply use masking to remove the transparent pixels.

CodePudding user response:

creating multiple regions and concatenating them is a very slow and resource/cpu-intensive operation. Instead, use ExtCreateRegion() to create a single region from an array of rectangles.

Alternatively, forget using a region at all. Simply display your bitmap on the window normally and fill in the desired areas of the window with a unique color that you can make transparent using SetLayeredWindowAttributes(), as described in @Substitute's answer.

  • Related