-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.cpp
More file actions
393 lines (325 loc) · 13.2 KB
/
Copy pathCore.cpp
File metadata and controls
393 lines (325 loc) · 13.2 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// ===========================================================================
/// <summary>
/// Core.cpp
/// DirectXIntroduction
/// created by Mehrdad Soleimanimajd on 19.07.2019
/// </summary>
/// <created>ʆϒʅ, 19.07.2019</created>
/// <changed>ʆϒʅ, 04.07.2023</changed>
// ===========================================================================
#include "Core.h"
#include "Shared.h"
TheCore::TheCore (HINSTANCE& hInstance, Game* gameObj) :
appInstance (hInstance), timer (nullptr), fps (0), mspf (0),
appWindow (nullptr), appHandle (NULL),
d3d (nullptr), d2d (nullptr), game (gameObj),
initialized (false), paused (false), resized (false)
{
try
{
// timer instantiation
timer = new (std::nothrow) Timer;
if (!timer->isInitialized ())
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Timer initialization failed!");
return;
}
// application window instantiation
appWindow = new (std::nothrow) Window (this);
if (!appWindow->isInitialized ())
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Window initialization failed!");
return;
}
appHandle = appWindow->getHandle (); // handle to the instantiated window
// Direct3D 10 instantiation
d3d = new (std::nothrow) Direct3D (this);
if (!d3d->isInitialized ())
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Direct3D initialization failed!");
return;
}
// Direct2D 10 instantiation
d2d = new (std::nothrow) Direct2D (this);
if (!d2d->isInitialized ())
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Direct2D initialization failed!");
return;
}
initialized = true;
PointerProvider::getFileLogger ()->push (logType::info, std::this_thread::get_id (), L"mainThread",
L"The framework is successfully initialized.");
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
const bool& TheCore::isInitialized (void)
{
return initialized;
};
const HINSTANCE& TheCore::getInstance (void)
{
return appInstance;
};
const HWND& TheCore::getHandle (void)
{
return appHandle;
};
const bool& TheCore::isPaused (void)
{
return paused;
};
Timer* TheCore::getTimer (void)
{
return timer;
};
const int& TheCore::getFPS (void)
{
return fps;
};
//Direct3D* TheCore::getd3d ( void )
//{
// return d3d;
//};
//Direct2D* TheCore::getd2d ( void )
//{
// return d2d;
//};
void TheCore::frameStatistics (void)
{
try
{
HRESULT hR;
// a static local variable retains its state between the calls:
static int frameCounter; // frame counter (a frame is a full cycle of the game loop)
static double elapsed; // the elapsed time since the last call
frameCounter++;
if ((timer->getTotal () - elapsed) >= 1e0)
{
// frame calculations:
fps = frameCounter; // the number of counted frames in one second
mspf = 1e3 / fps; // average taken time by a frame in milliseconds
if (!paused && PointerProvider::getConfiguration ()->isDebug () && d2d)
{
d2d->textLayoutsDebug = false;
//// results to window caption
//std::wstring caption = L"The Game ^,^ - FPS: " + std::to_wstring ( fps ) +
// L" - mSPF: " + std::to_wstring ( mspf );
//SetWindowTextW ( appHandle, caption.c_str () );
// results to client window area
// FPS information text layouts
std::wostringstream outFPS;
outFPS.precision (6);
outFPS << "Resolution: " << d3d->displayMode.Width << " x " << d3d->displayMode.Height
<< " - Display mode #" << d3d->displayModeIndex + 1 << " of " << d3d->displayModesCount << " @ "
<< d3d->displayMode.RefreshRate.Numerator / d3d->displayMode.RefreshRate.Denominator << " Hz" << std::endl
<< "Display Adapter: " << d3d->videoCardDescription
<< " - Dedicated memory: " << d3d->videoCardMemory << "MB" << std::endl
<< "^_^ - FPS: " << fps << L" - mSPF: " << mspf << std::endl;
// before rendering a text to a bitmap: the creation of the text layout
hR = d2d->writeFac->CreateTextLayout (outFPS.str ().c_str (), (UINT32) outFPS.str ().size (),
d2d->textFormatFPS.Get (), (float) appWindow->clientWidth,
(float) appWindow->clientHeight, &d2d->textLayoutFPS);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"The Creation of text layout for FPS information failed!");
return;
}
std::wstring out {L"Last event: "};
out += PointerProvider::getFileLogger ()->getLogRawStr ();
hR = d2d->writeFac->CreateTextLayout (out.c_str (), (UINT32) (UINT32) out.size (),
d2d->textFormatLogs.Get (), (float) appWindow->clientWidth,
(float) appWindow->clientHeight, &d2d->textLayoutLogs);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"The Creation of text layout for Logs failed!");
return;
}
d2d->textLayoutsDebug = true;
}
// reset
frameCounter = 0;
elapsed += 1.0;
}
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
void TheCore::setResolution (const bool& prm)
{
try
{
if (prm)
{
if (!d3d->fullscreen)
{
d3d->displayModeIndex = d3d->displayModesCount - 1;
d3d->displayMode = d3d->displayModes [d3d->displayModeIndex];
d3d->fullscreen = true;
}
} else
{
if (d3d->fullscreen)
{
d3d->displayModeIndex = 0;
d3d->displayMode = d3d->displayModes [d3d->displayModeIndex];
d3d->fullscreen = false;
}
}
resizeResources (true);
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
void TheCore::resizeResources (const bool& displayMode)
{
try
{
if (initialized)
{
unsigned long rC {0};
//HRESULT hR;
// free game resources
//if (game->vertexBuffer [0] && game->indexBuffer [1])
//{
//}
// free Direct2D resources
if (d2d && !rC)
{
rC = d2d->dcBitmap.Reset ();
rC = d2d->deviceCon.Reset ();
rC = d2d->dcBuffer.Reset ();
//if (rC)
//{
// rC = 0; // HACK debug
// PointerProvider::getFileLogger ()->push ( logType::warning, std::this_thread::get_id (), L"mainThread",
// L"Problem while releasing one or more resources!" );
//}
}
// free Direct3D resources
if (d3d->dSview && d3d->rTview && !rC)
{
d3d->device->ClearState ();
rC = d3d->rasterizerState.Reset ();
d3d->device->OMSetRenderTargets (0, nullptr, nullptr);
rC = d3d->dSview.Reset ();
rC = d3d->dSstate.Reset ();
rC = d3d->dSbuffer.Reset ();
rC = d3d->rTview.Reset ();
//if (rC)
//{
// rC = 0; // HACK debug
// PointerProvider::getFileLogger ()->push ( logType::warning, std::this_thread::get_id (), L"mainThread",
// L"Problem while releasing one or more resources!" );
//}
}
// reallocation procedures
if (!rC)
{
if (displayMode)
{
d3d->displayModeSetter ();
std::this_thread::sleep_for (std::chrono::milliseconds (500));
}
d3d->allocateResources ();
if (d2d)
{
d2d->allocateResources ();
}
//game->allocateResources ();
appWindow->isResized () = false;
resized = true;
} else
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Resources' deallocation failed!");
}
}
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
void TheCore::shutdown (void)
{
try
{
unsigned long rC {0};
//HRESULT hR;
initialized = false;
if (d3d)
// to prevent exceptions, switch back to windowed mode
d3d->swapChain->SetFullscreenState (false, nullptr);
// Direct2D application destruction
if (d2d)
{
d2d->initialized = false;
rC = d2d->dcBitmap.Reset ();
rC = d2d->deviceCon.Reset ();
//rC = d2d->dcBuffer.Reset ();
rC = d2d->device.Reset ();
rC = d2d->factory.Reset ();
//rC = d2d->writeFac.Reset ();
d2d->core = nullptr;
delete d2d;
PointerProvider::getFileLogger ()->push (logType::info, std::this_thread::get_id (), L"mainThread",
L"Direct2D is successfully destructed.");
}
// Direct3D application destruction
if (d3d)
{
d3d->initialized = false;
d3d->device->ClearState ();
rC = d3d->rasterizerState.Reset ();
d3d->device->OMSetRenderTargets (0, nullptr, nullptr);
rC = d3d->dSview.Reset ();
rC = d3d->dSstate.Reset ();
rC = d3d->dSbuffer.Reset ();
rC = d3d->rTview.Reset ();
rC = d3d->swapChain.Reset ();
#ifndef _NOT_DEBUGGING
rC = d3d->debug.Reset ();
#endif // !_NOT_DEBUGGING
rC = d3d->device.Reset ();
d3d->core = nullptr;
delete d3d;
PointerProvider::getFileLogger ()->push (logType::info, std::this_thread::get_id (), L"mainThread",
L"Direct3D is successfully destructed.");
}
// application main window destruction
if (appWindow)
{
appWindow->initialized = false;
appWindow->handle = NULL;
appWindow->appInstance = NULL;
appWindow->core = nullptr;
delete appWindow;
PointerProvider::getFileLogger ()->push (logType::info, std::this_thread::get_id (), L"mainThread",
L"Application main window class is successfully destructed.");
}
// timer application destruction
if (timer)
delete timer;
appInstance = NULL;
PointerProvider::getFileLogger ()->push (logType::info, std::this_thread::get_id (), L"mainThread",
L"The Application Core is successfully shut down.");
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};