Drawing a bitmap to a window

My goal is to make game "cannon ball" (I hope u know what game i'm tallking about)
However, I have problem with drawing a bitmap on window
I work in win32

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//object class (objects in this project are ball and pad, which are global variables)
class Object 
{
public:
	Object ();
	
	int x,y;
	int width, height;
	int dx, dy;

	HBITMAP mask;
	HBITMAP img;
};

Object::Object ()
{
	ZeroMemory (this, sizeof (Object));
}

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
//this is how i handle WM_PAINT and WM_CREATE messages
                case WM_CREATE:
			BITMAP bm;

			Pad.img = LoadBitmap (GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_PAD)); 
			if(Pad.img == NULL)
				Error(L"Error loading pad");
			Pad.mask = CreateMask (Pad.img, RGB(0, 0, 0));
			if(Pad.mask == NULL)
				Error(L"Error creating mask");

			GetObject (Pad.img, sizeof (HBITMAP), &bm);
			Pad.width = bm.bmWidth;
			Pad.height = bm.bmHeight;


			break;
		case WM_PAINT:
			{
				PAINTSTRUCT ps;
				HDC hdc = BeginPaint (hwnd, &ps);
				RECT rc;

				GetClientRect (hwnd, &rc);
				UpdatePad(&rc, &Pad);
				DrawObj(&Pad, hdc, &rc);

				ReleaseDC(hwnd, hdc);
			}
			break;

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
// here i create mask and update object
HBITMAP CreateMask (HBITMAP image, COLORREF crTransparent)
{
	HDC hdcMem, hdcMem2;
	HBITMAP hbmMask;
	BITMAP bm;
	
	GetObject(image, sizeof (BITMAP), &bm);
	hbmMask = CreateBitmap (bm.bmWidth, bm.bmHeight, 1, 1, NULL); 

	hdcMem = CreateCompatibleDC(0);
	hdcMem2 = CreateCompatibleDC(0);

	SelectObject (hdcMem2, hbmMask);
	SelectObject (hdcMem, image);

	SetBkColor(hdcMem, crTransparent);

	BitBlt (hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
	BitBlt (hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);

	DeleteDC(hdcMem);
	DeleteDC(hdcMem2);

	return hbmMask;
}

void DrawObj (Object *obj, HDC hdc, RECT *pRect)
{
	HDC hdcBuffer = CreateCompatibleDC (hdc);
	HBITMAP hbmBuffer = CreateCompatibleBitmap (hdcBuffer, pRect->right, pRect->bottom);
	HBITMAP hbmOldBuffer = (HBITMAP) SelectObject (hdcBuffer, hbmBuffer);

	HDC hdcMem = CreateCompatibleDC (hdc);
	HBITMAP hbmMask = (HBITMAP) SelectObject (hdcMem, obj->mask);

	FillRect (hdcBuffer, pRect, (HBRUSH)GetStockObject (WHITE_BRUSH));

	BitBlt(hdcBuffer, obj->x, obj->y, obj->width, obj->height, hdcMem, 0, 0, SRCAND); 

	SelectObject (hdcMem, obj->img);
	BitBlt(hdcBuffer, obj->x, obj->y, obj->width, obj->height, hdcMem, 0, 0, SRCPAINT);

	BitBlt (hdc, obj->x, obj->y, obj->width, obj->height, hdcBuffer, 0, 0, SRCCOPY);

	SelectObject (hdcMem, hbmMask);
	DeleteDC (hdcMem);

	SelectObject (hdcBuffer, hbmOldBuffer);
	DeleteDC(hdcBuffer);
	DeleteObject (hbmBuffer);

	return;
}

void UpdatePad(RECT *pRect,Object *pad)
{
	POINT p;
	
	if(GetCursorPos(&p))
	{
		pad->x = p.x;
	}
}


problem: Pad is not drawn on the window, (there's no failure on loading bitmap or mask creation )
Last edited on
Topic archived. No new replies allowed.