-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
111 lines (96 loc) · 4.94 KB
/
engine.cpp
File metadata and controls
111 lines (96 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "Engine/Engine.hpp"
#include "Utils/Logger.hpp"
#include "Utils/Path.hpp"
#include <numeric>
#include <ranges>
void printFps(std::array<float, 10> &fpsBuffer, int &fpsIndex, const float deltaTime)
{
fpsBuffer[fpsIndex % 10] = 1.0F / deltaTime;
fpsIndex++;
float avgFps = std::accumulate(fpsBuffer.begin(), fpsBuffer.end(), 0.0F) / 10.0F;
cae::utl::Logger::log(std::format("FPS: {}", avgFps), cae::utl::LogLevel::INFO);
}
cae::eng::Engine::Engine(const EngineConfig &config, const std::function<std::shared_ptr<IAudio>()> &audioFactory,
const std::function<std::shared_ptr<INetwork>()> &networkFactory,
const std::function<std::shared_ptr<IRenderer>()> &rendererFactory,
const std::function<std::shared_ptr<IShaderIR>()> &shaderIRFactory,
const std::vector<std::function<std::shared_ptr<IShaderFrontend>()>> &shaderFrontendFactories,
const std::function<std::shared_ptr<IWindow>()> &windowFactory)
: m_audioPlugin(audioFactory()), m_networkPlugin(networkFactory()), m_rendererPlugin(rendererFactory()),
m_windowPlugin(windowFactory()), m_clock(std::make_unique<utl::Clock>()),
m_shaderManager(std::make_unique<ShaderManager>(shaderFrontendFactories, shaderIRFactory)),
m_camera(std::make_unique<Camera>(config.camera_position, config.camera_rotation, config.camera_direction,
config.camera_move_speed, config.camera_look_speed, config.camera_fov,
config.camera_near_plane, config.camera_far_plane)),
m_logFps(config.log_fps)
{
constexpr auto boolToStr = [](const bool b) { return b ? "true" : "false"; };
std::ostringstream msg;
msg << "Starting engine with configuration:\n"
<< "\tAudio master volume: " << config.audio_master_volume << "\n"
<< "\tAudio muted: " << boolToStr(config.audio_muted) << "\n"
<< "\tLog FPS: " << boolToStr(config.log_fps) << "\n"
<< "\tNetwork host: " << config.network_host << "\n"
<< "\tNetwork port: " << config.network_port << "\n"
<< "\tRenderer vsync: " << boolToStr(config.renderer_vsync) << "\n"
<< "\tRenderer frame rate limit: " << config.renderer_frame_rate_limit << "\n"
<< "\tRenderer clear color: (" << config.renderer_clear_color.r << ", " << config.renderer_clear_color.g << ", "
<< config.renderer_clear_color.b << ", " << config.renderer_clear_color.a << ")\n"
<< "\tWindow width: " << config.window_width << "\n"
<< "\tWindow height: " << config.window_height << "\n"
<< "\tWindow fullscreen: " << boolToStr(config.window_fullscreen) << "\n"
<< "\tWindow name: " << config.window_name << "\n\tWindow icon path: " << config.window_icon_path << '\n';
utl::Logger::log(msg.str(), utl::LogLevel::INFO);
initWindow(config.window_name, {.width = config.window_width, .height = config.window_height},
config.window_icon_path);
m_rendererPlugin->initialize(m_windowPlugin->getNativeHandle(), config.renderer_clear_color);
}
void cae::eng::Engine::initializeRenderResources(const std::vector<ShaderSourceDesc> &shaderSources,
const std::vector<float> &vertices, const ShaderSourceType &type) const
{
initShaders(shaderSources, type);
m_rendererPlugin->createMesh(vertices);
}
void cae::eng::Engine::render()
{
constexpr auto model = glm::mat4(1.0F);
const glm::mat4 mvp = m_camera->getViewProjection(static_cast<float>(m_windowPlugin->getWindowSize().width) /
m_windowPlugin->getWindowSize().height) *
model;
m_rendererPlugin->draw(m_windowPlugin->getWindowSize(), "basic", mvp);
}
void cae::eng::Engine::update(std::array<float, 10> &fpsBuffer, int &fpsIndex)
{
if (m_logFps)
{
printFps(fpsBuffer, fpsIndex, m_clock->getDeltaSeconds());
}
m_clock->restart();
}
void cae::eng::Engine::stop()
{
utl::Logger::log("Stopping engine...", utl::LogLevel::INFO);
m_windowPlugin->close();
m_audioPlugin = nullptr;
m_networkPlugin = nullptr;
m_rendererPlugin = nullptr;
m_windowPlugin = nullptr;
m_clock = nullptr;
m_shaderManager = nullptr;
m_camera = nullptr;
}
void cae::eng::Engine::initWindow(const std::string &windowName, const WindowSize &windowSize,
const std::string &iconPath) const
{
m_windowPlugin->create(windowName, windowSize);
if (!iconPath.empty())
{
m_windowPlugin->setIcon(iconPath);
}
}
void cae::eng::Engine::initShaders(const std::vector<ShaderSourceDesc> &shaderSources, const ShaderSourceType &type) const
{
auto shaders = m_shaderManager->build(shaderSources, type);
m_shaderManager->optimizeAll(ShaderSourceType::SPIRV, shaders | std::views::values);
m_rendererPlugin->createPipeline("basic", shaders["basic_vertex"], shaders["basic_fragment"]);
}