engine/src/application.cpp

148 lines
3.9 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"
2023-01-20 16:30:35 +00:00
#include "resources/mesh.hpp"
#include "resources/material.hpp"
#include "resources/shader.hpp"
#include "resources/texture.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();
2023-01-20 16:30:35 +00:00
// register resource managers
2023-01-20 16:30:35 +00:00
registerResourceManager<engine::resources::Texture>();
registerResourceManager<engine::resources::Shader>();
registerResourceManager<engine::resources::Material>();
registerResourceManager<engine::resources::Mesh>();
2023-01-20 16:30:35 +00:00
// default resources
{
resources::Shader::VertexParams vertParams{};
vertParams.hasNormal = true;
vertParams.hasUV0 = true;
auto texturedShader = std::make_unique<engine::resources::Shader>(
gfx(),
getResourcePath("engine/shaders/texture.vert").c_str(),
getResourcePath("engine/shaders/texture.frag").c_str(),
vertParams,
false,
true
);
getResourceManager<engine::resources::Shader>()->addPersistent("engine.textured", std::move(texturedShader));
}
{
auto whiteTexture = std::make_unique<engine::resources::Texture>(
gfx(),
getResourcePath("engine/textures/white.png")
);
getResourceManager<engine::resources::Texture>()->addPersistent("engine.white", std::move(whiteTexture));
}
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;
2023-01-05 13:21:33 +00:00
auto lastTick = m_window->getNanos();
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
2023-01-06 16:45:39 +00:00
if(m_window->getKeyPress(inputs::Key::K_F)) [[unlikely]] {
2023-01-02 21:11:29 +00:00
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
}
2023-01-06 16:45:39 +00:00
uint64_t now = m_window->getNanos();
if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] {
lastTick = now;
INFO("fps: {}", m_window->getAvgFPS());
m_window->resetAvgFPS();
}
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
}
}