-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHUD.cpp
More file actions
174 lines (156 loc) · 3.5 KB
/
HUD.cpp
File metadata and controls
174 lines (156 loc) · 3.5 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
172
173
#include "Settings.h"
#include "CollisionDistances.h"
#include "Character.h"
#include "HUD.h"
#include "Engine.h"
HUD::HUD()
{
lives = 3;
coins = 0;
success = false;
}
void HUD::FinishedLevel()
{
success = true;
}
void HUD::Draw(ID2D1HwndRenderTarget* m_pRenderTarget)
{
// load any images and D2D resources if they are not already loaded
if (tileSetImg == NULL)
{
tileSetImg = engine->LoadImageW(L"tileset.png");
}
if (charSetImg == NULL)
{
charSetImg = engine->LoadImageW(L"characters.png");
}
if (m_pTextFormat1 == NULL || m_pTextFormat2 == NULL)
{
DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(m_pDWriteFactory),
reinterpret_cast<IUnknown**>(&m_pDWriteFactory)
);
m_pDWriteFactory->CreateTextFormat(
L"Verdana",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
20,
L"", //locale
&m_pTextFormat1
);
m_pTextFormat1->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
m_pTextFormat1->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
m_pDWriteFactory->CreateTextFormat(
L"Verdana",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
40,
L"", //locale
&m_pTextFormat2
);
m_pTextFormat2->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
m_pTextFormat2->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
}
if (m_pWhiteBrush == NULL)
{
m_pRenderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::White),
&m_pWhiteBrush
);
}
// Draw number of coins
D2D1_RECT_F rectangle1 = D2D1::RectF(
5, 0,
5 + TILE_WIDTH / 2, TILE_WIDTH / 2
);
D2D1_RECT_F rectangle2 = D2D1::RectF(
5 * TILE_WIDTH, 7 * TILE_WIDTH,
6 * TILE_WIDTH - 1, 8 * TILE_WIDTH - 1
);
m_pRenderTarget->DrawBitmap(tileSetImg, rectangle1, 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, rectangle2);
rectangle1 = D2D1::RectF(
5 + TILE_WIDTH / 2, 0,
5 + 5 * TILE_WIDTH / 2, TILE_WIDTH / 2
);
WCHAR scoreStr[64];
swprintf_s(scoreStr, L" x %d ", coins);
m_pRenderTarget->DrawText(
scoreStr,
6,
m_pTextFormat1,
rectangle1,
m_pWhiteBrush
);
// Draw number of lives
rectangle1 = D2D1::RectF(
5, TILE_WIDTH / 2,
5 + CHARACTER_WIDTH / 2, CHARACTER_HEIGHT / 2 + TILE_WIDTH / 2
);
rectangle2 = D2D1::RectF(
0, 3 * CHARACTER_TILE_HEIGHT - CHARACTER_HEIGHT,
TILE_WIDTH - 1, 3 * CHARACTER_TILE_HEIGHT - 1
);
m_pRenderTarget->DrawBitmap(charSetImg, rectangle1, 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, rectangle2);
rectangle1 = D2D1::RectF(
5 + TILE_WIDTH / 2, TILE_WIDTH / 2,
5 + 4 * TILE_WIDTH / 2 + CHARACTER_WIDTH / 2, CHARACTER_HEIGHT / 2 + TILE_WIDTH / 2
);
swprintf_s(scoreStr, L" x %d ", lives);
m_pRenderTarget->DrawText(
scoreStr,
6,
m_pTextFormat1,
rectangle1,
m_pWhiteBrush
);
// Draw Game Over if no more lives left
if (lives == 0)
{
rectangle1 = D2D1::RectF(
0, 0,
RESOLUTION_X, RESOLUTION_Y
);
swprintf_s(scoreStr, L"GAME OVER");
m_pRenderTarget->DrawText(
scoreStr,
9,
m_pTextFormat2,
rectangle1,
m_pWhiteBrush
);
}
// Draw Congratulations if you got out of the level
if (success)
{
rectangle1 = D2D1::RectF(
0, 0,
RESOLUTION_X, RESOLUTION_Y
);
swprintf_s(scoreStr, L"CONGRATULATIONS!");
m_pRenderTarget->DrawText(
scoreStr,
16,
m_pTextFormat2,
rectangle1,
m_pWhiteBrush
);
}
}
void HUD::AddCoins(int addCoins)
{
coins += addCoins;
}
void HUD::RemoveLife()
{
if (lives > 0)
lives--;
}
bool HUD::HasLives()
{
return lives > 0;
}