Win32 Bitmap not outputting

I have been trying to create a win32 program from a book, except using my own code with the book's concepts as opposed to just copying the program. I've run into an serious problem - No bitmaps will show. I've worked on it with several people, but nobody can figure it out. Here is the draw function from the bitmap class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Bitmap::Draw(HDC hDC, int x, int y, BOOL bTrans, COLORREF crTransColor)
{

  if (m_hBitmap != NULL)
  {
    // Create a memory device context for the bitmap
    HDC hMemDC = CreateCompatibleDC(hDC);

    // Select the bitmap into the device context
    HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);

    // Draw the bitmap to the destination device context
    if (bTrans)
      TransparentBlt(hDC, x, y, GetWidth(), GetHeight(), hMemDC, 0, 0,
        GetWidth(), GetHeight(), crTransColor);
    else
      BitBlt(hDC, x, y, GetWidth(), GetHeight(), hMemDC, 0, 0, SRCCOPY);


    // Restore and delete the memory device context
    SelectObject(hMemDC, hOldBitmap);
    DeleteDC(hMemDC);
  }
}


And here is the function that is calling this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Tile::Draw(HDC hDC)
{
    Bitmap* pTestBitmap = new Bitmap(hDC, IDB_TILE1, _hInstance);
    pTestBitmap->Draw(hDC, 0, 0, TRUE);

    std::cout << "Drawing tile at " << iX * 50 << ", " << iY * 50 << std::endl;

    Bitmap* test = new Bitmap(hDC, 50, 50, RGB(255, 0, 0));
    test->Draw(hDC, 0, 0, TRUE);

    //_pBoard->pTileImages[0]->Draw(hDC, 0, 0, FALSE);
    std::cout << "TEST\n";
    _pBoard->pTileImages[bState ? iType : 0]->Draw(hDC, iX * 50, iY * 50, TRUE);
}


As you can see, I have tried several different methods of drawing the bitmap, yet none seem to work. There is also an issue I am having that may or may not be related: the original method of calling the draw function (_pBoard->pTileImages[bState ? iType : 0]->Draw(hDC, iX * 50, iY * 50, TRUE);) halts mid-line without even calling the draw function. Here are some related functions that might be contributing to the original error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Board::Board(HDC hDC) // This a constructor of a class that calls in the
{                     // function that calls in the draw function
    // Initialize tile bitmaps
    pTileImages[0] = new Bitmap(hDC, IDB_TILEBLANK, _hInstance);
    pTileImages[1] = new Bitmap(hDC, IDB_TILE1, _hInstance);
    pTileImages[2] = new Bitmap(hDC, IDB_TILE2, _hInstance);
    pTileImages[3] = new Bitmap(hDC, IDB_TILE3, _hInstance);
    pTileImages[4] = new Bitmap(hDC, IDB_TILE4, _hInstance);
    pTileImages[5] = new Bitmap(hDC, IDB_TILE5, _hInstance);
    pTileImages[6] = new Bitmap(hDC, IDB_TILE6, _hInstance);
    pTileImages[7] = new Bitmap(hDC, IDB_TILE7, _hInstance);
    pTileImages[8] = new Bitmap(hDC, IDB_TILE8, _hInstance);

    // Seed the generator
    srand(GetTickCount());

    // Create the tiles
    for(int x = 0; x < 4; x++)
        for(int y = 0; y < 4; y++)
        {
            pTiles[x][y] = new Tile(x, y);
        }

    int iX, iY;

    for(int iImage = 1; iImage < 8; iImage++)
        for(int iSecond = 0; iSecond < 2; iSecond++)
            {
                do
                {
                    iX = rand() % 4;
                    iY = rand() % 4;
                    std::cout << "Tile " << iX << ", " << iY << " is type " << pTiles[iX][iY]->GetType() << std::endl;
                }while(pTiles[iX][iY]->GetType() != -1);

                pTiles[iX][iY]->InitializeImages(0, iImage);
                std::cout << "Tile " << iX << ", " << iY << " given image " << iImage << std::endl;
                pTiles[iX][iY]->Draw(hDC);
            }

}


1
2
3
4
5
6
7
8
void GameStart(HWND hWindow)
{ // This function is where the path starts, it calls in the constructor
    srand(GetTickCount());

    HDC hDC = GetDC(hWindow);
    _pBoard = new Board(hDC);
}


Please ask me any questions if I need to clarify anything. Right now I'm at a standstill with this project, and I would like to start learning again.
How are you creating the HBITMAP? I assume it's in your Bitmap constructor, but I don't see the code for that here.


Also, you don't need to (and probably shouldn't) recreate a DC every time you want to blit the bitmap. I would recommend your Bitmap class keeps the DC (and 'old' bitmap) as one of its members and only do the clean up once when it's destroyed, rather than every time it's drawn.


EDIT:

Also, GetDC locks the DC. You should only do that for very brief periods while you draw, and then should release the DC ASAP. It looks like you are getting the DC once at startup and then never releasing it, which might explain the weird behavior.

Just like you have to pair CreateDC->DeleteDC, you must also pair GetDC->ReleaseDC
Last edited on
I ended up just restarting the project. I never really figured out the problem, but thanks!
I'd try starting with drawing one bitmap when the program starts. Then move onto making classes/etc.
Topic archived. No new replies allowed.