engine/src/engine.cpp

57 lines
1.2 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"
2022-11-29 14:22:03 +00:00
#include "input_manager.hpp"
#include "scene_manager.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-29 14:22:03 +00:00
m_window = std::make_unique<Window>(appName, true, true);
m_gfx = std::make_unique<GFXDevice>(appName, appVersion, m_window->getHandle());
m_inputManager = std::make_unique<InputManager>(window());
m_sceneManager = std::make_unique<SceneManager>();
2022-10-04 10:54:23 +00:00
}
2022-11-29 14:22:03 +00:00
Application::~Application() {}
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
2022-11-29 14:22:03 +00:00
while (m_window->isRunning()) {
2022-10-04 10:54:23 +00:00
/* 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 */
2022-11-29 14:22:03 +00:00
m_window->getInputAndEvents();
2022-10-04 10:54:23 +00:00
/* 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
}
}