engine/src/application.cpp

311 lines
11 KiB
C++
Raw Normal View History

2023-05-01 13:13:35 +00:00
#include "application.h"
2022-09-13 18:25:18 +00:00
2023-10-01 10:38:27 +00:00
#include <cinttypes>
2023-04-29 14:22:25 +00:00
#include <filesystem>
#include <memory>
#include <stdexcept>
#include <string>
#include <thread>
2023-10-01 10:38:27 +00:00
#include <numeric>
2022-10-27 16:58:30 +00:00
2023-04-29 14:22:25 +00:00
#include <glm/mat4x4.hpp>
2023-10-01 10:38:27 +00:00
#include "imgui/imgui.h"
#include "imgui/imgui_impl_sdl2.h"
#include "imgui/imgui_impl_vulkan.h"
2023-05-01 13:13:35 +00:00
#include "gfx.h"
#include "gfx_device.h"
#include "input_manager.h"
#include "log.h"
2023-05-14 13:40:16 +00:00
#include "resources/font.h"
2023-05-01 13:13:35 +00:00
#include "resources/material.h"
#include "resources/mesh.h"
#include "resources/shader.h"
#include "resources/texture.h"
2023-08-31 09:51:12 +00:00
#include "systems/mesh_render_system.h"
#include "components/transform.h"
2023-05-01 13:13:35 +00:00
#include "scene.h"
#include "scene_manager.h"
#include "window.h"
2022-12-15 15:54:11 +00:00
#ifdef _MSC_VER
#include <windows.h>
#include <direct.h>
2023-04-29 14:22:25 +00:00
#define WIN_MAX_PATH 260
2022-12-15 15:54:11 +00:00
#endif
2023-10-01 10:38:27 +00:00
static struct ImGuiThings {
2023-10-02 11:54:37 +00:00
ImGuiContext* context;
2023-10-01 10:38:27 +00:00
} im_gui_things;
2023-03-13 17:10:46 +00:00
namespace engine {
2023-10-02 11:54:37 +00:00
static std::filesystem::path getResourcesPath()
{
std::filesystem::path resourcesPath{};
2022-12-15 15:54:11 +00:00
#ifdef _MSC_VER
2023-10-02 11:54:37 +00:00
// get the path of the currently running process
CHAR exeDirBuf[MAX_PATH + 1];
GetModuleFileNameA(NULL, exeDirBuf, WIN_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-10-02 11:54:37 +00:00
std::filesystem::path cwd = std::filesystem::current_path();
2022-12-15 15:54:11 +00:00
#endif
2023-10-02 11:54:37 +00:00
if (std::filesystem::is_directory(cwd / "res")) {
resourcesPath = cwd / "res";
}
else {
resourcesPath = cwd.parent_path() / "share" / "sdltest";
}
2023-05-14 23:27:31 +00:00
2023-10-02 11:54:37 +00:00
if (std::filesystem::is_directory(resourcesPath) == false) {
resourcesPath = cwd.root_path() / "usr" / "local" / "share" / "sdltest";
}
2023-05-14 23:27:31 +00:00
2023-10-02 11:54:37 +00:00
if (std::filesystem::is_directory(resourcesPath) == false) {
throw std::runtime_error("Unable to determine resources location. CWD: " + cwd.string());
}
2023-05-14 23:27:31 +00:00
2023-10-02 11:54:37 +00:00
return resourcesPath;
2023-05-14 23:27:31 +00:00
}
2024-02-14 00:30:51 +00:00
Application::Application(const char* appName, const char* appVersion, gfx::GraphicsSettings graphicsSettings, Configuration configuration)
: app_name(appName), app_version(appVersion), configuration_(configuration)
2023-10-02 11:54:37 +00:00
{
window_ = std::make_unique<Window>(appName, true, false);
input_manager_ = std::make_unique<InputManager>(window_.get());
scene_manager_ = std::make_unique<SceneManager>(this);
2023-05-14 23:27:31 +00:00
2023-10-02 11:54:37 +00:00
// get base path for resources
resources_path_ = getResourcesPath();
// register resource managers
2023-11-05 01:04:05 +00:00
RegisterResourceManager<Mesh>();
RegisterResourceManager<Material>();
RegisterResourceManager<Texture>();
RegisterResourceManager<Shader>();
RegisterResourceManager<Font>();
2023-10-02 11:54:37 +00:00
im_gui_things.context = ImGui::CreateContext();
// ImGuiIO& io = ImGui::GetIO()
ImGui_ImplSDL2_InitForVulkan(window_->GetHandle());
renderer_ = std::make_unique<Renderer>(*this, graphicsSettings);
2023-05-14 23:27:31 +00:00
2023-10-02 11:54:37 +00:00
/* default fonts */
{
2023-11-05 01:04:05 +00:00
auto monoFont = std::make_unique<Font>(GetResourcePath("engine/fonts/mono.ttf"));
GetResourceManager<Font>()->AddPersistent("builtin.mono", std::move(monoFont));
2023-05-14 13:40:16 +00:00
}
2022-09-13 18:25:18 +00:00
2023-10-02 11:54:37 +00:00
/* default shaders */
{
2023-11-05 01:04:05 +00:00
Shader::VertexParams vertParams{};
2023-10-02 11:54:37 +00:00
vertParams.has_normal = true;
2023-11-05 01:04:05 +00:00
vertParams.has_tangent = true;
2023-10-02 11:54:37 +00:00
vertParams.has_uv0 = true;
2023-11-05 01:04:05 +00:00
Shader::ShaderSettings shaderSettings{};
2023-10-02 11:54:37 +00:00
shaderSettings.vertexParams = vertParams;
shaderSettings.alpha_blending = false;
shaderSettings.cull_backface = true;
shaderSettings.write_z = true;
shaderSettings.render_order = 0;
auto fancyShader = std::make_unique<Shader>(renderer(), GetResourcePath("engine/shaders/fancy.vert"),
GetResourcePath("engine/shaders/fancy.frag"), shaderSettings);
2023-11-05 01:04:05 +00:00
GetResourceManager<Shader>()->AddPersistent("builtin.fancy", std::move(fancyShader));
}
2024-02-28 22:18:19 +00:00
{
Shader::VertexParams vertParams{};
vertParams.has_normal = true;
vertParams.has_tangent = true;
vertParams.has_uv0 = true;
Shader::ShaderSettings shaderSettings{};
shaderSettings.vertexParams = vertParams;
shaderSettings.alpha_blending = false;
shaderSettings.cull_backface = true;
shaderSettings.write_z = false;
shaderSettings.render_order = 1;
auto skyboxShader = std::make_unique<Shader>(renderer(), GetResourcePath("engine/shaders/skybox.vert"),
GetResourcePath("engine/shaders/skybox.frag"), shaderSettings);
2024-02-28 22:18:19 +00:00
GetResourceManager<Shader>()->AddPersistent("builtin.skybox", std::move(skyboxShader));
}
2023-10-01 10:38:27 +00:00
2023-10-02 11:54:37 +00:00
/* default textures */
{
2024-02-28 22:18:19 +00:00
const uint8_t pixel[4] = { 255, 255, 255, 255 };
gfx::SamplerInfo samplerInfo{};
samplerInfo.minify = gfx::Filter::kNearest;
samplerInfo.magnify = gfx::Filter::kNearest;
samplerInfo.mipmap = gfx::Filter::kNearest;
samplerInfo.anisotropic_filtering = false;
auto whiteTexture = std::make_unique<Texture>(renderer(), pixel, 1, 1, samplerInfo, true);
2023-11-05 01:04:05 +00:00
GetResourceManager<Texture>()->AddPersistent("builtin.white", std::move(whiteTexture));
2023-10-01 10:38:27 +00:00
}
2023-11-28 12:50:55 +00:00
{
2024-02-28 22:18:19 +00:00
const uint8_t pixel[4] = { 0, 0, 0, 255 };
gfx::SamplerInfo samplerInfo{};
samplerInfo.minify = gfx::Filter::kNearest;
samplerInfo.magnify = gfx::Filter::kNearest;
samplerInfo.mipmap = gfx::Filter::kNearest;
samplerInfo.anisotropic_filtering = false;
auto blackTexture = std::make_unique<Texture>(renderer(), pixel, 1, 1, samplerInfo, true);
GetResourceManager<Texture>()->AddPersistent("builtin.black", std::move(blackTexture));
}
{
const uint8_t pixel[4] = { 127, 127, 255, 255 };
gfx::SamplerInfo samplerInfo{};
samplerInfo.minify = gfx::Filter::kNearest;
samplerInfo.magnify = gfx::Filter::kNearest;
samplerInfo.mipmap = gfx::Filter::kNearest;
samplerInfo.anisotropic_filtering = false;
auto normalTexture = std::make_unique<Texture>(renderer(), pixel, 1, 1, samplerInfo, false);
2023-11-28 12:50:55 +00:00
GetResourceManager<Texture>()->AddPersistent("builtin.normal", std::move(normalTexture));
}
2024-02-28 22:18:19 +00:00
{
const uint8_t pixel[4] = { 255, 0, 127, 255 };
gfx::SamplerInfo samplerInfo{};
samplerInfo.minify = gfx::Filter::kNearest;
samplerInfo.magnify = gfx::Filter::kNearest;
samplerInfo.mipmap = gfx::Filter::kNearest;
samplerInfo.anisotropic_filtering = false;
auto mrTexture = std::make_unique<Texture>(renderer(), pixel, 1, 1, samplerInfo, false);
GetResourceManager<Texture>()->AddPersistent("builtin.mr", std::move(mrTexture));
}
2024-02-03 00:01:01 +00:00
/* default materials */
{
auto defaultMaterial = std::make_unique<Material>(renderer(), GetResource<Shader>("builtin.fancy"));
defaultMaterial->SetAlbedoTexture(GetResource<Texture>("builtin.white"));
defaultMaterial->SetNormalTexture(GetResource<Texture>("builtin.normal"));
GetResourceManager<Material>()->AddPersistent("builtin.default", std::move(defaultMaterial));
}
2023-10-02 11:54:37 +00:00
}
Application::~Application()
{
renderer_->GetDevice()->ShutdownImguiBackend();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext(im_gui_things.context);
}
void Application::GameLoop()
{
LOG_DEBUG("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;
auto lastTick = window_->GetNanos();
std::array<float, 20> delta_times{};
struct DebugMenuState {
bool menu_active = false;
bool show_info_window = true;
} debug_menu_state;
2023-10-01 10:38:27 +00:00
2023-10-02 11:54:37 +00:00
// single-threaded game loop
while (window_->IsRunning()) {
/* logic */
2023-10-01 10:38:27 +00:00
2023-10-02 11:54:37 +00:00
const float avg_fps = static_cast<float>(delta_times.size()) / std::accumulate(delta_times.begin(), delta_times.end(), 0.0f);
Scene* scene = scene_manager_->UpdateActiveScene(window_->dt());
uint64_t now = window_->GetNanos();
if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] {
lastTick = now;
LOG_INFO("fps: {}", std::lroundf(avg_fps));
// renderer()->GetDevice()->LogPerformanceInfo();
window_->ResetAvgFPS();
2023-10-01 10:38:27 +00:00
}
2023-10-02 11:54:37 +00:00
if (window_->GetKeyPress(inputs::Key::K_F5)) {
bool show_window = window_->MouseCaptured();
debug_menu_state.menu_active = show_window;
window_->SetRelativeMouseMode(!show_window);
}
2023-10-01 10:38:27 +00:00
2023-10-02 11:54:37 +00:00
if (window_->GetKeyPress(inputs::Key::K_F6)) {
debug_menu_state.show_info_window = !debug_menu_state.show_info_window;
}
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
if (debug_menu_state.menu_active) {
2023-12-10 20:57:47 +00:00
if (ImGui::Begin("debugMenu", 0)) {
2023-10-02 11:54:37 +00:00
ImGui::Text("Test!");
ImGui::Text("FPS: %.3f", std::roundf(avg_fps));
2023-10-01 10:38:27 +00:00
}
2023-10-02 11:54:37 +00:00
ImGui::End();
}
2023-10-01 10:38:27 +00:00
2023-10-02 11:54:37 +00:00
if (debug_menu_state.show_info_window) {
if (ImGui::Begin(
"infoWindow", nullptr,
ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoFocusOnAppearing)) {
ImGui::Text("Scene hierarchy:");
std::function<int(Entity, int)> find_depth = [&](Entity e, int current_depth) -> int {
Entity parent = scene->GetComponent<TransformComponent>(e)->parent;
if (parent == 0)
return current_depth;
else {
return find_depth(parent, current_depth + 1);
}
};
2024-02-14 00:30:51 +00:00
if (scene) {
2023-10-02 11:54:37 +00:00
for (Entity i = 1; i < scene->next_entity_id_; ++i) {
auto t = scene->GetComponent<TransformComponent>(i);
std::string tabs{};
int depth = find_depth(i, 0);
for (int j = 0; j < depth; ++j) tabs += std::string{" "};
ImGui::Text("%s%s", tabs.c_str(), t->tag.c_str());
2024-02-16 22:08:14 +00:00
//ImGui::Text("%.1f %.1f %.1f", t->position.x, t->position.y, t->position.z);
2023-10-02 11:54:37 +00:00
}
}
2023-11-05 01:04:05 +00:00
else {
ImGui::Text("No scene active!");
}
2023-10-02 11:54:37 +00:00
}
ImGui::End();
2023-10-01 10:38:27 +00:00
}
2023-10-02 11:54:37 +00:00
ImGui::Render();
const RenderList* static_list = nullptr;
const RenderList* dynamic_list = nullptr;
glm::mat4 camera_transform{1.0f};
if (scene) {
camera_transform = scene->GetComponent<TransformComponent>(scene->GetEntity("camera"))->world_matrix;
auto mesh_render_system = scene->GetSystem<MeshRenderSystem>();
static_list = mesh_render_system->GetStaticRenderList();
dynamic_list = mesh_render_system->GetDynamicRenderList();
}
renderer_->PreRender(window()->GetWindowResized(), camera_transform);
2023-11-05 01:04:05 +00:00
renderer_->Render(static_list, dynamic_list);
2023-10-02 11:54:37 +00:00
/* poll events */
window_->GetInputAndEvents();
2023-05-14 23:27:31 +00:00
2023-10-02 11:54:37 +00:00
/* fps limiter */
2024-02-14 00:30:51 +00:00
if (configuration_.enable_frame_limiter) {
2023-10-02 11:54:37 +00:00
std::this_thread::sleep_until(endFrame);
}
beginFrame = endFrame;
endFrame = beginFrame + FRAMETIME_LIMIT;
delta_times[window_->GetFrameCount() % delta_times.size()] = window_->dt();
2023-05-14 23:27:31 +00:00
}
2023-10-02 11:54:37 +00:00
renderer_->GetDevice()->WaitIdle();
2022-09-13 18:25:18 +00:00
}
2023-05-14 23:27:31 +00:00
2023-10-02 11:54:37 +00:00
} // namespace engine