-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.cpp
More file actions
269 lines (217 loc) · 8.87 KB
/
Graphics.cpp
File metadata and controls
269 lines (217 loc) · 8.87 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#pragma once
#include "Graphics.h"
#include <string>
Graphics::Graphics()
{
factory = NULL;
renderTarget = NULL;
brush = NULL;
}
Graphics::~Graphics()
{
if (factory) factory->Release();
if (renderTarget) renderTarget->Release();
if (brush) brush->Release();
}
bool Graphics::Init(HWND windowHandle) // Inizjalizacja direct2d/narz璠zi
{
HRESULT res = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &factory);
if (res != S_OK) return false;
res = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown**>(&wfactory));
if (res != S_OK) return false;
res = wfactory->CreateTextFormat(
L"Verdana",
NULL,
DWRITE_FONT_WEIGHT_REGULAR,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
12,
L"pl-pl",
&textFormat
);
if (res != S_OK) return false;
RECT rect;
GetClientRect(windowHandle, &rect);
res = factory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(windowHandle, D2D1::SizeU(rect.right, rect.bottom)),
&renderTarget);
res = renderTarget->CreateSolidColorBrush(D2D1::ColorF(0, 0, 0, 0), &brush);
if (res != S_OK) return false;
return true;
}
// Funkcje direct2d
void Graphics::ClearScreen(float r, float g, float b)
{
renderTarget->Clear(D2D1::ColorF(r, g, b));
}
void Graphics::DrawBoards(D2D1_RECT_F WindowSize, int Boards)
{
brush->SetColor(D2D1::ColorF(0, 0, 0));
renderTarget->DrawRectangle(WindowSize, brush, Boards);
}
bool Graphics::BoardsCollision(D2D1_RECT_F SnakeHead, int SnakeSize, D2D1_RECT_F Boards, int BoradsSize)
{
if ((SnakeHead.left - (SnakeSize / 2)) <= Boards.left)
{
return true;
}
if ((SnakeHead.top - (SnakeSize / 2)) <= Boards.top)
{
return true;
}
if ((SnakeHead.right + (SnakeSize / 2)) >= Boards.right)
{
return true;
}
if ((SnakeHead.bottom + (SnakeSize / 2)) >= Boards.bottom)
{
return true;
}
return false;
}
bool Graphics::BodyCollision(D2D1_RECT_F Snake, D2D1_RECT_F SnakeNP, int SnakeSize)
{
if (SnakeNP.left < Snake.left + SnakeSize &&
SnakeNP.left + SnakeSize > Snake.left &&
SnakeNP.top < Snake.top + SnakeSize &&
SnakeSize + SnakeNP.top > Snake.top)
{
return true;
}
return false;
}
bool Graphics::AppleCollision(D2D1_RECT_F SnakeHead, int SnakeSize, D2D1_RECT_F Apple, int AppleSize)
{
if (SnakeHead.left < Apple.left + AppleSize &&
SnakeHead.left + AppleSize > Apple.left &&
SnakeHead.top < Apple.top + AppleSize &&
AppleSize + SnakeHead.top > Apple.top)
{
return true;
}
return false;
}
void Graphics::DrawSnakeHead(D2D1_RECT_F Rect, int SnakeSize, char Direction) //Rysowanie g這wy w篹a
{
brush->SetColor(D2D1::ColorF(255, 255, 0)); //G這wa
renderTarget->FillRectangle(Rect, brush);
brush->SetColor(D2D1::ColorF(0, 0, 0));
if (Direction == 'n') //Pozycja startowa "neutralna"
{
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.left + SnakeSize / 4, Rect.top + SnakeSize / 2), SnakeSize / 6, SnakeSize / 6), brush, 2); // 1 oko
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.right - SnakeSize / 4, Rect.top + SnakeSize / 2), SnakeSize / 6, SnakeSize / 6), brush, 2); // 2 oko
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.left + SnakeSize / 4, Rect.top + SnakeSize / 2), 1, 1), brush, 1); // 1 enica
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.right - SnakeSize / 4, Rect.top + SnakeSize / 2), 1, 1), brush, 1); // 2 enica
}
if (Direction == 'u') //G這wa skierowana ku g鏎ze
{
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.left + SnakeSize / 4, Rect.top + SnakeSize / 4), SnakeSize / 6, SnakeSize / 6), brush, 2); // 1 oko
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.right - SnakeSize / 4, Rect.top + SnakeSize / 4), SnakeSize / 6, SnakeSize / 6), brush, 2); // 2 oko
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.left + SnakeSize / 4, Rect.top + SnakeSize / 4), 1, 1), brush, 1); // 1 enica
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.right - SnakeSize / 4, Rect.top + SnakeSize / 4), 1, 1), brush, 1); // 2 enica
}
if (Direction == 'd') //G這wa skierowana ku do這wi
{
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.left + SnakeSize / 4, Rect.bottom - SnakeSize / 4), SnakeSize / 6, SnakeSize / 6), brush, 2); // 1 oko
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.right - SnakeSize / 4, Rect.bottom - SnakeSize / 4), SnakeSize / 6, SnakeSize / 6), brush, 2); // 2 oko
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.left + SnakeSize / 4, Rect.bottom - SnakeSize / 4), 1, 1), brush, 1); // 1 enica
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.right - SnakeSize / 4, Rect.bottom - SnakeSize / 4), 1, 1), brush, 1); // 2 enica
}
if (Direction == 'l') //Pozycja startowa "neutralna"
{
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.left + SnakeSize / 4, Rect.top + SnakeSize / 4), SnakeSize / 6, SnakeSize / 6), brush, 2); // 1 oko
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.left + SnakeSize / 4, Rect.bottom - SnakeSize / 4), SnakeSize / 6, SnakeSize / 6), brush, 2); // 2 oko
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.left + SnakeSize / 4, Rect.top + SnakeSize / 4), 1, 1), brush, 1); // 1 enica
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.left + SnakeSize / 4, Rect.bottom - SnakeSize / 4), 1, 1), brush, 1); // 2 enica
}
if (Direction == 'r') //Pozycja startowa "neutralna"
{
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.right - SnakeSize / 4, Rect.top + SnakeSize / 4), SnakeSize / 6, SnakeSize / 6), brush, 2); // 1 oko
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.right - SnakeSize / 4, Rect.bottom - SnakeSize / 4), SnakeSize / 6, SnakeSize / 6), brush, 2); // 2 oko
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.right - SnakeSize / 4, Rect.top + SnakeSize / 4), 1, 1), brush, 1); // 1 enica
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(Rect.right - SnakeSize / 4, Rect.bottom - SnakeSize / 4), 1, 1), brush, 1); // 2 enica
}
}
void Graphics::DrawSnakeBody(D2D1_RECT_F Rect)
{
brush->SetColor(D2D1::ColorF(0, 255, 0));
renderTarget->FillRectangle(Rect, brush);
}
void Graphics::DrawSnakeBackground(D2D1_RECT_F Rect)
{
brush->SetColor(D2D1::ColorF(255, 255, 255));
renderTarget->FillRectangle(Rect, brush);
}
void Graphics::DrawApple(D2D1_RECT_F Apple)
{
brush->SetColor(D2D1::ColorF(255, 0, 0));
renderTarget->FillRectangle(Apple, brush);
}
void Graphics::UpdateApple(D2D1_RECT_F Apple, D2D1_RECT_F AppleNP) //Ruch w篹a
{
brush->SetColor(D2D1::ColorF(255, 255, 255));
renderTarget->FillRectangle(Apple, brush);
brush->SetColor(D2D1::ColorF(255, 0, 0));
renderTarget->FillRectangle(AppleNP, brush);
}
void Graphics::DrawScore() //Rysowanie pocz靖kowego wyniku
{
brush->SetColor(D2D1::ColorF(255, 0, 0));
renderTarget->DrawText(
L"Score: 0",
ARRAYSIZE(L"Score: 0") - 1,
textFormat,
D2D1::RectF(4, 2, 100, 12),
brush
);
}
void Graphics::UpdateScore(int score) //Aktualizacja wyniku
{
brush->SetColor(D2D1::ColorF(0, 0, 0)); //Czyszczenie poprzedniego wyniku
renderTarget->FillRectangle(D2D1::RectF(0, 0, 120, 20), brush);
// Konwersja na LPCWSTR
wchar_t buffer[256];
wsprintfW(buffer, L"%d", score);
LPCWSTR intScore = buffer;
std::wstring t1 = std::wstring(L"Score: ") + intScore;
LPCWSTR endScore = t1.c_str();
brush->SetColor(D2D1::ColorF(255, 0, 0)); //Czyszczenie poprzedniego wyniku
renderTarget->DrawText(
endScore,
ARRAYSIZE(L"Score: ") + (int)log10(score) + 1, //Wyznaczanie w bajtach ile "z章czony" string, zajmuje miejsca w pami璚i
textFormat,
D2D1::RectF(4, 2, 100, 12),
brush
);
}
void Graphics::DrawEndGame(int score, RECT WindowRect)
{
brush->SetColor(D2D1::ColorF(255, 0, 0));
// Konwersja na LPCWSTR
if (score > 0)
{
wchar_t buffer[256];
wsprintfW(buffer, L"%d", score);
LPCWSTR intScore = buffer;
std::wstring t1 = std::wstring(L"End score: ") + intScore + std::wstring(L"\n\nPress enter...");
LPCWSTR endScore = t1.c_str();
renderTarget->DrawText(
endScore,
ARRAYSIZE(L"End score: ") + (int)log10(score) + ARRAYSIZE(L"\n\nPress enter..."), //Wyznaczanie w bajtach ile "z章czony" string, zajmuje miejsca w pami璚i
textFormat,
D2D1::RectF(WindowRect.right / 2 - 50, WindowRect.bottom / 2 - textFormat->GetFontSize() * 3, WindowRect.right / 2 + 50, WindowRect.bottom / 2 + textFormat->GetFontSize() * 3), // WindowRect.bottom GetFontSize() * ilosc wierszy w tekscie
brush
);
}
else
{
renderTarget->DrawText(
L"End score: 0 \n\nPress enter...",
ARRAYSIZE(L"End score: 0 \n\nPress enter..."), //Wyznaczanie w bajtach ile "z章czony" string, zajmuje miejsca w pami璚i
textFormat,
D2D1::RectF(WindowRect.right / 2 - 50, WindowRect.bottom / 2 - textFormat->GetFontSize() * 3, WindowRect.right / 2 + 50, WindowRect.bottom / 2 + textFormat->GetFontSize() * 3), // WindowRect.bottom GetFontSize() * ilosc wierszy w tekscie
brush
);
}
}