A GDI question that is perhaps deeper than I think...

This is a discussion on A GDI question that is perhaps deeper than I think... within the Windows Programming forums, part of the Platform Specific Boards category; So here is the story: I am creating a tetris clone. And I have a a 2-d int array that ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    25

    A GDI question that is perhaps deeper than I think...

    So here is the story: I am creating a tetris clone. And I have a a 2-d int array that holds the whole board in terms of a number 0-7, 0 for nothing there, and 1-7 cooresponding to the color of square. (each of the 7 tetris piece types has its own color).

    Now I have a function called "RefreshBoard" that re outputs everything on the board from this array. Here is that function:
    hbmSquare is a HBITMAP array that holds in each cell an HBITMAP object for each of the 7 colors. Everything else should be self-explanatory.

    The problem I am having is something to do with this function, I am asking some of you experienced windows programmers to just look at this function and tell me if you see something innately wrong with it. The problem I have having is pretty much that BitBlt is in fact failing. The message box that goes off when it fails is poping up. I can't explain why this is, and so I will just ask you to tell me if you see something wrong with it, and I'll go from there. Here is this function:
    Code:
    void Board::RefreshBoard(Piece* currentPiece)
    {
       HDC hdcMemory2;
       hdcMemory2 = CreateCompatibleDC(hdc);
       HBITMAP hbmCurrentSq;
       bool check;
        for(int y=0; y<20; y++)
        {
            for(int x=0; x<10; x++)
            {
                if(theBoard[x][y] > 0) // if the square is not blank
                {
                    hbmCurrentSq = hbmSquare[theBoard[x][y]-1];
                    SelectObject(hdcMemory2, hbmCurrentSq);
                    check = BitBlt(hdc, x*17, y*17, 17, 17, hdcMemory2, 0, 0, SRCCOPY);
                     if (!check)
                     {
                         HWND hwnd;
                         MessageBox(hwnd, "BitBlt Fails!", "C baby", MB_OK | MB_ICONEXCLAMATION);
                     }
                }
                else if(theBoard[x][y] == 0)
                   currentPiece->RemoveOnePiece(x,y);
            }
        }   
       DeleteDC(hdcMemory2);
    }
    I appreciate your help! Keep in mind, this isn't throwing any compiler errors, the error is in the workings of the function, not its syntax.

    Nimit
    Last edited by conright; 01-02-2003 at 10:00 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Make sure when you select something into a DC you save the old object and select it back in when you are finished. That having been said, lets continue. You're really not doing to much error checking. How about throwing some checks for the main hdc and the bitmap before you select it into the DC. I think maybe you are selecting an invalid bitmap into the DC. If you want more help, you can e-mail me or post the project here and I can dig around in there further. Hope that helps.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    25
    Hey, I did get this initial problem fixed. The problem i was having isn't happening any more, but I still have some flaws in my understanding of HDC's and HWND...etc. I will work on this further, if anyone has any general advice on this matter, I'd like to hear it. I'm kind of a beginner in windows C++ programming, so I'm running into these basic problems. I will also read more about this, and then post again if I have more specific issues.

    BTW: MrWizard, Your post *did* kind of help me fix my problem, as my problem was that I was using the wrong HDC to write the bitblt to, and also some of the BITMAPS weren't loading correctly. I have added some more error checking now, and will ask for more help as I need it. Thanks again for your help!

    Nimit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. GDI object lifetime and C++ object lifetime
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 06-16-2006, 05:26 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Whats Peoples Problem With the GDI?
    By MicroFiend in forum Game Programming
    Replies: 6
    Last Post: 07-28-2003, 07:52 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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