Files
Lab9_UNIX/core/Engine.cpp
T
2026-06-05 11:45:04 +03:00

119 lines
2.2 KiB
C++

#include "Engine.h"
#include <chrono>
#include <thread>
Engine::ErrorCode Engine::run()
{
ErrorCode errorCode =
ErrorCode::success;
auto lastUpdate =
std::chrono::steady_clock::now();
auto lag =
std::chrono::milliseconds(0);
const auto timeQuantum =
std::chrono::milliseconds(15);
while (!end())
{
// INPUT
//=================================
updateInput();
//=================================
// UPDATE
//=================================
auto now =
std::chrono::steady_clock::now();
lag +=
std::chrono::duration_cast
<
std::chrono::milliseconds
>
(
now - lastUpdate
);
lastUpdate = now;
while (lag >= timeQuantum)
{
lag -= timeQuantum;
update(
static_cast<int>(
timeQuantum.count()
)
);
}
//=================================
// RENDER
//=================================
if (!m_PaintDevice.ready())
{
errorCode =
ErrorCode::paint_device_not_ready;
break;
}
m_PaintDevice.clear();
render(m_PaintDevice);
m_PaintDevice.render();
//=================================
std::this_thread::sleep_for(
std::chrono::milliseconds(1)
);
}
return errorCode;
}
void Engine::updateInput()
{
int key = getch();
while (key != ERR)
{
if (
m_TrackedKeys.find(key)
!=
m_TrackedKeys.end()
)
{
on_button_press(key);
}
key = getch();
}
}
void Engine::track_key(int key)
{
m_TrackedKeys.insert(key);
}
void Engine::untrack_key(int key)
{
auto it =
m_TrackedKeys.find(key);
if (it != m_TrackedKeys.end())
{
m_TrackedKeys.erase(it);
}
}