-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathEngine.h
More file actions
96 lines (73 loc) · 2.43 KB
/
Engine.h
File metadata and controls
96 lines (73 loc) · 2.43 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
// ------------------------------------------------------------
// Copyright(c) 2018-2022 Jesse Yurkovich
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// See the LICENSE file in the repo root for full license information.
// ------------------------------------------------------------
#pragma once
#include <string>
#include "DirectX/D3DEngine.h"
#include "EngineOptions.h"
#include "Platform/Mouse.h"
#include "Platform/platform.h"
#include "StepTimer.h"
class Engine
{
public:
explicit Engine(EngineOptions const & config)
: Config(config)
{
}
Engine(Engine const &) = delete;
Engine(Engine &&) = delete;
Engine & operator=(Engine const &) = delete;
Engine & operator=(Engine &&) = delete;
virtual ~Engine() = default;
const EngineOptions Config;
Mouse::ButtonStateTracker MouseTracker{};
void Initialize(HWND hwnd)
{
const int MaxTextLength = 32;
wchar_t currentWindowText[MaxTextLength] = { 0 };
GetWindowText(hwnd, currentWindowText, MaxTextLength);
m_window = hwnd;
m_windowTitle.assign(currentWindowText);
m_mouse.SetWindow(hwnd);
m_mouse.ResetScrollWheelValue();
InitializeCore(hwnd);
}
void Tick()
{
UpdateStats();
m_timer.Tick([this]() {
MouseTracker.Update(m_mouse.GetState());
Update(static_cast<float>(m_timer.GetElapsedSeconds()));
});
Render();
}
void ChangeWindowSize(int width, int height)
{
ChangeWindowSizeCore(width, height);
}
protected:
virtual void InitializeCore(HWND hwnd) = 0;
virtual void Update(float elapsedTime) noexcept = 0;
virtual void Render() = 0;
virtual void ChangeWindowSizeCore(int width, int height) = 0;
private:
HWND m_window{};
std::wstring m_windowTitle{};
Mouse m_mouse{};
StepTimer m_timer{};
void UpdateStats()
{
static double prevTime = 0.0f;
const double currentTime = m_timer.GetTotalSeconds();
if ((currentTime - prevTime) >= 1.0f)
{
std::wstring fps = std::to_wstring(m_timer.GetFramesPerSecond());
std::wstring windowText = m_windowTitle + L" : " + fps + L" fps";
SetWindowText(m_window, windowText.c_str());
prevTime = currentTime;
}
}
};