Merge remote-tracking branch 'origin/master' into Physics
This commit is contained in:
+47
-14
@@ -86,6 +86,8 @@
|
||||
#include "GUI/Button.h"
|
||||
#include "Game/GUI/MainMenu.h"
|
||||
#include "Game/GUI/HUD.h"
|
||||
#include "Game/GUI/ELoadingFrameComplete.h"
|
||||
#include "Game/GUI/LoadingFrame.h"
|
||||
|
||||
namespace dd
|
||||
{
|
||||
@@ -94,9 +96,18 @@ class Engine
|
||||
{
|
||||
|
||||
public:
|
||||
Engine(int argc, char* argv[]) {
|
||||
auto config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
Engine(int argc, char* argv[])
|
||||
{
|
||||
ResourceManager::RegisterType<Texture>("Texture");
|
||||
ResourceManager::RegisterType<Model>("Model");
|
||||
ResourceManager::RegisterType<Sound>("Sound");
|
||||
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||
ResourceManager::RegisterType<ShaderProgram>("ShaderProgram");
|
||||
ResourceManager::RegisterType<VertexShader>("VertexShader");
|
||||
ResourceManager::RegisterType<FragmentShader>("FragmentShader");
|
||||
ResourceManager::LoadPreloadsFromFile();
|
||||
|
||||
auto config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
LOG_LEVEL = static_cast<_LOG_LEVEL>(config->GetValue<int>("Debug.LogLevel", 1));
|
||||
|
||||
m_EventBroker = std::make_shared<EventBroker>();
|
||||
@@ -114,15 +125,11 @@ public:
|
||||
m_FrameStack = new GUI::Frame(m_EventBroker.get());
|
||||
m_FrameStack->Width = 675;
|
||||
m_FrameStack->Height = 1080;
|
||||
|
||||
if (config->GetValue<bool>("Debug.SkipStory", false)) {
|
||||
Events::GameStart e;
|
||||
m_EventBroker->Publish(e);
|
||||
auto hud = new GUI::HUD(m_FrameStack, "HUD");
|
||||
}
|
||||
else {
|
||||
auto menu = new GUI::MainMenu(m_FrameStack, "MainMenu");
|
||||
}
|
||||
m_MainMenu = new GUI::MainMenu(m_FrameStack, "MainMenu");
|
||||
m_MainMenu->Hide();
|
||||
m_LoadingFrame = new GUI::LoadingFrame(m_FrameStack, "Loading");
|
||||
m_LoadingFrame->SetTexture("Textures/GUI/Loading.png");
|
||||
PreloadAllTheShit();
|
||||
|
||||
m_InputManager = std::make_shared<InputManager>(m_Renderer->Window(), m_EventBroker);
|
||||
|
||||
@@ -216,15 +223,28 @@ public:
|
||||
|
||||
//auto particleEmitter= m_World->AddComponent<Components::Emitter>(Pe);
|
||||
|
||||
|
||||
|
||||
//EVENT_SUBSCRIBE_MEMBER(m_EGameStart, &Engine::OnGameStart);
|
||||
m_EGameStart = decltype(m_EGameStart)(std::bind(&Engine::OnGameStart, this, std::placeholders::_1));
|
||||
m_EventBroker->Subscribe(m_EGameStart);
|
||||
m_ELoadingFrameComplete = decltype(m_ELoadingFrameComplete)(std::bind(&Engine::OnLoadingFrameComplete, this, std::placeholders::_1));
|
||||
m_EventBroker->Subscribe(m_ELoadingFrameComplete);
|
||||
|
||||
m_LastTime = glfwGetTime();
|
||||
}
|
||||
|
||||
void PreloadAllTheShit()
|
||||
{
|
||||
if (ResourceManager::Load<ConfigFile>("Config.ini")->GetValue<bool>("Debug.SkipPreload", false)) {
|
||||
m_LoadingFrame->Hide();
|
||||
m_MainMenu->Show();
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& pair : ResourceManager::GetPreloads()) {
|
||||
m_LoadingFrame->AddResource(pair.first, pair.second);
|
||||
}
|
||||
m_LoadingFrame->Preload();
|
||||
}
|
||||
|
||||
bool Running() const { return !glfwWindowShouldClose(m_Renderer->Window()); }
|
||||
|
||||
void Tick()
|
||||
@@ -424,6 +444,8 @@ public:
|
||||
private:
|
||||
std::shared_ptr<EventBroker> m_EventBroker;
|
||||
GUI::Frame* m_FrameStack = nullptr;
|
||||
GUI::MainMenu* m_MainMenu = nullptr;
|
||||
GUI::LoadingFrame* m_LoadingFrame = nullptr;
|
||||
std::shared_ptr<Renderer> m_Renderer;
|
||||
RenderQueueCollection m_RendererQueue;
|
||||
std::shared_ptr<InputManager> m_InputManager;
|
||||
@@ -453,6 +475,17 @@ private:
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
EventRelay<Engine, Events::LoadingFrameComplete> m_ELoadingFrameComplete;
|
||||
bool OnLoadingFrameComplete(const Events::LoadingFrameComplete& event)
|
||||
{
|
||||
if (event.FrameName == "Loading") {
|
||||
event.Frame->Hide();
|
||||
m_MainMenu->Show();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
double m_LastTime;
|
||||
};
|
||||
|
||||
|
||||
@@ -22,7 +22,9 @@
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <fstream>
|
||||
|
||||
#include "Util/UnorderedMapPair.h"
|
||||
#include "Util/Factory.h"
|
||||
@@ -70,13 +72,8 @@ public:
|
||||
return s;
|
||||
}*/
|
||||
|
||||
/** Registers the factory function of a resource type
|
||||
|
||||
@tparam T Resource type.
|
||||
@param factoryFunction Lambda function that creates a new instance of the resource.
|
||||
*/
|
||||
template <typename T>
|
||||
static void RegisterType(std::function<Resource*(std::string)> factoryFunction);
|
||||
static void RegisterType(std::string typeName);
|
||||
|
||||
/** Preloads a resource and caches it for future use
|
||||
|
||||
@@ -85,6 +82,7 @@ public:
|
||||
*/
|
||||
template <typename T>
|
||||
static void Preload(std::string resourceName);
|
||||
static void Preload(std::string resourceType, std::string resourceName);
|
||||
|
||||
/** Checks if a resource is in cache
|
||||
|
||||
@@ -101,14 +99,7 @@ public:
|
||||
*/
|
||||
template <typename T>
|
||||
static T* Load(std::string resourceName, Resource* parent = nullptr);
|
||||
|
||||
/** Fetches a loaded resource
|
||||
|
||||
@tparam T Resource type.
|
||||
@param resourceName Fully qualified name of the resource to fetch.
|
||||
*/
|
||||
template <typename T>
|
||||
static T* Fetch(std::string resourceName);
|
||||
static Resource* Load(std::string resourceType, std::string resourceName, Resource* parent = nullptr);
|
||||
|
||||
/** Reloads an already loaded resource, keeping its resource ID intact.
|
||||
|
||||
@@ -116,14 +107,19 @@ public:
|
||||
@param resourceName Fully qualified name of the resource to reload.
|
||||
*/
|
||||
static void Reload(std::string resourceName);
|
||||
|
||||
static void LoadPreloadsFromFile();
|
||||
static const std::set<std::pair<std::string, std::string>>& GetPreloads() { return m_Preloads; }
|
||||
|
||||
static void Update();
|
||||
|
||||
private:
|
||||
static std::unordered_map<std::string, std::string> m_CompilerTypenameToResourceType;
|
||||
static std::unordered_map<std::string, std::function<Resource*(std::string)>> m_FactoryFunctions; // type -> factory function
|
||||
static std::unordered_map<std::pair<std::string, std::string>, Resource*> m_ResourceCache; // (type, name) -> resource
|
||||
static std::unordered_map<std::string, Resource*> m_ResourceFromName; // name -> resource
|
||||
static std::unordered_map<Resource*, Resource*> m_ResourceParents; // resource -> parent resource
|
||||
static std::set<std::pair<std::string, std::string>> m_Preloads; // (type, name)
|
||||
|
||||
// TODO: Getters for IDs
|
||||
static unsigned int m_CurrentResourceTypeID;
|
||||
@@ -140,93 +136,40 @@ private:
|
||||
static unsigned int GetNewResourceID(unsigned int typeID);
|
||||
|
||||
// Internal: Create a resource and cache it
|
||||
template <typename T>
|
||||
static T* CreateResource(std::string resourceName, Resource* parent);
|
||||
static Resource* CreateResource(std::string resourceType, std::string resourceName, Resource* parent);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr */)
|
||||
{
|
||||
auto resourceType = typeid(T).name();
|
||||
auto it = m_ResourceCache.find(std::make_pair(resourceType, resourceName));
|
||||
if (it != m_ResourceCache.end())
|
||||
return static_cast<T*>(it->second);
|
||||
|
||||
if (m_Preloading) {
|
||||
LOG_INFO("Preloading resource \"%s\"", resourceName.c_str());
|
||||
} else {
|
||||
LOG_WARNING("Hot-loading resource \"%s\"", resourceName.c_str());
|
||||
}
|
||||
|
||||
return CreateResource<T>(resourceName, parent);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T* ResourceManager::Fetch(std::string resourceName)
|
||||
{
|
||||
auto resourceType = typeid(T).name();
|
||||
auto it = m_ResourceCache.find(std::make_pair(resourceType, resourceName));
|
||||
if (it == m_ResourceCache.end())
|
||||
{
|
||||
LOG_ERROR("Failed to fetch resource \"%s\": Resource not loaded!", resourceName.c_str());
|
||||
auto resourceTypename = typeid(T).name();
|
||||
auto it = m_CompilerTypenameToResourceType.find(resourceTypename);
|
||||
if (it == m_CompilerTypenameToResourceType.end()) {
|
||||
LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename);
|
||||
return nullptr;
|
||||
} else
|
||||
{
|
||||
return static_cast<T*>(it->second);
|
||||
}
|
||||
|
||||
return static_cast<T*>(Load(it->second, resourceName, parent));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ResourceManager::RegisterType(std::function<Resource*(std::string)> factoryFunction)
|
||||
void ResourceManager::RegisterType(std::string typeName)
|
||||
{
|
||||
m_FactoryFunctions[typeid(T).name()] = factoryFunction;
|
||||
m_CompilerTypenameToResourceType[typeid(T).name()] = typeName;
|
||||
m_FactoryFunctions[typeName] = [](std::string resourceName) { return new T(resourceName); };
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ResourceManager::Preload(std::string resourceName)
|
||||
{
|
||||
auto resourceType = typeid(T).name();
|
||||
if (IsResourceLoaded(resourceType, resourceName))
|
||||
{
|
||||
LOG_WARNING("Attempted to preload resource \"%s\" multiple times!", resourceName.c_str());
|
||||
auto resourceTypename = typeid(T).name();
|
||||
auto it = m_CompilerTypenameToResourceType.find(resourceTypename);
|
||||
if (it == m_CompilerTypenameToResourceType.end()) {
|
||||
LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename);
|
||||
return;
|
||||
}
|
||||
|
||||
m_Preloading = true;
|
||||
LOG_INFO("Preloading resource \"%s\"", resourceName.c_str());
|
||||
CreateResource<T>(resourceName, nullptr);
|
||||
m_Preloading = false;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T* ResourceManager::CreateResource(std::string resourceName, Resource* parent)
|
||||
{
|
||||
const char* resourceType = typeid(T).name();
|
||||
/*auto facIt = m_FactoryFunctions.find(resourceType);
|
||||
if (facIt == m_FactoryFunctions.end())
|
||||
{
|
||||
LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": Type not registered", resourceName.c_str(), resourceType);
|
||||
return nullptr;
|
||||
}*/
|
||||
|
||||
// Call the factory function
|
||||
T* resource = new T(resourceName);
|
||||
// Store IDs
|
||||
resource->TypeID = GetTypeID(resourceType);
|
||||
resource->ResourceID = GetNewResourceID(resource->TypeID);
|
||||
// Cache
|
||||
m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource;
|
||||
m_ResourceFromName[resourceName] = resource;
|
||||
if (parent != nullptr)
|
||||
{
|
||||
m_ResourceParents[resource] = parent;
|
||||
}
|
||||
if (!boost::filesystem::is_directory(resourceName))
|
||||
{
|
||||
LOG_DEBUG("Adding watch for %s", resourceName.c_str());
|
||||
m_FileWatcher.AddWatch(resourceName, fileWatcherCallback);
|
||||
}
|
||||
return resource;
|
||||
Preload(it->second, resourceName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#include "GUI/Slider.h"
|
||||
#include "Core/ResourceManager.h"
|
||||
#include "Game/GUI/ELoadingFrameComplete.h"
|
||||
#include "Sound/EPlaySound.h"
|
||||
|
||||
namespace dd
|
||||
{
|
||||
@@ -29,9 +28,9 @@ public:
|
||||
m_Slider->X = Width / 2.f - m_Slider->Width / 2.f;
|
||||
}
|
||||
|
||||
void AddTexturePreload(std::string resourceName)
|
||||
void AddResource(std::string resourceType, std::string resourceName)
|
||||
{
|
||||
m_Items.push_back(resourceName);
|
||||
m_Preload.push_back(std::make_pair(resourceType, resourceName));
|
||||
}
|
||||
|
||||
void Preload()
|
||||
@@ -48,18 +47,15 @@ public:
|
||||
void Update(double dt) override
|
||||
{
|
||||
if (m_Preloading) {
|
||||
ResourceManager::Preload<dd::Texture>(m_Items[m_CurrentItem]);
|
||||
m_Slider->SetPercentage((m_CurrentItem + 1) / (float)m_Items.size());
|
||||
ResourceManager::Preload(m_Preload[m_CurrentItem].first, m_Preload[m_CurrentItem].second);
|
||||
m_Slider->SetPercentage((m_CurrentItem + 1) / (float)m_Preload.size());
|
||||
m_CurrentItem++;
|
||||
if (m_CurrentItem >= m_Items.size()) {
|
||||
if (m_CurrentItem >= m_Preload.size()) {
|
||||
m_Preloading = false;
|
||||
dd::Events::LoadingFrameComplete e;
|
||||
e.FrameName = Name();
|
||||
e.Frame = this;
|
||||
EventBroker->Publish(e);
|
||||
Events::PlaySound se;
|
||||
se.FilePath = "Sounds/jap-story.wav";
|
||||
EventBroker->Publish(se);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,7 +63,7 @@ public:
|
||||
private:
|
||||
bool m_Preloading = false;
|
||||
int m_CurrentItem = 0;
|
||||
std::vector<std::string> m_Items;
|
||||
std::vector<std::pair<std::string, std::string>> m_Preload;
|
||||
GUI::Slider* m_Slider = nullptr;
|
||||
};
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
#define GUI_MAINMENU_H__
|
||||
|
||||
#include "GUI/TextureFrame.h"
|
||||
#include "Game/GUI/LoadingFrame.h"
|
||||
#include "Game/GUI/MainMenuMain.h"
|
||||
#include "Game/GUI/MainMenuOptions.h"
|
||||
#include "Core/EKeyUp.h"
|
||||
#include "Game/EGameStart.h"
|
||||
|
||||
namespace dd
|
||||
{
|
||||
@@ -37,6 +39,18 @@ public:
|
||||
//EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &MainMenu::OnKeyUp);
|
||||
}
|
||||
|
||||
void Show() override
|
||||
{
|
||||
if (ResourceManager::Load<ConfigFile>("Config.ini")->GetValue<bool>("Debug.SkipMenu", false)) {
|
||||
Events::GameStart e;
|
||||
EventBroker->Publish(e);
|
||||
Hide();
|
||||
auto hud = new GUI::HUD(BaseFrame, "HUD");
|
||||
} else {
|
||||
TextureFrame::Show();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
TextureFrame* m_LeftBanner;
|
||||
MainMenuMain* m_MainMenuMain;
|
||||
|
||||
@@ -58,6 +58,7 @@ private:
|
||||
if (event.Button == m_SquidoutButton) {
|
||||
m_Parent->Hide();
|
||||
auto story = new GUI::Story1(BaseFrame, "Story1");
|
||||
story->Play();
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
+13
-43
@@ -3,11 +3,11 @@
|
||||
|
||||
#include "Core/ResourceManager.h"
|
||||
#include "Game/GUI/StoryboardFrame.h"
|
||||
#include "Game/GUI/ELoadingFrameComplete.h"
|
||||
#include "Game/GUI/LoadingFrame.h"
|
||||
#include "Game/EGameStart.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Game/GUI/HUD.h"
|
||||
#include "Sound/EPlaySound.h"
|
||||
#include "Sound/EStopSound.h"
|
||||
|
||||
namespace dd
|
||||
{
|
||||
@@ -20,37 +20,8 @@ public:
|
||||
Story1(Frame* parent, std::string name)
|
||||
: StoryboardFrame(parent, name)
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ELoadingFrameComplete, &Story1::OnLoadingFrameComplete);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Story1::OnKeyDown);
|
||||
|
||||
m_LoadingFrame = new GUI::LoadingFrame(this, "Story1LoadingFrame");
|
||||
m_LoadingFrame->SetTexture("Textures/GUI/Loading.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/Brunn1.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/Brunn2.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/Brunn3.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/Brunn4.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/Brunn5.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene1.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene2.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene3.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene4.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene5.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene6.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene7.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene8.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene9.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene10.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene11.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene12.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene13.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene14.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene15.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene16.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene17.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene18.png");
|
||||
m_LoadingFrame->AddTexturePreload("Textures/Story/SubScene19.png");
|
||||
m_LoadingFrame->Preload();
|
||||
|
||||
m_Spacebar = new TextureFrame(this, "ZZZSpacebar");
|
||||
m_Spacebar->SetTexture("Textures/GUI/SpacebarSkip.png");
|
||||
m_Spacebar->SetRight(Right() - 10);
|
||||
@@ -196,6 +167,14 @@ public:
|
||||
AddTransition(t);
|
||||
}
|
||||
|
||||
virtual void Play() override
|
||||
{
|
||||
StoryboardFrame::Play();
|
||||
Events::PlaySound se;
|
||||
se.FilePath = "Sounds/jap-story.wav";
|
||||
EventBroker->Publish(se);
|
||||
}
|
||||
|
||||
void Update(double dt) override
|
||||
{
|
||||
StoryboardFrame::Update(dt);
|
||||
@@ -209,7 +188,6 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
GUI::LoadingFrame* m_LoadingFrame = nullptr;
|
||||
TextureFrame* m_Spacebar = nullptr;
|
||||
|
||||
EventRelay<Frame, Events::KeyDown> m_EKeyDown;
|
||||
@@ -220,25 +198,17 @@ private:
|
||||
}
|
||||
|
||||
if (event.KeyCode == GLFW_KEY_SPACE) {
|
||||
m_LoadingFrame->CancelPreload();
|
||||
Events::GameStart e;
|
||||
EventBroker->Publish(e);
|
||||
Events::StopSound se;
|
||||
se.FilePath = "Sounds/jap-story.wav";
|
||||
EventBroker->Publish(se);
|
||||
Hide();
|
||||
auto hud = new GUI::HUD(BaseFrame, "HUD");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
EventRelay<Frame, Events::LoadingFrameComplete> m_ELoadingFrameComplete;
|
||||
bool OnLoadingFrameComplete(const Events::LoadingFrameComplete& event)
|
||||
{
|
||||
if (event.FrameName == "Story1LoadingFrame") {
|
||||
event.Frame->Hide();
|
||||
m_Spacebar->Hide();
|
||||
Play();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -33,12 +33,12 @@ public:
|
||||
m_Timeline.push_back(transition);
|
||||
}
|
||||
|
||||
void Play()
|
||||
virtual void Play()
|
||||
{
|
||||
m_Playing = true;
|
||||
}
|
||||
|
||||
void Skip()
|
||||
virtual void Skip()
|
||||
{
|
||||
m_Playing = false;
|
||||
OnComplete();
|
||||
|
||||
@@ -19,10 +19,12 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Core/ResourceManager.h"
|
||||
|
||||
std::unordered_map<std::string, std::string> dd::ResourceManager::m_CompilerTypenameToResourceType;
|
||||
std::unordered_map<std::string, std::function<dd::Resource*(std::string)>> dd::ResourceManager::m_FactoryFunctions;
|
||||
std::unordered_map<std::pair<std::string, std::string>, dd::Resource*> dd::ResourceManager::m_ResourceCache;
|
||||
std::unordered_map<std::string, dd::Resource*> dd::ResourceManager::m_ResourceFromName;
|
||||
std::unordered_map<dd::Resource*, dd::Resource*> dd::ResourceManager::m_ResourceParents;
|
||||
std::set<std::pair<std::string, std::string>> dd::ResourceManager::m_Preloads; // (type, name)
|
||||
unsigned int dd::ResourceManager::m_CurrentResourceTypeID = 0;
|
||||
std::unordered_map<std::string, unsigned int> dd::ResourceManager::m_ResourceTypeIDs;
|
||||
std::unordered_map<unsigned int, unsigned int> dd::ResourceManager::m_ResourceCount;
|
||||
@@ -82,4 +84,86 @@ void dd::ResourceManager::fileWatcherCallback(std::string path, FileWatcher::Fil
|
||||
void dd::ResourceManager::Update()
|
||||
{
|
||||
m_FileWatcher.Check();
|
||||
}
|
||||
|
||||
void dd::ResourceManager::LoadPreloadsFromFile()
|
||||
{
|
||||
std::ifstream f("PreloadResources");
|
||||
while (f.good()) {
|
||||
std::string resourceType;
|
||||
std::string resourceName;
|
||||
f >> resourceType >> resourceName;
|
||||
if (!resourceType.empty() && !resourceType.empty()) {
|
||||
m_Preloads.insert(std::make_pair(resourceType, resourceName));
|
||||
}
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
|
||||
void dd::ResourceManager::Preload(std::string resourceType, std::string resourceName)
|
||||
{
|
||||
if (IsResourceLoaded(resourceType, resourceName))
|
||||
{
|
||||
//LOG_WARNING("Attempted to preload resource \"%s\" multiple times!", resourceName.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
m_Preloading = true;
|
||||
LOG_INFO("Preloading resource \"%s\"", resourceName.c_str());
|
||||
CreateResource(resourceType, resourceName, nullptr);
|
||||
m_Preloading = false;
|
||||
}
|
||||
|
||||
dd::Resource* dd::ResourceManager::Load(std::string resourceType, std::string resourceName, dd::Resource* parent /*= nullptr*/)
|
||||
{
|
||||
auto it = m_ResourceCache.find(std::make_pair(resourceType, resourceName));
|
||||
if (it != m_ResourceCache.end())
|
||||
return it->second;
|
||||
|
||||
if (m_Preloading) {
|
||||
LOG_INFO("Preloading resource \"%s\"", resourceName.c_str());
|
||||
} else {
|
||||
LOG_WARNING("Hot-loading resource \"%s\"", resourceName.c_str());
|
||||
|
||||
if (!resourceType.empty() && !resourceName.empty()) {
|
||||
std::replace(resourceName.begin(), resourceName.end(), '\\', '/');
|
||||
if (m_Preloads.find(std::make_pair(resourceType, resourceName)) == m_Preloads.end()) {
|
||||
std::ofstream f("PreloadResources", std::ofstream::app);
|
||||
f << resourceType << " " << resourceName << std::endl;
|
||||
f.close();
|
||||
m_Preloads.insert(std::make_pair(resourceType, resourceName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return CreateResource(resourceType, resourceName, parent);
|
||||
}
|
||||
|
||||
dd::Resource* dd::ResourceManager::CreateResource(std::string resourceType, std::string resourceName, dd::Resource* parent)
|
||||
{
|
||||
auto facIt = m_FactoryFunctions.find(resourceType);
|
||||
if (facIt == m_FactoryFunctions.end())
|
||||
{
|
||||
LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceType.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Call the factory function
|
||||
Resource* resource = facIt->second(resourceName);
|
||||
// Store IDs
|
||||
resource->TypeID = GetTypeID(resourceType);
|
||||
resource->ResourceID = GetNewResourceID(resource->TypeID);
|
||||
// Cache
|
||||
m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource;
|
||||
m_ResourceFromName[resourceName] = resource;
|
||||
if (parent != nullptr)
|
||||
{
|
||||
m_ResourceParents[resource] = parent;
|
||||
}
|
||||
if (!boost::filesystem::is_directory(resourceName))
|
||||
{
|
||||
LOG_DEBUG("Adding watch for %s", resourceName.c_str());
|
||||
m_FileWatcher.AddWatch(resourceName, fileWatcherCallback);
|
||||
}
|
||||
return resource;
|
||||
}
|
||||
Reference in New Issue
Block a user