-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEngine.cpp
More file actions
307 lines (248 loc) · 8.4 KB
/
Engine.cpp
File metadata and controls
307 lines (248 loc) · 8.4 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "framework.h"
#include "Matrix.h"
#include "Stack.h"
#include "Piece.h"
#include "Engine.h"
#pragma comment(lib, "d2d1")
#pragma comment(lib, "dwrite")
#pragma comment(lib, "Windowscodecs.lib")
Engine::Engine() : m_pDirect2dFactory(NULL), m_pRenderTarget(NULL)
{
// Constructor
// Initialize your game elements here
srand(time(NULL));
stack = new Stack();
activePiece = new Piece();
activePiece->Activate();
waitingPiece = new Piece();
autoFallDelay = 1;
autoFallAccumulated = 0;
keyPressDelay = 0.07;
keyPressAccumulated = 0;
}
Engine::~Engine()
{
// Destructor
SafeRelease(&m_pDirect2dFactory);
SafeRelease(&m_pRenderTarget);
// Safe-release your game elements here
delete stack;
delete waitingPiece;
delete activePiece;
}
HRESULT Engine::InitializeD2D(HWND m_hwnd)
{
// Initializes Direct2D, for drawing
D2D1_SIZE_U size = D2D1::SizeU(RESOLUTION_X, RESOLUTION_Y);
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pDirect2dFactory);
m_pDirect2dFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(m_hwnd, size, D2D1_PRESENT_OPTIONS_IMMEDIATELY),
&m_pRenderTarget
);
// Initialize the D2D part of your game elements here
InitializeTextAndScore();
stack->InitializeD2D(m_pRenderTarget);
activePiece->InitializeD2D(m_pRenderTarget);
waitingPiece->InitializeD2D(m_pRenderTarget);
return S_OK;
}
void Engine::InitializeTextAndScore()
{
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_pTextFormat
);
m_pTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
m_pTextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
m_pRenderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::White),
&m_pWhiteBrush
);
}
void Engine::KeyUp(WPARAM wParam)
{
// If keyup, un-set the keys flags
// Don't do any logic here, you want to control the actual logic in the Logic method below
if (wParam == VK_DOWN)
downPressed = false;
if (wParam == VK_LEFT)
leftPressed = false;
if (wParam == VK_RIGHT)
rightPressed = false;
if (wParam == VK_SPACE || wParam == VK_UP)
spacePressed = false;
}
void Engine::KeyDown(WPARAM wParam)
{
// If keyup, set the keys flags
// Don't do any logic here, you want to control the actual logic in the Logic method below
if (wParam == VK_DOWN)
downPressed = true;
if (wParam == VK_LEFT)
leftPressed = true;
if (wParam == VK_RIGHT)
rightPressed = true;
if (wParam == VK_SPACE || wParam == VK_UP)
spacePressed = true;
}
void Engine::MousePosition(int x, int y)
{
// Campture mouse position when the mouse moves
// Don't do any logic here, you want to control the actual logic in the Logic method below
}
void Engine::MouseButtonUp(bool left, bool right)
{
// If mouse button is released, set the mouse flags
// Don't do any logic here, you want to control the actual logic in the Logic method below
}
void Engine::MouseButtonDown(bool left, bool right)
{
// If mouse button is pressed, set the mouse flags
// Don't do any logic here, you want to control the actual logic in the Logic method below
}
void Engine::Logic(double elapsedTime)
{
// This is the logic part of the engine. It receives the elapsed time from the app class, in seconds.
// It uses this value for a smooth and consistent movement, regardless of the CPU or graphics speed
if (gameOver) // Do nothing if game over
{
return;
}
// We will need the stack in several places below
Matrix* stackCells = stack->GetCells();
// Due to a high FPS, we can't consider the keys at every frame because movement will be very fast
// So we're using a delay, and if enough time has passed we take a key press into consideration
keyPressAccumulated += elapsedTime;
if (keyPressAccumulated > keyPressDelay)
{
keyPressAccumulated = 0;
if (leftPressed || rightPressed || spacePressed)
{
// Remove any full rows
int removed = stack->RemoveLines();
if (removed > 0)
{
score += pow(2, removed) * 100;
autoFallDelay = autoFallDelay * 0.98;
}
}
// Move left or right
if (leftPressed)
activePiece->GoLeft(stackCells);
if (rightPressed)
activePiece->GoRight(stackCells);
// Rotate
if (spacePressed)
{
activePiece->Rotate(stackCells);
spacePressed = false;
}
// Move down
// On this one we will just set autoFallAccumulated to be high, because we have the down movemenet logic below
if (downPressed)
autoFallAccumulated = autoFallDelay + 1;
}
// The piece falls automatically after a delay
autoFallAccumulated += elapsedTime;
if (autoFallAccumulated > autoFallDelay)
{
autoFallAccumulated = 0;
// Remove any full rows
int removed = stack->RemoveLines();
if (removed > 0)
{
score += pow(2, removed) * 100;
autoFallDelay = autoFallDelay * 0.98;
}
// Move down the active piece
bool isConflict = activePiece->Advance(stackCells);
// If we have a conflict with the stack, it means we were sitting on the stack or bottom wall already
if (isConflict)
{
// We add the piece to stack
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (activePiece->GetCells()->Get(j, i) == true)
{
int realx = activePiece->GetPosition().x + j;
int realy = activePiece->GetPosition().y + i;
stackCells->Set(realx, realy, true);
}
}
}
// Delete active piece, activate the waiting piece and generate new waiting piece
delete activePiece;
activePiece = waitingPiece;
activePiece->Activate();
waitingPiece = new Piece();
waitingPiece->InitializeD2D(m_pRenderTarget);
// If we have a collision right after we generate the new piece,
// it means the stack is too high, so game over
if (activePiece->StackCollision(stackCells))
gameOver = true;
}
}
}
HRESULT Engine::Draw()
{
// This is the drawing method of the engine.
// It runs every frame
// Draws the elements in the game using Direct2D
HRESULT hr;
m_pRenderTarget->BeginDraw();
m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::Black));
// Below you can add drawing logic for your game elements
DrawTextAndScore();
stack->Draw(m_pRenderTarget);
activePiece->Draw(m_pRenderTarget);
waitingPiece->Draw(m_pRenderTarget);
hr = m_pRenderTarget->EndDraw();
return S_OK;
}
void Engine::DrawTextAndScore()
{
// Text and score
int padding = (RESOLUTION_Y - (STACK_HEIGHT + 1) * CELL_SIZE) / 2;
int centerRight = RESOLUTION_X - (RESOLUTION_X - padding - (STACK_WIDTH + 2) * CELL_SIZE) / 2;
D2D1_RECT_F rectangle1 = D2D1::RectF(centerRight - 200, padding, centerRight + 200, padding + 100);
m_pRenderTarget->DrawText(
L"Next Piece",
15,
m_pTextFormat,
rectangle1,
m_pWhiteBrush
);
D2D1_RECT_F rectangle2 = D2D1::RectF(centerRight - 200, padding + 200, centerRight + 200, padding + 300);
m_pRenderTarget->DrawText(
L"Score",
5,
m_pTextFormat,
rectangle2,
m_pWhiteBrush
);
D2D1_RECT_F rectangle3 = D2D1::RectF(centerRight - 200, padding + 300, centerRight + 200, padding + 400);
WCHAR scoreStr[64];
swprintf_s(scoreStr, L"%d ", score);
m_pRenderTarget->DrawText(
scoreStr,
7,
m_pTextFormat,
rectangle3,
m_pWhiteBrush
);
}