-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEngine.cpp
More file actions
159 lines (129 loc) · 3.56 KB
/
Engine.cpp
File metadata and controls
159 lines (129 loc) · 3.56 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
#include "framework.h"
#include "Point2D.h"
#include "Engine.h"
#pragma comment(lib, "d2d1")
#pragma comment(lib, "dwrite")
Engine::Engine() : m_pDirect2dFactory(NULL), m_pRenderTarget(NULL), m_pWhiteBrush(NULL)
{
snake = new Snake();
food = new Food();
food->Reset(snake);
playing = true;
keyPressed = false;
score = 5;
highScore = 5;
}
Engine::~Engine()
{
SafeRelease(&m_pDirect2dFactory);
SafeRelease(&m_pRenderTarget);
SafeRelease(&m_pWhiteBrush);
}
HRESULT Engine::InitializeD2D(HWND m_hwnd)
{
// Initializes Direct2D, to draw with
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 text writing factory and format
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
);
snake->Initialize(m_pRenderTarget);
food->Initialize(m_pRenderTarget);
return S_OK;
}
void Engine::KeyUp(WPARAM wParam)
{
if (!keyPressed)
{
if (wParam == VK_UP)
snake->GoUp();
if (wParam == VK_DOWN)
snake->GoDown();
if (wParam == VK_LEFT)
snake->GoLeft();
if (wParam == VK_RIGHT)
snake->GoRight();
keyPressed = true;
}
}
void Engine::Reset()
{
// This method reset the game, given that the game was won or lost
if (!playing)
{
snake->Reset();
food->Reset(snake);
playing = true;
score = 5;
}
}
void Engine::Logic(double elapsedTime)
{
// This is the logic part of the engine.
if (playing)
{
snake->Advance();
if (snake->CheckFoodCollision(food->position.x, food->position.y))
{
food->Reset(snake);
snake->Grow();
score++;
if (score > highScore)
highScore = score;
}
if (snake->CheckSelfCollision())
{
playing = false;
}
keyPressed = false;
}
Sleep(FRAME_SLEEP);
}
HRESULT Engine::Draw()
{
// This is the drawing method of the engine.
// It simply draws all 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));
// Draw score
D2D1_RECT_F rectangle2 = D2D1::RectF(0, 0, RESOLUTION_X, 200);
WCHAR scoreStr[64];
swprintf_s(scoreStr, L"Score: %d Top Score: %d ", score, highScore);
m_pRenderTarget->DrawText(
scoreStr,
35,
m_pTextFormat,
rectangle2,
m_pWhiteBrush
);
snake->Draw(m_pRenderTarget);
food->Draw(m_pRenderTarget);
hr = m_pRenderTarget->EndDraw();
return S_OK;
}