engine/src/engine.cpp

60 lines
1.1 KiB
C++
Raw Normal View History

2022-09-13 18:25:18 +00:00
#include "engine.hpp"
2022-10-27 16:58:30 +00:00
#include "log.hpp"
2022-10-04 10:54:23 +00:00
#include "window.hpp"
2022-10-27 16:58:30 +00:00
#include "gfx_device.hpp"
#include "input.hpp"
2022-09-17 00:22:35 +00:00
// To allow the FPS-limiter to put the thread to sleep
#include <thread>
2022-09-13 18:25:18 +00:00
namespace engine {
2022-10-04 10:54:23 +00:00
Application::Application(const char* appName, const char* appVersion)
{
2022-11-23 16:20:08 +00:00
m_win = new Window(appName, true, true);
m_gfx = new GFXDevice(appName, appVersion, m_win->getHandle());
2022-10-31 16:21:07 +00:00
m_input = new Input(*m_win);
2022-10-04 10:54:23 +00:00
}
Application::~Application()
{
2022-10-31 16:21:07 +00:00
delete m_input;
delete m_gfx;
2022-10-31 16:21:07 +00:00
delete m_win;
2022-10-04 10:54:23 +00:00
}
void Application::gameLoop()
{
2022-10-22 12:15:25 +00:00
TRACE("Begin game loop...");
constexpr int FPS_LIMIT = 240;
constexpr auto FRAMETIME_LIMIT = std::chrono::nanoseconds(1000000000 / FPS_LIMIT);
auto beginFrame = std::chrono::steady_clock::now();
auto endFrame = beginFrame + FRAMETIME_LIMIT;
2022-10-04 10:54:23 +00:00
// single-threaded game loop
while (m_win->isRunning()) {
/* logic */
2022-10-22 12:15:25 +00:00
2022-10-27 16:58:30 +00:00
/* draw */
m_gfx->renderFrame();
2022-10-04 10:54:23 +00:00
/* poll events */
m_win->getInputAndEvents();
/* fps limiter */
std::this_thread::sleep_until(endFrame);
beginFrame = endFrame;
endFrame = beginFrame + FRAMETIME_LIMIT;
2022-10-04 10:54:23 +00:00
}
m_gfx->waitIdle();
2022-11-15 19:53:40 +00:00
2022-09-13 18:25:18 +00:00
}
}