-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmap.cpp
More file actions
171 lines (148 loc) · 4.51 KB
/
Bitmap.cpp
File metadata and controls
171 lines (148 loc) · 4.51 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "Bitmap.h"
Bitmap::Bitmap(HDC hDC, UINT uiBitmapID, int iBitmapWidth, int iBitmapHeight, BOOL bTrans,
HINSTANCE hInstance, BOUNDSACTION BoundsAction, int iFrames)
{
m_bTrans = bTrans;
m_iWidth = iBitmapWidth;
m_iHeight = iBitmapHeight;
m_iFrames = iFrames;
m_BoundsAction = BoundsAction;
ptPos.x = ptPos.y = 0;
ptVelocity.x = ptVelocity.y = 0;
m_rcCollision.left = m_rcCollision.right = m_rcCollision.top = m_rcCollision.bottom = 0;
m_rcBoundary.left = 0;
m_rcBoundary.top = 0;
m_rcBoundary.right = 640;
m_rcBoundary.bottom = 480;
m_iBitmap = uiBitmapID;
m_iShrinkSize = 0;
m_hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(uiBitmapID));
}
Bitmap::~Bitmap()
{
}
BOOL Bitmap::CheckCollision(Bitmap* pBitmap)
{
if((GetCollision().left <= pBitmap->GetCollision().right) && (GetCollision().right >= pBitmap->GetCollision().left) &&
(GetCollision().top <= pBitmap->GetCollision().bottom) && (GetCollision().bottom >= pBitmap->GetCollision().top))
return TRUE;
return FALSE;
}
void Bitmap::SetVelocity(int x, int y)
{
ptVelocity.x = x;
ptVelocity.y = y;
}
void Bitmap::SetBounds(RECT rcBoundary)
{
m_rcBoundary = rcBoundary;
}
void Bitmap::SetBounds(int x, int y, int w, int h)
{
m_rcBoundary.left = x;
m_rcBoundary.top = y;
m_rcBoundary.right = w;
m_rcBoundary.bottom = h;
}
BOOL Bitmap::Update()
{
//update the bitmap's position
ptPos.x += ptVelocity.x;
ptPos.y += ptVelocity.y;
BOOL g_bCollision = FALSE;
//Check to see if the bitmap crossed the boundary
if(ptPos.x + GetWidth() >= m_rcBoundary.right)
{
if(m_BoundsAction == BA_STOP)
{
ptVelocity.x = 0;
ptPos.x = m_rcBoundary.right - GetWidth();
}
else if(m_BoundsAction == BA_BOUNCE)
{
ptVelocity.x = -ptVelocity.x;
}
//If the bounds action is kill, delete the bitmap
else
delete this;
//Tell the game that the bitmap has been deleted to avoid a segmentation fault
g_bCollision = TRUE;
}
else if(ptPos.x <= m_rcBoundary.left)
{
if(m_BoundsAction == BA_STOP)
{
ptVelocity.x = 0;
ptPos.x = m_rcBoundary.left;
}
else if(m_BoundsAction == BA_BOUNCE)
{
ptVelocity.x = -ptVelocity.x;
}
else
delete this;
//Tell the game that the bitmap has been deleted to avoid a segmentation fault
g_bCollision = TRUE;
}
if(ptPos.y + GetHeight() >= m_rcBoundary.bottom)
{
if(m_BoundsAction == BA_STOP)
{
ptVelocity.y = 0;
ptPos.y = m_rcBoundary.bottom - GetHeight();
}
else if(m_BoundsAction == BA_BOUNCE)
{
ptVelocity.y = -ptVelocity.y;
}
else
delete this;
//Tell the game that the bitmap has been deleted to avoid a segmentation fault
g_bCollision = TRUE;
}
else if(ptPos.y <= m_rcBoundary.top)
{
if(m_BoundsAction == BA_STOP)
{
ptVelocity.y = 0;
ptPos.y = m_rcBoundary.top;
}
else if(m_BoundsAction == BA_BOUNCE)
{
ptVelocity.y = -ptVelocity.y;
}
else
delete this;
//Tell the game that the bitmap has been deleted to avoid a segmentation fault
g_bCollision = TRUE;
}
//Update the collision
m_rcCollision.left = ptPos.x + m_iShrinkSize;
m_rcCollision.top = ptPos.y + m_iShrinkSize;
m_rcCollision.right = ptPos.x + GetWidth() - m_iShrinkSize;
m_rcCollision.bottom = ptPos.y + GetHeight() - m_iShrinkSize;
//Tell the program that the bitmap has not passed the boundary
return g_bCollision;
}
void Bitmap::ShrinkCollision(int iPixellsSmaller)
{
m_iShrinkSize = iPixellsSmaller;
}
void Bitmap::Draw(HDC hDC, int iCurrentFrame, COLORREF crColorRef)
{
if(m_hBitmap != NULL)
{
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);
//Calculate what frame to draw
int iYPart = 0;
if(iCurrentFrame <= m_iFrames)
iYPart = iCurrentFrame * GetHeight();
if(m_bTrans)
TransparentBlt(hDC, ptPos.x, ptPos.y, m_iWidth, m_iHeight, hMemDC, 0, iYPart, m_iWidth, m_iHeight, crColorRef);
else
BitBlt(hDC, ptPos.x, ptPos.y, m_iWidth, m_iHeight, hMemDC, 0, iYPart, SRCCOPY);
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);
}
}