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-28 16:33:50 +00:00
|
|
|
#include "input.hpp"
|
2022-09-17 00:22:35 +00:00
|
|
|
|
2022-11-10 14:12:29 +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);
|
2022-11-28 16:33:50 +00:00
|
|
|
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;
|
2022-11-28 16:33:50 +00:00
|
|
|
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...");
|
|
|
|
|
2022-11-10 14:12:29 +00:00
|
|
|
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()) {
|
|
|
|
|
2022-11-28 16:33:50 +00:00
|
|
|
/* logic */
|
2022-10-22 12:15:25 +00:00
|
|
|
|
2022-10-27 16:58:30 +00:00
|
|
|
/* draw */
|
2022-11-28 16:33:50 +00:00
|
|
|
m_gfx->renderFrame();
|
2022-10-04 10:54:23 +00:00
|
|
|
|
|
|
|
/* poll events */
|
|
|
|
m_win->getInputAndEvents();
|
|
|
|
|
2022-11-10 14:12:29 +00:00
|
|
|
/* fps limiter */
|
2022-11-28 16:33:50 +00:00
|
|
|
std::this_thread::sleep_until(endFrame);
|
2022-11-10 14:12:29 +00:00
|
|
|
beginFrame = endFrame;
|
|
|
|
endFrame = beginFrame + FRAMETIME_LIMIT;
|
|
|
|
|
2022-10-04 10:54:23 +00:00
|
|
|
}
|
2022-10-20 19:28:51 +00:00
|
|
|
|
2022-11-28 16:33:50 +00:00
|
|
|
m_gfx->waitIdle();
|
2022-11-15 19:53:40 +00:00
|
|
|
|
2022-09-13 18:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|