Bunch of Bugs

This is a discussion on Bunch of Bugs within the Windows Programming forums, part of the Platform Specific Boards category; I'm making a game in windows. MY game has many child windows, and it does double buffering. i'm using WS_CLIPCHILDREN, ...

  1. #1
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377

    Exclamation Bunch of Bugs

    I'm making a game in windows. MY game has many child windows, and it does double buffering.
    i'm using WS_CLIPCHILDREN, so that my children is visible next to buffering.
    Double buffering only happens when screen is updated, for example when you select something in
    a listbox.
    Double buffering isn't constant. it is executed only when needed, thats about 3 times in 10 seconds.

    So heres my problem:
    I run my app, and i play with my children. every time i do something about them double buffering
    does it's work. After some time, my children are wasted ( they are misplaced, and my whole app
    freaks out)

    So i gues there is something wrong in the double buffering... so i ask you is this right:

    // this is happening when screen is updated
    Code:
    HDC image_dc = CreateCompatibleDC(hDC);
    SelectObject(image_dc, paint.BMP1);
    BitBlt(hDC, x, y, x1, y1, image_dc, 0, 0, SRCCOPY);
    DeleteObject(image_dc);
    I don't know where else the error could be, all my PENS, BRUSHES, BITMAPS are created when program
    is executed and destroyed when program is quiting.
    All my child windows are created with CreateWindowEx() and destroyed with DestroyWindow()
    All my pens, brushes, bitmaps, fonts are destroyed with DeleteObject()
    I could give you my code, but it has many files with many classes, so it wouldn't be a good idea...

    I have done some filtering and came to conclusion, when it does work. I have deleted a part of my code
    which load a BITMAP, the part of code is in double buffering.
    There is a
    Code:
    HBITMAP buffer
    and whenever i need to draw something i load stuff into that
    bitmap. I load image in that bitmap about 20 times in a loop.
    Is this ok? Should i do something with this BMP?
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  2. #2
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    And i forgot to say, When my program freaks out, all the drawn bitmaps turn black.
    And behavior of my childs is undescribeable.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Use DeleteDC, not DeleteObject for freeing memory device contexts. You also need to restore the dc back to its original state before deleting it to prevent resource leaks, ie:
    Code:
    HDC image_dc = CreateCompatibleDC(hDC);
    HBITMAP hOldBmp=(HBITMAP)SelectObject(image_dc, paint.BMP1);
    BitBlt(hDC, x, y, x1, y1, image_dc, 0, 0, SRCCOPY);
    SelectObject(image_dc,hOldBmp);
    DeleteDC(image_dc)
    You may wish to consider creating the memory dc once rather than creating and deleting it repeatedly, particularly if you're going to be using it a lot.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    ok, i did that.
    And it helped. Now i wanted to test it to maximum so i made double buffering constant.
    And after a minute the same thing happend...

    This is ok
    Code:
    HDC image_dc = CreateCompatibleDC(hDC);
    HBITMAP hOldBmp=(HBITMAP)SelectObject(image_dc, paint.BMP1);
    BitBlt(hDC, x, y, x1, y1, image_dc, 0, 0, SRCCOPY);
    SelectObject(image_dc,hOldBmp);
    DeleteDC(image_dc)
    But what if i declare image_dc at the begining and destroy it in the end.
    How would i destroy it? hOldBmp would contain the las bitmap selection.
    And should i do something betwen bitmap loadings?
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  5. #5
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Heres current state:

    Code:
    while ( blaaball ) // double bufering loop
    {
    HDC image_dc = CreateCompatibleDC(hDC);
    
    SelectObject(image_dc, MyBitmap1);
    BitBlt(xxxxxxxx);
    
    SelectObject(image_dc, MyBitmap2);
    BitBlt(xxxxxxxx);
    
    SelectObject(image_dc, MyBitmap3);
    BitBlt(xxxxxxxx);
    
    SelectObject(image_dc, MyBitmap4);
    BitBlt(xxxxxxxx);
    
    SelectObject(image_dc, (HBITMAP)SelectObject(image_dc, MyBitmap4));
    DeleteDC(image_dc);
    }
    this is probably wrong...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Code:
    /*take dc creation out of the while loop ie do it once*/
    HDC image_dc = CreateCompatibleDC(hDC);
    /*the dc is created with default 
    gdi objects - store the default bitmap since that's being replaced*/
    HBITMAP hOldBmp=(HBITMAP)SelectObject(image_dc,MyBitmap1);
    
    while ( blaaball ) // double bufering loop
    {
    BitBlt(xxxxxxxx);
    
    SelectObject(image_dc, MyBitmap2);
    BitBlt(xxxxxxxx);
    
    SelectObject(image_dc, MyBitmap3);
    BitBlt(xxxxxxxx);
    
    SelectObject(image_dc, MyBitmap4);
    BitBlt(xxxxxxxx);
    
    SelectObject(image_dc,MyBitmap1);
    }
    /*take the restoration and deletion of the dc out of the while loop ie. do it once*/
    SelectObject(image_dc,hOldBmp); 
    DeleteDC(image_dc);
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    so you suggest that i create and destroy image_dc only at the beginging and the end of a program?
    'cause that double buffer loop is called multiple times...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. relation between bugs and compilation time
    By KIBO in forum Tech Board
    Replies: 10
    Last Post: 01-29-2009, 01:29 PM
  2. Bugs in Program Question
    By racerday182 in forum C++ Programming
    Replies: 14
    Last Post: 12-05-2008, 10:30 AM
  3. Need help finding bugs
    By Shakti in forum Game Programming
    Replies: 16
    Last Post: 02-13-2005, 01:42 AM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  5. Getting a percentage from a bunch of random numbers?
    By kidkash in forum C Programming
    Replies: 6
    Last Post: 02-11-2003, 08:42 AM

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