a question on a few of the API functions

This is a discussion on a question on a few of the API functions within the Windows Programming forums, part of the Platform Specific Boards category; here is the case WM_PAINT code. I know what is going on with the blits, but I'm not so sure ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    a question on a few of the API functions

    here is the case WM_PAINT code. I know what is going on with the blits, but I'm not so sure what is going on with the whole SelectObject and HBITMAP vs. BITMAP and hbmOld, HDC hDC, HDC hDCMem, why I need them all, etc.

    1) Could someone just explain what each line is doing and why it is needed? I hate having code from tutorials that works but i dont know why.
    Code:
    case WM_PAINT:
    		{
            BITMAP bm;
            PAINTSTRUCT ps;
    
            HDC hDC = BeginPaint(hWnd, &ps);
    
            HDC hDCMem = CreateCompatibleDC(hDC);
            HBITMAP hbmOld = (HBITMAP)SelectObject(hDCMem, shipmask);
    
    		GetObject(ship, sizeof(bm), &bm);
    		BitBlt(hDC, 0, 0, bm.bmWidth, bm.bmHeight, hDCMem, 0, 0, SRCAND);
    
    		SelectObject(hDCMem, ship);
    
            BitBlt(hDC, 0, 0, bm.bmWidth, bm.bmHeight, hDCMem, 0, 0, SRCPAINT); //(destination, x, y, size w, size h)
    
            SelectObject(hDCMem, hbmOld);
            DeleteDC(hDCMem);
    
            EndPaint(hWnd, &ps);
    		}
            break;
    2) I am learning this to start remaking my allegro game in windows api. could someone explain WHERE i put the code for drawing updated pictures like the ship, bullets, alien ship, etc. and WHERE should the code go for updating their positions? in the message loop (all in functions of course)? do i have to do something like PostMessage(WM_PAINT) whenever the user hits a key to move the ship or something that requires an updated picture to be drawn? thanks
    Last edited by Leeman_s; 11-03-2002 at 09:08 PM.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,854
    BeginPaint()

    Get the area that need to be redraw and its objects (ie HDC). This area has been invalidated by a call to UpdateWindow() or InvalidateRect() (or InvalidateRegion() )

    EndPaint() is the cue for windows to actually redraw the current screen.

    Before BeginPaint() validates the update area a call to GetUpdateRect() should be made to minimise the area redrawn.

    HDC hDCMem = CreateCompatibleDC(hDC);

    This creates a second HDC (same as the update region) so the BMP images can be loaded into it and then drawn to the update region with BitBlt() ect.

    HBITMAP hbmOld = (HBITMAP)SelectObject(hDCMem, shipmask);
    Some other (global?) BMP is selected into the HDC to 'push' out the current (system BMP). An extra operation that could be avoided with better code.

    This is to ensure the HDC is returned to its original state before the function ends. Else will loose GDI memory.

    Objects currently selected into a HDC can not be deleted.

    GetDC() ect requires a ReleaseDC() ect

    CreateDC() ect requires a DeleteDC() ect

    Code:
    //create objects
    //select into HDC
    SysObject = SelectObject(HDC, NewObject);
    
    //draw
    
    //clean up by returning hdc to original state and deleting created objects 
    SelectObject(HDC,SysObject);
    DeleteObject(NewObject);

    GetObject(ship, sizeof(bm), &bm);

    this stacks the BITMAP struct bm with info on the image. Handy to get the images size.

    BitBlt(hDC, 0, 0, bm.bmWidth, bm.bmHeight, hDCMem, 0, 0, SRCAND);
    draw by 'and'ing the pixcels

    then clean up by returning the original GDI objects and deleteing the new created ones.

    This is a slow drawing code. It has many operations between the call for a draw and the drawing to the screen.

    All drawing should be done BEFORE WM_PAINT is called. Only when you have finished drawing a screen to a buffer (then coping it to a screen HDC) should you ask windows to repaint.

    Having a seperate HDC that contains the current screen image allows normal redrawing (due to other user actions) to take place without all these objects being created and destroyed each time it tries to update a small area.
    It also allows quick resize if the user changes the window size. (stretchBlt() being so slow and its results unsatistying)

    Try a search on 'frame buffers' as there is plenty of code posted before. If you need more help let me know.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question on learning win API...
    By yaya in forum Windows Programming
    Replies: 7
    Last Post: 01-05-2008, 05:14 PM
  2. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  3. question about functions and char *
    By Bittrexx in forum C Programming
    Replies: 4
    Last Post: 07-22-2003, 12:27 PM
  4. Functions question
    By Lurker in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2003, 11:24 AM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21