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"
|
|
|
|
|
2023-03-13 17:10:46 +00:00
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
|
2022-11-10 14:12:29 +00:00
|
|
|
// 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
|
|
|
|
|
2023-03-13 17:10:46 +00:00
|
|
|
namespace engine {
|
|
|
|
|
|
|
|
static std::filesystem::path getResourcesPath()
|
|
|
|
{
|
|
|
|
std::filesystem::path resourcesPath{};
|
2022-12-15 15:54:11 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2023-03-13 17:10:46 +00:00
|
|
|
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());
|
2022-12-15 15:54:11 +00:00
|
|
|
#else
|
2023-03-13 17:10:46 +00:00
|
|
|
std::filesystem::path cwd = std::filesystem::current_path();
|
2022-12-15 15:54:11 +00:00
|
|
|
#endif
|
|
|
|
|
2023-03-13 17:10:46 +00:00
|
|
|
if (std::filesystem::is_directory(cwd / "res")) {
|
|
|
|
resourcesPath = cwd / "res";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
resourcesPath = cwd.parent_path() / "share" / "sdltest";
|
|
|
|
}
|
2022-12-15 15:54:11 +00:00
|
|
|
|
2023-03-13 17:10:46 +00:00
|
|
|
if (std::filesystem::is_directory(resourcesPath) == false) {
|
|
|
|
resourcesPath = cwd.root_path() / "usr" / "local" / "share" / "sdltest";
|
|
|
|
}
|
2022-12-15 15:54:11 +00:00
|
|
|
|
2023-03-13 17:10:46 +00:00
|
|
|
if (std::filesystem::is_directory(resourcesPath) == false) {
|
|
|
|
throw std::runtime_error("Unable to determine resources location. CWD: " + cwd.string());
|
|
|
|
}
|
2022-12-15 15:54:11 +00:00
|
|
|
|
2023-03-13 17:10:46 +00:00
|
|
|
return resourcesPath;
|
|
|
|
}
|
2022-09-13 18:25:18 +00:00
|
|
|
|
2023-02-19 13:55:08 +00:00
|
|
|
Application::Application(const char* appName, const char* appVersion, gfx::GraphicsSettings graphicsSettings)
|
2022-10-04 10:54:23 +00:00
|
|
|
{
|
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_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
|
|
|
|
2023-01-20 20:59:35 +00:00
|
|
|
// register resource managers
|
2023-01-26 21:17:07 +00:00
|
|
|
registerResourceManager<resources::Texture>();
|
|
|
|
registerResourceManager<resources::Shader>();
|
|
|
|
registerResourceManager<resources::Material>();
|
|
|
|
registerResourceManager<resources::Mesh>();
|
2023-01-20 16:30:35 +00:00
|
|
|
|
2023-03-13 17:10:46 +00:00
|
|
|
// initialise the render data
|
|
|
|
renderData.gfxdev = std::make_unique<GFXDevice>(appName, appVersion, m_window->getHandle(), graphicsSettings);
|
2023-03-15 23:11:24 +00:00
|
|
|
|
2023-03-13 17:10:46 +00:00
|
|
|
renderData.setZeroLayout = gfx()->createDescriptorSetLayout();
|
|
|
|
renderData.setZero = gfx()->allocateDescriptorSet(renderData.setZeroLayout);
|
2023-03-15 23:11:24 +00:00
|
|
|
RenderData::SetZeroBuffer initialSetZeroData{
|
2023-03-13 17:10:46 +00:00
|
|
|
.proj = glm::perspectiveZO(glm::radians(70.0f), 1024.0f / 768.0f, 0.1f, 1000.0f),
|
|
|
|
};
|
2023-03-15 23:11:24 +00:00
|
|
|
renderData.setZeroBuffer = gfx()->createDescriptorBuffer(sizeof(RenderData::SetZeroBuffer), &initialSetZeroData);
|
2023-03-13 17:10:46 +00:00
|
|
|
gfx()->updateDescriptor(renderData.setZero, 0, renderData.setZeroBuffer, 0, sizeof(RenderData::SetZeroBuffer));
|
|
|
|
|
2023-03-15 23:11:24 +00:00
|
|
|
renderData.setOneLayout = gfx()->createDescriptorSetLayout();
|
|
|
|
renderData.setOne = gfx()->allocateDescriptorSet(renderData.setOneLayout);
|
|
|
|
RenderData::SetOneBuffer initialSetOneData{
|
|
|
|
.view = glm::mat4{ 1.0f },
|
|
|
|
};
|
|
|
|
renderData.setOneBuffer = gfx()->createDescriptorBuffer(sizeof(RenderData::SetOneBuffer), &initialSetOneData);
|
|
|
|
gfx()->updateDescriptor(renderData.setOne, 0, renderData.setOneBuffer, 0, sizeof(RenderData::SetOneBuffer));
|
|
|
|
|
2023-01-20 20:59:35 +00:00
|
|
|
// default resources
|
|
|
|
{
|
|
|
|
resources::Shader::VertexParams vertParams{};
|
|
|
|
vertParams.hasNormal = true;
|
|
|
|
vertParams.hasUV0 = true;
|
2023-01-26 21:17:07 +00:00
|
|
|
auto texturedShader = std::make_unique<resources::Shader>(
|
2023-03-13 17:10:46 +00:00
|
|
|
&renderData,
|
2023-03-12 16:14:55 +00:00
|
|
|
getResourcePath("engine/shaders/standard.vert").c_str(),
|
|
|
|
getResourcePath("engine/shaders/standard.frag").c_str(),
|
2023-01-20 20:59:35 +00:00
|
|
|
vertParams,
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
2023-03-12 16:14:55 +00:00
|
|
|
getResourceManager<resources::Shader>()->addPersistent("builtin.standard", std::move(texturedShader));
|
2023-01-20 20:59:35 +00:00
|
|
|
}
|
2023-03-15 23:11:24 +00:00
|
|
|
if (0) {
|
2023-02-02 17:32:19 +00:00
|
|
|
resources::Shader::VertexParams vertParams{};
|
|
|
|
vertParams.hasNormal = true;
|
|
|
|
vertParams.hasUV0 = true;
|
|
|
|
auto texturedShader = std::make_unique<resources::Shader>(
|
2023-03-13 17:10:46 +00:00
|
|
|
&renderData,
|
2023-02-02 17:32:19 +00:00
|
|
|
getResourcePath("engine/shaders/skybox.vert").c_str(),
|
|
|
|
getResourcePath("engine/shaders/skybox.frag").c_str(),
|
|
|
|
vertParams,
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
2023-03-12 16:14:55 +00:00
|
|
|
getResourceManager<resources::Shader>()->addPersistent("builtin.skybox", std::move(texturedShader));
|
2023-02-02 17:32:19 +00:00
|
|
|
}
|
2023-01-20 20:59:35 +00:00
|
|
|
{
|
2023-01-26 21:17:07 +00:00
|
|
|
auto whiteTexture = std::make_unique<resources::Texture>(
|
2023-01-20 20:59:35 +00:00
|
|
|
gfx(),
|
2023-01-26 21:17:07 +00:00
|
|
|
getResourcePath("engine/textures/white.png"),
|
|
|
|
resources::Texture::Filtering::OFF,
|
|
|
|
false,
|
|
|
|
false
|
2023-01-20 20:59:35 +00:00
|
|
|
);
|
2023-03-12 16:14:55 +00:00
|
|
|
getResourceManager<resources::Texture>()->addPersistent("builtin.white", std::move(whiteTexture));
|
2023-01-20 20:59:35 +00:00
|
|
|
}
|
2022-10-04 10:54:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 17:10:46 +00:00
|
|
|
Application::~Application()
|
|
|
|
{
|
2023-03-15 23:11:24 +00:00
|
|
|
gfx()->destroyDescriptorBuffer(renderData.setOneBuffer);
|
|
|
|
gfx()->destroyDescriptorSetLayout(renderData.setOneLayout);
|
2023-03-13 17:10:46 +00:00
|
|
|
gfx()->destroyDescriptorBuffer(renderData.setZeroBuffer);
|
|
|
|
gfx()->destroyDescriptorSetLayout(renderData.setZeroLayout);
|
|
|
|
}
|
2022-10-04 10:54:23 +00:00
|
|
|
|
|
|
|
void Application::gameLoop()
|
|
|
|
{
|
2023-03-12 16:14:55 +00:00
|
|
|
LOG_TRACE("Begin game loop...");
|
2022-10-22 12:15:25 +00:00
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
2022-11-28 16:33:50 +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;
|
2023-03-12 16:14:55 +00:00
|
|
|
LOG_INFO("fps: {}", m_window->getAvgFPS());
|
2023-01-06 16:45:39 +00:00
|
|
|
m_window->resetAvgFPS();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-11-10 14:12:29 +00:00
|
|
|
/* fps limiter */
|
2022-12-20 23:51:04 +00:00
|
|
|
if (m_enableFrameLimiter) {
|
|
|
|
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
|
|
|
|
2023-03-13 17:10:46 +00:00
|
|
|
gfx()->waitIdle();
|
2022-09-13 18:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|