-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEngineBase.cpp
More file actions
217 lines (179 loc) · 4.4 KB
/
EngineBase.cpp
File metadata and controls
217 lines (179 loc) · 4.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
#include "framework.h"
#include "Settings.h"
#include "Point2D.h"
#include "EngineBase.h"
#include "GameObjectBase.h"
#include "Collisions.h"
#pragma comment(lib, "d2d1")
#pragma comment(lib, "dwrite")
#pragma comment(lib, "Windowscodecs.lib")
EngineBase::EngineBase() : m_pDirect2dFactory(NULL), m_pRenderTarget(NULL)
{
srand(time(NULL));
}
EngineBase::~EngineBase()
{
SafeRelease(&m_pDirect2dFactory);
SafeRelease(&m_pRenderTarget);
}
HRESULT EngineBase::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, V_SYNC ? D2D1_PRESENT_OPTIONS_NONE : D2D1_PRESENT_OPTIONS_IMMEDIATELY),
&m_pRenderTarget
);
return S_OK;
}
void EngineBase::MousePosition(int x, int y)
{
mousePosition.x = x;
mousePosition.y = y;
}
void EngineBase::KeyUp(WPARAM wParam)
{
}
void EngineBase::KeyDown(WPARAM wParam)
{
}
void EngineBase::MouseButtonUp(bool left, bool right)
{
}
void EngineBase::MouseButtonDown(bool left, bool right)
{
}
void EngineBase::AddGameObject(GameObjectBase* gameObj)
{
gameObj->engine = this;
objectList[noObjects] = gameObj;
noObjects++;
}
void EngineBase::RemoveGameObject(GameObjectBase* gameObj)
{
for (int i = 0; i < noObjects; i++)
{
if (objectList[i] == gameObj)
{
noObjects--;
for (int j = i; j < noObjects; j++)
{
objectList[j] = objectList[j + 1];
}
return;
}
}
}
void EngineBase::Logic(double elapsedTime)
{
for (int i = 0; i < noObjects; i++)
{
objectList[i]->Logic(elapsedTime);
if (objectList[i]->GetCollisionType() == poly)
{
objectList[i]->CalculateRotatedCollisionPolyPoints();
}
}
// Auto collision detection
if (ENABLE_AUTO_COLLISION_DETECTION)
{
for (int i = 0; i < noObjects; i++)
{
objectList[i]->ClearAutoCollisions();
}
for (int i = 0; i < noObjects - 1; i++)
{
if (objectList[i]->GetCollisionType() != none)
{
for (int j = i + 1; j < noObjects; j++)
{
if (objectList[j]->GetCollisionType() != none)
{
CollisionDetails cDet = Collisions::ObjectsCollide(objectList[i], objectList[j]);
if (cDet.Collides)
{
objectList[i]->AddAutoCollision(objectList[j], cDet);
cDet.SwapObjects();
objectList[j]->AddAutoCollision(objectList[i], cDet);
}
}
}
}
}
}
// Auto collision reaction
if (ENABLE_AUTO_COLLISION_REACTION)
{
for (int i = 0; i < noObjects; i++)
{
objectList[i]->ReactToCollisions(elapsedTime);
}
}
}
HRESULT EngineBase::Draw()
{
// Draws the elements in the game using Direct2D
HRESULT hr;
m_pRenderTarget->BeginDraw();
m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
m_pRenderTarget->Clear(D2D1::ColorF(D2D_BACKGROUND_COLOR));
for (int i = 0; i < noObjects; i++)
objectList[i]->Draw(m_pRenderTarget);
hr = m_pRenderTarget->EndDraw();
return S_OK;
}
ID2D1Bitmap* EngineBase::LoadImage(LPCWSTR imageFile)
{
IWICBitmapDecoder* pDecoder = NULL;
IWICBitmapFrameDecode* pSource = NULL;
IWICFormatConverter* pConverter = NULL;
IWICImagingFactory* pIWICFactory = NULL;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, reinterpret_cast<void**>(&pIWICFactory));
HRESULT hr = pIWICFactory->CreateDecoderFromFilename(
imageFile,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pDecoder
);
if (SUCCEEDED(hr))
{
// Create the initial frame.
hr = pDecoder->GetFrame(0, &pSource);
}
if (SUCCEEDED(hr))
{
// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = pIWICFactory->CreateFormatConverter(&pConverter);
}
if (SUCCEEDED(hr))
{
hr = pConverter->Initialize(
pSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
ID2D1Bitmap* toReturn = NULL;
if (SUCCEEDED(hr))
{
// Create a Direct2D bitmap from the WIC bitmap.
hr = m_pRenderTarget->CreateBitmapFromWicBitmap(
pConverter,
NULL,
&toReturn
);
}
SafeRelease(&pIWICFactory);
SafeRelease(&pDecoder);
SafeRelease(&pSource);
SafeRelease(&pConverter);
return toReturn;
}