-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphicsEngine.cpp
More file actions
116 lines (87 loc) · 3.12 KB
/
GraphicsEngine.cpp
File metadata and controls
116 lines (87 loc) · 3.12 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
#include "GraphicsEngine.h"
dh::GraphicsEngine::GraphicsEngine(dh::GameDataRef gameData, sf::Vector2u uWindowSize, std::string appName, bool bFullscreen, float fFpsLimit) :
m_gameData(gameData),
m_uWindowSize(uWindowSize),
m_fFPSLimit(fFpsLimit)
{
m_initialize(appName, bFullscreen);
}
dh::GraphicsEngine::GraphicsEngine(dh::GameDataRef gameData, std::string appName, bool bFullscreen, float fFpsLimit):
m_gameData(gameData),
m_uWindowSize(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height),
m_fFPSLimit(fFpsLimit)
{
m_initialize(appName, bFullscreen);
}
dh::GraphicsEngine::~GraphicsEngine()
{
if (m_renderThread.joinable())
m_renderThread.join();
m_renderWindow.close();
}
sf::RenderWindow & dh::GraphicsEngine::getRenderWindow()
{
return m_renderWindow;
}
sf::Vector2u dh::GraphicsEngine::getWindowSize()
{
return m_uWindowSize;
}
dh::Resource_Manager<sf::Texture, std::string> & dh::GraphicsEngine::getTextureManager()
{
return m_textureManager;
}
dh::Resource_Manager<sf::Font, std::string> & dh::GraphicsEngine::getFontManager()
{
return m_fontManager;
}
void dh::GraphicsEngine::startRenderingThread(std::function<void()> drawFunc)
{
m_renderThread = std::thread(&dh::GraphicsEngine::m_renderingThread, this, drawFunc);
}
void dh::GraphicsEngine::m_initialize(std::string appName, bool bFullscreen)
{
//creating render window
std::cout << "Creating window..." << std::endl;
if (bFullscreen)
this->m_renderWindow.create(sf::VideoMode::getDesktopMode(), appName, sf::Style::Fullscreen);
else
this->m_renderWindow.create(sf::VideoMode(m_uWindowSize.x, m_uWindowSize.y), appName, sf::Style::Close | sf::Style::Titlebar);
std::cout << "\tWindow created.\n" << std::endl;
//setting up views
std::cout << "Setting up views..." << std::endl;
for (auto view : this->m_gameData->viewsMap) {
view.second.setSize(static_cast<float>(m_uWindowSize.x), static_cast<float>(m_uWindowSize.y));
view.second.setCenter(static_cast<float>(m_uWindowSize.x / 2), static_cast<float>(m_uWindowSize.y / 2));
}
std::cout << "\tViews initialized.\n" << std::endl;
}
void dh::GraphicsEngine::m_renderingThread(std::function<void()> drawFunc)
{
std::cout << "\tRendering thread started." << std::endl;
while (this->m_gameData->bGameRunning)
{
m_updateRenderingClock();
drawFunc();
}
}
void dh::GraphicsEngine::m_updateRenderingClock()
{
double renderingTimeDiff = 0.0;
//elapsed time update
this->m_renderTime = this->m_renderClk.restart();
this->m_fRenderElapsedTime = this->m_renderTime.asSeconds();
m_fRenderCounter += m_fRenderElapsedTime;
if (this->m_fRenderCounter >= 0.2f) {
this->m_fRenderCounter -= 0.2f;
}
if (this->m_fRenderElapsedTime >= 0.5f) {
this->m_fRenderElapsedTime = 0.5f;
}
//lock the updates per second
if (this->m_fRenderElapsedTime < m_fFPSLimit) {
renderingTimeDiff = m_fFPSLimit - this->m_fRenderElapsedTime;
this->m_fRenderElapsedTime = static_cast<float>(m_fFPSLimit);
std::this_thread::sleep_for(std::chrono::duration<float>(renderingTimeDiff));
}
}