Problem with splitting a Bitmap into sections

Hi,

I am trying to load a bmp image and split it into tiles that have the same dimensions. I then save these new smaller bmps to a structure that stores them until they are used. It then also deals with destroying them once the program is ended or they are no longer needed. Here is my code:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
void TilePalette::AddTileSet(PWSTR filePath)
{
	//only here for compatability
	HDC hdc;
	hdc = GetDC(tile);

	//for the main bitmap
	HDC bmpHdc;
	bmpHdc = CreateCompatibleDC(hdc);

	//the main bitmap
	HBITMAP bmp;
	bmp = (HBITMAP)::LoadImage(NULL, filePath, IMAGE_BITMAP, 0, 0,	LR_LOADFROMFILE);

	//get a copy of the bitmaps properties
	BITMAP bmpProperties;
	int iReturn = GetObject(reinterpret_cast<HGDIOBJ>(bmp), 
		                    sizeof(BITMAP),	
				    reinterpret_cast<LPVOID>(&bmpProperties));

	//load the bmp into the device context
	HBITMAP oldBmp = (HBITMAP)SelectObject(bmpHdc, bmp);

	int xBound,yBound, tileWidth, tileHeight;

	tileWidth = Map::TileWidth_;
	tileHeight = Map::TileHeight_;
	xBound = bmpProperties.bmWidth / tileWidth;
	yBound = bmpProperties.bmHeight / tileHeight;

	for (int y = 0; y < yBound; y++)
	{
		for (int x = 0; x < xBound; x++)
		{
			//the bitmap of a single tile
			HBITMAP tile;
			tile = CreateCompatibleBitmap(hdc, tileWidth,tileHeight);

			//the device that holds the tile bitmap
			HDC tileHdc;
			tileHdc = CreateCompatibleDC(hdc);

			//insert the tile bitmap into the device context
			HBITMAP oldTile = (HBITMAP)SelectObject(tileHdc, tile);

			//blit from the main bitmap onto the tile bitmap
			::BitBlt(bmpHdc,
                                 x*tileWidth,
                                 y*tileHeight,
                                 tileWidth,
                                 tileHeight,
                                 tileHdc,
                                 0,
                                 0,
                                 SRCCOPY);

			//select the tile bitmap
			SelectObject(tileHdc, oldTile);

			//assign the tile bitmap to the new Tile struct
			Tile newTile(tile, transparent);

			//add it onto the tile vector
			tilePalette_.push_back(newTile);

			//delete the device context
			::DeleteDC(tileHdc);
		}
	}

	//update the tile window
	InvalidateRect(tile, NULL, TRUE);

	//select the main bitmap which we no longer want
	::SelectObject(bmpHdc, bmp);
	//delete the main device context
	::DeleteDC(bmpHdc);
	//delete the main bitmap
	::DeleteObject(bmp);
}


This works an the associated drop function draws them to the screen:

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
void TilePalette::DrawTiles(HDC hdc)
{
	HDC bmpHdc;
	bmpHdc = CreateCompatibleDC(hdc);

	int length = tilePalette_.size();
	int xBound = 0, yBound = 0, margin = 4;

	if (length > 6)
	{
		yBound = sqrt(length);

		xBound = length - yBound;
	}
	else
	{
		yBound = 1;
		xBound = length;
	}

	int i = 0;
	for (int y = 0; y < yBound; y++)
	{
		for (int x = 0; x < xBound; x++)
		{
			BlitBitmap(tilePalette_[i].BGImage, 
				       hdc, 
					   (x*Map::TileWidth_) + (x * margin) + margin, 
					   (y * Map::TileHeight_) + (y * margin) + margin);
			++i;
		}
	}
}


The problem is that the tiles are all black. Although the first tile of every tile set should be black since it represents transparency the other tiles should retain their original color. Can anyone see why they are being drawn black?
Try checking anything related to the palette and the srcopy statement.
My fault, i called the bitblt() function incorrectly. I must have been on autopilot slightly. The call should read:

1
2
3
4
5
6
7
8
9
::BitBlt(tileHdc, 
                     0,
                     0, 
                     tileWidth, 
                     tileHeight, 
                     bmpHdc, 
                     x * tileWidth, 
                     y * tileHeight, 
                     SRCCOPY);


I was essentially just copying the first tile multiple times which happenedto be black.
Last edited on
Topic archived. No new replies allowed.