engine/include/application.h

101 lines
3.1 KiB
C
Raw Permalink Normal View History

2024-03-31 10:11:22 +00:00
#pragma once
2023-03-13 17:10:46 +00:00
2023-04-29 14:22:25 +00:00
#include <filesystem>
2022-11-29 14:22:03 +00:00
#include <memory>
2022-12-15 15:54:11 +00:00
#include <string>
2023-01-20 16:30:35 +00:00
#include <unordered_map>
2023-04-29 14:22:25 +00:00
2024-06-04 18:01:49 +00:00
#include "debug_line.h"
2023-05-01 13:13:35 +00:00
#include "gfx.h"
#include "resource_manager.h"
2022-11-29 14:22:03 +00:00
namespace engine {
2024-06-04 18:01:49 +00:00
class Window; // forward-dec
class InputManager; // forward-dec
class Renderer; // forward-dec
class SceneManager; // forward-dec
2024-06-02 13:29:59 +00:00
struct AppConfiguration {
bool enable_frame_limiter;
};
2024-02-14 00:30:51 +00:00
2024-06-02 13:29:59 +00:00
class Application {
private:
std::unique_ptr<Window> m_window;
std::unique_ptr<InputManager> m_input_manager;
std::unique_ptr<Renderer> m_renderer;
2024-06-04 18:01:49 +00:00
std::unique_ptr<SceneManager> m_scene_manager;
2024-06-02 13:29:59 +00:00
std::unordered_map<size_t, std::unique_ptr<IResourceManager>> m_resource_managers{};
std::filesystem::path m_resources_path;
AppConfiguration m_configuration;
2024-06-04 18:01:49 +00:00
2024-06-02 13:29:59 +00:00
public:
const char* const app_name;
const char* const app_version;
2024-06-04 18:01:49 +00:00
std::vector<DebugLine> debug_lines{};
2024-06-02 13:29:59 +00:00
public:
Application(const char* app_name, const char* app_version, gfx::GraphicsSettings graphics_settings, AppConfiguration configuration);
2024-02-14 00:30:51 +00:00
Application(const Application&) = delete;
2024-06-02 13:29:59 +00:00
~Application();
2024-02-14 00:30:51 +00:00
2024-06-02 13:29:59 +00:00
Application& operator=(const Application&) = delete;
2024-02-14 00:30:51 +00:00
template <typename T>
2024-06-02 13:29:59 +00:00
void registerResourceManager();
2024-02-14 00:30:51 +00:00
template <typename T>
2024-06-02 13:29:59 +00:00
std::shared_ptr<T> addResource(const std::string& name, std::unique_ptr<T>&& resource);
2024-02-14 00:30:51 +00:00
template <typename T>
2024-06-02 13:29:59 +00:00
std::shared_ptr<T> getResource(const std::string& name);
void gameLoop();
void setFrameLimiter(bool on) { m_configuration.enable_frame_limiter = on; }
2024-06-04 18:01:49 +00:00
Window* getWindow() { return m_window.get(); }
InputManager* getInputManager() { return m_input_manager.get(); }
SceneManager* getSceneManager() { return m_scene_manager.get(); }
Renderer* getRenderer() { return m_renderer.get(); }
2024-06-02 13:29:59 +00:00
std::string getResourcePath(const std::string relative_path) const { return (m_resources_path / relative_path).string(); }
private:
template <typename T>
ResourceManager<T>* getResourceManager();
2023-04-29 14:22:25 +00:00
};
2024-06-02 13:29:59 +00:00
template <typename T>
void Application::registerResourceManager()
{
size_t hash = typeid(T).hash_code();
assert(m_resource_managers.contains(hash) == false && "Registering resource manager type more than once.");
m_resource_managers.emplace(hash, std::make_unique<ResourceManager<T>>());
}
template <typename T>
std::shared_ptr<T> Application::addResource(const std::string& name, std::unique_ptr<T>&& resource)
{
auto resource_manager = getResourceManager<T>();
return resource_manager->Add(name, std::move(resource));
}
template <typename T>
std::shared_ptr<T> Application::getResource(const std::string& name)
{
auto resource_manager = getResourceManager<T>();
return resource_manager->Get(name);
}
template <typename T>
ResourceManager<T>* Application::getResourceManager()
{
size_t hash = typeid(T).hash_code();
auto it = m_resource_managers.find(hash);
if (it == m_resource_managers.end()) {
throw std::runtime_error("Cannot find resource manager.");
}
auto ptr = it->second.get();
auto casted_ptr = dynamic_cast<ResourceManager<T>*>(ptr);
assert(casted_ptr != nullptr);
return casted_ptr;
}
2024-03-31 10:11:22 +00:00
} // namespace engine