engine/src/application.cpp

106 lines
2.6 KiB
C++
Raw Normal View History

2022-11-30 10:36:50 +00:00
#include "application.hpp"
2022-09-13 18:25:18 +00:00
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
2022-11-30 10:36:50 +00:00
#include "scene.hpp"
// To allow the FPS-limiter to put the thread to sleep
#include <thread>
2022-12-15 15:54:11 +00:00
#ifdef _MSC_VER
#include <windows.h>
#include <direct.h>
#define MAX_PATH 260
#endif
static std::filesystem::path getResourcesPath()
{
std::filesystem::path resourcesPath{};
#ifdef _MSC_VER
CHAR exeDirBuf[MAX_PATH + 1];
GetModuleFileNameA(NULL, exeDirBuf, MAX_PATH + 1);
std::filesystem::path cwd = std::filesystem::path(exeDirBuf).parent_path();
(void)_chdir((const char*)std::filesystem::absolute(cwd).c_str());
#else
std::filesystem::path cwd = std::filesystem::current_path();
#endif
if (std::filesystem::is_directory(cwd / "res")) {
resourcesPath = cwd / "res";
}
else {
resourcesPath = cwd.parent_path() / "share" / "sdltest";
}
if (std::filesystem::is_directory(resourcesPath) == false) {
resourcesPath = cwd.root_path() / "usr" / "local" / "share" / "sdltest";
}
if (std::filesystem::is_directory(resourcesPath) == false) {
throw std::runtime_error("Unable to determine resources location. CWD: " + cwd.string());
}
return resourcesPath;
}
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-30 10:36:50 +00:00
m_window = std::make_unique<Window>(appName, true, false);
2022-11-29 14:22:03 +00:00
m_gfx = std::make_unique<GFXDevice>(appName, appVersion, m_window->getHandle());
m_inputManager = std::make_unique<InputManager>(window());
2022-12-20 23:51:04 +00:00
m_sceneManager = std::make_unique<SceneManager>(this);
2022-12-15 15:54:11 +00:00
// get base path for resources
m_resourcesPath = getResourcesPath();
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-12-15 10:07:22 +00:00
m_sceneManager->updateActiveScene(m_window->dt());
2022-10-22 12:15:25 +00:00
2022-12-20 23:51:04 +00:00
if(m_window->getKeyPress(inputs::Key::K_L)) {
2023-01-02 21:11:29 +00:00
// m_enableFrameLimiter = !m_enableFrameLimiter;
m_window->infoBox("fps", std::to_string(m_window->getFPS()) + " fps " + std::to_string(m_window->dt() * 1000.0f) + " ms");
2022-12-20 23:51:04 +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 */
2022-12-20 23:51:04 +00:00
if (m_enableFrameLimiter) {
std::this_thread::sleep_until(endFrame);
}
beginFrame = endFrame;
endFrame = beginFrame + FRAMETIME_LIMIT;
2022-10-04 10:54:23 +00:00
}
m_gfx->waitIdle();
2022-09-13 18:25:18 +00:00
}
}