Results 1 to 3 of 3

Thread: Double Buffering in Win32 API

  1. #1
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default Double Buffering in Win32 API

    Having some trouble here. Trying to get double buffering working properly. Here is what I'm working with:

    Basically, the flickering is gone, but it consumes more memory than I want it to and after sometime, it doesn't crash, but it just locks up so I assume there is a memory leak. Can anyone tell me please what I can do to resolve this?
    PHP Code:

    #include <windows.h>
    #include <stdio.h>

    int screenWidth 0;
    int screenHeight 0;

    //Center Window
    VOID CenterWindow(HWND hwndHWND hwndParentint Widthint Height)
    {
        
    /* Variables */
        
    RECT rc;
     
        
    /* If Parent Window Is Set As Null, Get The Desktop Window */
        
    if(hwndParent == NULL)
        {
            
    hwndParent GetDesktopWindow();
        }
     
        
    /* Get Parent Client Area Measurements */
        
    GetClientRect(hwndParent, &rc);
     
        
    /* Center The Window */
        
    MoveWindow(hwnd, (rc.right rc.left Width) / 2, (rc.bottom rc.top Height) / 2WidthHeightTRUE);
     
        return;
    }

    bool isRunning()
    {
        
    //This checks if the process is already running so you don't have multiple instances running.
        
    HANDLE handle CreateMutex(NULLtrue"myPong");
        if(
    GetLastError() != ERROR_SUCCESS)
        {
            
    MessageBox(0"Process is already running""Warning"MB_ICONWARNING);
            return 
    false;
        }
        else
            return 
    true;
    }

    RECT initRecArea(int leftint topint widthint height

        
    RECT rec = {lefttopwidthheight}; 
        return 
    rec


    void DrawFilledRectHWND hWndHDC hDCRECTr

        
    hDC GetDC(hWnd);
        
    HDC hdcMem CreateCompatibleDC(hDC);
        
    HBITMAP hBmMem CreateCompatibleBitmap(hDCscreenWidthscreenHeight);
        
    HBITMAP hBmOld = (HBITMAPSelectObject(hdcMemhBmMem);

        
    RECT rec initRecArea(rec.leftrec.top00);
        
        
    FillRect(hDC, &recCreateSolidBrush(RGB(00255))); 
        
    BitBlt(hDC00screenWidthscreenHeighthdcMem00SRCCOPY);
        
        
    InvalidateRect(hWnd,&rec,TRUE);
        
    ReleaseDC(hWndhDC);
        
    DeleteObject(hBmMem);
        
    DeleteDC(hdcMem);


    LRESULT CALLBACK WndProc(HWND hWndUINT messageWPARAM wParamLPARAM lParam)
    {
        
    PAINTSTRUCT ps;
        
    HDC hdc;
        
    RECT rc = {0};
        
    HBRUSH hBr;
        
    HGDIOBJ hOld;

        switch (
    message)
        {
            
            case 
    WM_ERASEBKGND:
            {
                return 
    TRUE;
            }
            break;

            case 
    WM_PAINT:
            {
                
    hdc GetDC(hWnd);
                
    ReleaseDChWndhdc );
                
    hdc BeginPaint(hWnd, &ps);            

                
    // TODO: Add any drawing code here...
                
    GetClientRecthWnd, &rc ); 
                
    AdjustWindowRectEx( &rcWS_OVERLAPPED WS_CAPTION WS_SYSMENUFALSENULL ); 
                
    DrawFilledRect(hWndhdcrc);
                
    ReleaseDChWndhdc );
                
    InvalidateRect(hWnd,&rc,TRUE);

                
    EndPaint(hWnd, &ps);
            }
            break;

            case 
    WM_CREATE:
            {
                
    SetTimerhWndNULLUSER_TIMER_MINIMUMNULL );
            }
            break;

            case 
    WM_TIMER:
            {
                
    GetClientRecthWnd, &rc );
                
    InvalidateRecthWnd, &rcTRUE );
            }
            break;

            case 
    WM_KEYDOWN:
            {
                 if(
    wParam == VK_ESCAPE
                 {
                     
    int ret MessageBox(NULLTEXT("Are you sure to quit?"), TEXT("Message"), MB_OKCANCEL);
                     
                     if(
    ret == IDOK
                     {
                         
    SendMessage(hWndWM_CLOSE00);
                     }
                 }
            }
            break;

            case 
    WM_DESTROY:
                
    PostQuitMessage(0);
            break;

            default:
                return 
    DefWindowProc(hWndmessagewParamlParam);
        }
        return 
    0;
    }

    int APIENTRY WinMainHINSTANCE hInstanceHINSTANCE hPrevInstanceLPSTR lpCmdLineint nCmdShow )
    {
        
    //Check if this process is running
        
    if(isRunning() == false)
            return 
    0;
        
        
    char szWindowClass[] = "myPong_Game";
        
    MSG msg;

        
    WNDCLASSEX wcex        = {0};
        
    wcex.cbSize            sizeof(WNDCLASSEX);
        
    wcex.style          CS_HREDRAW CS_VREDRAW;
        
    wcex.lpfnWndProc    WndProc;
        
    wcex.hInstance      hInstance;
        
    wcex.hCursor        LoadCursorNULLIDC_ARROW );
        
    wcex.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
        
    wcex.lpszClassName  szWindowClass;
        
    wcex.hIcon            LoadIcon(NULLIDI_APPLICATION);
        
    wcex.hIconSm        LoadIcon(NULLIDI_APPLICATION);
        
    RegisterClassEx( &wcex );

                
        
    //Main Application Window
        
    HWND hWnd CreateWindowEx(0szWindowClass"myPong"WS_POPUPWINDOW WS_CAPTION WS_MAXIMIZEBOX00600400NULLNULLhInstanceNULL);
        
        
    // Do stuff here

        //End

        
    if(!hWnd)
        {
            return 
    FALSE;
        }

        
    //Activate our Main Window
        
    CenterWindow(hWndNULL600400);
        
    ShowWindow(hWndnCmdShow);
        
    UpdateWindow(hWnd);

        while(
    GetMessage(&msgNULL00))
        {
            
    TranslateMessage( &msg );
            
    DispatchMessage( &msg );
        }

        return (int) 
    msg.wParam;

    Here are a few other things I tried:

    PHP Code:
    void DrawFilledRectHWND hWndHDC hDCRECTr

        
    HBRUSH hBrush
        
    RECT rec initRecArea(rec.leftrec.top00); 
        
        
    //We take our color from above and color the rectangle 
        
        
    hBrush CreateSolidBrush(RGB(00255));
        
    FillRect(hDC, &rechBrush); 
        
    DeleteObject (hBrush); 

    and
    PHP Code:
    void DrawFilledRectHWND hWndHDC hDCRECTr

          
    //hDC = GetDC(hWnd);
        
    HDC memDC CreateCompatibleDC(hDC);
        
    HBITMAP hMemBmp CreateCompatibleBitmap(hDCscreenWidthscreenHeight);
        
    HBITMAP hOldBmp = (HBITMAP)SelectObject(memDChMemBmp);

            
    RECT rec initRecArea(rec.leftrec.top00); 
        
            
    //We take our color from above and color the rectangle    
            
    FillRect(hDC, &recCreateSolidBrush(RGB(00255)));
        
    // As a last step copy the memdc to the hdc
        
    BitBlt(hDC00screenWidthscreenHeightmemDC00SRCCOPY);

        
    ReleaseDCNULLhDC );
            
    SelectObject(memDChOldBmp);
        
    DeleteObject(hMemBmp);
        
    DeleteObject(hOldBmp); //added
        
    DeleteDC(memDC); 
        
    DeleteDC(hDC); //added


  2. #2

    Default

    At line 58 of the first bit of code you do the following:
    PHP Code:
    RECT rec initRecArea(rec.leftrec.top00); 
    Which you're not allowed to do, it will define rec as REC but not fill it's values (thus rec is not initialized) after which you try to pass rec.left and left.top to initRecArea. That could lead to a crash or at least my debugger sincerely disliked it but it's dangerous either way.
    What you probably meant to do there:
    PHP Code:
    RECT rec initRecArea(r.leftr.top00); 
    Not sure if that will fix the lockup you speak of but I though I'd post that straight away.
    Will have a closer look at your code tomorrow or so.
    =|UWS|=|SA|Vince - Head Serveradmin - mohaa.uwsclan.us (MOH:AA - FT)

  3. #3
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    Don't I feel like a rytard (In the wise words of Alan)... Anywho, made the change. and I'm testing it out. I'm sure I have alot of code that may not be necessary but I been testing several things. The instant that I am doing any rendering it literally jumps in double+ the size. The app is only 9kb and for it to be using up 2 MB is IMO absurd. I do appreciate you to take a look at this though!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •